MediaWiki REL1_31
HooksTest.php
Go to the documentation of this file.
1<?php
2
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
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
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
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
131 public function testUncallableFunction() {
132 Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
133 Hooks::run( 'MediaWikiHooksTest001', [] );
134 }
135
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
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
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
203 public function testFatalError() {
204 Hooks::register( 'MediaWikiHooksTest001', function () {
205 return 'test';
206 } );
207 Hooks::run( 'MediaWikiHooksTest001', [] );
208 }
209}
210
211function NothingFunction( &$foo, &$bar ) {
212 $foo = 'changed-func';
213
214 return true;
215}
216
217function NothingFunctionData( $data, &$foo, &$bar ) {
218 $foo = $data;
219
220 return true;
221}
222
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}
wfRunHooks( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
NothingFunction(&$foo, &$bar)
NothingFunctionData( $data, &$foo, &$bar)
testFatalError()
FatalError Hooks::run.
testUncallableFunction()
MWException Hooks::run Hooks::callHook.
testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar)
provideHooks wfRunHooks
Definition HooksTest.php:55
testFalseReturn()
Hooks::run Hooks::callHook.
testNewStyleHookInteraction()
Hooks::isRegistered Hooks::register Hooks::getHandlers Hooks::run Hooks::callHook.
Definition HooksTest.php:91
testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar)
provideHooks Hooks::register Hooks::run Hooks::callHook
Definition HooksTest.php:74
testRunWithoutAbortWarning()
Hooks::runWithoutAbort Hooks::callHook.
static provideHooks()
Definition HooksTest.php:12
testRunWithoutAbort()
Hooks::runWithoutAbort Hooks::callHook.
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
static someStatic(&$foo, &$bar)
someNonStaticWithData( $data, &$foo, &$bar)
someNonStatic(&$foo, &$bar)
onMediaWikiHooksTest001(&$foo, &$bar)
$wgHooks['ArticleShow'][]
Definition hooks.txt:108