Merge "Convert Special:NewFiles to use OOUI."
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderFileModuleTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group ResourceLoader
6 */
7 class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
8
9 protected function setUp() {
10 parent::setUp();
11
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 () {
18 }
19 );
20 }
21
22 private static function getModules() {
23 $base = [
24 'localBasePath' => realpath( __DIR__ ),
25 ];
26
27 return [
28 'noTemplateModule' => [],
29
30 'htmlTemplateModule' => $base + [
31 'templates' => [
32 'templates/template.html',
33 'templates/template2.html',
34 ]
35 ],
36
37 'aliasedHtmlTemplateModule' => $base + [
38 'templates' => [
39 'foo.html' => 'templates/template.html',
40 'bar.html' => 'templates/template2.html',
41 ]
42 ],
43
44 'templateModuleHandlebars' => $base + [
45 'templates' => [
46 'templates/template_awesome.handlebars',
47 ],
48 ],
49
50 'aliasFooFromBar' => $base + [
51 'templates' => [
52 'foo.foo' => 'templates/template.bar',
53 ],
54 ],
55 ];
56 }
57
58 public static function providerTemplateDependencies() {
59 $modules = self::getModules();
60
61 return [
62 [
63 $modules['noTemplateModule'],
64 [],
65 ],
66 [
67 $modules['htmlTemplateModule'],
68 [
69 'mediawiki.template',
70 ],
71 ],
72 [
73 $modules['templateModuleHandlebars'],
74 [
75 'mediawiki.template',
76 'mediawiki.template.handlebars',
77 ],
78 ],
79 [
80 $modules['aliasFooFromBar'],
81 [
82 'mediawiki.template',
83 'mediawiki.template.foo',
84 ],
85 ],
86 ];
87 }
88
89 /**
90 * @dataProvider providerTemplateDependencies
91 * @covers ResourceLoaderFileModule::__construct
92 * @covers ResourceLoaderFileModule::getDependencies
93 */
94 public function testTemplateDependencies( $module, $expected ) {
95 $rl = new ResourceLoaderFileModule( $module );
96 $this->assertEquals( $rl->getDependencies(), $expected );
97 }
98
99 /**
100 * @covers ResourceLoaderFileModule::getAllStyleFiles
101 * @covers ResourceLoaderFileModule::getAllSkinStyleFiles
102 * @covers ResourceLoaderFileModule::getSkinStyleFiles
103 */
104 public function testGetAllSkinStyleFiles() {
105 $baseParams = [
106 'scripts' => [
107 'foo.js',
108 'bar.js',
109 ],
110 'styles' => [
111 'foo.css',
112 'bar.css' => [ 'media' => 'print' ],
113 'screen.less' => [ 'media' => 'screen' ],
114 'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
115 ],
116 'skinStyles' => [
117 'default' => 'quux-fallback.less',
118 'fakeskin' => [
119 'baz-vector.css',
120 'quux-vector.less',
121 ],
122 ],
123 'messages' => [
124 'hello',
125 'world',
126 ],
127 ];
128
129 $module = new ResourceLoaderFileModule( $baseParams );
130
131 $this->assertEquals(
132 [
133 'foo.css',
134 'baz-vector.css',
135 'quux-vector.less',
136 'quux-fallback.less',
137 'bar.css',
138 'screen.less',
139 'screen-query.css',
140 ],
141 array_map( 'basename', $module->getAllStyleFiles() )
142 );
143 }
144
145 /**
146 * Strip @noflip annotations from CSS code.
147 * @param string $css
148 * @return string
149 */
150 private static function stripNoflip( $css ) {
151 return str_replace( '/*@noflip*/ ', '', $css );
152 }
153
154 /**
155 * What happens when you mix @embed and @noflip?
156 * This really is an integration test, but oh well.
157 *
158 * @covers ResourceLoaderFileModule::getStyles
159 * @covers ResourceLoaderFileModule::getStyleFiles
160 */
161 public function testMixedCssAnnotations() {
162 $basePath = __DIR__ . '/../../data/css';
163 $testModule = new ResourceLoaderFileModule( [
164 'localBasePath' => $basePath,
165 'styles' => [ 'test.css' ],
166 ] );
167 $expectedModule = new ResourceLoaderFileModule( [
168 'localBasePath' => $basePath,
169 'styles' => [ 'expected.css' ],
170 ] );
171
172 $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
173 $contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' );
174
175 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
176 // the @noflip annotations are always preserved, we need to strip them first.
177 $this->assertEquals(
178 $expectedModule->getStyles( $contextLtr ),
179 self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
180 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
181 );
182 $this->assertEquals(
183 $expectedModule->getStyles( $contextLtr ),
184 self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
185 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
186 );
187 }
188
189 public static function providerGetTemplates() {
190 $modules = self::getModules();
191
192 return [
193 [
194 $modules['noTemplateModule'],
195 [],
196 ],
197 [
198 $modules['templateModuleHandlebars'],
199 [
200 'templates/template_awesome.handlebars' => "wow\n",
201 ],
202 ],
203 [
204 $modules['htmlTemplateModule'],
205 [
206 'templates/template.html' => "<strong>hello</strong>\n",
207 'templates/template2.html' => "<div>goodbye</div>\n",
208 ],
209 ],
210 [
211 $modules['aliasedHtmlTemplateModule'],
212 [
213 'foo.html' => "<strong>hello</strong>\n",
214 'bar.html' => "<div>goodbye</div>\n",
215 ],
216 ],
217 ];
218 }
219
220 /**
221 * @dataProvider providerGetTemplates
222 * @covers ResourceLoaderFileModule::getTemplates
223 */
224 public function testGetTemplates( $module, $expected ) {
225 $rl = new ResourceLoaderFileModule( $module );
226
227 $this->assertEquals( $rl->getTemplates(), $expected );
228 }
229
230 public function testBomConcatenation() {
231 $basePath = __DIR__ . '/../../data/css';
232 $testModule = new ResourceLoaderFileModule( [
233 'localBasePath' => $basePath,
234 'styles' => [ 'bom.css' ],
235 ] );
236 $this->assertEquals(
237 substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
238 "\xef\xbb\xbf.efbbbf",
239 'File has leading BOM'
240 );
241
242 $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
243 $this->assertEquals(
244 $testModule->getStyles( $contextLtr ),
245 [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
246 'Leading BOM removed when concatenating files'
247 );
248 }
249 }