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
38 * @covers ::wfRunHooks
40 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
42 $foo = $bar = 'original';
44 $wgHooks['MediaWikiHooksTest001'][] = $hook;
45 wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
47 $this->assertSame( $expectedFoo, $foo, $msg );
48 $this->assertSame( $expectedBar, $bar, $msg );
52 * @dataProvider provideHooks
53 * @covers Hooks::register
56 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
57 $foo = $bar = 'original';
59 Hooks
::register( 'MediaWikiHooksTest001', $hook );
60 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
62 $this->assertSame( $expectedFoo, $foo, $msg );
63 $this->assertSame( $expectedBar, $bar, $msg );
67 * @covers Hooks::isRegistered
68 * @covers Hooks::register
69 * @covers Hooks::getHandlers
72 public function testNewStyleHookInteraction() {
75 $a = new NothingClass();
76 $b = new NothingClass();
78 $wgHooks['MediaWikiHooksTest001'][] = $a;
79 $this->assertTrue( Hooks
::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
81 Hooks
::register( 'MediaWikiHooksTest001', $b );
82 $this->assertEquals( 2, count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
87 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
88 $this->assertEquals( 1, $a->calls
, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
89 $this->assertEquals( 1, $b->calls
, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
93 * @expectedException MWException
96 public function testUncallableFunction() {
97 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
98 Hooks
::run( 'MediaWikiHooksTest001', array() );
104 public function testFalseReturn() {
105 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
108 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
114 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo ) );
115 $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
119 * @expectedException FatalError
122 public function testFatalError() {
123 Hooks
::register( 'MediaWikiHooksTest001', function () {
126 Hooks
::run( 'MediaWikiHooksTest001', array() );
130 function NothingFunction( &$foo, &$bar ) {
131 $foo = 'changed-func';
136 function NothingFunctionData( $data, &$foo, &$bar ) {
145 public static function someStatic( &$foo, &$bar ) {
146 $foo = 'changed-static';
151 public function someNonStatic( &$foo, &$bar ) {
153 $foo = 'changed-nonstatic';
154 $bar = 'changed-nonstatic';
159 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
161 $foo = 'changed-onevent';
166 public function someNonStaticWithData( $data, &$foo, &$bar ) {