3 class HooksTest
extends MediaWikiTestCase
{
8 Hooks
::clear( 'MediaWikiHooksTest001' );
9 unset( $wgHooks['MediaWikiHooksTest001'] );
12 public static function provideHooks() {
13 $i = new NothingClass();
18 [ $i, 'someNonStatic' ],
22 [ 'Object and no method', [ $i ], 'changed-onevent', 'original' ],
24 'Object and method with data',
25 [ $i, 'someNonStaticWithData', 'data' ],
29 [ 'Object and static method', [ $i, 'someStatic' ], 'changed-static', 'original' ],
31 'Class::method static call',
32 [ 'NothingClass::someStatic' ],
37 'Class::method static call as array',
38 [ [ 'NothingClass::someStatic' ] ],
42 [ 'Global function', [ 'NothingFunction' ], 'changed-func', 'original' ],
43 [ 'Global function with data', [ 'NothingFunctionData', 'data' ], 'data', 'original' ],
44 [ 'Closure', [ function ( &$foo, $bar ) {
45 $foo = 'changed-closure';
48 } ], 'changed-closure', 'original' ],
49 [ 'Closure with data', [ function ( $data, &$foo, $bar ) {
53 }, 'data' ], 'data', 'original' ]
58 * @dataProvider provideHooks
59 * @covers Hooks::register
61 * @covers Hooks::callHook
63 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
64 $foo = $bar = 'original';
66 Hooks
::register( 'MediaWikiHooksTest001', $hook );
67 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
69 $this->assertSame( $expectedFoo, $foo, $msg );
70 $this->assertSame( $expectedBar, $bar, $msg );
74 * @covers Hooks::getHandlers
76 public function testGetHandlers() {
81 Hooks
::getHandlers( 'MediaWikiHooksTest001' ),
85 $a = new NothingClass();
86 $b = new NothingClass();
88 $wgHooks['MediaWikiHooksTest001'][] = $a;
92 Hooks
::getHandlers( 'MediaWikiHooksTest001' ),
93 'Hook registered by $wgHooks'
96 Hooks
::register( 'MediaWikiHooksTest001', $b );
99 Hooks
::getHandlers( 'MediaWikiHooksTest001' ),
100 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
103 Hooks
::clear( 'MediaWikiHooksTest001' );
104 unset( $wgHooks['MediaWikiHooksTest001'] );
106 Hooks
::register( 'MediaWikiHooksTest001', $b );
109 Hooks
::getHandlers( 'MediaWikiHooksTest001' ),
110 'Hook registered by Hook::register'
115 * @covers Hooks::isRegistered
116 * @covers Hooks::register
118 * @covers Hooks::callHook
120 public function testNewStyleHookInteraction() {
123 $a = new NothingClass();
124 $b = new NothingClass();
126 $wgHooks['MediaWikiHooksTest001'][] = $a;
128 Hooks
::isRegistered( 'MediaWikiHooksTest001' ),
129 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
132 Hooks
::register( 'MediaWikiHooksTest001', $b );
135 count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ),
136 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
142 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
146 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
151 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
156 * @expectedException MWException
158 * @covers Hooks::callHook
160 public function testUncallableFunction() {
161 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
162 Hooks
::run( 'MediaWikiHooksTest001', [] );
167 * @covers Hooks::callHook
169 public function testFalseReturn() {
170 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
173 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
179 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
180 $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
186 public function testNullReturn() {
187 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
190 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
196 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
197 $this->assertSame( 'test', $foo, 'Hooks continue after a null return.' );
201 * @covers Hooks::callHook
203 public function testCallHook_FalseHook() {
204 Hooks
::register( 'MediaWikiHooksTest001', false );
205 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
211 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
212 $this->assertSame( 'test', $foo, 'Hooks that are falsey are skipped.' );
216 * @covers Hooks::callHook
217 * @expectedException MWException
219 public function testCallHook_UnknownDatatype() {
220 Hooks
::register( 'MediaWikiHooksTest001', 12345 );
221 Hooks
::run( 'MediaWikiHooksTest001' );
225 * @covers Hooks::callHook
226 * @expectedException PHPUnit_Framework_Error_Deprecated
228 public function testCallHook_Deprecated() {
229 Hooks
::register( 'MediaWikiHooksTest001', 'NothingClass::someStatic' );
230 Hooks
::run( 'MediaWikiHooksTest001', [], '1.31' );
234 * @covers Hooks::runWithoutAbort
235 * @covers Hooks::callHook
237 public function testRunWithoutAbort() {
239 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
241 return true; // Explicit true
243 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
245 return; // Implicit null
247 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
252 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
253 $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
257 * @covers Hooks::runWithoutAbort
258 * @covers Hooks::callHook
260 public function testRunWithoutAbortWarning() {
261 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
264 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
270 $this->setExpectedException(
271 UnexpectedValueException
::class,
272 'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
273 'unabortable MediaWikiHooksTest001'
275 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
279 * @expectedException FatalError
282 public function testFatalError() {
283 Hooks
::register( 'MediaWikiHooksTest001', function () {
286 Hooks
::run( 'MediaWikiHooksTest001', [] );
290 function NothingFunction( &$foo, &$bar ) {
291 $foo = 'changed-func';
296 function NothingFunctionData( $data, &$foo, &$bar ) {
305 public static function someStatic( &$foo, &$bar ) {
306 $foo = 'changed-static';
311 public function someNonStatic( &$foo, &$bar ) {
313 $foo = 'changed-nonstatic';
314 $bar = 'changed-nonstatic';
319 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
321 $foo = 'changed-onevent';
326 public function someNonStaticWithData( $data, &$foo, &$bar ) {