Debian Codegraph @

tests/phpunit/unit/includes/HookContainer/HookContainerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\HookContainer {
4
5 use Error;
6 use InvalidArgumentException;
7 use LogicException;
8 use MediaWiki\HookContainer\HookContainer;
9 use MediaWiki\HookContainer\StaticHookRegistry;
10 use MediaWiki\Tests\Unit\DummyServicesTrait;
11 use MediaWikiUnitTestCase;
12 use stdClass;
13 use UnexpectedValueException;
14 use Wikimedia\ScopedCallback;
15 use Wikimedia\TestingAccessWrapper;
16
17 class HookContainerTest extends MediaWikiUnitTestCase {
18 use DummyServicesTrait;
19
20 private const HANDLER_FUNCTION = FooClass::class . '::fooStaticMethod';
21
22 private const HANDLER_REGISTRATION = [
23 'extensionPath' => __DIR__,
24 'handler' => [
25 'name' => 'TestHookHandler',
26 'class' => 'FooExtension\Hooks'
27 ]
28 ];
29
30 /**
31 * Creates a new hook container with StaticHookRegistry and empty ObjectFactory
32 *
33 * @param null|array $oldHooks
34 * @param null|array $newHooks
35 * @param array $deprecatedHooksArray
36 *
37 * @return HookContainer
38 */
39 private function newHookContainer(
40 $oldHooks = null, $newHooks = null, $deprecatedHooksArray = []
41 ) {
42 if ( $oldHooks === null ) {
43 $oldHooks[ 'FoobarActionComplete' ][] = static function ( &$called ) {
44 $called[] = 11;
45 };
46 }
47 if ( $newHooks === null ) {
48 $handler = [ 'handler' => [
49 'name' => 'FooExtension-FooActionHandler',
50 'class' => 'FooExtension\\Hooks',
51 'services' => [] ]
52 ];
53 $newHooks = [ 'FooActionComplete' => [ $handler ] ];
54 }
55
56 // fake object factory
57 $objectFactory = $this->getDummyObjectFactory(
58 [
59 'SomeService' => static function () {
60 return new stdClass();
61 }
62 ]
63 );
64
65 $registry = new StaticHookRegistry( $oldHooks, $newHooks, $deprecatedHooksArray );
66 $hookContainer = new HookContainer( $registry, $objectFactory );
67 return $hookContainer;
68 }
69
70 public static function provideRegister() {
71 return [
72 'function' => [ 'strtoupper', 'strtoupper' ],
73 'object' => [ new \FooExtension\Hooks(), 'FooExtension\Hooks::onFooActionComplete' ],
74 'object and method' => [ [ new FooClass(), 'fooMethod' ], 'MediaWiki\Tests\HookContainer\FooClass::fooMethod' ],
75 'extension' => [
76 self::HANDLER_REGISTRATION,
77 'FooExtension\Hooks::onFooActionComplete'
78 ],
79 'callable referencing a class that extends an unknown class' => [
80 [ 'MediaWiki\Tests\BrokenClass', 'aMethod' ],
81 'MediaWiki\Tests\BrokenClass::aMethod'
82 ],
83 ];
84 }
85
86 /**
87 * @covers \MediaWiki\HookContainer\HookContainer::register
88 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
89 * @dataProvider provideRegister
90 */
91 public function testRegister( $handler, $expected ) {
92 $hookContainer = $this->newHookContainer( [], [
93 'FooActionComplete' => [ $handler ]
94 ], [] );
95
96 $handlers = $hookContainer->getHandlerDescriptions( 'FooActionComplete' );
97
98 $this->assertSame( $expected, $handlers[0] );
99 }
100
101 /**
102 * @covers \MediaWiki\HookContainer\HookContainer::getHandlerDescriptions
103 */
104 public function testGetHandlerDescriptions() {
105 $handler = 'MediaWiki\Tests\HookContainer\FooClass::fooStaticMethod';
106 $expected = [ $handler ];
107
108 $hookContainer = $this->newHookContainer(
109 [
110 'BarActionComplete' => [ $handler ]
111 ],
112 [
113 'FooActionComplete' => [ $handler ]
114 ], [] );
115
116 $this->assertSame( $expected, $hookContainer->getHandlerDescriptions( 'FooActionComplete' ) );
117 $this->assertSame( $expected, $hookContainer->getHandlerDescriptions( 'BarActionComplete' ) );
118
119 // Fire the hooks, then check again
120 $hookContainer->run( 'FooActionComplete', [ 1 ] );
121 $hookContainer->run( 'BarActionComplete', [ 1 ] );
122
123 $this->assertSame( $expected, $hookContainer->getHandlerDescriptions( 'FooActionComplete' ) );
124 $this->assertSame( $expected, $hookContainer->getHandlerDescriptions( 'BarActionComplete' ) );
125 }
126
127 /**
128 * Values returned: hook, handlersToRegister, expectedReturn
129 */
130 public static function provideGetHandlerDescriptions() {
131 return [
132 'NoHandlersExist' => [
133 'MWTestHook',
134 null,
135 0,
136 0
137 ],
138 'SuccessfulHandlerReturn' => [
139 'FooActionComplete',
140 [
141 'handler' => [
142 'name' => 'FooExtension-FooActionHandler',
143 'class' => 'FooExtension\\Hooks',
144 'services' => [],
145 ],
146 ],
147 1,
148 1
149 ],
150 'SkipDeprecated' => [
151 'FooActionCompleteDeprecated',
152 [
153 'handler' => [
154 'name' => 'FooExtension-FooActionHandler',
155 'class' => 'FooExtension\\Hooks',
156 'services' => [],
157 ],
158 'deprecated' => true,
159 ],
160 1,
161 0
162 ],
163 ];
164 }
165
166 /**
167 * @covers \MediaWiki\HookContainer\HookContainer::salvage
168 */
169 public function testSalvage() {
170 $firstHookContainer = $this->newHookContainer( [], [] );
171 $secondHookContainer = $this->newHookContainer();
172
173 $firstHookContainer->register( 'TestHook', self::HANDLER_FUNCTION );
174
175 $secondHookContainer->salvage( $firstHookContainer );
176
177 $this->assertTrue( $secondHookContainer->isRegistered( 'TestHook' ) );
178 }
179
180 /**
181 * @covers \MediaWiki\HookContainer\HookContainer::salvage
182 */
183 public function testSalvageThrows() {
184 $firstHookContainer = $this->newHookContainer( [], [] );
185 $secondHookContainer = $this->newHookContainer();
186
187 $secondHookContainer->register( 'TestHook', self::HANDLER_FUNCTION );
188
189 $this->expectException( LogicException::class );
190 $secondHookContainer->salvage( $firstHookContainer );
191 }
192
193 /**
194 * @covers \MediaWiki\HookContainer\HookContainer::isRegistered
195 * @covers \MediaWiki\HookContainer\HookContainer::register
196 * @covers \MediaWiki\HookContainer\HookContainer::clear
197 */
198 public function testIsRegistered() {
199 $hookContainer = $this->newHookContainer(
200 [ 'XyzHook' => [ self::HANDLER_FUNCTION ] ],
201 [ 'MWTestHook' => [ self::HANDLER_REGISTRATION ] ],
202 );
203
204 $hookContainer->register( 'AbcHook', self::HANDLER_FUNCTION );
205
206 $this->assertFalse( $hookContainer->isRegistered( 'XyzzyHook' ) );
207
208 $this->assertTrue( $hookContainer->isRegistered( 'XyzHook' ) );
209 $this->assertTrue( $hookContainer->isRegistered( 'MWTestHook' ) );
210 $this->assertTrue( $hookContainer->isRegistered( 'AbcHook' ) );
211
212 $hookContainer->clear( 'AbcHook' );
213 $hookContainer->clear( 'XyzHook' );
214 $hookContainer->clear( 'MWTestHook' );
215
216 $this->assertFalse( $hookContainer->isRegistered( 'XyzHook' ) );
217 $this->assertFalse( $hookContainer->isRegistered( 'MWTestHook' ) );
218 $this->assertFalse( $hookContainer->isRegistered( 'AbcHook' ) );
219 }
220
221 /**
222 * @covers \MediaWiki\HookContainer\HookContainer::scopedRegister
223 */
224 public function testScopedRegister() {
225 $hookContainer = $this->newHookContainer();
226 $reset = $hookContainer->scopedRegister( 'MWTestHook', [ new FooClass(),
227 'fooMethod'
228 ] );
229 $this->assertTrue( $hookContainer->isRegistered( 'MWTestHook' ) );
230 ScopedCallback::consume( $reset );
231 $this->assertFalse( $hookContainer->isRegistered( 'MWTestHook' ) );
232 }
233
234 /**
235 * @covers \MediaWiki\HookContainer\HookContainer::scopedRegister
236 */
237 public function testScopedRegisterTwoHandlers() {
238 $hookContainer = $this->newHookContainer();
239 $called1 = $called2 = false;
240 $reset1 = $hookContainer->scopedRegister( 'MWTestHook',
241 static function () use ( &$called1 ) {
242 $called1 = true;
243 }
244 );
245 $reset2 = $hookContainer->scopedRegister( 'MWTestHook',
246 static function () use ( &$called2 ) {
247 $called2 = true;
248 }
249 );
250 $hookContainer->run( 'MWTestHook' );
251 $this->assertTrue( $called1 );
252 $this->assertTrue( $called2 );
253
254 $called1 = $called2 = false;
255 ScopedCallback::consume( $reset1 );
256 $hookContainer->run( 'MWTestHook' );
257 $this->assertFalse( $called1 );
258 $this->assertTrue( $called2 );
259
260 $called1 = $called2 = false;
261 ScopedCallback::consume( $reset2 );
262 $hookContainer->run( 'MWTestHook' );
263 $this->assertFalse( $called1 );
264 $this->assertFalse( $called2 );
265 }
266
267 /**
268 * Register handlers with scopedRegister() and register()
269 * @covers \MediaWiki\HookContainer\HookContainer::scopedRegister
270 */
271 public function testHandlersRegisteredWithScopedRegisterAndRegister() {
272 $hookContainer = $this->newHookContainer();
273 $numCalls = 0;
274 $hookContainer->register( 'MWTestHook', static function () use ( &$numCalls ) {
275 $numCalls++;
276 } );
277 $reset = $hookContainer->scopedRegister( 'MWTestHook', static function () use ( &$numCalls ) {
278 $numCalls++;
279 } );
280
281 // handlers registered in 2 different ways
282 $this->assertCount( 2, $hookContainer->getHandlerDescriptions( 'MWTestHook' ) );
283 $hookContainer->run( 'MWTestHook' );
284 $this->assertEquals( 2, $numCalls );
285
286 // Remove one of the handlers that increments $called
287 ScopedCallback::consume( $reset );
288 $this->assertCount( 1, $hookContainer->getHandlerDescriptions( 'MWTestHook' ) );
289
290 $numCalls = 0;
291 $hookContainer->run( 'MWTestHook' );
292 $this->assertSame( 1, $numCalls );
293 }
294
295 /**
296 * @covers \MediaWiki\HookContainer\HookContainer::getHandlerDescriptions
297 * @covers \MediaWiki\HookContainer\HookContainer::getHandlerCallbacks
298 * @dataProvider provideGetHandlerDescriptions
299 */
300 public function testGetHandlers(
301 string $hook,
302 ?array $handlerToRegister,
303 int $expectedDescriptions,
304 int $expectedCallbacks
305 ) {
306 if ( $handlerToRegister ) {
307 $hooks = [ $hook => [ $handlerToRegister ] ];
308 } else {
309 $hooks = [];
310 }
311 $fakeDeprecatedHooks = [
312 'FooActionCompleteDeprecated' => [ 'deprecatedVersion' => '1.35' ]
313 ];
314 $hookContainer = $this->newHookContainer( [], $hooks, $fakeDeprecatedHooks );
315
316 $descriptions = $hookContainer->getHandlerDescriptions( $hook );
317 $this->assertCount(
318 $expectedDescriptions,
319 $descriptions,
320 'getHandlerDescriptions()'
321 );
322
323 $this->expectDeprecationAndContinue( '/getHandlerCallbacks/' );
324 $callbacks = $hookContainer->getHandlerCallbacks( $hook );
325 $this->assertCount(
326 $expectedCallbacks,
327 $callbacks,
328 'getHandlerCallbacks()'
329 );
330
331 foreach ( $callbacks as $clbk ) {
332 $this->assertIsCallable( $clbk );
333 }
334 }
335
336 public static function provideRunConfigured() {
337 $fooObj = new FooClass();
338 $closure = static function ( &$count ) {
339 $count++;
340 };
341 $extra = 10;
342 return [
343 // Callables
344 'Function' => [ 'fooGlobalFunction' ],
345 'Object and method' => [ [ $fooObj, 'fooMethod' ] ],
346 'Class name and static method' => [ [ 'MediaWiki\Tests\HookContainer\FooClass', 'fooStaticMethod' ] ],
347 'static method' => [ 'MediaWiki\Tests\HookContainer\FooClass::fooStaticMethod' ],
348 'Closure' => [ $closure ],
349
350 // Shorthand
351 'Object' => [ $fooObj ],
352
353 // No-ops
354 'NOOP' => [ HookContainer::NOOP, 1 ],
355 ];
356 }
357
358 /**
359 * @covers \MediaWiki\HookContainer\HookContainer::run
360 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
361 * @dataProvider provideRunConfigured
362 */
363 public function testRunConfigured( $handler, $expectedCount = 2 ) {
364 $hookContainer = $this->newHookContainer( [ 'Increment' => [ $handler ] ] );
365
366 $count = 1;
367 $hookValue = $hookContainer->run( 'Increment', [ &$count ] );
368 $this->assertTrue( $hookValue );
369 $this->assertSame( $expectedCount, $count );
370 }
371
372 public static function provideRunDeprecatedStyle() {
373 $fooObj = new FooClass();
374 $closure = static function ( &$count ) {
375 $count++;
376 };
377 $extra = 10;
378 return [
379 // Handlers with extra data attached
380 'static method with extra data' => [
381 [ 'MediaWiki\Tests\HookContainer\FooClass::fooStaticMethodWithExtra', $extra ],
382 11
383 ],
384 'Object and method with extra data' => [ [ [ $fooObj, 'fooMethodWithExtra' ], $extra ], 11 ],
385 'Function extra data' => [ [ 'fooGlobalFunctionWithExtra', $extra ], 11 ],
386 'Closure with extra data' => [
387 [
388 static function ( int $inc, &$count ) {
389 $count += $inc;
390 },
391 10
392 ],
393 11
394 ],
395
396 // No-ops
397 'empty array' => [ [], 1 ],
398 'null' => [ null, 1 ],
399 'false' => [ false, 1 ],
400
401 // Strange edge cases
402 'Object in array without method' => [ [ $fooObj ] ],
403 'Callable in array' => [ [ [ $fooObj, 'fooMethod' ] ] ],
404 'Closure in array with no extra data' => [ [ $closure ] ],
405 'Function in array' => [ [ 'fooGlobalFunction' ] ],
406 'Function in array in array' => [ [ [ 'fooGlobalFunction' ] ] ],
407 'static method as array in array' => [
408 [ [ 'MediaWiki\Tests\HookContainer\FooClass', 'fooStaticMethod' ] ]
409 ],
410 'Object and fully-qualified non-static method' => [
411 [ $fooObj, 'MediaWiki\Tests\HookContainer\FooClass::fooMethod' ]
412 ]
413 ];
414 }
415
416 /**
417 * @covers \MediaWiki\HookContainer\HookContainer::run
418 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
419 * @dataProvider provideRunDeprecatedStyle
420 */
421 public function testRunDeprecatedStyle( $handler, $expectedCount = 2 ) {
422 $hookContainer = $this->newHookContainer( [ 'Increment' => [ $handler ] ] );
423
424 $this->expectDeprecationAndContinue( '/Deprecated handler style/' );
425
426 $count = 1;
427 $hookValue = $hookContainer->run( 'Increment', [ &$count ] );
428 $this->assertTrue( $hookValue );
429 $this->assertSame( $expectedCount, $count );
430 }
431
432 /**
433 * @covers \MediaWiki\HookContainer\HookContainer::run
434 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
435 * @dataProvider provideRunConfigured
436 * @dataProvider provideRunExtensionHook
437 */
438 public function testRegisterAndRun( $handler, $expectedCount = 2 ) {
439 $hookContainer = $this->newHookContainer( [], [] );
440 $hookContainer->register( 'Increment', $handler );
441
442 $count = 1;
443 $hookValue = $hookContainer->run( 'Increment', [ &$count ] );
444 $this->assertTrue( $hookValue );
445 $this->assertSame( $expectedCount, $count );
446 }
447
448 /**
449 * @covers \MediaWiki\HookContainer\HookContainer::run
450 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
451 * @dataProvider provideRunDeprecatedStyle
452 */
453 public function testRegisterDeprecatedStyle( $handler ) {
454 $hookContainer = $this->newHookContainer( [], [] );
455
456 // Force the handler list to be initialized, so register() will normalize the handler immediately.
457 $hookContainer->run( 'Increment' );
458
459 $this->expectDeprecationAndContinue( '/Deprecated handler style for hook/' );
460 $hookContainer->register( 'Increment', $handler );
461 }
462
463 /**
464 * Values returned: hook, handler, handler arguments, options
465 */
466 public static function provideRegisterAndRunCallback() {
467 $fooObj = new FooClass();
468 return [
469 // Callables
470 'Function' => [ 'fooGlobalFunction' ],
471 'Object and method' => [ [ $fooObj, 'fooMethod' ] ],
472 'Class name and static method' => [ [ 'MediaWiki\Tests\HookContainer\FooClass', 'fooStaticMethod' ] ],
473 'static method' => [ 'MediaWiki\Tests\HookContainer\FooClass::fooStaticMethod' ],
474 'Closure' => [
475 static function ( &$count ) {
476 $count++;
477 }
478 ],
479
480 // Extension-style handler
481 'Extension handler' => [ self::HANDLER_REGISTRATION ],
482
483 // NOTE: hook handlers with extra data are not supported for callbacks!
484 ];
485 }
486
487 /**
488 * @covers \MediaWiki\HookContainer\HookContainer::getHandlerCallbacks
489 * @dataProvider provideRegisterAndRunCallback
490 */
491 public function testRegisterAndRunCallback( $handler, $expectedCount = 2 ) {
492 $hookContainer = $this->newHookContainer( [], [] );
493 $hookContainer->register( 'Increment', $handler );
494
495 $this->expectDeprecationAndContinue( '/getHandlerCallbacks/' );
496
497 $count = 1;
498 foreach ( $hookContainer->getHandlerCallbacks( 'Increment' ) as $callback ) {
499 $callback( $count );
500 }
501 $this->assertSame( $expectedCount, $count );
502 }
503
504 /**
505 * Values returned: hook, handler, handler arguments, options
506 */
507 public static function provideRunExtensionHook() {
508 return [
509 [ self::HANDLER_REGISTRATION ],
510 ];
511 }
512
513 /**
514 * @covers \MediaWiki\HookContainer\HookContainer::run
515 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
516 * @dataProvider provideRunExtensionHook
517 */
518 public function testRunExtensionHook( array $handler, $expectedCount = 1 ) {
519 $hookContainer = $this->newHookContainer( [], [ 'X\\Y::Increment' => [ $handler ] ] );
520
521 $count = 0;
522 $hookValue = $hookContainer->run( 'X\\Y::Increment', [ &$count ] );
523 $this->assertTrue( $hookValue );
524 $this->assertSame( $expectedCount, $count );
525 }
526
527 public static function provideRunFailsWithNoService() {
528 $handler = self::HANDLER_REGISTRATION;
529 $handler['handler']['services'] = [ 'SomeService' ];
530
531 yield [ $handler ];
532
533 $handler = self::HANDLER_REGISTRATION;
534 $handler['handler']['optional_services'] = [ 'SomeService' ];
535
536 yield [ $handler ];
537 }
538
539 /**
540 * @covers \MediaWiki\HookContainer\HookContainer::run
541 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
542 * @dataProvider provideRunFailsWithNoService
543 */
544 public function testRunFailsWithNoService( array $handler ) {
545 $hookContainer = $this->newHookContainer( [], [ 'Increment' => [ $handler ] ] );
546
547 $this->expectException( UnexpectedValueException::class );
548
549 $count = 0;
550 $options = [ 'noServices' => true ];
551 $hookContainer->run( 'Increment', [ &$count ], $options );
552 }
553
554 /**
555 * @covers \MediaWiki\HookContainer\HookContainer::run
556 */
557 public function testRunOrder() {
558 $configured1 = static function ( &$seq ) {
559 $seq[] = 'configured1';
560 };
561
562 $configured2 = static function ( &$seq ) {
563 $seq[] = 'configured2';
564 };
565
566 $registered = static function ( &$seq ) {
567 $seq[] = 'registered';
568 };
569
570 $hookContainer = $this->newHookContainer(
571 [ 'Append' => [ $configured1, $configured2 ] ],
572 [ 'Append' => [ self::HANDLER_REGISTRATION ] ]
573 );
574
575 $hookContainer->register( 'Append', $registered );
576
577 $seq = [ 'start' ];
578 $hookContainer->run( 'Append', [ &$seq ] );
579
580 $expected = [ 'start', 'configured1', 'configured2', 'FooExtension', 'registered' ];
581 $this->assertSame( $expected, $seq );
582 }
583
584 /**
585 * @covers \MediaWiki\HookContainer\HookContainer::run
586 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
587 * Test HookContainer::run() when the handler returns false
588 */
589 public function testRunAbort() {
590 $handler1 = [ 'handler' => [
591 'name' => 'FooExtension-Abort1',
592 'class' => 'FooExtension\\AbortHooks1'
593 ] ];
594 $handler2 = [ 'handler' => [
595 'name' => 'FooExtension-Abort2',
596 'class' => 'FooExtension\\AbortHooks2'
597 ] ];
598 $handler3 = [ 'handler' => [
599 'name' => 'FooExtension-Abort3',
600 'class' => 'FooExtension\\AbortHooks3'
601 ] ];
602 $hooks = [
603 'Abort' => [
604 $handler1,
605 $handler2,
606 $handler3
607 ]
608 ];
609 $hookContainer = $this->newHookContainer( [], $hooks );
610 $called = [];
611 $ret = $hookContainer->run( 'Abort', [ &$called ] );
612 $this->assertFalse( $ret );
613 $this->assertArrayEquals( [ 1, 2 ], $called );
614 }
615
616 public static function provideRegisterDeprecated() {
617 // registering a deprecated hook should trigger a warning
618 yield [ [ 'deprecatedVersion' => '1.0' ], true ];
619
620 // the silent flag should suppress the warning
621 yield [ [ 'deprecatedVersion' => '1.0', 'silent' => true ], false ];
622 }
623
624 /**
625 * Test HookContainer::register() successfully registers even when hook is deprecated.
626 * @covers \MediaWiki\HookContainer\HookContainer::register
627 * @dataProvider provideRegisterDeprecated
628 */
629 public function testRegisterDeprecated( array $deprecationInfo, bool $expectWarning ) {
630 $deprecations = [ 'FooActionComplete' => $deprecationInfo ];
631
632 // Assert we don't get any deprecation warnings during initialization!
633 $this->newHookContainer(
634 [ 'FooActionComplete' => [ self::HANDLER_FUNCTION ] ],
635 [ 'FooActionComplete' => [ self::HANDLER_REGISTRATION ] ],
636 $deprecations
637 );
638
639 // Make a hook container with no hooks registered yet
640 $hookContainer = $this->newHookContainer( [], [], $deprecations );
641
642 // Expected deprecation?
643 if ( $expectWarning ) {
644 $this->expectDeprecationAndContinue( '/FooActionComplete hook/' );
645 }
646
647 $hookContainer->register( 'FooActionComplete', self::HANDLER_FUNCTION );
648
649 // Deprecated hooks should still be functional!
650 $this->assertTrue( $hookContainer->isRegistered( 'FooActionComplete' ) );
651 }
652
653 /**
654 * Test running deprecated hooks from $wgHooks with the deprecation declared in HookContainer.
655 * @covers \MediaWiki\HookContainer\HookContainer::run
656 * @dataProvider provideRegisterDeprecated
657 */
658 public function testRunConfiguredDeprecated( array $deprecationInfo ) {
659 $hookContainer = $this->newHookContainer(
660 [ 'Increment' => [ self::HANDLER_FUNCTION ] ],
661 [],
662 [ 'Increment' => $deprecationInfo ]
663 );
664
665 // No warning expected when running the hook!
666 // Deprecated hooks should still be functional!
667 $count = 0;
668 $hookContainer->run( 'Increment', [ &$count ] );
669 $this->assertSame( 1, $count );
670 }
671
672 /**
673 * Test running deprecated hooks from $wgHooks with the deprecation passed in the options parameter.
674 * @covers \MediaWiki\HookContainer\HookContainer::run
675 * @dataProvider provideRegisterDeprecated
676 */
677 public function testRunConfiguredDeprecatedWithOption( array $deprecationInfo, bool $expectWarning ) {
678 // Assert we don't get any deprecation warnings during initialization!
679 $hookContainer = $this->newHookContainer(
680 [ 'Increment' => [ self::HANDLER_FUNCTION ] ],
681 );
682
683 // Expected deprecation?
684 if ( $expectWarning ) {
685 $this->expectDeprecationAndContinue( '/Use of Increment hook/' );
686 }
687
688 // Deprecated hooks should still be functional!
689 $count = 0;
690 $hookContainer->run( 'Increment', [ &$count ], $deprecationInfo );
691 $this->assertSame( 1, $count );
692 }
693
694 /**
695 * Test running deprecated hooks from extensions.
696 *
697 * @covers \MediaWiki\HookContainer\HookContainer::run
698 */
699 public function testRunHandlerObjectDeprecated() {
700 $deprecationInfo = [ 'deprecatedVersion' => '1.0' ];
701
702 // If the handler acknowledges deprecation, it should be skipped
703 $knownDeprecated = self::HANDLER_REGISTRATION + [ 'deprecated' => true ];
704
705 $hookContainer = $this->newHookContainer(
706 [],
707 [ 'Increment' => [ self::HANDLER_REGISTRATION, $knownDeprecated ] ],
708 [ 'Increment' => $deprecationInfo ]
709 );
710
711 // Deprecated hooks should be functional, the handle that acknowledges deprecation should be skipped.
712 // We do not expect deprecation warnings here. They are covered by emitDeprecationWarnings()
713 $count = 0;
714 $hookContainer->run( 'Increment', [ &$count ] );
715 $this->assertSame( 1, $count );
716 }
717
718 /**
719 * Test running deprecated hooks from extensions.
720 *
721 * @covers \MediaWiki\HookContainer\HookContainer::run
722 */
723 public function testRunHandlerObjectDeprecatedWithOption() {
724 $deprecationInfo = [ 'deprecatedVersion' => '1.0' ];
725
726 // If the handler acknowledges deprecation, it should be skipped
727 $knownDeprecated = self::HANDLER_REGISTRATION + [ 'deprecated' => true ];
728
729 $hookContainer = $this->newHookContainer(
730 [],
731 [ 'Increment' => [ self::HANDLER_REGISTRATION, $knownDeprecated ] ],
732 [ 'Increment' => $deprecationInfo ]
733 );
734
735 // We do expect deprecation warnings when the 'deprecationVersion' key is provided in the $options parameter.
736 $this->expectDeprecationAndContinue( '/Use of Increment hook/' );
737
738 // Deprecated hooks should be functional, the handle that acknowledges deprecation should be skipped.
739 $count = 0;
740 $hookContainer->run( 'Increment', [ &$count ], $deprecationInfo );
741 $this->assertSame( 1, $count );
742 }
743
744 /**
745 * @covers \MediaWiki\HookContainer\HookContainer::getHookNames
746 */
747 public function testGetHookNames() {
748 $fooHandler = [ 'handler' => [
749 'name' => 'FooHookHandler',
750 'class' => 'FooExtension\\Hooks'
751 ] ];
752
753 $noop = static function () {
754 // noop
755 };
756
757 $container = $this->newHookContainer(
758 [
759 'A' => [ $noop ]
760 ],
761 [
762 'B' => [ $fooHandler ]
763 ]
764 );
765
766 $container->register( 'C', 'strtoupper' );
767
768 // Ask for a few hooks that have no handlers.
769 // Negative caching inside HookHandler should not cause them to be returned from getHookNames
770 $container->isRegistered( 'X' );
771
772 $this->expectDeprecationAndContinue( '/getHandlerCallbacks/' );
773 $container->getHandlerCallbacks( 'Y' );
774
775 $this->assertArrayEquals( [ 'A', 'B', 'C' ], $container->getHookNames() );
776
777 // make sure we are getting each hook name only once
778 $container->register( 'B', 'strtoupper' );
779 $container->register( 'A', 'strtoupper' );
780
781 $this->assertArrayEquals( [ 'A', 'B', 'C' ], $container->getHookNames() );
782 }
783
784 /**
785 * Values returned: hook, handlersToRegister, options
786 */
787 public static function provideRunErrors() {
788 // XXX: should also fail: non-function string, empty array
789 return [
790 'return a string' => [
791 static function () {
792 return 'string';
793 },
794 []
795 ],
796 'abort even though not abortable' => [
797 static function () {
798 return false;
799 },
800 [ 'abortable' => false ]
801 ],
802 'callable referencing a class that extends an unknown class' => [
803 [ 'MediaWiki\\Tests\\BrokenClass2', 'aMethod' ],
804 [],
805 Error::class
806 ],
807 ];
808 }
809
810 /**
811 * @dataProvider provideRunErrors
812 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
813 * Test errors thrown with invalid handlers
814 */
815 public function testRunErrors( $handler, $options, $expected = UnexpectedValueException::class ) {
816 $hookContainer = $this->newHookContainer();
817 $hookContainer->register( 'MWTestHook', $handler );
818
819 $this->filterDeprecated( '/^Returning a string from a hook handler/' );
820 $this->expectException( $expected );
821 $hookContainer->run( 'MWTestHook', [], $options );
822 }
823
824 /**
825 * Values returned: hook, handlersToRegister, options
826 */
827 public static function provideRegisterErrors() {
828 // XXX: should also fail: non-function string, empty array
829 return [
830 'a number' => [ 123 ],
831 'non-callable string' => [ 'a, b, c' ],
832 'array referencing an unknown method' => [ [ self::class, 'thisMethodDoesNotExist' ] ],
833 'empty string' => [ '' ],
834 'zero' => [ 0 ],
835 'true' => [ true ],
836 'callable referencing an unknown class' => [
837 [ 'FooExtension\DoesNotExist', 'onFoo' ],
838 'FooExtension\DoesNotExist::onFoo'
839 ],
840 ];
841 }
842
843 /**
844 * @dataProvider provideRegisterErrors
845 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
846 * @covers \MediaWiki\HookContainer\HookContainer::register
847 */
848 public function testRegisterErrors( $badHandler ) {
849 $hookContainer = $this->newHookContainer();
850
851 // Force the handler list to be initialized, so register() will normalize the handler immediately.
852 $hookContainer->run( 'MWTestHook' );
853
854 $this->expectException( InvalidArgumentException::class );
855 $hookContainer->register( 'MWTestHook', $badHandler );
856 }
857
858 /**
859 * @dataProvider provideRegisterErrors
860 * @covers \MediaWiki\HookContainer\HookContainer::normalizeHandler
861 * @covers \MediaWiki\HookContainer\HookContainer::run
862 */
863 public function testRunWithBadHandlers( $badHandler ) {
864 $goodHandler = self::HANDLER_FUNCTION;
865 $hookContainer = $this->newHookContainer( [ 'MWTestHook' => [ $badHandler, $goodHandler ] ] );
866
867 // Bad handlers from the constructor should fail silently
868 $count = 0;
869 $hookContainer->run( 'MWTestHook', [ &$count ] );
870
871 $this->assertSame( 1, $count );
872 }
873
874 public static function provideEmitDeprecationWarnings() {
875 yield 'Deprecated extension hook' => [
876 '$oldHooks' => [],
877 '$newHooks' => [ self::HANDLER_REGISTRATION ],
878 '$deprecationInfo' => [ 'deprecatedVersion' => '1.35' ],
879 '$expectWarning' => true,
880 ];
881
882 yield 'Deprecated extension hook, silent' => [
883 '$oldHooks' => [],
884 '$newHooks' => [ self::HANDLER_REGISTRATION ],
885 '$deprecationInfo' => [ 'deprecatedVersion' => '1.35', 'silent' => true ],
886 '$expectWarning' => false,
887 ];
888
889 yield 'Deprecated extension hook, acknowledged' => [
890 '$oldHooks' => [],
891 '$newHooks' => [ self::HANDLER_REGISTRATION + [ 'deprecated' => true ] ],
892 '$deprecationInfo' => [ 'deprecatedVersion' => '1.35' ],
893 '$expectWarning' => false,
894 ];
895
896 yield 'Deprecated configured hook' => [
897 '$oldHooks' => [ self::HANDLER_FUNCTION ],
898 '$newHooks' => [],
899 '$deprecationInfo' => [ 'deprecatedVersion' => '1.35' ],
900 '$expectWarning' => false, // NOTE: Currently expected to be ignored. This may change.
901 ];
902
903 yield 'Deprecated configured hook, silent' => [
904 '$oldHooks' => [ self::HANDLER_FUNCTION ],
905 '$newHooks' => [],
906 '$deprecationInfo' => [ 'deprecatedVersion' => '1.35', 'silent' => true ],
907 '$expectWarning' => false,
908 ];
909 }
910
911 /**
912 * @covers \MediaWiki\HookContainer\HookContainer::emitDeprecationWarnings
913 * @dataProvider provideEmitDeprecationWarnings
914 */
915 public function testEmitDeprecationWarnings( $oldHandlers, $newHandlers, $deprecationInfo, $expectWarning ) {
916 $hookContainer = $this->newHookContainer(
917 [ 'FooActionComplete' => $oldHandlers ],
918 [ 'FooActionComplete' => $newHandlers ],
919 [ 'FooActionComplete' => $deprecationInfo ]
920 );
921
922 if ( $expectWarning ) {
923 $this->expectDeprecationAndContinue( '/Hook FooActionComplete was deprecated/' );
924 }
925
926 $hookContainer->emitDeprecationWarnings();
927 $this->addToAssertionCount( 1 );
928 }
929
930 /**
931 * @covers \MediaWiki\HookContainer\HookContainer::emitDeprecationWarnings
932 */
933 public function testEmitDeprecationWarningsSilent() {
934 $hooks = [
935 'FooActionComplete' => [
936 [
937 'handler' => 'fooGlobalFunction',
938 'extensionPath' => 'fake-extension.json'
939 ]
940 ]
941 ];
942 $deprecatedHooksArray = [
943 'FooActionComplete' => [
944 'deprecatedVersion' => '1.35',
945 'silent' => true
946 ]
947 ];
948 $hookContainer = $this->newHookContainer( [], $hooks, $deprecatedHooksArray );
949 $hookContainer->emitDeprecationWarnings();
950 $this->assertTrue( true );
951 }
952
953 /**
954 * @covers \MediaWiki\HookContainer\HookContainer::isRegistered
955 * @covers \MediaWiki\HookContainer\HookContainer::getHandlerCallbacks
956 * @covers \MediaWiki\HookContainer\HookContainer::register
957 * @covers \MediaWiki\HookContainer\HookContainer::clear
958 */
959 public function testClear() {
960 $increment = [ new \FooExtension\Hooks(), 'onIncrement' ];
961
962 $hookContainer = $this->newHookContainer(
963 [ 'Increment' => [ $increment ], 'XyzHook' => [ self::HANDLER_FUNCTION ], ],
964 [ 'Increment' => [ self::HANDLER_REGISTRATION ], 'FooActionComplete' => [ self::HANDLER_REGISTRATION ] ],
965 );
966
967 $hookContainer->register( 'AbcHook', self::HANDLER_FUNCTION );
968 $hookContainer->register( 'Increment', static function ( &$count ) {
969 $count++;
970 } );
971
972 // Check: all three handlers should be called initially.
973 $count = 0;
974 $hookContainer->run( 'Increment', [ &$count ] );
975 $this->assertSame( 3, $count );
976
977 $hookContainer->clear( 'Increment' );
978
979 $this->assertFalse( $hookContainer->isRegistered( 'Increment' ) );
980 $this->assertTrue( $hookContainer->isRegistered( 'AbcHook' ) );
981 $this->assertTrue( $hookContainer->isRegistered( 'XyzHook' ) );
982 $this->assertTrue( $hookContainer->isRegistered( 'FooActionComplete' ) );
983
984 $this->assertCount( 0, $hookContainer->getHandlerDescriptions( 'Increment' ) );
985 $this->assertNotEmpty( $hookContainer->getHandlerDescriptions( 'AbcHook' ) );
986 $this->assertNotEmpty( $hookContainer->getHandlerDescriptions( 'FooActionComplete' ) );
987 $this->assertNotEmpty( $hookContainer->getHandlerDescriptions( 'XyzHook' ) );
988
989 // No more increment!
990 $hookContainer->run( 'Increment', [ &$count ] );
991 $this->assertSame( 3, $count );
992
993 // When adding a handler again...
994 $hookContainer->register( 'Increment', static function ( &$count ) {
995 $count = 11;
996 } );
997
998 // ...the new handler should be called, but not the old ones.
999 $count = 0;
1000 $hookContainer->run( 'Increment', [ &$count ] );
1001 $this->assertSame( 11, $count );
1002 $this->assertTrue( $hookContainer->isRegistered( 'Increment' ) );
1003 }
1004
1005 public static function provideMayBeCallable() {
1006 yield 'function' => [
1007 'strtoupper',
1008 ];
1009 yield 'closure' => [
1010 static function () {
1011 // noop
1012 },
1013 ];
1014 yield 'object and method' => [
1015 [ new FooClass(), 'fooMethod' ],
1016 ];
1017 yield 'static method as array' => [
1018 [ FooClass::class, 'fooStaticMethod', ],
1019 ];
1020 yield 'static method as string' => [
1021 'MediaWiki\Tests\HookContainer\FooClass::fooStaticMethod',
1022 ];
1023 yield 'callable referencing a class that extends an unknown class' => [
1024 [ 'MediaWiki\Tests\BrokenClass3', 'aMethod' ],
1025 ];
1026 }
1027
1028 public static function provideNotCallable() {
1029 yield 'object' => [
1030 new \FooExtension\Hooks(),
1031 ];
1032 yield 'object and non-existing method' => [
1033 [ new FooClass(), 'noSuchMethod' ],
1034 ];
1035 yield 'object and method and extra stuff' => [
1036 [ new FooClass(), 'fooMethod', 'extra', 'stuff' ],
1037 ];
1038 yield 'object and method assoc' => [
1039 [ 'a' => new FooClass(), 'b' => 'fooMethod' ],
1040 ];
1041 yield 'object and method nested in array' => [
1042 [ [ new FooClass(), 'fooMethod' ], 'whatever' ],
1043 ];
1044 yield 'non-existing static method on existing class' => [
1045 'MediaWiki\Tests\HookContainer\FooClass::noSuchMethod',
1046 ];
1047 yield 'global function with extra data in array' => [
1048 [ 'strtoupper', 'extra' ],
1049 ];
1050 yield 'non-existing static method on existing class as array' => [
1051 [ FooClass::class, 'noSuchMethod' ],
1052 ];
1053 yield 'non-function text' => [
1054 'just some text',
1055 ];
1056 yield 'object in array with no method' => [
1057 [ new \FooExtension\Hooks() ],
1058 ];
1059 yield 'callable referencing an unknown class' => [
1060 [ 'FooExtension\DoesNotExist', 'onFoo' ],
1061 ];
1062 }
1063
1064 /**
1065 * @covers \MediaWiki\HookContainer\HookContainer::mayBeCallable
1066 * @dataProvider provideMayBeCallable
1067 */
1068 public function testMayBeCallable_true( $v ) {
1069 $access = TestingAccessWrapper::newFromClass( HookContainer::class );
1070 $this->assertTrue( $access->mayBeCallable( $v ) );
1071 }
1072
1073 /**
1074 * @covers \MediaWiki\HookContainer\HookContainer::mayBeCallable
1075 * @dataProvider provideNotCallable
1076 */
1077 public function testMayBeCallable_false( $v ) {
1078 $access = TestingAccessWrapper::newFromClass( HookContainer::class );
1079 $this->assertFalse( $access->mayBeCallable( $v ) );
1080 }
1081 }
1082
1083 // Mock class for different types of handler functions
1084 class FooClass {
1085
1086 public function fooMethod( &$count ) {
1087 $count++;
1088 return true;
1089 }
1090
1091 public function onIncrement( &$count ) {
1092 $count++;
1093 }
1094
1095 public static function fooStaticMethod( &$count ) {
1096 $count++;
1097 return null;
1098 }
1099
1100 public function fooMethodWithExtra( int $inc, &$count ) {
1101 $count += $inc;
1102 return true;
1103 }
1104
1105 public static function fooStaticMethodWithExtra( int $inc, &$count ) {
1106 $count += $inc;
1107 return null;
1108 }
1109
1110 public static function fooMethodReturnValueError() {
1111 return 'a string';
1112 }
1113
1114 public static function onMWTestHook() {
1115 // noop
1116 }
1117 }
1118
1119 }
1120
1121 // Function in global namespace
1122 namespace {
1123
1124 function fooGlobalFunction( &$count ) {
1125 $count++;
1126 return true;
1127 }
1128
1129 function fooGlobalFunctionWithExtra( $inc, &$count ) {
1130 $count += $inc;
1131 }
1132
1133 }
1134
1135 // Mock Extension
1136 namespace FooExtension {
1137
1138 class Hooks {
1139
1140 public function onFooActionComplete() {
1141 return true;
1142 }
1143
1144 public function onMWTest() {
1145 // noop
1146 }
1147
1148 public function onIncrement( &$count ) {
1149 $count++;
1150 }
1151
1152 public function onX_Y__Increment( &$count ) {
1153 $count++;
1154 }
1155
1156 public function onAppend( &$list ) {
1157 $list[] = 'FooExtension';
1158 }
1159 }
1160
1161 class AbortHooks1 {
1162 public function onAbort( &$called ) {
1163 $called[] = 1;
1164 return true;
1165 }
1166 }
1167
1168 class AbortHooks2 {
1169 public function onAbort( &$called ) {
1170 $called[] = 2;
1171 return false;
1172 }
1173 }
1174
1175 class AbortHooks3 {
1176 public function onAbort( &$called ) {
1177 $called[] = 3;
1178 return true;
1179 }
1180 }
1181
1182 }