4 * @covers ApiModuleManager
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() {
24 'plain class' => array(
31 'with factory' => array(
35 array( $this, 'newApiLogin' ),
38 'with closure' => array(
42 function ( ApiMain
$main, $action ) {
43 return new ApiLogout( $main, $action );
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() {
69 'login' => 'ApiLogin',
70 'logout' => 'ApiLogout',
75 'with factories' => array(
78 'class' => 'ApiLogin',
79 'factory' => array( $this, 'newApiLogin' ),
82 'class' => 'ApiLogout',
83 'factory' => function ( ApiMain
$main, $action ) {
84 return new ApiLogout( $main, $action );
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() {
110 'feedrecentchanges' => 'ApiFeedRecentChanges',
111 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
113 'class' => 'ApiLogin',
114 'factory' => array( $this, 'newApiLogin' ),
117 'class' => 'ApiLogout',
118 'factory' => function ( ApiMain
$main, $action ) {
119 return new ApiLogout( $main, $action );
125 'legacy entry' => array(
128 'ApiFeedRecentChanges',
131 'just a class' => array(
134 'ApiFeedContributions',
137 'with factory' => array(
143 'with closure' => array(
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() {
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() {
197 'login' => 'ApiLogin',
198 'logout' => 'ApiLogout',
202 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
203 'feedrecentchanges' => array( '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() {
223 'login' => 'ApiLogin',
224 'logout' => 'ApiLogout',
228 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
229 'feedrecentchanges' => array( '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, array(
241 'feedcontributions' => 'ApiFeedContributions',
242 'feedrecentchanges' => 'ApiFeedRecentChanges',
244 $this->assertArrayEquals( $allModules, $allNamesWithClasses );
248 * @covers ApiModuleManager::getModuleGroup
250 public function testGetModuleGroup() {
252 'login' => 'ApiLogin',
253 'logout' => 'ApiLogout',
257 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
258 'feedrecentchanges' => array( '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() {
275 'login' => 'ApiLogin',
276 'logout' => 'ApiLogout',
280 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
281 'feedrecentchanges' => array( 'class' => 'ApiFeedRecentChanges' ),
284 $moduleManager = $this->getModuleManager();
285 $moduleManager->addModules( $fooModules, 'foo' );
286 $moduleManager->addModules( $barModules, 'bar' );
288 $groups = $moduleManager->getGroups();
289 $this->assertArrayEquals( array( 'foo', 'bar' ), $groups );
293 * @covers ApiModuleManager::getClassName
295 public function testGetClassName() {
297 'login' => 'ApiLogin',
298 'logout' => 'ApiLogout',
302 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
303 'feedrecentchanges' => array( 'class' => 'ApiFeedRecentChanges' ),
306 $moduleManager = $this->getModuleManager();
307 $moduleManager->addModules( $fooModules, 'foo' );
308 $moduleManager->addModules( $barModules, 'bar' );
310 $this->assertEquals( 'ApiLogin', $moduleManager->getClassName( 'login' ) );
311 $this->assertEquals( 'ApiLogout', $moduleManager->getClassName( 'logout' ) );
312 $this->assertEquals( 'ApiFeedContributions', $moduleManager->getClassName( 'feedcontributions' ) );
313 $this->assertEquals( 'ApiFeedRecentChanges', $moduleManager->getClassName( 'feedrecentchanges' ) );
314 $this->assertFalse( $moduleManager->getClassName( 'nonexistentmodule' ) );