Merge "RCFilters: Move parameter operations to ViewModel"
[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 */
73 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
74 $foo = $bar = 'original';
75
76 Hooks::register( 'MediaWikiHooksTest001', $hook );
77 Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
78
79 $this->assertSame( $expectedFoo, $foo, $msg );
80 $this->assertSame( $expectedBar, $bar, $msg );
81 }
82
83 /**
84 * @covers Hooks::isRegistered
85 * @covers Hooks::register
86 * @covers Hooks::getHandlers
87 * @covers Hooks::run
88 */
89 public function testNewStyleHookInteraction() {
90 global $wgHooks;
91
92 $a = new NothingClass();
93 $b = new NothingClass();
94
95 $wgHooks['MediaWikiHooksTest001'][] = $a;
96 $this->assertTrue(
97 Hooks::isRegistered( 'MediaWikiHooksTest001' ),
98 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
99 );
100
101 Hooks::register( 'MediaWikiHooksTest001', $b );
102 $this->assertEquals(
103 2,
104 count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ),
105 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
106 );
107
108 $foo = 'quux';
109 $bar = 'qaax';
110
111 Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
112 $this->assertEquals(
113 1,
114 $a->calls,
115 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
116 );
117 $this->assertEquals(
118 1,
119 $b->calls,
120 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
121 );
122 }
123
124 /**
125 * @expectedException MWException
126 * @covers Hooks::run
127 */
128 public function testUncallableFunction() {
129 Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
130 Hooks::run( 'MediaWikiHooksTest001', [] );
131 }
132
133 /**
134 * @covers Hooks::run
135 */
136 public function testFalseReturn() {
137 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
138 return false;
139 } );
140 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
141 $foo = 'test';
142
143 return true;
144 } );
145 $foo = 'original';
146 Hooks::run( 'MediaWikiHooksTest001', [ &$foo ] );
147 $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
148 }
149
150 /**
151 * @covers Hooks::runWithoutAbort
152 */
153 public function testRunWithoutAbort() {
154 $list = [];
155 Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
156 $list[] = 1;
157 return true; // Explicit true
158 } );
159 Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
160 $list[] = 2;
161 return; // Implicit null
162 } );
163 Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
164 $list[] = 3;
165 // No return
166 } );
167
168 Hooks::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
169 $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
170 }
171
172 /**
173 * @covers Hooks::runWithoutAbort
174 */
175 public function testRunWithoutAbortWarning() {
176 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
177 return false;
178 } );
179 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
180 $foo = 'test';
181 return true;
182 } );
183 $foo = 'original';
184
185 $this->setExpectedException(
186 UnexpectedValueException::class,
187 'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
188 'unabortable MediaWikiHooksTest001'
189 );
190 Hooks::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
191 }
192
193 /**
194 * @expectedException FatalError
195 * @covers Hooks::run
196 */
197 public function testFatalError() {
198 Hooks::register( 'MediaWikiHooksTest001', function () {
199 return 'test';
200 } );
201 Hooks::run( 'MediaWikiHooksTest001', [] );
202 }
203 }
204
205 function NothingFunction( &$foo, &$bar ) {
206 $foo = 'changed-func';
207
208 return true;
209 }
210
211 function NothingFunctionData( $data, &$foo, &$bar ) {
212 $foo = $data;
213
214 return true;
215 }
216
217 class NothingClass {
218 public $calls = 0;
219
220 public static function someStatic( &$foo, &$bar ) {
221 $foo = 'changed-static';
222
223 return true;
224 }
225
226 public function someNonStatic( &$foo, &$bar ) {
227 $this->calls++;
228 $foo = 'changed-nonstatic';
229 $bar = 'changed-nonstatic';
230
231 return true;
232 }
233
234 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
235 $this->calls++;
236 $foo = 'changed-onevent';
237
238 return true;
239 }
240
241 public function someNonStaticWithData( $data, &$foo, &$bar ) {
242 $this->calls++;
243 $foo = $data;
244
245 return true;
246 }
247 }