MediaWiki  REL1_22
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 
00039     public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
00040         global $wgHooks;
00041         $foo = $bar = 'original';
00042 
00043         $wgHooks['MediaWikiHooksTest001'][] = $hook;
00044         wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00045 
00046         $this->assertSame( $expectedFoo, $foo, $msg );
00047         $this->assertSame( $expectedBar, $bar, $msg );
00048     }
00049 
00053     public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
00054         $foo = $bar = 'original';
00055 
00056         Hooks::register( 'MediaWikiHooksTest001', $hook );
00057         Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00058 
00059         $this->assertSame( $expectedFoo, $foo, $msg );
00060         $this->assertSame( $expectedBar, $bar, $msg );
00061     }
00062 
00063     public function testNewStyleHookInteraction() {
00064         global $wgHooks;
00065 
00066         $a = new NothingClass();
00067         $b = new NothingClass();
00068 
00069         $wgHooks['MediaWikiHooksTest001'][] = $a;
00070         $this->assertTrue( Hooks::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
00071 
00072         Hooks::register( 'MediaWikiHooksTest001', $b );
00073         $this->assertEquals( 2, count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
00074 
00075         $foo = 'quux';
00076         $bar = 'qaax';
00077 
00078         Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00079         $this->assertEquals( 1, $a->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
00080         $this->assertEquals( 1, $b->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
00081     }
00082 
00086     public function testUncallableFunction() {
00087         Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
00088         Hooks::run( 'MediaWikiHooksTest001', array() );
00089     }
00090 
00091     public function testFalseReturn() {
00092         Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
00093             return false;
00094         } );
00095         Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
00096             $foo = 'test';
00097 
00098             return true;
00099         } );
00100         $foo = 'original';
00101         Hooks::run( 'MediaWikiHooksTest001', array( &$foo ) );
00102         $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
00103     }
00104 
00108     public function testFatalError() {
00109         Hooks::register( 'MediaWikiHooksTest001', function () {
00110             return 'test';
00111         } );
00112         Hooks::run( 'MediaWikiHooksTest001', array() );
00113     }
00114 }
00115 
00116 function NothingFunction( &$foo, &$bar ) {
00117     $foo = 'changed-func';
00118 
00119     return true;
00120 }
00121 
00122 function NothingFunctionData( $data, &$foo, &$bar ) {
00123     $foo = $data;
00124 
00125     return true;
00126 }
00127 
00128 class NothingClass {
00129     public $calls = 0;
00130 
00131     public static function someStatic( &$foo, &$bar ) {
00132         $foo = 'changed-static';
00133 
00134         return true;
00135     }
00136 
00137     public function someNonStatic( &$foo, &$bar ) {
00138         $this->calls++;
00139         $foo = 'changed-nonstatic';
00140         $bar = 'changed-nonstatic';
00141 
00142         return true;
00143     }
00144 
00145     public function onMediaWikiHooksTest001( &$foo, &$bar ) {
00146         $this->calls++;
00147         $foo = 'changed-onevent';
00148 
00149         return true;
00150     }
00151 
00152     public function someNonStaticWithData( $data, &$foo, &$bar ) {
00153         $this->calls++;
00154         $foo = $data;
00155 
00156         return true;
00157     }
00158 }