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 $rl = new ResourceLoaderFileModule( $modules[$name] );
136 $rl->setName( $name );
137 $ctx = $this->getResourceLoaderContext();
138 $this->assertEquals( $rl->getScript( $ctx ), $expected );
142 * @covers ResourceLoaderFileModule::getAllStyleFiles
143 * @covers ResourceLoaderFileModule::getAllSkinStyleFiles
144 * @covers ResourceLoaderFileModule::getSkinStyleFiles
146 public function testGetAllSkinStyleFiles() {
154 'bar.css' => [ 'media' => 'print' ],
155 'screen.less' => [ 'media' => 'screen' ],
156 'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
159 'default' => 'quux-fallback.less',
171 $module = new ResourceLoaderFileModule( $baseParams );
172 $module->setName( 'testing' );
179 'quux-fallback.less',
184 array_map( 'basename', $module->getAllStyleFiles() )
189 * Strip @noflip annotations from CSS code.
193 private static function stripNoflip( $css ) {
194 return str_replace( '/*@noflip*/ ', '', $css );
198 * What happens when you mix @embed and @noflip?
199 * This really is an integration test, but oh well.
201 * @covers ResourceLoaderFileModule::getStyles
202 * @covers ResourceLoaderFileModule::getStyleFiles
204 public function testMixedCssAnnotations() {
205 $basePath = __DIR__
. '/../../data/css';
206 $testModule = new ResourceLoaderFileModule( [
207 'localBasePath' => $basePath,
208 'styles' => [ 'test.css' ],
210 $testModule->setName( 'testing' );
211 $expectedModule = new ResourceLoaderFileModule( [
212 'localBasePath' => $basePath,
213 'styles' => [ 'expected.css' ],
215 $expectedModule->setName( 'testing' );
217 $contextLtr = $this->getResourceLoaderContext( [
221 $contextRtl = $this->getResourceLoaderContext( [
226 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
227 // the @noflip annotations are always preserved, we need to strip them first.
229 $expectedModule->getStyles( $contextLtr ),
230 self
::stripNoflip( $testModule->getStyles( $contextLtr ) ),
231 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
234 $expectedModule->getStyles( $contextLtr ),
235 self
::stripNoflip( $testModule->getStyles( $contextRtl ) ),
236 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
240 public static function providerGetTemplates() {
241 $modules = self
::getModules();
245 $modules['noTemplateModule'],
249 $modules['templateModuleHandlebars'],
251 'templates/template_awesome.handlebars' => "wow\n",
255 $modules['htmlTemplateModule'],
257 'templates/template.html' => "<strong>hello</strong>\n",
258 'templates/template2.html' => "<div>goodbye</div>\n",
262 $modules['aliasedHtmlTemplateModule'],
264 'foo.html' => "<strong>hello</strong>\n",
265 'bar.html' => "<div>goodbye</div>\n",
269 $modules['htmlTemplateUnknown'],
276 * @dataProvider providerGetTemplates
277 * @covers ResourceLoaderFileModule::getTemplates
279 public function testGetTemplates( $module, $expected ) {
280 $rl = new ResourceLoaderFileModule( $module );
281 $rl->setName( 'testing' );
283 if ( $expected === false ) {
284 $this->setExpectedException( MWException
::class );
287 $this->assertEquals( $rl->getTemplates(), $expected );
292 * @covers ResourceLoaderFileModule::stripBom
294 public function testBomConcatenation() {
295 $basePath = __DIR__
. '/../../data/css';
296 $testModule = new ResourceLoaderFileModule( [
297 'localBasePath' => $basePath,
298 'styles' => [ 'bom.css' ],
300 $testModule->setName( 'testing' );
302 substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
303 "\xef\xbb\xbf.efbbbf",
304 'File has leading BOM'
307 $context = $this->getResourceLoaderContext();
309 $testModule->getStyles( $context ),
310 [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
311 'Leading BOM removed when concatenating files'