MediaWiki  REL1_23
HooksTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class HooksTest extends MediaWikiTestCase {
00004 
00005     function setUp() {
00006         global $wgHooks;
00007         parent::setUp();
00008         Hooks::clear( 'MediaWikiHooksTest001' );
00009         unset( $wgHooks['MediaWikiHooksTest001'] );
00010     }
00011 
00012     public static function provideHooks() {
00013         $i = new NothingClass();
00014 
00015         return array(
00016             array( 'Object and method', array( $i, 'someNonStatic' ), 'changed-nonstatic', 'changed-nonstatic' ),
00017             array( 'Object and no method', array( $i ), 'changed-onevent', 'original' ),
00018             array( 'Object and method with data', array( $i, 'someNonStaticWithData', 'data' ), 'data', 'original' ),
00019             array( 'Object and static method', array( $i, 'someStatic' ), 'changed-static', 'original' ),
00020             array( 'Class::method static call', array( 'NothingClass::someStatic' ), 'changed-static', 'original' ),
00021             array( 'Global function', array( 'NothingFunction' ), 'changed-func', 'original' ),
00022             array( 'Global function with data', array( 'NothingFunctionData', 'data' ), 'data', 'original' ),
00023             array( 'Closure', array( function ( &$foo, $bar ) {
00024                 $foo = 'changed-closure';
00025 
00026                 return true;
00027             } ), 'changed-closure', 'original' ),
00028             array( 'Closure with data', array( function ( $data, &$foo, $bar ) {
00029                 $foo = $data;
00030 
00031                 return true;
00032             }, 'data' ), 'data', 'original' )
00033         );
00034     }
00035 
00040     public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
00041         global $wgHooks;
00042         $foo = $bar = 'original';
00043 
00044         $wgHooks['MediaWikiHooksTest001'][] = $hook;
00045         wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00046 
00047         $this->assertSame( $expectedFoo, $foo, $msg );
00048         $this->assertSame( $expectedBar, $bar, $msg );
00049     }
00050 
00056     public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
00057         $foo = $bar = 'original';
00058 
00059         Hooks::register( 'MediaWikiHooksTest001', $hook );
00060         Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00061 
00062         $this->assertSame( $expectedFoo, $foo, $msg );
00063         $this->assertSame( $expectedBar, $bar, $msg );
00064     }
00065 
00072     public function testNewStyleHookInteraction() {
00073         global $wgHooks;
00074 
00075         $a = new NothingClass();
00076         $b = new NothingClass();
00077 
00078         $wgHooks['MediaWikiHooksTest001'][] = $a;
00079         $this->assertTrue( Hooks::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
00080 
00081         Hooks::register( 'MediaWikiHooksTest001', $b );
00082         $this->assertEquals( 2, count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
00083 
00084         $foo = 'quux';
00085         $bar = 'qaax';
00086 
00087         Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00088         $this->assertEquals( 1, $a->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
00089         $this->assertEquals( 1, $b->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
00090     }
00091 
00096     public function testUncallableFunction() {
00097         Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
00098         Hooks::run( 'MediaWikiHooksTest001', array() );
00099     }
00100 
00104     public function testFalseReturn() {
00105         Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
00106             return false;
00107         } );
00108         Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
00109             $foo = 'test';
00110 
00111             return true;
00112         } );
00113         $foo = 'original';
00114         Hooks::run( 'MediaWikiHooksTest001', array( &$foo ) );
00115         $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
00116     }
00117 
00122     public function testFatalError() {
00123         Hooks::register( 'MediaWikiHooksTest001', function () {
00124             return 'test';
00125         } );
00126         Hooks::run( 'MediaWikiHooksTest001', array() );
00127     }
00128 }
00129 
00130 function NothingFunction( &$foo, &$bar ) {
00131     $foo = 'changed-func';
00132 
00133     return true;
00134 }
00135 
00136 function NothingFunctionData( $data, &$foo, &$bar ) {
00137     $foo = $data;
00138 
00139     return true;
00140 }
00141 
00142 class NothingClass {
00143     public $calls = 0;
00144 
00145     public static function someStatic( &$foo, &$bar ) {
00146         $foo = 'changed-static';
00147 
00148         return true;
00149     }
00150 
00151     public function someNonStatic( &$foo, &$bar ) {
00152         $this->calls++;
00153         $foo = 'changed-nonstatic';
00154         $bar = 'changed-nonstatic';
00155 
00156         return true;
00157     }
00158 
00159     public function onMediaWikiHooksTest001( &$foo, &$bar ) {
00160         $this->calls++;
00161         $foo = 'changed-onevent';
00162 
00163         return true;
00164     }
00165 
00166     public function someNonStaticWithData( $data, &$foo, &$bar ) {
00167         $this->calls++;
00168         $foo = $data;
00169 
00170         return true;
00171     }
00172 }