MediaWiki  1.30.0
ApiModuleManagerTest.php
Go to the documentation of this file.
1 <?php
2 
11 
12  private function getModuleManager() {
13  $request = new FauxRequest();
14  $main = new ApiMain( $request );
15  return new ApiModuleManager( $main );
16  }
17 
18  public function newApiLogin( $main, $action ) {
19  return new ApiLogin( $main, $action );
20  }
21 
22  public function addModuleProvider() {
23  return [
24  'plain class' => [
25  'login',
26  'action',
27  'ApiLogin',
28  null,
29  ],
30 
31  'with factory' => [
32  'login',
33  'action',
34  'ApiLogin',
35  [ $this, 'newApiLogin' ],
36  ],
37 
38  'with closure' => [
39  'logout',
40  'action',
41  'ApiLogout',
42  function ( ApiMain $main, $action ) {
43  return new ApiLogout( $main, $action );
44  },
45  ],
46  ];
47  }
48 
52  public function testAddModule( $name, $group, $class, $factory = null ) {
53  $moduleManager = $this->getModuleManager();
54  $moduleManager->addModule( $name, $group, $class, $factory );
55 
56  $this->assertTrue( $moduleManager->isDefined( $name, $group ), 'isDefined' );
57  $this->assertNotNull( $moduleManager->getModule( $name, $group, true ), 'getModule' );
58  }
59 
60  public function addModulesProvider() {
61  return [
62  'empty' => [
63  [],
64  'action',
65  ],
66 
67  'simple' => [
68  [
69  'login' => 'ApiLogin',
70  'logout' => 'ApiLogout',
71  ],
72  'action',
73  ],
74 
75  'with factories' => [
76  [
77  'login' => [
78  'class' => 'ApiLogin',
79  'factory' => [ $this, 'newApiLogin' ],
80  ],
81  'logout' => [
82  'class' => 'ApiLogout',
83  'factory' => function ( ApiMain $main, $action ) {
84  return new ApiLogout( $main, $action );
85  },
86  ],
87  ],
88  'action',
89  ],
90  ];
91  }
92 
96  public function testAddModules( array $modules, $group ) {
97  $moduleManager = $this->getModuleManager();
98  $moduleManager->addModules( $modules, $group );
99 
100  foreach ( array_keys( $modules ) as $name ) {
101  $this->assertTrue( $moduleManager->isDefined( $name, $group ), 'isDefined' );
102  $this->assertNotNull( $moduleManager->getModule( $name, $group, true ), 'getModule' );
103  }
104 
105  $this->assertTrue( true ); // Don't mark the test as risky if $modules is empty
106  }
107 
108  public function getModuleProvider() {
109  $modules = [
110  'feedrecentchanges' => 'ApiFeedRecentChanges',
111  'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
112  'login' => [
113  'class' => 'ApiLogin',
114  'factory' => [ $this, 'newApiLogin' ],
115  ],
116  'logout' => [
117  'class' => 'ApiLogout',
118  'factory' => function ( ApiMain $main, $action ) {
119  return new ApiLogout( $main, $action );
120  },
121  ],
122  ];
123 
124  return [
125  'legacy entry' => [
126  $modules,
127  'feedrecentchanges',
128  'ApiFeedRecentChanges',
129  ],
130 
131  'just a class' => [
132  $modules,
133  'feedcontributions',
134  'ApiFeedContributions',
135  ],
136 
137  'with factory' => [
138  $modules,
139  'login',
140  'ApiLogin',
141  ],
142 
143  'with closure' => [
144  $modules,
145  'logout',
146  'ApiLogout',
147  ],
148  ];
149  }
150 
155  public function testGetModule( $modules, $name, $expectedClass ) {
156  $moduleManager = $this->getModuleManager();
157  $moduleManager->addModules( $modules, 'test' );
158 
159  // should return the right module
160  $module1 = $moduleManager->getModule( $name, null, false );
161  $this->assertInstanceOf( $expectedClass, $module1 );
162 
163  // should pass group check (with caching disabled)
164  $module2 = $moduleManager->getModule( $name, 'test', true );
165  $this->assertNotNull( $module2 );
166 
167  // should use cached instance
168  $module3 = $moduleManager->getModule( $name, null, false );
169  $this->assertSame( $module1, $module3 );
170 
171  // should not use cached instance if caching is disabled
172  $module4 = $moduleManager->getModule( $name, null, true );
173  $this->assertNotSame( $module1, $module4 );
174  }
175 
179  public function testGetModule_null() {
180  $modules = [
181  'login' => 'ApiLogin',
182  'logout' => 'ApiLogout',
183  ];
184 
185  $moduleManager = $this->getModuleManager();
186  $moduleManager->addModules( $modules, 'test' );
187 
188  $this->assertNull( $moduleManager->getModule( 'quux' ), 'unknown name' );
189  $this->assertNull( $moduleManager->getModule( 'login', 'bla' ), 'wrong group' );
190  }
191 
195  public function testGetNames() {
196  $fooModules = [
197  'login' => 'ApiLogin',
198  'logout' => 'ApiLogout',
199  ];
200 
201  $barModules = [
202  'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
203  'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
204  ];
205 
206  $moduleManager = $this->getModuleManager();
207  $moduleManager->addModules( $fooModules, 'foo' );
208  $moduleManager->addModules( $barModules, 'bar' );
209 
210  $fooNames = $moduleManager->getNames( 'foo' );
211  $this->assertArrayEquals( array_keys( $fooModules ), $fooNames );
212 
213  $allNames = $moduleManager->getNames();
214  $allModules = array_merge( $fooModules, $barModules );
215  $this->assertArrayEquals( array_keys( $allModules ), $allNames );
216  }
217 
221  public function testGetNamesWithClasses() {
222  $fooModules = [
223  'login' => 'ApiLogin',
224  'logout' => 'ApiLogout',
225  ];
226 
227  $barModules = [
228  'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
229  'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
230  ];
231 
232  $moduleManager = $this->getModuleManager();
233  $moduleManager->addModules( $fooModules, 'foo' );
234  $moduleManager->addModules( $barModules, 'bar' );
235 
236  $fooNamesWithClasses = $moduleManager->getNamesWithClasses( 'foo' );
237  $this->assertArrayEquals( $fooModules, $fooNamesWithClasses );
238 
239  $allNamesWithClasses = $moduleManager->getNamesWithClasses();
240  $allModules = array_merge( $fooModules, [
241  'feedcontributions' => 'ApiFeedContributions',
242  'feedrecentchanges' => 'ApiFeedRecentChanges',
243  ] );
244  $this->assertArrayEquals( $allModules, $allNamesWithClasses );
245  }
246 
250  public function testGetModuleGroup() {
251  $fooModules = [
252  'login' => 'ApiLogin',
253  'logout' => 'ApiLogout',
254  ];
255 
256  $barModules = [
257  'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
258  'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
259  ];
260 
261  $moduleManager = $this->getModuleManager();
262  $moduleManager->addModules( $fooModules, 'foo' );
263  $moduleManager->addModules( $barModules, 'bar' );
264 
265  $this->assertEquals( 'foo', $moduleManager->getModuleGroup( 'login' ) );
266  $this->assertEquals( 'bar', $moduleManager->getModuleGroup( 'feedrecentchanges' ) );
267  $this->assertNull( $moduleManager->getModuleGroup( 'quux' ) );
268  }
269 
273  public function testGetGroups() {
274  $fooModules = [
275  'login' => 'ApiLogin',
276  'logout' => 'ApiLogout',
277  ];
278 
279  $barModules = [
280  'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
281  'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
282  ];
283 
284  $moduleManager = $this->getModuleManager();
285  $moduleManager->addModules( $fooModules, 'foo' );
286  $moduleManager->addModules( $barModules, 'bar' );
287 
288  $groups = $moduleManager->getGroups();
289  $this->assertArrayEquals( [ 'foo', 'bar' ], $groups );
290  }
291 
295  public function testGetClassName() {
296  $fooModules = [
297  'login' => 'ApiLogin',
298  'logout' => 'ApiLogout',
299  ];
300 
301  $barModules = [
302  'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
303  'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
304  ];
305 
306  $moduleManager = $this->getModuleManager();
307  $moduleManager->addModules( $fooModules, 'foo' );
308  $moduleManager->addModules( $barModules, 'bar' );
309 
310  $this->assertEquals(
311  'ApiLogin',
312  $moduleManager->getClassName( 'login' )
313  );
314  $this->assertEquals(
315  'ApiLogout',
316  $moduleManager->getClassName( 'logout' )
317  );
318  $this->assertEquals(
319  'ApiFeedContributions',
320  $moduleManager->getClassName( 'feedcontributions' )
321  );
322  $this->assertEquals(
323  'ApiFeedRecentChanges',
324  $moduleManager->getClassName( 'feedrecentchanges' )
325  );
326  $this->assertFalse(
327  $moduleManager->getClassName( 'nonexistentmodule' )
328  );
329  }
330 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:45
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:1552
ApiModuleManagerTest\addModulesProvider
addModulesProvider()
Definition: ApiModuleManagerTest.php:60
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ApiLogout
API module to allow users to log out of the wiki.
Definition: ApiLogout.php:35
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:35
ApiModuleManagerTest\getModuleManager
getModuleManager()
Definition: ApiModuleManagerTest.php:12
ApiModuleManagerTest\addModuleProvider
addModuleProvider()
Definition: ApiModuleManagerTest.php:22
ApiModuleManagerTest\getModuleProvider
getModuleProvider()
Definition: ApiModuleManagerTest.php:108
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
ApiModuleManagerTest\testGetClassName
testGetClassName()
ApiModuleManager::getClassName.
Definition: ApiModuleManagerTest.php:295
$modules
$modules
Definition: HTMLFormElement.php:12
ApiModuleManagerTest\testGetGroups
testGetGroups()
ApiModuleManager::getGroups.
Definition: ApiModuleManagerTest.php:273
ApiModuleManagerTest\testGetNames
testGetNames()
ApiModuleManager::getNames.
Definition: ApiModuleManagerTest.php:195
ApiModuleManagerTest\testGetModule
testGetModule( $modules, $name, $expectedClass)
ApiModuleManager::getModule getModuleProvider.
Definition: ApiModuleManagerTest.php:155
ApiModuleManagerTest
ApiModuleManager.
Definition: ApiModuleManagerTest.php:10
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2581
ApiModuleManager
This class holds a list of modules and handles instantiation.
Definition: ApiModuleManager.php:34
ApiModuleManagerTest\testGetModule_null
testGetModule_null()
ApiModuleManager::getModule.
Definition: ApiModuleManagerTest.php:179
ApiModuleManagerTest\testAddModule
testAddModule( $name, $group, $class, $factory=null)
addModuleProvider
Definition: ApiModuleManagerTest.php:52
ApiModuleManagerTest\testGetNamesWithClasses
testGetNamesWithClasses()
ApiModuleManager::getNamesWithClasses.
Definition: ApiModuleManagerTest.php:221
ApiModuleManagerTest\testGetModuleGroup
testGetModuleGroup()
ApiModuleManager::getModuleGroup.
Definition: ApiModuleManagerTest.php:250
ApiModuleManagerTest\newApiLogin
newApiLogin( $main, $action)
Definition: ApiModuleManagerTest.php:18
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiLogin
Unit to authenticate log-in attempts to the current wiki.
Definition: ApiLogin.php:38
ApiModuleManagerTest\testAddModules
testAddModules(array $modules, $group)
addModulesProvider
Definition: ApiModuleManagerTest.php:96
array
the array() calling protocol came about after MediaWiki 1.4rc1.