8f70b3dfc027e43591c8d84a0d4e9b46ced6fd21
3 class HooksTest
extends MediaWikiTestCase
{
8 Hooks
::clear( 'MediaWikiHooksTest001' );
9 unset( $wgHooks['MediaWikiHooksTest001'] );
12 public static function provideHooks() {
13 $i = new NothingClass();
15 array( 'Object and method', array( $i, 'someNonStatic' ), 'changed-nonstatic', 'changed-nonstatic' ),
16 array( 'Object and no method', array( $i ), 'changed-onevent', 'original' ),
17 array( 'Object and method with data', array( $i, 'someNonStaticWithData', 'data' ), 'data', 'original' ),
18 array( 'Object and static method', array( $i, 'someStatic' ), 'changed-static', 'original' ),
19 array( 'Class::method static call', array( 'NothingClass::someStatic' ), 'changed-static', 'original' ),
20 array( 'Global function', array( 'NothingFunction' ), 'changed-func', 'original' ),
21 array( 'Global function with data', array( 'NothingFunctionData', 'data' ), 'data', 'original' ),
22 array( 'Closure', array( function( &$foo, $bar ) {
23 $foo = 'changed-closure';
25 } ), 'changed-closure', 'original' ),
26 array( 'Closure with data', array( function( $data, &$foo, $bar ) {
29 }, 'data' ), 'data', 'original' )
34 * @dataProvider provideHooks
36 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
38 $foo = $bar = 'original';
40 $wgHooks['MediaWikiHooksTest001'][] = $hook;
41 wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
43 $this->assertSame( $expectedFoo, $foo, $msg );
44 $this->assertSame( $expectedBar, $bar, $msg );
48 * @dataProvider provideHooks
50 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
51 $foo = $bar = 'original';
53 Hooks
::register( 'MediaWikiHooksTest001', $hook );
54 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
56 $this->assertSame( $expectedFoo, $foo, $msg );
57 $this->assertSame( $expectedBar, $bar, $msg );
60 public function testNewStyleHookInteraction() {
63 $a = new NothingClass();
64 $b = new NothingClass();
66 $wgHooks['MediaWikiHooksTest001'][] = $a;
67 $this->assertTrue( Hooks
::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
69 Hooks
::register( 'MediaWikiHooksTest001', $b );
70 $this->assertEquals( 2, count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
75 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
76 $this->assertEquals( 1, $a->calls
, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
77 $this->assertEquals( 1, $b->calls
, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
81 * @expectedException MWException
83 public function testUncallableFunction() {
84 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
85 Hooks
::run( 'MediaWikiHooksTest001', array() );
88 public function testFalseReturn() {
89 Hooks
::register( 'MediaWikiHooksTest001', function( &$foo ) { return false; } );
90 Hooks
::register( 'MediaWikiHooksTest001', function( &$foo ) { $foo = 'test'; return true; } );
92 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo ) );
93 $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
97 * @expectedException FatalError
99 public function testFatalError() {
100 Hooks
::register( 'MediaWikiHooksTest001', function() { return 'test'; } );
101 Hooks
::run( 'MediaWikiHooksTest001', array() );
105 function NothingFunction( &$foo, &$bar ) {
106 $foo = 'changed-func';
110 function NothingFunctionData( $data, &$foo, &$bar ) {
118 public static function someStatic( &$foo, &$bar ) {
119 $foo = 'changed-static';
123 public function someNonStatic( &$foo, &$bar ) {
125 $foo = 'changed-nonstatic';
126 $bar = 'changed-nonstatic';
130 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
132 $foo = 'changed-onevent';
136 public function someNonStaticWithData( $data, &$foo, &$bar ) {