Debian Codegraph @

tests/phpunit/unit/includes/content/ContentHandlerFactoryTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Content;
4
5 use DummyContentHandlerForTesting;
6 use Error;
7 use InvalidArgumentException;
8 use MediaWiki\Content\ContentHandler;
9 use MediaWiki\Content\ContentHandlerFactory;
10 use MediaWiki\HookContainer\HookContainer;
11 use MediaWikiUnitTestCase;
12 use MWUnknownContentModelException;
13 use Psr\Log\LogLevel;
14 use TestLogger;
15 use UnexpectedValueException;
16 use Wikimedia\ObjectFactory\ObjectFactory;
17
18 /**
19 * @group ContentHandlerFactory
20 * @covers \MediaWiki\Content\ContentHandlerFactory
21 */
22 class ContentHandlerFactoryTest extends MediaWikiUnitTestCase {
23
24 private TestLogger $logger;
25
26 protected function setUp(): void {
27 parent::setUp();
28
29 $this->logger = new TestLogger( false, static function ( $message, $level ) {
30 return $level === LogLevel::INFO ? null : $message;
31 } );
32 }
33
34 public static function provideHandlerSpecs() {
35 return [
36 'typical list' => [
37 [
38 'ExistClassName' => DummyContentHandlerForTesting::class,
39 'ExistCallbackWithExistClassName' => static function ( $modelID ) {
40 return new DummyContentHandlerForTesting( $modelID );
41 },
42 ],
43 DummyContentHandlerForTesting::class,
44 ],
45 ];
46 }
47
48 /**
49 * @dataProvider provideHandlerSpecs $handlerSpecs
50 */
51 public function testGetContentHandler_callWithProvider_same(
52 array $handlerSpecs, string $contentHandlerClass
53 ): void {
54 $contentHandlerExpected = new $contentHandlerClass( 'dummy' );
55 $objectFactory = $this->createMock( ObjectFactory::class );
56 $hookContainer = $this->createMock( HookContainer::class );
57
58 $factory = new ContentHandlerFactory(
59 $handlerSpecs,
60 $objectFactory,
61 $hookContainer,
62 $this->logger
63 );
64
65 $returnMap = [];
66 foreach ( $handlerSpecs as $modelID => $handlerSpec ) {
67 $returnMap[] = [
68 $handlerSpec,
69 [
70 'assertClass' => ContentHandler::class,
71 'allowCallable' => true,
72 'allowClassName' => true,
73 'extraArgs' => [ $modelID ],
74 ],
75 $contentHandlerExpected
76 ];
77 }
78 $objectFactory
79 ->method( 'createObject' )
80 ->willReturnMap( $returnMap );
81
82 foreach ( $handlerSpecs as $modelID => $handlerSpec ) {
83 $this->assertSame( $contentHandlerExpected, $factory->getContentHandler( $modelID ) );
84 }
85 }
86
87 /**
88 * @dataProvider provideHandlerSpecs $handlerSpecs
89 */
90 public function testGetContentHandler_hookWithProvider_same(
91 array $handlerSpecs,
92 string $contentHandlerClass
93 ): void {
94 $contentHandlerExpected = new $contentHandlerClass( 'dummy' );
95 $hookContainer = $this->createHookContainer();
96 $factory = new ContentHandlerFactory(
97 [],
98 $this->createMock( ObjectFactory::class ),
99 $hookContainer,
100 $this->logger
101 );
102
103 foreach ( $handlerSpecs as $modelID => $handlerSpec ) {
104 $this->assertFalse( $factory->isDefinedModel( $modelID ) );
105 $contentHandler = null;
106 $hookContainer->register( 'ContentHandlerForModelID',
107 static function ( $handlerSpec, &$contentHandler ) use (
108 $contentHandlerExpected
109 ) {
110 $contentHandler = $contentHandlerExpected;
111
112 return true;
113 } );
114
115 $contentHandler = $factory->getContentHandler( $modelID );
116 $this->assertSame( $contentHandlerExpected, $contentHandler, $modelID );
117 $this->assertTrue( $factory->isDefinedModel( $modelID ), $modelID );
118 }
119 }
120
121 public static function provideHandlerSpecsWithException(): array {
122 return [
123 'Callback exists but returns the wrong type' => [
124 [
125 'ExistCallbackWithWrongType' => static function () {
126 return true;
127 }
128 ],
129 UnexpectedValueException::class
130 ],
131 'Callback exists but returns null' => [
132 [
133 'ExistCallbackWithNull' => static function () {
134 return null;
135 }
136 ],
137 UnexpectedValueException::class
138 ],
139 'Callback exists but returns the empty string' => [
140 [
141 'ExistCallbackWithEmptyString' => static function () {
142 return '';
143 }
144 ],
145 UnexpectedValueException::class
146 ],
147 'Wrong class name' => [
148 [
149 'WrongClassName' => self::class
150 ],
151 UnexpectedValueException::class
152 ],
153 'Wrong type' => [
154 [
155 'WrongType' => true
156 ],
157 InvalidArgumentException::class
158 ],
159 'Is null' => [
160 [
161 'NullType' => null
162 ],
163 MWUnknownContentModelException::class
164 ],
165 'Class does not exist' => [
166 [
167 'WrongClassNameNotExist' => 'ClassNameNotExist'
168 ],
169 InvalidArgumentException::class
170 ],
171 'Callback with non-existing class name' => [
172 [
173 'ExistCallbackWithNotExistClassName' => static function () {
174 return ClassNameNotExist();
175 },
176 ],
177 Error::class
178 ],
179 'Empty string' => [
180 [
181 'EmptyString' => '',
182 ],
183 InvalidArgumentException::class
184 ],
185 ];
186 }
187
188 /**
189 * @dataProvider provideHandlerSpecsWithException
190 */
191 public function testCreateContentHandlerForModelID_callWithProvider_throwsException(
192 array $handlerSpecs,
193 string $exceptionClassName
194 ): void {
195 if ( count( $handlerSpecs ) !== 1 ) {
196 $this->fail( 'Dataprovider provided wrong amount of specs' );
197 }
198
199 $factory = new ContentHandlerFactory(
200 $handlerSpecs,
201 $this->createSimpleObjectFactory(),
202 $this->createMock( HookContainer::class ),
203 $this->logger
204 );
205
206 $modelID = key( $handlerSpecs );
207 $this->expectException( $exceptionClassName );
208 $factory->getContentHandler( $modelID );
209 }
210
211 public function testCreateContentHandlerForModelID_callNotExist_throwMWUCMException() {
212 $this->expectException( MWUnknownContentModelException::class );
213 $factory = new ContentHandlerFactory(
214 [],
215 $this->createMock( ObjectFactory::class ),
216 $this->createMock( HookContainer::class ),
217 $this->logger
218 );
219 $factory->getContentHandler( 'ModelNameNotExist' );
220 }
221
222 public function testDefineContentHandler_flow_throwsException() {
223 $objectFactory = $this->createMock( ObjectFactory::class );
224 $objectFactory
225 ->method( 'createObject' )
226 ->willReturn( $this->createMock( DummyContentHandlerForTesting::class ) );
227 $factory = new ContentHandlerFactory(
228 [],
229 $objectFactory,
230 $this->createMock( HookContainer::class ),
231 $this->logger
232 );
233 $this->assertFalse( $factory->isDefinedModel( 'define test' ) );
234
235 $factory->defineContentHandler( 'define test', DummyContentHandlerForTesting::class );
236 $this->assertTrue( $factory->isDefinedModel( 'define test' ) );
237 $this->assertInstanceOf(
238 DummyContentHandlerForTesting::class,
239 $factory->getContentHandler( 'define test' )
240 );
241 }
242
243 /**
244 * @dataProvider provideValidDummySpecList
245 */
246 public function testGetContentModels_flow_same(
247 string $name1, string $name2, string $name3, string $name4
248 ): void {
249 $hookContainer = $this->createHookContainer();
250 $factory = new ContentHandlerFactory(
251 [
252 $name1 => DummyContentHandlerForTesting::class,
253 $name2 => DummyContentHandlerForTesting::class,
254 ],
255 $this->createMock( ObjectFactory::class ),
256 $hookContainer,
257 $this->logger
258 );
259 $this->assertArrayEquals(
260 [ $name1, $name2, ],
261 $factory->getContentModels() );
262
263 $factory->defineContentHandler(
264 $name3,
265 static function () {
266 }
267 );
268
269 $this->assertArrayEquals(
270 [ $name1, $name2, $name3, ],
271 $factory->getContentModels()
272 );
273
274 $hookContainer->register( 'GetContentModels',
275 static function ( &$models ) use ( $name4 ) {
276 $models[] = $name4;
277 } );
278 $this->assertArrayEquals(
279 [ $name1, $name2, $name3, $name4, ],
280 $factory->getContentModels()
281 );
282 }
283
284 /**
285 * @dataProvider provideValidDummySpecList
286 */
287 public function testIsDefinedModel_flow_same(
288 string $name1, string $name2, string $name3, string $name4
289 ): void {
290 $hookContainer = $this->createHookContainer();
291 $factory = new ContentHandlerFactory(
292 [
293 $name1 => DummyContentHandlerForTesting::class,
294 $name2 => DummyContentHandlerForTesting::class,
295 ],
296 $this->createMock( ObjectFactory::class ),
297 $hookContainer,
298 $this->logger
299 );
300
301 $this->assertTrue( $factory->isDefinedModel( $name1 ) );
302 $this->assertTrue( $factory->isDefinedModel( $name2 ) );
303 $this->assertFalse( $factory->isDefinedModel( $name3 ) );
304 $this->assertFalse( $factory->isDefinedModel( $name4 ) );
305 $this->assertFalse( $factory->isDefinedModel( 'not exist name' ) );
306
307 $factory->defineContentHandler(
308 $name3,
309 static function () {
310 }
311 );
312
313 $this->assertTrue( $factory->isDefinedModel( $name1 ) );
314 $this->assertTrue( $factory->isDefinedModel( $name2 ) );
315 $this->assertTrue( $factory->isDefinedModel( $name3 ) );
316 $this->assertFalse( $factory->isDefinedModel( $name4 ) );
317 $this->assertFalse( $factory->isDefinedModel( 'not exist name' ) );
318
319 $hookContainer->register(
320 'GetContentModels',
321 static function ( &$models ) use ( $name4 ) {
322 $models[] = $name4;
323 } );
324
325 $this->assertTrue( $factory->isDefinedModel( $name1 ) );
326 $this->assertTrue( $factory->isDefinedModel( $name2 ) );
327 $this->assertTrue( $factory->isDefinedModel( $name3 ) );
328 $this->assertTrue( $factory->isDefinedModel( $name4 ) );
329 $this->assertFalse( $factory->isDefinedModel( 'not exist name' ) );
330 }
331
332 public static function provideValidDummySpecList() {
333 return [
334 '1-0-3' => [
335 'mock name 1',
336 'mock name 0',
337 'mock name 3',
338 'mock name 4',
339 ],
340 ];
341 }
342
343 public function testGetContentModels_empty_empty() {
344 $factory = new ContentHandlerFactory(
345 [],
346 $this->createMock( ObjectFactory::class ),
347 $this->createMock( HookContainer::class ),
348 $this->logger
349 );
350
351 $this->assertArrayEquals( [], $factory->getContentModels() );
352 }
353
354 public function testGetAllContentFormats_flow_same() {
355 $contentHandler1 = $this->createMock( DummyContentHandlerForTesting::class );
356 $contentHandler1->method( 'getSupportedFormats' )->willReturn( [ 'format 1' ] );
357
358 $contentHandler2 = $this->createMock( DummyContentHandlerForTesting::class );
359 $contentHandler2->method( 'getSupportedFormats' )->willReturn( [ 'format 0' ] );
360
361 $contentHandler3 = $this->createMock( DummyContentHandlerForTesting::class );
362 $contentHandler3->method( 'getSupportedFormats' )->willReturn( [ 'format 3' ] );
363
364 $objectFactory = $this->createMock( ObjectFactory::class );
365 $objectFactory
366 ->method( 'createObject' )
367 ->willReturnOnConsecutiveCalls( $contentHandler1, $contentHandler2, $contentHandler3 );
368
369 $factory = new ContentHandlerFactory(
370 [
371 'mock name 1' => static function () {
372 // return new DummyContentHandlerForTesting( 'mock 1', [ 'format 1' ] );
373 },
374 'mock name 2' => static function () {
375 // return new DummyContentHandlerForTesting( 'mock 0', [ 'format 0' ] );
376 },
377 ],
378 $objectFactory,
379 $this->createMock( HookContainer::class ),
380 $this->logger
381 );
382
383 $this->assertArrayEquals( [
384 'format 1',
385 'format 0',
386 ],
387 $factory->getAllContentFormats() );
388
389 $factory->defineContentHandler( 'some new name',
390 static function () {
391 // return new DummyContentHandlerForTesting( 'mock defined', [ 'format defined' ] );
392 } );
393
394 $this->assertArrayEquals( [
395 'format 1',
396 'format 0',
397 'format 3',
398 ],
399 $factory->getAllContentFormats() );
400 }
401
402 }