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 ::wfRunHooks
61 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
64 $this->hideDeprecated( 'wfRunHooks' );
65 $foo = $bar = 'original';
67 $wgHooks['MediaWikiHooksTest001'][] = $hook;
68 wfRunHooks( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
70 $this->assertSame( $expectedFoo, $foo, $msg );
71 $this->assertSame( $expectedBar, $bar, $msg );
75 * @dataProvider provideHooks
76 * @covers Hooks::register
78 * @covers Hooks::callHook
80 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
81 $foo = $bar = 'original';
83 Hooks
::register( 'MediaWikiHooksTest001', $hook );
84 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
86 $this->assertSame( $expectedFoo, $foo, $msg );
87 $this->assertSame( $expectedBar, $bar, $msg );
91 * @covers Hooks::getHandlers
93 public function testGetHandlers() {
98 Hooks
::getHandlers( 'MediaWikiHooksTest001' ),
102 $a = new NothingClass();
103 $b = new NothingClass();
105 $wgHooks['MediaWikiHooksTest001'][] = $a;
109 Hooks
::getHandlers( 'MediaWikiHooksTest001' ),
110 'Hook registered by $wgHooks'
113 Hooks
::register( 'MediaWikiHooksTest001', $b );
116 Hooks
::getHandlers( 'MediaWikiHooksTest001' ),
117 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
120 Hooks
::clear( 'MediaWikiHooksTest001' );
121 unset( $wgHooks['MediaWikiHooksTest001'] );
123 Hooks
::register( 'MediaWikiHooksTest001', $b );
126 Hooks
::getHandlers( 'MediaWikiHooksTest001' ),
127 'Hook registered by Hook::register'
132 * @covers Hooks::isRegistered
133 * @covers Hooks::register
135 * @covers Hooks::callHook
137 public function testNewStyleHookInteraction() {
140 $a = new NothingClass();
141 $b = new NothingClass();
143 $wgHooks['MediaWikiHooksTest001'][] = $a;
145 Hooks
::isRegistered( 'MediaWikiHooksTest001' ),
146 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
149 Hooks
::register( 'MediaWikiHooksTest001', $b );
152 count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ),
153 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
159 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
163 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
168 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
173 * @expectedException MWException
175 * @covers Hooks::callHook
177 public function testUncallableFunction() {
178 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
179 Hooks
::run( 'MediaWikiHooksTest001', [] );
184 * @covers Hooks::callHook
186 public function testFalseReturn() {
187 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
190 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
196 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
197 $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
203 public function testNullReturn() {
204 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
207 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
213 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
214 $this->assertSame( 'test', $foo, 'Hooks continue after a null return.' );
218 * @covers Hooks::callHook
220 public function testCallHook_FalseHook() {
221 Hooks
::register( 'MediaWikiHooksTest001', false );
222 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
228 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
229 $this->assertSame( 'test', $foo, 'Hooks that are falsey are skipped.' );
233 * @covers Hooks::callHook
234 * @expectedException MWException
236 public function testCallHook_UnknownDatatype() {
237 Hooks
::register( 'MediaWikiHooksTest001', 12345 );
238 Hooks
::run( 'MediaWikiHooksTest001' );
242 * @covers Hooks::callHook
243 * @expectedException PHPUnit_Framework_Error_Deprecated
245 public function testCallHook_Deprecated() {
246 Hooks
::register( 'MediaWikiHooksTest001', 'NothingClass::someStatic' );
247 Hooks
::run( 'MediaWikiHooksTest001', [], '1.31' );
251 * @covers Hooks::runWithoutAbort
252 * @covers Hooks::callHook
254 public function testRunWithoutAbort() {
256 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
258 return true; // Explicit true
260 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
262 return; // Implicit null
264 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
269 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
270 $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
274 * @covers Hooks::runWithoutAbort
275 * @covers Hooks::callHook
277 public function testRunWithoutAbortWarning() {
278 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
281 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
287 $this->setExpectedException(
288 UnexpectedValueException
::class,
289 'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
290 'unabortable MediaWikiHooksTest001'
292 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
296 * @expectedException FatalError
299 public function testFatalError() {
300 Hooks
::register( 'MediaWikiHooksTest001', function () {
303 Hooks
::run( 'MediaWikiHooksTest001', [] );
307 function NothingFunction( &$foo, &$bar ) {
308 $foo = 'changed-func';
313 function NothingFunctionData( $data, &$foo, &$bar ) {
322 public static function someStatic( &$foo, &$bar ) {
323 $foo = 'changed-static';
328 public function someNonStatic( &$foo, &$bar ) {
330 $foo = 'changed-nonstatic';
331 $bar = 'changed-nonstatic';
336 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
338 $foo = 'changed-onevent';
343 public function someNonStaticWithData( $data, &$foo, &$bar ) {