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 ) {
57 $foo = $bar = 'original';
59 $wgHooks['MediaWikiHooksTest001'][] = $hook;
60 wfRunHooks( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
62 $this->assertSame( $expectedFoo, $foo, $msg );
63 $this->assertSame( $expectedBar, $bar, $msg );
67 * @dataProvider provideHooks
68 * @covers Hooks::register
71 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
72 $foo = $bar = 'original';
74 Hooks
::register( 'MediaWikiHooksTest001', $hook );
75 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
77 $this->assertSame( $expectedFoo, $foo, $msg );
78 $this->assertSame( $expectedBar, $bar, $msg );
82 * @covers Hooks::isRegistered
83 * @covers Hooks::register
84 * @covers Hooks::getHandlers
87 public function testNewStyleHookInteraction() {
90 $a = new NothingClass();
91 $b = new NothingClass();
93 $wgHooks['MediaWikiHooksTest001'][] = $a;
95 Hooks
::isRegistered( 'MediaWikiHooksTest001' ),
96 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
99 Hooks
::register( 'MediaWikiHooksTest001', $b );
102 count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ),
103 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
109 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
113 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
118 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
123 * @expectedException MWException
126 public function testUncallableFunction() {
127 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
128 Hooks
::run( 'MediaWikiHooksTest001', [] );
134 public function testFalseReturn() {
135 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
138 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
144 Hooks
::run( 'MediaWikiHooksTest001', [ &$foo ] );
145 $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
149 * @covers Hooks::runWithoutAbort
151 public function testRunWithoutAbort() {
153 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
155 return true; // Explicit true
157 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
159 return; // Implicit null
161 Hooks
::register( 'MediaWikiHooksTest001', function ( &$list ) {
166 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
167 $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
171 * @covers Hooks::runWithoutAbort
173 public function testRunWithoutAbortWarning() {
174 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
177 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
183 $this->setExpectedException(
184 UnexpectedValueException
::class,
185 'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
186 'unabortable MediaWikiHooksTest001'
188 Hooks
::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
192 * @expectedException FatalError
195 public function testFatalError() {
196 Hooks
::register( 'MediaWikiHooksTest001', function () {
199 Hooks
::run( 'MediaWikiHooksTest001', [] );
203 function NothingFunction( &$foo, &$bar ) {
204 $foo = 'changed-func';
209 function NothingFunctionData( $data, &$foo, &$bar ) {
218 public static function someStatic( &$foo, &$bar ) {
219 $foo = 'changed-static';
224 public function someNonStatic( &$foo, &$bar ) {
226 $foo = 'changed-nonstatic';
227 $bar = 'changed-nonstatic';
232 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
234 $foo = 'changed-onevent';
239 public function someNonStaticWithData( $data, &$foo, &$bar ) {