HooksTest: Covers Hooks::callHook too
[lhc/web/wiklou.git] / tests / phpunit / includes / HooksTest.php
index 74d4b09..efe92ec 100644 (file)
@@ -12,40 +12,40 @@ class HooksTest extends MediaWikiTestCase {
        public static function provideHooks() {
                $i = new NothingClass();
 
-               return array(
-                       array(
+               return [
+                       [
                                'Object and method',
-                               array( $i, 'someNonStatic' ),
+                               [ $i, 'someNonStatic' ],
                                'changed-nonstatic',
                                'changed-nonstatic'
-                       ),
-                       array( 'Object and no method', array( $i ), 'changed-onevent', 'original' ),
-                       array(
+                       ],
+                       [ 'Object and no method', [ $i ], 'changed-onevent', 'original' ],
+                       [
                                'Object and method with data',
-                               array( $i, 'someNonStaticWithData', 'data' ),
+                               [ $i, 'someNonStaticWithData', 'data' ],
                                'data',
                                'original'
-                       ),
-                       array( 'Object and static method', array( $i, 'someStatic' ), 'changed-static', 'original' ),
-                       array(
+                       ],
+                       [ 'Object and static method', [ $i, 'someStatic' ], 'changed-static', 'original' ],
+                       [
                                'Class::method static call',
-                               array( 'NothingClass::someStatic' ),
+                               [ 'NothingClass::someStatic' ],
                                'changed-static',
                                'original'
-                       ),
-                       array( 'Global function', array( 'NothingFunction' ), 'changed-func', 'original' ),
-                       array( 'Global function with data', array( 'NothingFunctionData', 'data' ), 'data', 'original' ),
-                       array( 'Closure', array( function ( &$foo, $bar ) {
+                       ],
+                       [ 'Global function', [ 'NothingFunction' ], 'changed-func', 'original' ],
+                       [ 'Global function with data', [ 'NothingFunctionData', 'data' ], 'data', 'original' ],
+                       [ 'Closure', [ function ( &$foo, $bar ) {
                                $foo = 'changed-closure';
 
                                return true;
-                       } ), 'changed-closure', 'original' ),
-                       array( 'Closure with data', array( function ( $data, &$foo, $bar ) {
+                       } ], 'changed-closure', 'original' ],
+                       [ 'Closure with data', [ function ( $data, &$foo, $bar ) {
                                $foo = $data;
 
                                return true;
-                       }, 'data' ), 'data', 'original' )
-               );
+                       }, 'data' ], 'data', 'original' ]
+               ];
        }
 
        /**
@@ -54,10 +54,12 @@ class HooksTest extends MediaWikiTestCase {
         */
        public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
                global $wgHooks;
+
+               $this->hideDeprecated( 'wfRunHooks' );
                $foo = $bar = 'original';
 
                $wgHooks['MediaWikiHooksTest001'][] = $hook;
-               wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
+               wfRunHooks( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
 
                $this->assertSame( $expectedFoo, $foo, $msg );
                $this->assertSame( $expectedBar, $bar, $msg );
@@ -67,12 +69,13 @@ class HooksTest extends MediaWikiTestCase {
         * @dataProvider provideHooks
         * @covers Hooks::register
         * @covers Hooks::run
+        * @covers Hooks::callHook
         */
        public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
                $foo = $bar = 'original';
 
                Hooks::register( 'MediaWikiHooksTest001', $hook );
-               Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
+               Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
 
                $this->assertSame( $expectedFoo, $foo, $msg );
                $this->assertSame( $expectedBar, $bar, $msg );
@@ -83,6 +86,7 @@ class HooksTest extends MediaWikiTestCase {
         * @covers Hooks::register
         * @covers Hooks::getHandlers
         * @covers Hooks::run
+        * @covers Hooks::callHook
         */
        public function testNewStyleHookInteraction() {
                global $wgHooks;
@@ -106,7 +110,7 @@ class HooksTest extends MediaWikiTestCase {
                $foo = 'quux';
                $bar = 'qaax';
 
-               Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
+               Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
                $this->assertEquals(
                        1,
                        $a->calls,
@@ -122,14 +126,16 @@ class HooksTest extends MediaWikiTestCase {
        /**
         * @expectedException MWException
         * @covers Hooks::run
+        * @covers Hooks::callHook
         */
        public function testUncallableFunction() {
                Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
-               Hooks::run( 'MediaWikiHooksTest001', array() );
+               Hooks::run( 'MediaWikiHooksTest001', [] );
        }
 
        /**
         * @covers Hooks::run
+        * @covers Hooks::callHook
         */
        public function testFalseReturn() {
                Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
@@ -141,8 +147,53 @@ class HooksTest extends MediaWikiTestCase {
                        return true;
                } );
                $foo = 'original';
-               Hooks::run( 'MediaWikiHooksTest001', array( &$foo ) );
-               $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
+               Hooks::run( 'MediaWikiHooksTest001', [ &$foo ] );
+               $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
+       }
+
+       /**
+        * @covers Hooks::runWithoutAbort
+        * @covers Hooks::callHook
+        */
+       public function testRunWithoutAbort() {
+               $list = [];
+               Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
+                       $list[] = 1;
+                       return true; // Explicit true
+               } );
+               Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
+                       $list[] = 2;
+                       return; // Implicit null
+               } );
+               Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
+                       $list[] = 3;
+                       // No return
+               } );
+
+               Hooks::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
+               $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
+       }
+
+       /**
+        * @covers Hooks::runWithoutAbort
+        * @covers Hooks::callHook
+        */
+       public function testRunWithoutAbortWarning() {
+               Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
+                       return false;
+               } );
+               Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
+                       $foo = 'test';
+                       return true;
+               } );
+               $foo = 'original';
+
+               $this->setExpectedException(
+                       UnexpectedValueException::class,
+                       'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
+                               'unabortable MediaWikiHooksTest001'
+               );
+               Hooks::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
        }
 
        /**
@@ -153,7 +204,7 @@ class HooksTest extends MediaWikiTestCase {
                Hooks::register( 'MediaWikiHooksTest001', function () {
                        return 'test';
                } );
-               Hooks::run( 'MediaWikiHooksTest001', array() );
+               Hooks::run( 'MediaWikiHooksTest001', [] );
        }
 }