3 class HooksTest
extends MediaWikiTestCase
{
8 Hooks
::clear( 'MediaWikiHooksTest001' );
9 unset( $wgHooks['MediaWikiHooksTest001'] );
12 public static function provideHooks() {
13 $i = new NothingClass();
16 array( 'Object and method', array( $i, 'someNonStatic' ), 'changed-nonstatic', 'changed-nonstatic' ),
17 array( 'Object and no method', array( $i ), 'changed-onevent', 'original' ),
18 array( 'Object and method with data', array( $i, 'someNonStaticWithData', 'data' ), 'data', 'original' ),
19 array( 'Object and static method', array( $i, 'someStatic' ), 'changed-static', 'original' ),
20 array( 'Class::method static call', array( 'NothingClass::someStatic' ), 'changed-static', 'original' ),
21 array( 'Global function', array( 'NothingFunction' ), 'changed-func', 'original' ),
22 array( 'Global function with data', array( 'NothingFunctionData', 'data' ), 'data', 'original' ),
23 array( 'Closure', array( function ( &$foo, $bar ) {
24 $foo = 'changed-closure';
27 } ), 'changed-closure', 'original' ),
28 array( 'Closure with data', array( function ( $data, &$foo, $bar ) {
32 }, 'data' ), 'data', 'original' )
37 * @dataProvider provideHooks
39 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
41 $foo = $bar = 'original';
43 $wgHooks['MediaWikiHooksTest001'][] = $hook;
44 wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
46 $this->assertSame( $expectedFoo, $foo, $msg );
47 $this->assertSame( $expectedBar, $bar, $msg );
51 * @dataProvider provideHooks
53 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
54 $foo = $bar = 'original';
56 Hooks
::register( 'MediaWikiHooksTest001', $hook );
57 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
59 $this->assertSame( $expectedFoo, $foo, $msg );
60 $this->assertSame( $expectedBar, $bar, $msg );
63 public function testNewStyleHookInteraction() {
66 $a = new NothingClass();
67 $b = new NothingClass();
69 $wgHooks['MediaWikiHooksTest001'][] = $a;
70 $this->assertTrue( Hooks
::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
72 Hooks
::register( 'MediaWikiHooksTest001', $b );
73 $this->assertEquals( 2, count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
78 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
79 $this->assertEquals( 1, $a->calls
, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
80 $this->assertEquals( 1, $b->calls
, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
84 * @expectedException MWException
86 public function testUncallableFunction() {
87 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
88 Hooks
::run( 'MediaWikiHooksTest001', array() );
91 public function testFalseReturn() {
92 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
95 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
101 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo ) );
102 $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
106 * @expectedException FatalError
108 public function testFatalError() {
109 Hooks
::register( 'MediaWikiHooksTest001', function () {
112 Hooks
::run( 'MediaWikiHooksTest001', array() );
116 function NothingFunction( &$foo, &$bar ) {
117 $foo = 'changed-func';
122 function NothingFunctionData( $data, &$foo, &$bar ) {
131 public static function someStatic( &$foo, &$bar ) {
132 $foo = 'changed-static';
137 public function someNonStatic( &$foo, &$bar ) {
139 $foo = 'changed-nonstatic';
140 $bar = 'changed-nonstatic';
145 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
147 $foo = 'changed-onevent';
152 public function someNonStaticWithData( $data, &$foo, &$bar ) {