Merge "Some more editor files to ignore."
[lhc/web/wiklou.git] / tests / phpunit / includes / HooksTest.php
1 <?php
2
3 class HooksTest extends MediaWikiTestCase {
4
5 function setUp() {
6 global $wgHooks;
7 parent::setUp();
8 Hooks::clear( 'MediaWikiHooksTest001' );
9 unset( $wgHooks['MediaWikiHooksTest001'] );
10 }
11
12 public static function provideHooks() {
13 $i = new NothingClass();
14 return array(
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';
24 return true;
25 } ), 'changed-closure', 'original' ),
26 array( 'Closure with data', array( function( $data, &$foo, $bar ) {
27 $foo = $data;
28 return true;
29 }, 'data' ), 'data', 'original' )
30 );
31 }
32
33 /**
34 * @dataProvider provideHooks
35 */
36 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
37 global $wgHooks;
38 $foo = $bar = 'original';
39
40 $wgHooks['MediaWikiHooksTest001'][] = $hook;
41 wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
42
43 $this->assertSame( $expectedFoo, $foo, $msg );
44 $this->assertSame( $expectedBar, $bar, $msg );
45 }
46
47 /**
48 * @dataProvider provideHooks
49 */
50 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
51 $foo = $bar = 'original';
52
53 Hooks::register( 'MediaWikiHooksTest001', $hook );
54 Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
55
56 $this->assertSame( $expectedFoo, $foo, $msg );
57 $this->assertSame( $expectedBar, $bar, $msg );
58 }
59
60 public function testNewStyleHookInteraction() {
61 global $wgHooks;
62
63 $a = new NothingClass();
64 $b = new NothingClass();
65
66 $wgHooks['MediaWikiHooksTest001'][] = $a;
67 $this->assertTrue( Hooks::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
68
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' );
71
72 $foo = 'quux';
73 $bar = 'qaax';
74
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' );
78 }
79
80 /**
81 * @expectedException MWException
82 */
83 public function testUncallableFunction() {
84 Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
85 Hooks::run( 'MediaWikiHooksTest001', array() );
86 }
87
88 public function testFalseReturn() {
89 Hooks::register( 'MediaWikiHooksTest001', function( &$foo ) { return false; } );
90 Hooks::register( 'MediaWikiHooksTest001', function( &$foo ) { $foo = 'test'; return true; } );
91 $foo = 'original';
92 Hooks::run( 'MediaWikiHooksTest001', array( &$foo ) );
93 $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
94 }
95
96 /**
97 * @expectedException FatalError
98 */
99 public function testFatalError() {
100 Hooks::register( 'MediaWikiHooksTest001', function() { return 'test'; } );
101 Hooks::run( 'MediaWikiHooksTest001', array() );
102 }
103 }
104
105 function NothingFunction( &$foo, &$bar ) {
106 $foo = 'changed-func';
107 return true;
108 }
109
110 function NothingFunctionData( $data, &$foo, &$bar ) {
111 $foo = $data;
112 return true;
113 }
114
115 class NothingClass {
116 public $calls = 0;
117
118 public static function someStatic( &$foo, &$bar ) {
119 $foo = 'changed-static';
120 return true;
121 }
122
123 public function someNonStatic( &$foo, &$bar ) {
124 $this->calls++;
125 $foo = 'changed-nonstatic';
126 $bar = 'changed-nonstatic';
127 return true;
128 }
129
130 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
131 $this->calls++;
132 $foo = 'changed-onevent';
133 return true;
134 }
135
136 public function someNonStaticWithData( $data, &$foo, &$bar ) {
137 $this->calls++;
138 $foo = $data;
139 return true;
140 }
141 }