MediaWiki  REL1_31
HooksTest.php
Go to the documentation of this file.
1 <?php
2 
3 class HooksTest extends MediaWikiTestCase {
4 
5  function setUp() {
7  parent::setUp();
8  Hooks::clear( 'MediaWikiHooksTest001' );
9  unset( $wgHooks['MediaWikiHooksTest001'] );
10  }
11 
12  public static function provideHooks() {
13  $i = new NothingClass();
14 
15  return [
16  [
17  'Object and method',
18  [ $i, 'someNonStatic' ],
19  'changed-nonstatic',
20  'changed-nonstatic'
21  ],
22  [ 'Object and no method', [ $i ], 'changed-onevent', 'original' ],
23  [
24  'Object and method with data',
25  [ $i, 'someNonStaticWithData', 'data' ],
26  'data',
27  'original'
28  ],
29  [ 'Object and static method', [ $i, 'someStatic' ], 'changed-static', 'original' ],
30  [
31  'Class::method static call',
32  [ 'NothingClass::someStatic' ],
33  'changed-static',
34  'original'
35  ],
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';
40 
41  return true;
42  } ], 'changed-closure', 'original' ],
43  [ 'Closure with data', [ function ( $data, &$foo, $bar ) {
44  $foo = $data;
45 
46  return true;
47  }, 'data' ], 'data', 'original' ]
48  ];
49  }
50 
55  public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
57 
58  $this->hideDeprecated( 'wfRunHooks' );
59  $foo = $bar = 'original';
60 
61  $wgHooks['MediaWikiHooksTest001'][] = $hook;
62  wfRunHooks( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
63 
64  $this->assertSame( $expectedFoo, $foo, $msg );
65  $this->assertSame( $expectedBar, $bar, $msg );
66  }
67 
74  public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
75  $foo = $bar = 'original';
76 
77  Hooks::register( 'MediaWikiHooksTest001', $hook );
78  Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
79 
80  $this->assertSame( $expectedFoo, $foo, $msg );
81  $this->assertSame( $expectedBar, $bar, $msg );
82  }
83 
91  public function testNewStyleHookInteraction() {
93 
94  $a = new NothingClass();
95  $b = new NothingClass();
96 
97  $wgHooks['MediaWikiHooksTest001'][] = $a;
98  $this->assertTrue(
99  Hooks::isRegistered( 'MediaWikiHooksTest001' ),
100  'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
101  );
102 
103  Hooks::register( 'MediaWikiHooksTest001', $b );
104  $this->assertEquals(
105  2,
106  count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ),
107  'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
108  );
109 
110  $foo = 'quux';
111  $bar = 'qaax';
112 
113  Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
114  $this->assertEquals(
115  1,
116  $a->calls,
117  'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
118  );
119  $this->assertEquals(
120  1,
121  $b->calls,
122  'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
123  );
124  }
125 
131  public function testUncallableFunction() {
132  Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
133  Hooks::run( 'MediaWikiHooksTest001', [] );
134  }
135 
140  public function testFalseReturn() {
141  Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
142  return false;
143  } );
144  Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
145  $foo = 'test';
146 
147  return true;
148  } );
149  $foo = 'original';
150  Hooks::run( 'MediaWikiHooksTest001', [ &$foo ] );
151  $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
152  }
153 
158  public function testRunWithoutAbort() {
159  $list = [];
160  Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
161  $list[] = 1;
162  return true; // Explicit true
163  } );
164  Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
165  $list[] = 2;
166  return; // Implicit null
167  } );
168  Hooks::register( 'MediaWikiHooksTest001', function ( &$list ) {
169  $list[] = 3;
170  // No return
171  } );
172 
173  Hooks::runWithoutAbort( 'MediaWikiHooksTest001', [ &$list ] );
174  $this->assertSame( [ 1, 2, 3 ], $list, 'All hooks ran.' );
175  }
176 
181  public function testRunWithoutAbortWarning() {
182  Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
183  return false;
184  } );
185  Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
186  $foo = 'test';
187  return true;
188  } );
189  $foo = 'original';
190 
191  $this->setExpectedException(
193  'Invalid return from hook-MediaWikiHooksTest001-closure for ' .
194  'unabortable MediaWikiHooksTest001'
195  );
196  Hooks::runWithoutAbort( 'MediaWikiHooksTest001', [ &$foo ] );
197  }
198 
203  public function testFatalError() {
204  Hooks::register( 'MediaWikiHooksTest001', function () {
205  return 'test';
206  } );
207  Hooks::run( 'MediaWikiHooksTest001', [] );
208  }
209 }
210 
211 function NothingFunction( &$foo, &$bar ) {
212  $foo = 'changed-func';
213 
214  return true;
215 }
216 
217 function NothingFunctionData( $data, &$foo, &$bar ) {
218  $foo = $data;
219 
220  return true;
221 }
222 
224  public $calls = 0;
225 
226  public static function someStatic( &$foo, &$bar ) {
227  $foo = 'changed-static';
228 
229  return true;
230  }
231 
232  public function someNonStatic( &$foo, &$bar ) {
233  $this->calls++;
234  $foo = 'changed-nonstatic';
235  $bar = 'changed-nonstatic';
236 
237  return true;
238  }
239 
240  public function onMediaWikiHooksTest001( &$foo, &$bar ) {
241  $this->calls++;
242  $foo = 'changed-onevent';
243 
244  return true;
245  }
246 
247  public function someNonStaticWithData( $data, &$foo, &$bar ) {
248  $this->calls++;
249  $foo = $data;
250 
251  return true;
252  }
253 }
HooksTest\provideHooks
static provideHooks()
Definition: HooksTest.php:12
HooksTest\testFalseReturn
testFalseReturn()
Hooks::run Hooks::callHook.
Definition: HooksTest.php:140
Hooks\getHandlers
static getHandlers( $name)
Returns an array of all the event functions attached to a hook This combines functions registered via...
Definition: Hooks.php:97
array
the array() calling protocol came about after MediaWiki 1.4rc1.
NothingClass\someNonStatic
someNonStatic(&$foo, &$bar)
Definition: HooksTest.php:232
HooksTest\testFatalError
testFatalError()
FatalError Hooks::run.
Definition: HooksTest.php:203
NothingFunctionData
NothingFunctionData( $data, &$foo, &$bar)
Definition: HooksTest.php:217
HooksTest\setUp
setUp()
Definition: HooksTest.php:5
Hooks\clear
static clear( $name)
Clears hooks registered via Hooks::register().
Definition: Hooks.php:66
HooksTest\testUncallableFunction
testUncallableFunction()
MWException Hooks::run Hooks::callHook.
Definition: HooksTest.php:131
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
NothingFunction
NothingFunction(&$foo, &$bar)
Definition: HooksTest.php:211
HooksTest\testRunWithoutAbortWarning
testRunWithoutAbortWarning()
Hooks::runWithoutAbort Hooks::callHook.
Definition: HooksTest.php:181
NothingClass\someStatic
static someStatic(&$foo, &$bar)
Definition: HooksTest.php:226
HooksTest\testRunWithoutAbort
testRunWithoutAbort()
Hooks::runWithoutAbort Hooks::callHook.
Definition: HooksTest.php:158
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
NothingClass\$calls
$calls
Definition: HooksTest.php:224
NothingClass\onMediaWikiHooksTest001
onMediaWikiHooksTest001(&$foo, &$bar)
Definition: HooksTest.php:240
MediaWikiTestCase\hideDeprecated
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
Definition: MediaWikiTestCase.php:1675
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
HooksTest\testOldStyleHooks
testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar)
provideHooks wfRunHooks
Definition: HooksTest.php:55
NothingClass
Definition: HooksTest.php:223
$wgHooks
$wgHooks['ArticleShow'][]
Definition: hooks.txt:108
Hooks\runWithoutAbort
static runWithoutAbort( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:234
Hooks\register
static register( $name, $callback)
Attach an event handler to a given hook.
Definition: Hooks.php:49
HooksTest
Definition: HooksTest.php:3
Hooks\isRegistered
static isRegistered( $name)
Returns true if a hook has a function registered to it.
Definition: Hooks.php:83
wfRunHooks
wfRunHooks( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:3183
HooksTest\testNewStyleHooks
testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar)
provideHooks Hooks::register Hooks::run Hooks::callHook
Definition: HooksTest.php:74
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
NothingClass\someNonStaticWithData
someNonStaticWithData( $data, &$foo, &$bar)
Definition: HooksTest.php:247
HooksTest\testNewStyleHookInteraction
testNewStyleHookInteraction()
Hooks::isRegistered Hooks::register Hooks::getHandlers Hooks::run Hooks::callHook.
Definition: HooksTest.php:91