Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderFileModuleTest.php
blob4a3b90a29426409e592fcedd5126afceb8332fc6
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' => [
35 'message' => 'Will be removed tomorrow.'
39 'htmlTemplateModule' => $base + [
40 'templates' => [
41 'templates/template.html',
42 'templates/template2.html',
46 'aliasedHtmlTemplateModule' => $base + [
47 'templates' => [
48 'foo.html' => 'templates/template.html',
49 'bar.html' => 'templates/template2.html',
53 'templateModuleHandlebars' => $base + [
54 'templates' => [
55 'templates/template_awesome.handlebars',
59 'aliasFooFromBar' => $base + [
60 'templates' => [
61 'foo.foo' => 'templates/template.bar',
67 public static function providerTemplateDependencies() {
68 $modules = self::getModules();
70 return [
72 $modules['noTemplateModule'],
73 [],
76 $modules['htmlTemplateModule'],
78 'mediawiki.template',
82 $modules['templateModuleHandlebars'],
84 'mediawiki.template',
85 'mediawiki.template.handlebars',
89 $modules['aliasFooFromBar'],
91 'mediawiki.template',
92 'mediawiki.template.foo',
98 /**
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() {
110 return [
112 'deprecatedModule',
113 'mw.log.warn("This page is using the deprecated ResourceLoader module \"deprecatedModule\".");',
116 'deprecatedTomorrow',
117 'mw.log.warn(' .
118 '"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\\n' .
119 "Will be removed tomorrow." .
120 '");'
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() {
143 $baseParams = [
144 'scripts' => [
145 'foo.js',
146 'bar.js',
148 'styles' => [
149 'foo.css',
150 'bar.css' => [ 'media' => 'print' ],
151 'screen.less' => [ 'media' => 'screen' ],
152 'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
154 'skinStyles' => [
155 'default' => 'quux-fallback.less',
156 'fakeskin' => [
157 'baz-vector.css',
158 'quux-vector.less',
161 'messages' => [
162 'hello',
163 'world',
167 $module = new ResourceLoaderFileModule( $baseParams );
168 $module->setName( 'testing' );
170 $this->assertEquals(
172 'foo.css',
173 'baz-vector.css',
174 'quux-vector.less',
175 'quux-fallback.less',
176 'bar.css',
177 'screen.less',
178 'screen-query.css',
180 array_map( 'basename', $module->getAllStyleFiles() )
185 * Strip @noflip annotations from CSS code.
186 * @param string $css
187 * @return string
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' ],
205 ] );
206 $testModule->setName( 'testing' );
207 $expectedModule = new ResourceLoaderFileModule( [
208 'localBasePath' => $basePath,
209 'styles' => [ 'expected.css' ],
210 ] );
211 $expectedModule->setName( 'testing' );
213 $contextLtr = $this->getResourceLoaderContext( [
214 'lang' => 'en',
215 'dir' => 'ltr',
216 ] );
217 $contextRtl = $this->getResourceLoaderContext( [
218 'lang' => 'he',
219 'dir' => 'rtl',
220 ] );
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.
224 $this->assertEquals(
225 $expectedModule->getStyles( $contextLtr ),
226 self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
227 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
229 $this->assertEquals(
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();
239 return [
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' ],
283 ] );
284 $testModule->setName( 'testing' );
285 $this->assertEquals(
286 substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
287 "\xef\xbb\xbf.efbbbf",
288 'File has leading BOM'
291 $context = $this->getResourceLoaderContext();
292 $this->assertEquals(
293 $testModule->getStyles( $context ),
294 [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
295 'Leading BOM removed when concatenating files'