rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderFileModuleTest.php
blobe82bab72df53d522d8916bb0921b034ec004e4f3
1 <?php
3 /**
4 * @group Database
5 * @group ResourceLoader
6 */
7 class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
9 protected function setUp() {
10 parent::setUp();
12 // The return value of the closure shouldn't matter since this test should
13 // never call it
14 SkinFactory::getDefaultInstance()->register(
15 'fakeskin',
16 'FakeSkin',
17 function () {
22 private static function getModules() {
23 $base = [
24 'localBasePath' => realpath( __DIR__ ),
27 return [
28 'noTemplateModule' => [],
30 'deprecatedModule' => $base + [
31 'deprecated' => true,
33 'deprecatedTomorrow' => $base + [
34 'deprecated' => 'Will be removed tomorrow.'
37 'htmlTemplateModule' => $base + [
38 'templates' => [
39 'templates/template.html',
40 'templates/template2.html',
44 'htmlTemplateUnknown' => $base + [
45 'templates' => [
46 'templates/notfound.html',
50 'aliasedHtmlTemplateModule' => $base + [
51 'templates' => [
52 'foo.html' => 'templates/template.html',
53 'bar.html' => 'templates/template2.html',
57 'templateModuleHandlebars' => $base + [
58 'templates' => [
59 'templates/template_awesome.handlebars',
63 'aliasFooFromBar' => $base + [
64 'templates' => [
65 'foo.foo' => 'templates/template.bar',
71 public static function providerTemplateDependencies() {
72 $modules = self::getModules();
74 return [
76 $modules['noTemplateModule'],
77 [],
80 $modules['htmlTemplateModule'],
82 'mediawiki.template',
86 $modules['templateModuleHandlebars'],
88 'mediawiki.template',
89 'mediawiki.template.handlebars',
93 $modules['aliasFooFromBar'],
95 'mediawiki.template',
96 'mediawiki.template.foo',
103 * @dataProvider providerTemplateDependencies
104 * @covers ResourceLoaderFileModule::__construct
105 * @covers ResourceLoaderFileModule::getDependencies
107 public function testTemplateDependencies( $module, $expected ) {
108 $rl = new ResourceLoaderFileModule( $module );
109 $rl->setName( 'testing' );
110 $this->assertEquals( $rl->getDependencies(), $expected );
113 public static function providerDeprecatedModules() {
114 return [
116 'deprecatedModule',
117 'mw.log.warn("This page is using the deprecated ResourceLoader module \"deprecatedModule\".");',
120 'deprecatedTomorrow',
121 'mw.log.warn(' .
122 '"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\\n' .
123 "Will be removed tomorrow." .
124 '");'
130 * @dataProvider providerDeprecatedModules
131 * @covers ResourceLoaderFileModule::getScript
133 public function testDeprecatedModules( $name, $expected ) {
134 $modules = self::getModules();
135 $module = new ResourceLoaderFileModule( $modules[$name] );
136 $module->setName( $name );
137 $ctx = $this->getResourceLoaderContext();
138 $this->assertEquals( $module->getScript( $ctx ), $expected );
142 * @covers ResourceLoaderFileModule::getScript
144 public function testGetScript() {
145 $module = new ResourceLoaderFileModule( [
146 'localBasePath' => __DIR__ . '/../../data/resourceloader',
147 'scripts' => [ 'script-nosemi.js', 'script-comment.js' ],
148 ] );
149 $module->setName( 'testing' );
150 $ctx = $this->getResourceLoaderContext();
151 $this->assertEquals(
152 "/* eslint-disable */\nmw.foo()\n" .
153 "\n" .
154 "/* eslint-disable */\nmw.foo()\n// mw.bar();\n" .
155 "\n",
156 $module->getScript( $ctx ),
157 'scripts are concatenated with a new-line'
162 * @covers ResourceLoaderFileModule::getAllStyleFiles
163 * @covers ResourceLoaderFileModule::getAllSkinStyleFiles
164 * @covers ResourceLoaderFileModule::getSkinStyleFiles
166 public function testGetAllSkinStyleFiles() {
167 $baseParams = [
168 'scripts' => [
169 'foo.js',
170 'bar.js',
172 'styles' => [
173 'foo.css',
174 'bar.css' => [ 'media' => 'print' ],
175 'screen.less' => [ 'media' => 'screen' ],
176 'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
178 'skinStyles' => [
179 'default' => 'quux-fallback.less',
180 'fakeskin' => [
181 'baz-vector.css',
182 'quux-vector.less',
185 'messages' => [
186 'hello',
187 'world',
191 $module = new ResourceLoaderFileModule( $baseParams );
192 $module->setName( 'testing' );
194 $this->assertEquals(
196 'foo.css',
197 'baz-vector.css',
198 'quux-vector.less',
199 'quux-fallback.less',
200 'bar.css',
201 'screen.less',
202 'screen-query.css',
204 array_map( 'basename', $module->getAllStyleFiles() )
209 * Strip @noflip annotations from CSS code.
210 * @param string $css
211 * @return string
213 private static function stripNoflip( $css ) {
214 return str_replace( '/*@noflip*/ ', '', $css );
218 * What happens when you mix @embed and @noflip?
219 * This really is an integration test, but oh well.
221 * @covers ResourceLoaderFileModule::getStyles
222 * @covers ResourceLoaderFileModule::getStyleFiles
224 public function testMixedCssAnnotations() {
225 $basePath = __DIR__ . '/../../data/css';
226 $testModule = new ResourceLoaderFileModule( [
227 'localBasePath' => $basePath,
228 'styles' => [ 'test.css' ],
229 ] );
230 $testModule->setName( 'testing' );
231 $expectedModule = new ResourceLoaderFileModule( [
232 'localBasePath' => $basePath,
233 'styles' => [ 'expected.css' ],
234 ] );
235 $expectedModule->setName( 'testing' );
237 $contextLtr = $this->getResourceLoaderContext( [
238 'lang' => 'en',
239 'dir' => 'ltr',
240 ] );
241 $contextRtl = $this->getResourceLoaderContext( [
242 'lang' => 'he',
243 'dir' => 'rtl',
244 ] );
246 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
247 // the @noflip annotations are always preserved, we need to strip them first.
248 $this->assertEquals(
249 $expectedModule->getStyles( $contextLtr ),
250 self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
251 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
253 $this->assertEquals(
254 $expectedModule->getStyles( $contextLtr ),
255 self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
256 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
260 public static function providerGetTemplates() {
261 $modules = self::getModules();
263 return [
265 $modules['noTemplateModule'],
269 $modules['templateModuleHandlebars'],
271 'templates/template_awesome.handlebars' => "wow\n",
275 $modules['htmlTemplateModule'],
277 'templates/template.html' => "<strong>hello</strong>\n",
278 'templates/template2.html' => "<div>goodbye</div>\n",
282 $modules['aliasedHtmlTemplateModule'],
284 'foo.html' => "<strong>hello</strong>\n",
285 'bar.html' => "<div>goodbye</div>\n",
289 $modules['htmlTemplateUnknown'],
290 false,
296 * @dataProvider providerGetTemplates
297 * @covers ResourceLoaderFileModule::getTemplates
299 public function testGetTemplates( $module, $expected ) {
300 $rl = new ResourceLoaderFileModule( $module );
301 $rl->setName( 'testing' );
303 if ( $expected === false ) {
304 $this->setExpectedException( MWException::class );
305 $rl->getTemplates();
306 } else {
307 $this->assertEquals( $rl->getTemplates(), $expected );
312 * @covers ResourceLoaderFileModule::stripBom
314 public function testBomConcatenation() {
315 $basePath = __DIR__ . '/../../data/css';
316 $testModule = new ResourceLoaderFileModule( [
317 'localBasePath' => $basePath,
318 'styles' => [ 'bom.css' ],
319 ] );
320 $testModule->setName( 'testing' );
321 $this->assertEquals(
322 substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
323 "\xef\xbb\xbf.efbbbf",
324 'File has leading BOM'
327 $context = $this->getResourceLoaderContext();
328 $this->assertEquals(
329 $testModule->getStyles( $context ),
330 [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
331 'Leading BOM removed when concatenating files'
336 * @covers ResourceLoaderFileModule::getDefinitionSummary
338 public function testGetVersionHash() {
339 $context = $this->getResourceLoaderContext();
341 // Less variables
342 $module = new ResourceLoaderFileTestModule();
343 $version = $module->getVersionHash( $context );
344 $module = new ResourceLoaderFileTestModule( [], [
345 'lessVars' => [ 'key' => 'value' ],
346 ] );
347 $this->assertNotEquals(
348 $version,
349 $module->getVersionHash( $context ),
350 'Using less variables is significant'