Don't check namespace in SpecialWantedtemplates
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderTest.php
1 <?php
2
3 class ResourceLoaderTest extends ResourceLoaderTestCase {
4
5 protected function setUp() {
6 parent::setUp();
7
8 $this->setMwGlobals( array(
9 'wgResourceLoaderLESSFunctions' => array(
10 'test-sum' => function ( $frame, $less ) {
11 $sum = 0;
12 foreach ( $frame[2] as $arg ) {
13 $sum += (int)$arg[1];
14 }
15 return $sum;
16 },
17 ),
18 'wgResourceLoaderLESSImportPaths' => array(
19 dirname( dirname( __DIR__ ) ) . '/data/less/common',
20 ),
21 'wgResourceLoaderLESSVars' => array(
22 'foo' => '2px',
23 'Foo' => '#eeeeee',
24 'bar' => 5,
25 ),
26 ) );
27 }
28
29 public static function provideValidModules() {
30 return array(
31 array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
32 );
33 }
34
35 /**
36 * Ensures that the ResourceLoaderRegisterModules hook is called when a new
37 * ResourceLoader object is constructed.
38 * @covers ResourceLoader::__construct
39 */
40 public function testCreatingNewResourceLoaderCallsRegistrationHook() {
41 $resourceLoaderRegisterModulesHook = false;
42
43 $this->setMwGlobals( 'wgHooks', array(
44 'ResourceLoaderRegisterModules' => array(
45 function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
46 $resourceLoaderRegisterModulesHook = true;
47 }
48 )
49 ) );
50
51 $resourceLoader = new ResourceLoader();
52 $this->assertTrue(
53 $resourceLoaderRegisterModulesHook,
54 'Hook ResourceLoaderRegisterModules called'
55 );
56
57 return $resourceLoader;
58 }
59
60 /**
61 * @dataProvider provideValidModules
62 * @depends testCreatingNewResourceLoaderCallsRegistrationHook
63 * @covers ResourceLoader::register
64 * @covers ResourceLoader::getModule
65 */
66 public function testRegisteredValidModulesAreAccessible(
67 $name, ResourceLoaderModule $module, ResourceLoader $resourceLoader
68 ) {
69 $resourceLoader->register( $name, $module );
70 $this->assertEquals( $module, $resourceLoader->getModule( $name ) );
71 }
72
73 /**
74 * @covers ResourceLoaderFileModule::compileLessFile
75 */
76 public function testLessFileCompilation() {
77 $context = $this->getResourceLoaderContext();
78 $basePath = __DIR__ . '/../../data/less/module';
79 $module = new ResourceLoaderFileModule( array(
80 'localBasePath' => $basePath,
81 'styles' => array( 'styles.less' ),
82 ) );
83 $module->setName( 'test.less' );
84 $styles = $module->getStyles( $context );
85 $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
86 }
87
88 /**
89 * Strip @noflip annotations from CSS code.
90 * @param string $css
91 * @return string
92 */
93 private static function stripNoflip( $css ) {
94 return str_replace( '/*@noflip*/ ', '', $css );
95 }
96
97 /**
98 * @dataProvider providePackedModules
99 * @covers ResourceLoader::makePackedModulesString
100 */
101 public function testMakePackedModulesString( $desc, $modules, $packed ) {
102 $this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
103 }
104
105 /**
106 * @dataProvider providePackedModules
107 * @covers ResourceLoaderContext::expandModuleNames
108 */
109 public function testexpandModuleNames( $desc, $modules, $packed ) {
110 $this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
111 }
112
113 public static function providePackedModules() {
114 return array(
115 array(
116 'Example from makePackedModulesString doc comment',
117 array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
118 'foo.bar,baz|bar.baz,quux',
119 ),
120 array(
121 'Example from expandModuleNames doc comment',
122 array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
123 'jquery.foo,bar|jquery.ui.baz,quux',
124 ),
125 array(
126 'Regression fixed in r88706 with dotless names',
127 array( 'foo', 'bar', 'baz' ),
128 'foo,bar,baz',
129 ),
130 array(
131 'Prefixless modules after a prefixed module',
132 array( 'single.module', 'foobar', 'foobaz' ),
133 'single.module|foobar,foobaz',
134 ),
135 );
136 }
137
138 public static function provideAddSource() {
139 return array(
140 array( 'examplewiki', '//example.org/w/load.php', 'examplewiki' ),
141 array( 'example2wiki', array( 'loadScript' => '//example.com/w/load.php' ), 'example2wiki' ),
142 array(
143 array( 'foowiki' => '//foo.org/w/load.php', 'bazwiki' => '//baz.org/w/load.php' ),
144 null,
145 array( 'foowiki', 'bazwiki' )
146 ),
147 array(
148 array( 'foowiki' => '//foo.org/w/load.php' ),
149 null,
150 false,
151 ),
152 );
153 }
154
155 /**
156 * @dataProvider provideAddSource
157 * @covers ResourceLoader::addSource
158 * @covers ResourceLoader::getSources
159 */
160 public function testAddSource( $name, $info, $expected ) {
161 $rl = new ResourceLoader;
162 if ( $expected === false ) {
163 $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
164 $rl->addSource( $name, $info );
165 }
166 $rl->addSource( $name, $info );
167 if ( is_array( $expected ) ) {
168 foreach ( $expected as $source ) {
169 $this->assertArrayHasKey( $source, $rl->getSources() );
170 }
171 } else {
172 $this->assertArrayHasKey( $expected, $rl->getSources() );
173 }
174 }
175
176 public static function fakeSources() {
177 return array(
178 'examplewiki' => array(
179 'loadScript' => '//example.org/w/load.php',
180 'apiScript' => '//example.org/w/api.php',
181 ),
182 'example2wiki' => array(
183 'loadScript' => '//example.com/w/load.php',
184 'apiScript' => '//example.com/w/api.php',
185 ),
186 );
187 }
188
189 public static function provideLoaderImplement() {
190 return array(
191 array( array(
192 'title' => 'Implement scripts, styles and messages',
193
194 'name' => 'test.example',
195 'scripts' => 'mw.example();',
196 'styles' => array( 'css' => array( '.mw-example {}' ) ),
197 'messages' => array( 'example' => '' ),
198 'templates' => array(),
199
200 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
201 mw.example();
202 }, {
203 "css": [
204 ".mw-example {}"
205 ]
206 }, {
207 "example": ""
208 } );',
209 ) ),
210 array( array(
211 'title' => 'Implement scripts',
212
213 'name' => 'test.example',
214 'scripts' => 'mw.example();',
215 'styles' => array(),
216 'messages' => new XmlJsCode( '{}' ),
217 'templates' => array(),
218 'title' => 'scripts, styles and messags',
219
220 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
221 mw.example();
222 } );',
223 ) ),
224 array( array(
225 'title' => 'Implement styles',
226
227 'name' => 'test.example',
228 'scripts' => array(),
229 'styles' => array( 'css' => array( '.mw-example {}' ) ),
230 'messages' => new XmlJsCode( '{}' ),
231 'templates' => array(),
232
233 'expected' => 'mw.loader.implement( "test.example", [], {
234 "css": [
235 ".mw-example {}"
236 ]
237 } );',
238 ) ),
239 array( array(
240 'title' => 'Implement scripts and messages',
241
242 'name' => 'test.example',
243 'scripts' => 'mw.example();',
244 'styles' => array(),
245 'messages' => array( 'example' => '' ),
246 'templates' => array(),
247
248 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
249 mw.example();
250 }, {}, {
251 "example": ""
252 } );',
253 ) ),
254 array( array(
255 'title' => 'Implement scripts and templates',
256
257 'name' => 'test.example',
258 'scripts' => 'mw.example();',
259 'styles' => array(),
260 'messages' => new XmlJsCode( '{}' ),
261 'templates' => array( 'example.html' => '' ),
262
263 'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery ) {
264 mw.example();
265 }, {}, {}, {
266 "example.html": ""
267 } );',
268 ) ),
269 );
270 }
271
272 /**
273 * @dataProvider provideLoaderImplement
274 * @covers ResourceLoader::makeLoaderImplementScript
275 */
276 public function testMakeLoaderImplementScript( $case ) {
277 $this->assertEquals(
278 $case['expected'],
279 ResourceLoader::makeLoaderImplementScript(
280 $case['name'],
281 $case['scripts'],
282 $case['styles'],
283 $case['messages'],
284 $case['templates']
285 )
286 );
287 }
288
289 /**
290 * @covers ResourceLoader::getLoadScript
291 */
292 public function testGetLoadScript() {
293 $this->setMwGlobals( 'wgResourceLoaderSources', array() );
294 $rl = new ResourceLoader();
295 $sources = self::fakeSources();
296 $rl->addSource( $sources );
297 foreach ( array( 'examplewiki', 'example2wiki' ) as $name ) {
298 $this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
299 }
300
301 try {
302 $rl->getLoadScript( 'thiswasneverreigstered' );
303 $this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
304 } catch ( MWException $e ) {
305 $this->assertTrue( true );
306 }
307 }
308
309 /**
310 * @covers ResourceLoader::isModuleRegistered
311 */
312 public function testIsModuleRegistered() {
313 $rl = new ResourceLoader();
314 $rl->register( 'test.module', new ResourceLoaderTestModule() );
315 $this->assertTrue( $rl->isModuleRegistered( 'test.module' ) );
316 $this->assertFalse( $rl->isModuleRegistered( 'test.modulenotregistered' ) );
317 }
318 }