MediaWiki  1.28.0
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  $foo = $bar = 'original';
58 
59  $wgHooks['MediaWikiHooksTest001'][] = $hook;
60  wfRunHooks( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
61 
62  $this->assertSame( $expectedFoo, $foo, $msg );
63  $this->assertSame( $expectedBar, $bar, $msg );
64  }
65 
71  public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
72  $foo = $bar = 'original';
73 
74  Hooks::register( 'MediaWikiHooksTest001', $hook );
75  Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
76 
77  $this->assertSame( $expectedFoo, $foo, $msg );
78  $this->assertSame( $expectedBar, $bar, $msg );
79  }
80 
87  public function testNewStyleHookInteraction() {
89 
90  $a = new NothingClass();
91  $b = new NothingClass();
92 
93  $wgHooks['MediaWikiHooksTest001'][] = $a;
94  $this->assertTrue(
95  Hooks::isRegistered( 'MediaWikiHooksTest001' ),
96  'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
97  );
98 
99  Hooks::register( 'MediaWikiHooksTest001', $b );
100  $this->assertEquals(
101  2,
102  count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ),
103  'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
104  );
105 
106  $foo = 'quux';
107  $bar = 'qaax';
108 
109  Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
110  $this->assertEquals(
111  1,
112  $a->calls,
113  'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
114  );
115  $this->assertEquals(
116  1,
117  $b->calls,
118  'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
119  );
120  }
121 
126  public function testUncallableFunction() {
127  Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
128  Hooks::run( 'MediaWikiHooksTest001', [] );
129  }
130 
134  public function testFalseReturn() {
135  Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
136  return false;
137  } );
138  Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
139  $foo = 'test';
140 
141  return true;
142  } );
143  $foo = 'original';
144  Hooks::run( 'MediaWikiHooksTest001', [ &$foo ] );
145  $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
146  }
147 
152  public function testFatalError() {
153  Hooks::register( 'MediaWikiHooksTest001', function () {
154  return 'test';
155  } );
156  Hooks::run( 'MediaWikiHooksTest001', [] );
157  }
158 }
159 
160 function NothingFunction( &$foo, &$bar ) {
161  $foo = 'changed-func';
162 
163  return true;
164 }
165 
166 function NothingFunctionData( $data, &$foo, &$bar ) {
167  $foo = $data;
168 
169  return true;
170 }
171 
173  public $calls = 0;
174 
175  public static function someStatic( &$foo, &$bar ) {
176  $foo = 'changed-static';
177 
178  return true;
179  }
180 
181  public function someNonStatic( &$foo, &$bar ) {
182  $this->calls++;
183  $foo = 'changed-nonstatic';
184  $bar = 'changed-nonstatic';
185 
186  return true;
187  }
188 
189  public function onMediaWikiHooksTest001( &$foo, &$bar ) {
190  $this->calls++;
191  $foo = 'changed-onevent';
192 
193  return true;
194  }
195 
196  public function someNonStaticWithData( $data, &$foo, &$bar ) {
197  $this->calls++;
198  $foo = $data;
199 
200  return true;
201  }
202 }
NothingFunction(&$foo, &$bar)
Definition: HooksTest.php:160
the array() calling protocol came about after MediaWiki 1.4rc1.
testFatalError()
FatalError Hooks::run.
Definition: HooksTest.php:152
testOldStyleHooks($msg, array $hook, $expectedFoo, $expectedBar)
provideHooks wfRunHooks
Definition: HooksTest.php:55
static isRegistered($name)
Returns true if a hook has a function registered to it.
Definition: Hooks.php:83
wfRunHooks($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
static getHandlers($name)
Returns an array of all the event functions attached to a hook This combines functions registered via...
Definition: Hooks.php:97
$wgHooks['ArticleShow'][]
Definition: hooks.txt:110
testUncallableFunction()
MWException Hooks::run.
Definition: HooksTest.php:126
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static provideHooks()
Definition: HooksTest.php:12
NothingFunctionData($data, &$foo, &$bar)
Definition: HooksTest.php:166
static register($name, $callback)
Attach an event handler to a given hook.
Definition: Hooks.php:49
static clear($name)
Clears hooks registered via Hooks::register().
Definition: Hooks.php:66
onMediaWikiHooksTest001(&$foo, &$bar)
Definition: HooksTest.php:189
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
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:35
testNewStyleHooks($msg, $hook, $expectedFoo, $expectedBar)
provideHooks Hooks::register Hooks::run
Definition: HooksTest.php:71
static someStatic(&$foo, &$bar)
Definition: HooksTest.php:175
testFalseReturn()
Hooks::run.
Definition: HooksTest.php:134
testNewStyleHookInteraction()
Hooks::isRegistered Hooks::register Hooks::getHandlers Hooks::run.
Definition: HooksTest.php:87
someNonStaticWithData($data, &$foo, &$bar)
Definition: HooksTest.php:196
someNonStatic(&$foo, &$bar)
Definition: HooksTest.php:181