5 * @group ResourceLoader
7 class ResourceLoaderFileModuleTest
extends ResourceLoaderTestCase
{
9 protected function setUp() {
12 // The return value of the closure shouldn't matter since this test should
14 SkinFactory
::getDefaultInstance()->register(
22 private static function getModules() {
24 'localBasePath' => realpath( __DIR__
),
28 'noTemplateModule' => [],
30 'deprecatedModule' => $base +
[
33 'deprecatedTomorrow' => $base +
[
34 'deprecated' => 'Will be removed tomorrow.'
37 'htmlTemplateModule' => $base +
[
39 'templates/template.html',
40 'templates/template2.html',
44 'htmlTemplateUnknown' => $base +
[
46 'templates/notfound.html',
50 'aliasedHtmlTemplateModule' => $base +
[
52 'foo.html' => 'templates/template.html',
53 'bar.html' => 'templates/template2.html',
57 'templateModuleHandlebars' => $base +
[
59 'templates/template_awesome.handlebars',
63 'aliasFooFromBar' => $base +
[
65 'foo.foo' => 'templates/template.bar',
71 public static function providerTemplateDependencies() {
72 $modules = self
::getModules();
76 $modules['noTemplateModule'],
80 $modules['htmlTemplateModule'],
86 $modules['templateModuleHandlebars'],
89 'mediawiki.template.handlebars',
93 $modules['aliasFooFromBar'],
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() {
117 'mw.log.warn("This page is using the deprecated ResourceLoader module \"deprecatedModule\".");',
120 'deprecatedTomorrow',
122 '"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\\n' .
123 "Will be removed tomorrow." .
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' ],
149 $module->setName( 'testing' );
150 $ctx = $this->getResourceLoaderContext();
152 "/* eslint-disable */\nmw.foo()\n" .
154 "/* eslint-disable */\nmw.foo()\n// mw.bar();\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() {
174 'bar.css' => [ 'media' => 'print' ],
175 'screen.less' => [ 'media' => 'screen' ],
176 'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
179 'default' => 'quux-fallback.less',
191 $module = new ResourceLoaderFileModule( $baseParams );
192 $module->setName( 'testing' );
199 'quux-fallback.less',
204 array_map( 'basename', $module->getAllStyleFiles() )
209 * Strip @noflip annotations from CSS code.
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' ],
230 $testModule->setName( 'testing' );
231 $expectedModule = new ResourceLoaderFileModule( [
232 'localBasePath' => $basePath,
233 'styles' => [ 'expected.css' ],
235 $expectedModule->setName( 'testing' );
237 $contextLtr = $this->getResourceLoaderContext( [
241 $contextRtl = $this->getResourceLoaderContext( [
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.
249 $expectedModule->getStyles( $contextLtr ),
250 self
::stripNoflip( $testModule->getStyles( $contextLtr ) ),
251 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
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();
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'],
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 );
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' ],
320 $testModule->setName( 'testing' );
322 substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
323 "\xef\xbb\xbf.efbbbf",
324 'File has leading BOM'
327 $context = $this->getResourceLoaderContext();
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();
342 $module = new ResourceLoaderFileTestModule();
343 $version = $module->getVersionHash( $context );
344 $module = new ResourceLoaderFileTestModule( [], [
345 'lessVars' => [ 'key' => 'value' ],
347 $this->assertNotEquals(
349 $module->getVersionHash( $context ),
350 'Using less variables is significant'