TitleTest: Break secure and split test into two tests with providers
[mediawiki.git] / tests / phpunit / includes / api / ApiModuleManagerTest.php
blob201eed188c2474ed834cba9c728cd20905ae35f6
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 array(
24 'plain class' => array(
25 'login',
26 'action',
27 'ApiLogin',
28 null,
31 'with factory' => array(
32 'login',
33 'action',
34 'ApiLogin',
35 array( $this, 'newApiLogin' ),
38 'with closure' => array(
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 array(
62 'empty' => array(
63 array(),
64 'action',
67 'simple' => array(
68 array(
69 'login' => 'ApiLogin',
70 'logout' => 'ApiLogout',
72 'action',
75 'with factories' => array(
76 array(
77 'login' => array(
78 'class' => 'ApiLogin',
79 'factory' => array( $this, 'newApiLogin' ),
81 'logout' => array(
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 = array(
110 'feedrecentchanges' => 'ApiFeedRecentChanges',
111 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
112 'login' => array(
113 'class' => 'ApiLogin',
114 'factory' => array( $this, 'newApiLogin' ),
116 'logout' => array(
117 'class' => 'ApiLogout',
118 'factory' => function ( ApiMain $main, $action ) {
119 return new ApiLogout( $main, $action );
124 return array(
125 'legacy entry' => array(
126 $modules,
127 'feedrecentchanges',
128 'ApiFeedRecentChanges',
131 'just a class' => array(
132 $modules,
133 'feedcontributions',
134 'ApiFeedContributions',
137 'with factory' => array(
138 $modules,
139 'login',
140 'ApiLogin',
143 'with closure' => array(
144 $modules,
145 'logout',
146 'ApiLogout',
152 * @dataProvider getModuleProvider
154 public function testGetModule( $modules, $name, $expectedClass ) {
155 $moduleManager = $this->getModuleManager();
156 $moduleManager->addModules( $modules, 'test' );
158 // should return the right module
159 $module1 = $moduleManager->getModule( $name, null, false );
160 $this->assertInstanceOf( $expectedClass, $module1 );
162 // should pass group check (with caching disabled)
163 $module2 = $moduleManager->getModule( $name, 'test', true );
164 $this->assertNotNull( $module2 );
166 // should use cached instance
167 $module3 = $moduleManager->getModule( $name, null, false );
168 $this->assertSame( $module1, $module3 );
170 // should not use cached instance if caching is disabled
171 $module4 = $moduleManager->getModule( $name, null, true );
172 $this->assertNotSame( $module1, $module4 );
175 public function testGetModule_null() {
176 $modules = array(
177 'login' => 'ApiLogin',
178 'logout' => 'ApiLogout',
181 $moduleManager = $this->getModuleManager();
182 $moduleManager->addModules( $modules, 'test' );
184 $this->assertNull( $moduleManager->getModule( 'quux' ), 'unknown name' );
185 $this->assertNull( $moduleManager->getModule( 'login', 'bla' ), 'wrong group' );
188 public function testGetNames() {
189 $fooModules = array(
190 'login' => 'ApiLogin',
191 'logout' => 'ApiLogout',
194 $barModules = array(
195 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
196 'feedrecentchanges' => array( 'class' => 'ApiFeedRecentChanges' ),
199 $moduleManager = $this->getModuleManager();
200 $moduleManager->addModules( $fooModules, 'foo' );
201 $moduleManager->addModules( $barModules, 'bar' );
203 $fooNames = $moduleManager->getNames( 'foo' );
204 $this->assertArrayEquals( array_keys( $fooModules ), $fooNames );
206 $allNames = $moduleManager->getNames();
207 $allModules = array_merge( $fooModules, $barModules );
208 $this->assertArrayEquals( array_keys( $allModules ), $allNames );
211 public function testGetNamesWithClasses() {
212 $fooModules = array(
213 'login' => 'ApiLogin',
214 'logout' => 'ApiLogout',
217 $barModules = array(
218 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
219 'feedrecentchanges' => array( 'class' => 'ApiFeedRecentChanges' ),
222 $moduleManager = $this->getModuleManager();
223 $moduleManager->addModules( $fooModules, 'foo' );
224 $moduleManager->addModules( $barModules, 'bar' );
226 $fooNamesWithClasses = $moduleManager->getNamesWithClasses( 'foo' );
227 $this->assertArrayEquals( $fooModules, $fooNamesWithClasses );
229 $allNamesWithClasses = $moduleManager->getNamesWithClasses();
230 $allModules = array_merge( $fooModules, array(
231 'feedcontributions' => 'ApiFeedContributions',
232 'feedrecentchanges' => 'ApiFeedRecentChanges',
233 ) );
234 $this->assertArrayEquals( $allModules, $allNamesWithClasses );
237 public function testGetModuleGroup() {
238 $fooModules = array(
239 'login' => 'ApiLogin',
240 'logout' => 'ApiLogout',
243 $barModules = array(
244 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
245 'feedrecentchanges' => array( 'class' => 'ApiFeedRecentChanges' ),
248 $moduleManager = $this->getModuleManager();
249 $moduleManager->addModules( $fooModules, 'foo' );
250 $moduleManager->addModules( $barModules, 'bar' );
252 $this->assertEquals( 'foo', $moduleManager->getModuleGroup( 'login' ) );
253 $this->assertEquals( 'bar', $moduleManager->getModuleGroup( 'feedrecentchanges' ) );
254 $this->assertNull( $moduleManager->getModuleGroup( 'quux' ) );
257 public function testGetGroups() {
258 $fooModules = array(
259 'login' => 'ApiLogin',
260 'logout' => 'ApiLogout',
263 $barModules = array(
264 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ),
265 'feedrecentchanges' => array( 'class' => 'ApiFeedRecentChanges' ),
268 $moduleManager = $this->getModuleManager();
269 $moduleManager->addModules( $fooModules, 'foo' );
270 $moduleManager->addModules( $barModules, 'bar' );
272 $groups = $moduleManager->getGroups();
273 $this->assertArrayEquals( array( 'foo', 'bar' ), $groups );