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
73 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
74 $foo = $bar = 'original';
76 Hooks
::register( 'MediaWikiHooksTest001', $hook );
77 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
79 $this->assertSame( $expectedFoo, $foo, $msg );
80 $this->assertSame( $expectedBar, $bar, $msg );
84 * @covers Hooks::isRegistered
85 * @covers Hooks::register
86 * @covers Hooks::getHandlers
89 public function testNewStyleHookInteraction() {
92 $a = new NothingClass();
93 $b = new NothingClass();
95 $wgHooks['MediaWikiHooksTest001'][] = $a;
97 Hooks
::isRegistered( 'MediaWikiHooksTest001' ),
98 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
101 Hooks
::register( 'MediaWikiHooksTest001', $b );
104 count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ),
105 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
111 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
115 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
120 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
125 * @expectedException MWException
128 public function testUncallableFunction() {
129 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
130 Hooks
::run( 'MediaWikiHooksTest001', [] );
136 public function testFalseReturn() {
137 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
140 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
146 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
147 $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
151 * @covers Hooks::runWithoutAbort
153 public function testRunWithoutAbort() {
155 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
157 return true; // Explicit true
159 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
161 return; // Implicit null
163 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
168 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
169 $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
173 * @covers Hooks::runWithoutAbort
175 public function testRunWithoutAbortWarning() {
176 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
179 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
185 $this->setExpectedException(
186 UnexpectedValueException
::class,
187 'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
188 'unabortable MediaWikiHooksTest001'
190 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
194 * @expectedException FatalError
197 public function testFatalError() {
198 Hooks
::register( 'MediaWikiHooksTest001', function () {
201 Hooks
::run( 'MediaWikiHooksTest001', [] );
205 function NothingFunction( &$foo, &$bar ) {
206 $foo = 'changed-func';
211 function NothingFunctionData( $data, &$foo, &$bar ) {
220 public static function someStatic( &$foo, &$bar ) {
221 $foo = 'changed-static';
226 public function someNonStatic( &$foo, &$bar ) {
228 $foo = 'changed-nonstatic';
229 $bar = 'changed-nonstatic';
234 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
236 $foo = 'changed-onevent';
241 public function someNonStaticWithData( $data, &$foo, &$bar ) {