2011-01-17 16:43:12 +00:00
< ? php
class HooksTest extends MediaWikiTestCase {
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
function setUp () {
2011-01-17 16:43:12 +00:00
global $wgHooks ;
2013-04-22 14:38:47 +00:00
parent :: setUp ();
Hooks :: clear ( 'MediaWikiHooksTest001' );
2011-01-17 16:43:12 +00:00
unset ( $wgHooks [ 'MediaWikiHooksTest001' ] );
}
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
public static function provideHooks () {
2011-01-17 16:43:12 +00:00
$i = new NothingClass ();
2013-04-26 12:00:22 +00:00
2013-04-22 14:38:47 +00:00
return array (
array ( 'Object and method' , array ( $i , 'someNonStatic' ), 'changed-nonstatic' , 'changed-nonstatic' ),
array ( 'Object and no method' , array ( $i ), 'changed-onevent' , 'original' ),
array ( 'Object and method with data' , array ( $i , 'someNonStaticWithData' , 'data' ), 'data' , 'original' ),
array ( 'Object and static method' , array ( $i , 'someStatic' ), 'changed-static' , 'original' ),
array ( 'Class::method static call' , array ( 'NothingClass::someStatic' ), 'changed-static' , 'original' ),
array ( 'Global function' , array ( 'NothingFunction' ), 'changed-func' , 'original' ),
array ( 'Global function with data' , array ( 'NothingFunctionData' , 'data' ), 'data' , 'original' ),
2013-04-26 12:00:22 +00:00
array ( 'Closure' , array ( function ( & $foo , $bar ) {
$foo = 'changed-closure' ;
return true ;
} ), 'changed-closure' , 'original' ),
array ( 'Closure with data' , array ( function ( $data , & $foo , $bar ) {
$foo = $data ;
return true ;
}, 'data' ), 'data' , 'original' )
2013-04-22 14:38:47 +00:00
);
}
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
/**
* @ dataProvider provideHooks
2013-10-24 10:54:02 +00:00
* @ covers :: wfRunHooks
2013-04-22 14:38:47 +00:00
*/
public function testOldStyleHooks ( $msg , array $hook , $expectedFoo , $expectedBar ) {
global $wgHooks ;
$foo = $bar = 'original' ;
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
$wgHooks [ 'MediaWikiHooksTest001' ][] = $hook ;
wfRunHooks ( 'MediaWikiHooksTest001' , array ( & $foo , & $bar ) );
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
$this -> assertSame ( $expectedFoo , $foo , $msg );
$this -> assertSame ( $expectedBar , $bar , $msg );
}
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
/**
* @ dataProvider provideHooks
2013-10-24 10:54:02 +00:00
* @ covers Hooks :: register
* @ covers Hooks :: run
2013-04-22 14:38:47 +00:00
*/
public function testNewStyleHooks ( $msg , $hook , $expectedFoo , $expectedBar ) {
$foo = $bar = 'original' ;
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
Hooks :: register ( 'MediaWikiHooksTest001' , $hook );
2011-01-17 16:43:12 +00:00
Hooks :: run ( 'MediaWikiHooksTest001' , array ( & $foo , & $bar ) );
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
$this -> assertSame ( $expectedFoo , $foo , $msg );
$this -> assertSame ( $expectedBar , $bar , $msg );
2012-10-05 16:28:21 +00:00
}
2013-10-24 10:54:02 +00:00
/**
* @ covers Hooks :: isRegistered
* @ covers Hooks :: register
* @ covers Hooks :: getHandlers
* @ covers Hooks :: run
*/
2012-10-05 16:28:21 +00:00
public function testNewStyleHookInteraction () {
global $wgHooks ;
$a = new NothingClass ();
$b = new NothingClass ();
$wgHooks [ 'MediaWikiHooksTest001' ][] = $a ;
$this -> assertTrue ( Hooks :: isRegistered ( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
Hooks :: register ( 'MediaWikiHooksTest001' , $b );
$this -> assertEquals ( 2 , count ( Hooks :: getHandlers ( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
$foo = 'quux' ;
$bar = 'qaax' ;
Hooks :: run ( 'MediaWikiHooksTest001' , array ( & $foo , & $bar ) );
$this -> assertEquals ( 1 , $a -> calls , 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
$this -> assertEquals ( 1 , $b -> calls , 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
2013-04-22 14:38:47 +00:00
}
2012-10-05 16:28:21 +00:00
2013-04-22 14:38:47 +00:00
/**
* @ expectedException MWException
2013-10-24 10:54:02 +00:00
* @ covers Hooks :: run
2013-04-22 14:38:47 +00:00
*/
public function testUncallableFunction () {
Hooks :: register ( 'MediaWikiHooksTest001' , 'ThisFunctionDoesntExist' );
Hooks :: run ( 'MediaWikiHooksTest001' , array () );
}
2013-10-24 10:54:02 +00:00
/**
* @ covers Hooks :: run
*/
2013-04-22 14:38:47 +00:00
public function testFalseReturn () {
2013-04-26 12:00:22 +00:00
Hooks :: register ( 'MediaWikiHooksTest001' , function ( & $foo ) {
return false ;
} );
Hooks :: register ( 'MediaWikiHooksTest001' , function ( & $foo ) {
$foo = 'test' ;
return true ;
} );
2013-04-22 14:38:47 +00:00
$foo = 'original' ;
Hooks :: run ( 'MediaWikiHooksTest001' , array ( & $foo ) );
$this -> assertSame ( 'original' , $foo , 'Hooks continued processing after a false return.' );
2011-01-17 16:43:12 +00:00
}
2013-04-22 14:38:47 +00:00
/**
* @ expectedException FatalError
2013-10-24 10:54:02 +00:00
* @ covers Hooks :: run
2013-04-22 14:38:47 +00:00
*/
public function testFatalError () {
2013-04-26 12:00:22 +00:00
Hooks :: register ( 'MediaWikiHooksTest001' , function () {
return 'test' ;
} );
2013-04-22 14:38:47 +00:00
Hooks :: run ( 'MediaWikiHooksTest001' , array () );
}
}
function NothingFunction ( & $foo , & $bar ) {
$foo = 'changed-func' ;
2013-04-26 12:00:22 +00:00
2013-04-22 14:38:47 +00:00
return true ;
}
function NothingFunctionData ( $data , & $foo , & $bar ) {
$foo = $data ;
2013-04-26 12:00:22 +00:00
2013-04-22 14:38:47 +00:00
return true ;
2011-01-17 16:43:12 +00:00
}
class NothingClass {
2012-10-05 16:28:21 +00:00
public $calls = 0 ;
2013-01-28 10:27:15 +00:00
public static function someStatic ( & $foo , & $bar ) {
2013-04-22 14:38:47 +00:00
$foo = 'changed-static' ;
2013-04-26 12:00:22 +00:00
2011-01-17 16:43:12 +00:00
return true ;
}
2011-03-27 01:27:02 +00:00
2011-01-17 16:43:12 +00:00
public function someNonStatic ( & $foo , & $bar ) {
2012-10-05 16:28:21 +00:00
$this -> calls ++ ;
2013-04-22 14:38:47 +00:00
$foo = 'changed-nonstatic' ;
$bar = 'changed-nonstatic' ;
2013-04-26 12:00:22 +00:00
2011-01-17 16:43:12 +00:00
return true ;
}
2011-03-27 01:27:02 +00:00
2011-01-17 16:43:12 +00:00
public function onMediaWikiHooksTest001 ( & $foo , & $bar ) {
2012-10-05 16:28:21 +00:00
$this -> calls ++ ;
2013-04-22 14:38:47 +00:00
$foo = 'changed-onevent' ;
2013-04-26 12:00:22 +00:00
2011-01-17 16:43:12 +00:00
return true ;
}
2011-03-27 01:27:02 +00:00
2013-04-22 14:38:47 +00:00
public function someNonStaticWithData ( $data , & $foo , & $bar ) {
2012-10-05 16:28:21 +00:00
$this -> calls ++ ;
2013-04-22 14:38:47 +00:00
$foo = $data ;
2013-04-26 12:00:22 +00:00
2011-01-17 16:43:12 +00:00
return true ;
}
}