Don't check namespace in SpecialWantedtemplates
[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 = array(
24 'localBasePath' => realpath( dirname( __FILE__ ) ),
25 );
26
27 return array(
28 'noTemplateModule' => array(),
29
30 'htmlTemplateModule' => $base + array(
31 'templates' => array(
32 'templates/template.html',
33 'templates/template2.html',
34 )
35 ),
36
37 'aliasedHtmlTemplateModule' => $base + array(
38 'templates' => array(
39 'foo.html' => 'templates/template.html',
40 'bar.html' => 'templates/template2.html',
41 )
42 ),
43
44 'templateModuleHandlebars' => $base + array(
45 'templates' => array(
46 'templates/template_awesome.handlebars',
47 ),
48 ),
49
50 'aliasFooFromBar' => $base + array(
51 'templates' => array(
52 'foo.foo' => 'templates/template.bar',
53 ),
54 ),
55 );
56 }
57
58 public static function providerTemplateDependencies() {
59 $modules = self::getModules();
60
61 return array(
62 array(
63 $modules['noTemplateModule'],
64 array(),
65 ),
66 array(
67 $modules['htmlTemplateModule'],
68 array(
69 'mediawiki.template',
70 ),
71 ),
72 array(
73 $modules['templateModuleHandlebars'],
74 array(
75 'mediawiki.template',
76 'mediawiki.template.handlebars',
77 ),
78 ),
79 array(
80 $modules['aliasFooFromBar'],
81 array(
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 = array(
106 'scripts' => array(
107 'foo.js',
108 'bar.js',
109 ),
110 'styles' => array(
111 'foo.css',
112 'bar.css' => array( 'media' => 'print' ),
113 'screen.less' => array( 'media' => 'screen' ),
114 'screen-query.css' => array( 'media' => 'screen and (min-width: 400px)' ),
115 ),
116 'skinStyles' => array(
117 'default' => 'quux-fallback.less',
118 'fakeskin' => array(
119 'baz-vector.css',
120 'quux-vector.less',
121 ),
122 ),
123 'messages' => array(
124 'hello',
125 'world',
126 ),
127 );
128
129 $module = new ResourceLoaderFileModule( $baseParams );
130
131 $this->assertEquals(
132 array(
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( array(
164 'localBasePath' => $basePath,
165 'styles' => array( 'test.css' ),
166 ) );
167 $expectedModule = new ResourceLoaderFileModule( array(
168 'localBasePath' => $basePath,
169 'styles' => array( '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 array(
193 array(
194 $modules['noTemplateModule'],
195 array(),
196 ),
197 array(
198 $modules['templateModuleHandlebars'],
199 array(
200 'templates/template_awesome.handlebars' => "wow\n",
201 ),
202 ),
203 array(
204 $modules['htmlTemplateModule'],
205 array(
206 'templates/template.html' => "<strong>hello</strong>\n",
207 'templates/template2.html' => "<div>goodbye</div>\n",
208 ),
209 ),
210 array(
211 $modules['aliasedHtmlTemplateModule'],
212 array(
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 }