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 +
[
35 'message' => 'Will be removed tomorrow.'
39 'htmlTemplateModule' => $base +
[
41 'templates/template.html',
42 'templates/template2.html',
46 'aliasedHtmlTemplateModule' => $base +
[
48 'foo.html' => 'templates/template.html',
49 'bar.html' => 'templates/template2.html',
53 'templateModuleHandlebars' => $base +
[
55 'templates/template_awesome.handlebars',
59 'aliasFooFromBar' => $base +
[
61 'foo.foo' => 'templates/template.bar',
67 public static function providerTemplateDependencies() {
68 $modules = self
::getModules();
72 $modules['noTemplateModule'],
76 $modules['htmlTemplateModule'],
82 $modules['templateModuleHandlebars'],
85 'mediawiki.template.handlebars',
89 $modules['aliasFooFromBar'],
92 'mediawiki.template.foo',
99 * @dataProvider providerTemplateDependencies
100 * @covers ResourceLoaderFileModule::__construct
101 * @covers ResourceLoaderFileModule::getDependencies
103 public function testTemplateDependencies( $module, $expected ) {
104 $rl = new ResourceLoaderFileModule( $module );
105 $rl->setName( 'testing' );
106 $this->assertEquals( $rl->getDependencies(), $expected );
109 public static function providerDeprecatedModules() {
113 'mw.log.warn("This page is using the deprecated ResourceLoader module \"deprecatedModule\".");',
116 'deprecatedTomorrow',
118 '"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\\n' .
119 "Will be removed tomorrow." .
126 * @dataProvider providerDeprecatedModules
127 * @covers ResourceLoaderFileModule::getScript
129 public function testDeprecatedModules( $name, $expected ) {
130 $modules = self
::getModules();
131 $rl = new ResourceLoaderFileModule( $modules[$name] );
132 $rl->setName( $name );
133 $ctx = $this->getResourceLoaderContext();
134 $this->assertEquals( $rl->getScript( $ctx ), $expected );
138 * @covers ResourceLoaderFileModule::getAllStyleFiles
139 * @covers ResourceLoaderFileModule::getAllSkinStyleFiles
140 * @covers ResourceLoaderFileModule::getSkinStyleFiles
142 public function testGetAllSkinStyleFiles() {
150 'bar.css' => [ 'media' => 'print' ],
151 'screen.less' => [ 'media' => 'screen' ],
152 'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
155 'default' => 'quux-fallback.less',
167 $module = new ResourceLoaderFileModule( $baseParams );
168 $module->setName( 'testing' );
175 'quux-fallback.less',
180 array_map( 'basename', $module->getAllStyleFiles() )
185 * Strip @noflip annotations from CSS code.
189 private static function stripNoflip( $css ) {
190 return str_replace( '/*@noflip*/ ', '', $css );
194 * What happens when you mix @embed and @noflip?
195 * This really is an integration test, but oh well.
197 * @covers ResourceLoaderFileModule::getStyles
198 * @covers ResourceLoaderFileModule::getStyleFiles
200 public function testMixedCssAnnotations() {
201 $basePath = __DIR__
. '/../../data/css';
202 $testModule = new ResourceLoaderFileModule( [
203 'localBasePath' => $basePath,
204 'styles' => [ 'test.css' ],
206 $testModule->setName( 'testing' );
207 $expectedModule = new ResourceLoaderFileModule( [
208 'localBasePath' => $basePath,
209 'styles' => [ 'expected.css' ],
211 $expectedModule->setName( 'testing' );
213 $contextLtr = $this->getResourceLoaderContext( [
217 $contextRtl = $this->getResourceLoaderContext( [
222 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
223 // the @noflip annotations are always preserved, we need to strip them first.
225 $expectedModule->getStyles( $contextLtr ),
226 self
::stripNoflip( $testModule->getStyles( $contextLtr ) ),
227 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
230 $expectedModule->getStyles( $contextLtr ),
231 self
::stripNoflip( $testModule->getStyles( $contextRtl ) ),
232 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
236 public static function providerGetTemplates() {
237 $modules = self
::getModules();
241 $modules['noTemplateModule'],
245 $modules['templateModuleHandlebars'],
247 'templates/template_awesome.handlebars' => "wow\n",
251 $modules['htmlTemplateModule'],
253 'templates/template.html' => "<strong>hello</strong>\n",
254 'templates/template2.html' => "<div>goodbye</div>\n",
258 $modules['aliasedHtmlTemplateModule'],
260 'foo.html' => "<strong>hello</strong>\n",
261 'bar.html' => "<div>goodbye</div>\n",
268 * @dataProvider providerGetTemplates
269 * @covers ResourceLoaderFileModule::getTemplates
271 public function testGetTemplates( $module, $expected ) {
272 $rl = new ResourceLoaderFileModule( $module );
273 $rl->setName( 'testing' );
275 $this->assertEquals( $rl->getTemplates(), $expected );
278 public function testBomConcatenation() {
279 $basePath = __DIR__
. '/../../data/css';
280 $testModule = new ResourceLoaderFileModule( [
281 'localBasePath' => $basePath,
282 'styles' => [ 'bom.css' ],
284 $testModule->setName( 'testing' );
286 substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
287 "\xef\xbb\xbf.efbbbf",
288 'File has leading BOM'
291 $context = $this->getResourceLoaderContext();
293 $testModule->getStyles( $context ),
294 [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
295 'Leading BOM removed when concatenating files'