Merge "Warn if stateful ParserOutput transforms are used"
[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
15 return [
16 [
17 'Object and method',
18 [ $i, 'someNonStatic' ],
19 'changed-nonstatic',
20 'changed-nonstatic'
21 ],
22 [ 'Object and no method', [ $i ], 'changed-onevent', 'original' ],
23 [
24 'Object and method with data',
25 [ $i, 'someNonStaticWithData', 'data' ],
26 'data',
27 'original'
28 ],
29 [ 'Object and static method', [ $i, 'someStatic' ], 'changed-static', 'original' ],
30 [
31 'Class::method static call',
32 [ 'NothingClass::someStatic' ],
33 'changed-static',
34 'original'
35 ],
36 [ 'Global function', [ 'NothingFunction' ], 'changed-func', 'original' ],
37 [ 'Global function with data', [ 'NothingFunctionData', 'data' ], 'data', 'original' ],
38 [ 'Closure', [ function ( &$foo, $bar ) {
39 $foo = 'changed-closure';
40
41 return true;
42 } ], 'changed-closure', 'original' ],
43 [ 'Closure with data', [ function ( $data, &$foo, $bar ) {
44 $foo = $data;
45
46 return true;
47 }, 'data' ], 'data', 'original' ]
48 ];
49 }
50
51 /**
52 * @dataProvider provideHooks
53 * @covers ::wfRunHooks
54 */
55 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
56 global $wgHooks;
57
58 $this->hideDeprecated( 'wfRunHooks' );
59 $foo = $bar = 'original';
60
61 $wgHooks['MediaWikiHooksTest001'][] = $hook;
62 wfRunHooks( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
63
64 $this->assertSame( $expectedFoo, $foo, $msg );
65 $this->assertSame( $expectedBar, $bar, $msg );
66 }
67
68 /**
69 * @dataProvider provideHooks
70 * @covers Hooks::register
71 * @covers Hooks::run
72 * @covers Hooks::callHook
73 */
74 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
75 $foo = $bar = 'original';
76
77 Hooks::register( 'MediaWikiHooksTest001', $hook );
78 Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
79
80 $this->assertSame( $expectedFoo, $foo, $msg );
81 $this->assertSame( $expectedBar, $bar, $msg );
82 }
83
84 /**
85 * @covers Hooks::isRegistered
86 * @covers Hooks::register
87 * @covers Hooks::getHandlers
88 * @covers Hooks::run
89 * @covers Hooks::callHook
90 */
91 public function testNewStyleHookInteraction() {
92 global $wgHooks;
93
94 $a = new NothingClass();
95 $b = new NothingClass();
96
97 $wgHooks['MediaWikiHooksTest001'][] = $a;
98 $this->assertTrue(
99 Hooks::isRegistered( 'MediaWikiHooksTest001' ),
100 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
101 );
102
103 Hooks::register( 'MediaWikiHooksTest001', $b );
104 $this->assertEquals(
105 2,
106 count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ),
107 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
108 );
109
110 $foo = 'quux';
111 $bar = 'qaax';
112
113 Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
114 $this->assertEquals(
115 1,
116 $a->calls,
117 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
118 );
119 $this->assertEquals(
120 1,
121 $b->calls,
122 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
123 );
124 }
125
126 /**
127 * @expectedException MWException
128 * @covers Hooks::run
129 * @covers Hooks::callHook
130 */
131 public function testUncallableFunction() {
132 Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
133 Hooks::run( 'MediaWikiHooksTest001', [] );
134 }
135
136 /**
137 * @covers Hooks::run
138 * @covers Hooks::callHook
139 */
140 public function testFalseReturn() {
141 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
142 return false;
143 } );
144 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
145 $foo = 'test';
146
147 return true;
148 } );
149 $foo = 'original';
150 Hooks::run( 'MediaWikiHooksTest001', [ &$foo ] );
151 $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
152 }
153
154 /**
155 * @covers Hooks::runWithoutAbort
156 * @covers Hooks::callHook
157 */
158 public function testRunWithoutAbort() {
159 $list = [];
160 Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
161 $list[] = 1;
162 return true; // Explicit true
163 } );
164 Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
165 $list[] = 2;
166 return; // Implicit null
167 } );
168 Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
169 $list[] = 3;
170 // No return
171 } );
172
173 Hooks::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
174 $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
175 }
176
177 /**
178 * @covers Hooks::runWithoutAbort
179 * @covers Hooks::callHook
180 */
181 public function testRunWithoutAbortWarning() {
182 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
183 return false;
184 } );
185 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
186 $foo = 'test';
187 return true;
188 } );
189 $foo = 'original';
190
191 $this->setExpectedException(
192 UnexpectedValueException::class,
193 'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
194 'unabortable MediaWikiHooksTest001'
195 );
196 Hooks::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
197 }
198
199 /**
200 * @expectedException FatalError
201 * @covers Hooks::run
202 */
203 public function testFatalError() {
204 Hooks::register( 'MediaWikiHooksTest001', function () {
205 return 'test';
206 } );
207 Hooks::run( 'MediaWikiHooksTest001', [] );
208 }
209 }
210
211 function NothingFunction( &$foo, &$bar ) {
212 $foo = 'changed-func';
213
214 return true;
215 }
216
217 function NothingFunctionData( $data, &$foo, &$bar ) {
218 $foo = $data;
219
220 return true;
221 }
222
223 class NothingClass {
224 public $calls = 0;
225
226 public static function someStatic( &$foo, &$bar ) {
227 $foo = 'changed-static';
228
229 return true;
230 }
231
232 public function someNonStatic( &$foo, &$bar ) {
233 $this->calls++;
234 $foo = 'changed-nonstatic';
235 $bar = 'changed-nonstatic';
236
237 return true;
238 }
239
240 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
241 $this->calls++;
242 $foo = 'changed-onevent';
243
244 return true;
245 }
246
247 public function someNonStaticWithData( $data, &$foo, &$bar ) {
248 $this->calls++;
249 $foo = $data;
250
251 return true;
252 }
253 }