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' ],
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';
42 } ], 'changed-closure', 'original' ],
43 [ 'Closure with data', [ function ( $data, &$foo, $bar ) {
47 }, 'data' ], 'data', 'original' ]
52 * @dataProvider provideHooks
53 * @covers ::wfRunHooks
55 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
58 $this->hideDeprecated( 'wfRunHooks' );
59 $foo = $bar = 'original';
61 $wgHooks['MediaWikiHooksTest001'][] = $hook;
62 wfRunHooks( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
64 $this->assertSame( $expectedFoo, $foo, $msg );
65 $this->assertSame( $expectedBar, $bar, $msg );
69 * @dataProvider provideHooks
70 * @covers Hooks::register
72 * @covers Hooks::callHook
74 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
75 $foo = $bar = 'original';
77 Hooks
::register( 'MediaWikiHooksTest001', $hook );
78 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
80 $this->assertSame( $expectedFoo, $foo, $msg );
81 $this->assertSame( $expectedBar, $bar, $msg );
85 * @covers Hooks::isRegistered
86 * @covers Hooks::register
87 * @covers Hooks::getHandlers
89 * @covers Hooks::callHook
91 public function testNewStyleHookInteraction() {
94 $a = new NothingClass();
95 $b = new NothingClass();
97 $wgHooks['MediaWikiHooksTest001'][] = $a;
99 Hooks
::isRegistered( 'MediaWikiHooksTest001' ),
100 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
103 Hooks
::register( 'MediaWikiHooksTest001', $b );
106 count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ),
107 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
113 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
117 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
122 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
127 * @expectedException MWException
129 * @covers Hooks::callHook
131 public function testUncallableFunction() {
132 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
133 Hooks
::run( 'MediaWikiHooksTest001', [] );
138 * @covers Hooks::callHook
140 public function testFalseReturn() {
141 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
144 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
150 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
151 $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
155 * @covers Hooks::runWithoutAbort
156 * @covers Hooks::callHook
158 public function testRunWithoutAbort() {
160 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
162 return true; // Explicit true
164 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
166 return; // Implicit null
168 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
173 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
174 $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
178 * @covers Hooks::runWithoutAbort
179 * @covers Hooks::callHook
181 public function testRunWithoutAbortWarning() {
182 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
185 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
191 $this->setExpectedException(
192 UnexpectedValueException
::class,
193 'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
194 'unabortable MediaWikiHooksTest001'
196 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
200 * @expectedException FatalError
203 public function testFatalError() {
204 Hooks
::register( 'MediaWikiHooksTest001', function () {
207 Hooks
::run( 'MediaWikiHooksTest001', [] );
211 function NothingFunction( &$foo, &$bar ) {
212 $foo = 'changed-func';
217 function NothingFunctionData( $data, &$foo, &$bar ) {
226 public static function someStatic( &$foo, &$bar ) {
227 $foo = 'changed-static';
232 public function someNonStatic( &$foo, &$bar ) {
234 $foo = 'changed-nonstatic';
235 $bar = 'changed-nonstatic';
240 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
242 $foo = 'changed-onevent';
247 public function someNonStaticWithData( $data, &$foo, &$bar ) {