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