Move DatabaseDomain to Rdbms namespace
[mediawiki.git] / tests / phpunit / includes / api / ApiModuleManagerTest.php
blobbe17bba2b364041476a2785272a816361dbaa626
1 <?php
3 /**
4 * @covers ApiModuleManager
6 * @group API
7 * @group Database
8 * @group medium
9 */
10 class ApiModuleManagerTest extends MediaWikiTestCase {
12 private function getModuleManager() {
13 $request = new FauxRequest();
14 $main = new ApiMain( $request );
15 return new ApiModuleManager( $main );
18 public function newApiLogin( $main, $action ) {
19 return new ApiLogin( $main, $action );
22 public function addModuleProvider() {
23 return [
24 'plain class' => [
25 'login',
26 'action',
27 'ApiLogin',
28 null,
31 'with factory' => [
32 'login',
33 'action',
34 'ApiLogin',
35 [ $this, 'newApiLogin' ],
38 'with closure' => [
39 'logout',
40 'action',
41 'ApiLogout',
42 function ( ApiMain $main, $action ) {
43 return new ApiLogout( $main, $action );
49 /**
50 * @dataProvider addModuleProvider
52 public function testAddModule( $name, $group, $class, $factory = null ) {
53 $moduleManager = $this->getModuleManager();
54 $moduleManager->addModule( $name, $group, $class, $factory );
56 $this->assertTrue( $moduleManager->isDefined( $name, $group ), 'isDefined' );
57 $this->assertNotNull( $moduleManager->getModule( $name, $group, true ), 'getModule' );
60 public function addModulesProvider() {
61 return [
62 'empty' => [
63 [],
64 'action',
67 'simple' => [
69 'login' => 'ApiLogin',
70 'logout' => 'ApiLogout',
72 'action',
75 'with factories' => [
77 'login' => [
78 'class' => 'ApiLogin',
79 'factory' => [ $this, 'newApiLogin' ],
81 'logout' => [
82 'class' => 'ApiLogout',
83 'factory' => function ( ApiMain $main, $action ) {
84 return new ApiLogout( $main, $action );
88 'action',
93 /**
94 * @dataProvider addModulesProvider
96 public function testAddModules( array $modules, $group ) {
97 $moduleManager = $this->getModuleManager();
98 $moduleManager->addModules( $modules, $group );
100 foreach ( array_keys( $modules ) as $name ) {
101 $this->assertTrue( $moduleManager->isDefined( $name, $group ), 'isDefined' );
102 $this->assertNotNull( $moduleManager->getModule( $name, $group, true ), 'getModule' );
105 $this->assertTrue( true ); // Don't mark the test as risky if $modules is empty
108 public function getModuleProvider() {
109 $modules = [
110 'feedrecentchanges' => 'ApiFeedRecentChanges',
111 'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
112 'login' => [
113 'class' => 'ApiLogin',
114 'factory' => [ $this, 'newApiLogin' ],
116 'logout' => [
117 'class' => 'ApiLogout',
118 'factory' => function ( ApiMain $main, $action ) {
119 return new ApiLogout( $main, $action );
124 return [
125 'legacy entry' => [
126 $modules,
127 'feedrecentchanges',
128 'ApiFeedRecentChanges',
131 'just a class' => [
132 $modules,
133 'feedcontributions',
134 'ApiFeedContributions',
137 'with factory' => [
138 $modules,
139 'login',
140 'ApiLogin',
143 'with closure' => [
144 $modules,
145 'logout',
146 'ApiLogout',
152 * @covers ApiModuleManager::getModule
153 * @dataProvider getModuleProvider
155 public function testGetModule( $modules, $name, $expectedClass ) {
156 $moduleManager = $this->getModuleManager();
157 $moduleManager->addModules( $modules, 'test' );
159 // should return the right module
160 $module1 = $moduleManager->getModule( $name, null, false );
161 $this->assertInstanceOf( $expectedClass, $module1 );
163 // should pass group check (with caching disabled)
164 $module2 = $moduleManager->getModule( $name, 'test', true );
165 $this->assertNotNull( $module2 );
167 // should use cached instance
168 $module3 = $moduleManager->getModule( $name, null, false );
169 $this->assertSame( $module1, $module3 );
171 // should not use cached instance if caching is disabled
172 $module4 = $moduleManager->getModule( $name, null, true );
173 $this->assertNotSame( $module1, $module4 );
177 * @covers ApiModuleManager::getModule
179 public function testGetModule_null() {
180 $modules = [
181 'login' => 'ApiLogin',
182 'logout' => 'ApiLogout',
185 $moduleManager = $this->getModuleManager();
186 $moduleManager->addModules( $modules, 'test' );
188 $this->assertNull( $moduleManager->getModule( 'quux' ), 'unknown name' );
189 $this->assertNull( $moduleManager->getModule( 'login', 'bla' ), 'wrong group' );
193 * @covers ApiModuleManager::getNames
195 public function testGetNames() {
196 $fooModules = [
197 'login' => 'ApiLogin',
198 'logout' => 'ApiLogout',
201 $barModules = [
202 'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
203 'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
206 $moduleManager = $this->getModuleManager();
207 $moduleManager->addModules( $fooModules, 'foo' );
208 $moduleManager->addModules( $barModules, 'bar' );
210 $fooNames = $moduleManager->getNames( 'foo' );
211 $this->assertArrayEquals( array_keys( $fooModules ), $fooNames );
213 $allNames = $moduleManager->getNames();
214 $allModules = array_merge( $fooModules, $barModules );
215 $this->assertArrayEquals( array_keys( $allModules ), $allNames );
219 * @covers ApiModuleManager::getNamesWithClasses
221 public function testGetNamesWithClasses() {
222 $fooModules = [
223 'login' => 'ApiLogin',
224 'logout' => 'ApiLogout',
227 $barModules = [
228 'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
229 'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
232 $moduleManager = $this->getModuleManager();
233 $moduleManager->addModules( $fooModules, 'foo' );
234 $moduleManager->addModules( $barModules, 'bar' );
236 $fooNamesWithClasses = $moduleManager->getNamesWithClasses( 'foo' );
237 $this->assertArrayEquals( $fooModules, $fooNamesWithClasses );
239 $allNamesWithClasses = $moduleManager->getNamesWithClasses();
240 $allModules = array_merge( $fooModules, [
241 'feedcontributions' => 'ApiFeedContributions',
242 'feedrecentchanges' => 'ApiFeedRecentChanges',
243 ] );
244 $this->assertArrayEquals( $allModules, $allNamesWithClasses );
248 * @covers ApiModuleManager::getModuleGroup
250 public function testGetModuleGroup() {
251 $fooModules = [
252 'login' => 'ApiLogin',
253 'logout' => 'ApiLogout',
256 $barModules = [
257 'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
258 'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
261 $moduleManager = $this->getModuleManager();
262 $moduleManager->addModules( $fooModules, 'foo' );
263 $moduleManager->addModules( $barModules, 'bar' );
265 $this->assertEquals( 'foo', $moduleManager->getModuleGroup( 'login' ) );
266 $this->assertEquals( 'bar', $moduleManager->getModuleGroup( 'feedrecentchanges' ) );
267 $this->assertNull( $moduleManager->getModuleGroup( 'quux' ) );
271 * @covers ApiModuleManager::getGroups
273 public function testGetGroups() {
274 $fooModules = [
275 'login' => 'ApiLogin',
276 'logout' => 'ApiLogout',
279 $barModules = [
280 'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
281 'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
284 $moduleManager = $this->getModuleManager();
285 $moduleManager->addModules( $fooModules, 'foo' );
286 $moduleManager->addModules( $barModules, 'bar' );
288 $groups = $moduleManager->getGroups();
289 $this->assertArrayEquals( [ 'foo', 'bar' ], $groups );
293 * @covers ApiModuleManager::getClassName
295 public function testGetClassName() {
296 $fooModules = [
297 'login' => 'ApiLogin',
298 'logout' => 'ApiLogout',
301 $barModules = [
302 'feedcontributions' => [ 'class' => 'ApiFeedContributions' ],
303 'feedrecentchanges' => [ 'class' => 'ApiFeedRecentChanges' ],
306 $moduleManager = $this->getModuleManager();
307 $moduleManager->addModules( $fooModules, 'foo' );
308 $moduleManager->addModules( $barModules, 'bar' );
310 $this->assertEquals(
311 'ApiLogin',
312 $moduleManager->getClassName( 'login' )
314 $this->assertEquals(
315 'ApiLogout',
316 $moduleManager->getClassName( 'logout' )
318 $this->assertEquals(
319 'ApiFeedContributions',
320 $moduleManager->getClassName( 'feedcontributions' )
322 $this->assertEquals(
323 'ApiFeedRecentChanges',
324 $moduleManager->getClassName( 'feedrecentchanges' )
326 $this->assertFalse(
327 $moduleManager->getClassName( 'nonexistentmodule' )