MediaWiki  REL1_24
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(
00017                 'Object and method',
00018                 array( $i, 'someNonStatic' ),
00019                 'changed-nonstatic',
00020                 'changed-nonstatic'
00021             ),
00022             array( 'Object and no method', array( $i ), 'changed-onevent', 'original' ),
00023             array(
00024                 'Object and method with data',
00025                 array( $i, 'someNonStaticWithData', 'data' ),
00026                 'data',
00027                 'original'
00028             ),
00029             array( 'Object and static method', array( $i, 'someStatic' ), 'changed-static', 'original' ),
00030             array(
00031                 'Class::method static call',
00032                 array( 'NothingClass::someStatic' ),
00033                 'changed-static',
00034                 'original'
00035             ),
00036             array( 'Global function', array( 'NothingFunction' ), 'changed-func', 'original' ),
00037             array( 'Global function with data', array( 'NothingFunctionData', 'data' ), 'data', 'original' ),
00038             array( 'Closure', array( function ( &$foo, $bar ) {
00039                 $foo = 'changed-closure';
00040 
00041                 return true;
00042             } ), 'changed-closure', 'original' ),
00043             array( 'Closure with data', array( function ( $data, &$foo, $bar ) {
00044                 $foo = $data;
00045 
00046                 return true;
00047             }, 'data' ), 'data', 'original' )
00048         );
00049     }
00050 
00055     public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
00056         global $wgHooks;
00057         $foo = $bar = 'original';
00058 
00059         $wgHooks['MediaWikiHooksTest001'][] = $hook;
00060         wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00061 
00062         $this->assertSame( $expectedFoo, $foo, $msg );
00063         $this->assertSame( $expectedBar, $bar, $msg );
00064     }
00065 
00071     public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
00072         $foo = $bar = 'original';
00073 
00074         Hooks::register( 'MediaWikiHooksTest001', $hook );
00075         Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00076 
00077         $this->assertSame( $expectedFoo, $foo, $msg );
00078         $this->assertSame( $expectedBar, $bar, $msg );
00079     }
00080 
00087     public function testNewStyleHookInteraction() {
00088         global $wgHooks;
00089 
00090         $a = new NothingClass();
00091         $b = new NothingClass();
00092 
00093         $wgHooks['MediaWikiHooksTest001'][] = $a;
00094         $this->assertTrue(
00095             Hooks::isRegistered( 'MediaWikiHooksTest001' ),
00096             'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
00097         );
00098 
00099         Hooks::register( 'MediaWikiHooksTest001', $b );
00100         $this->assertEquals(
00101             2,
00102             count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ),
00103             'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
00104         );
00105 
00106         $foo = 'quux';
00107         $bar = 'qaax';
00108 
00109         Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
00110         $this->assertEquals(
00111             1,
00112             $a->calls,
00113             'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
00114         );
00115         $this->assertEquals(
00116             1,
00117             $b->calls,
00118             'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
00119         );
00120     }
00121 
00126     public function testUncallableFunction() {
00127         Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
00128         Hooks::run( 'MediaWikiHooksTest001', array() );
00129     }
00130 
00134     public function testFalseReturn() {
00135         Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
00136             return false;
00137         } );
00138         Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
00139             $foo = 'test';
00140 
00141             return true;
00142         } );
00143         $foo = 'original';
00144         Hooks::run( 'MediaWikiHooksTest001', array( &$foo ) );
00145         $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
00146     }
00147 
00152     public function testFatalError() {
00153         Hooks::register( 'MediaWikiHooksTest001', function () {
00154             return 'test';
00155         } );
00156         Hooks::run( 'MediaWikiHooksTest001', array() );
00157     }
00158 }
00159 
00160 function NothingFunction( &$foo, &$bar ) {
00161     $foo = 'changed-func';
00162 
00163     return true;
00164 }
00165 
00166 function NothingFunctionData( $data, &$foo, &$bar ) {
00167     $foo = $data;
00168 
00169     return true;
00170 }
00171 
00172 class NothingClass {
00173     public $calls = 0;
00174 
00175     public static function someStatic( &$foo, &$bar ) {
00176         $foo = 'changed-static';
00177 
00178         return true;
00179     }
00180 
00181     public function someNonStatic( &$foo, &$bar ) {
00182         $this->calls++;
00183         $foo = 'changed-nonstatic';
00184         $bar = 'changed-nonstatic';
00185 
00186         return true;
00187     }
00188 
00189     public function onMediaWikiHooksTest001( &$foo, &$bar ) {
00190         $this->calls++;
00191         $foo = 'changed-onevent';
00192 
00193         return true;
00194     }
00195 
00196     public function someNonStaticWithData( $data, &$foo, &$bar ) {
00197         $this->calls++;
00198         $foo = $data;
00199 
00200         return true;
00201     }
00202 }