f3dff1701e9f4e869acb2588885eb23eadf5f194
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderFileModuleTest.php
1 <?php
2
3 /**
4 * @group ResourceLoader
5 */
6 class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
7
8 protected function setUp() {
9 parent::setUp();
10
11 // The return value of the closure shouldn't matter since this test should
12 // never call it
13 SkinFactory::getDefaultInstance()->register(
14 'fakeskin',
15 'FakeSkin',
16 function () {
17 }
18 );
19 }
20
21 private static function getModules() {
22 $base = array(
23 'localBasePath' => realpath( dirname( __FILE__ ) ),
24 );
25
26 return array(
27 'noTemplateModule' => array(),
28
29 'htmlTemplateModule' => $base + array(
30 'templates' => array(
31 'templates/template.html',
32 'templates/template2.html',
33 )
34 ),
35
36 'aliasedHtmlTemplateModule' => $base + array(
37 'templates' => array(
38 'foo.html' => 'templates/template.html',
39 'bar.html' => 'templates/template2.html',
40 )
41 ),
42
43 'templateModuleHandlebars' => $base + array(
44 'templates' => array(
45 'templates/template_awesome.handlebars',
46 ),
47 ),
48 );
49 }
50
51 /**
52 * @covers ResourceLoaderFileModule::getAllStyleFiles
53 * @covers ResourceLoaderFileModule::getAllSkinStyleFiles
54 * @covers ResourceLoaderFileModule::getSkinStyleFiles
55 */
56 public function testGetAllSkinStyleFiles() {
57 $baseParams = array(
58 'scripts' => array(
59 'foo.js',
60 'bar.js',
61 ),
62 'styles' => array(
63 'foo.css',
64 'bar.css' => array( 'media' => 'print' ),
65 'screen.less' => array( 'media' => 'screen' ),
66 'screen-query.css' => array( 'media' => 'screen and (min-width: 400px)' ),
67 ),
68 'skinStyles' => array(
69 'default' => 'quux-fallback.less',
70 'fakeskin' => array(
71 'baz-vector.css',
72 'quux-vector.less',
73 ),
74 ),
75 'messages' => array(
76 'hello',
77 'world',
78 ),
79 );
80
81 $module = new ResourceLoaderFileModule( $baseParams );
82
83 $this->assertEquals(
84 array(
85 'foo.css',
86 'baz-vector.css',
87 'quux-vector.less',
88 'quux-fallback.less',
89 'bar.css',
90 'screen.less',
91 'screen-query.css',
92 ),
93 array_map( 'basename', $module->getAllStyleFiles() )
94 );
95 }
96
97 /**
98 * Strip @noflip annotations from CSS code.
99 * @param string $css
100 * @return string
101 */
102 private static function stripNoflip( $css ) {
103 return str_replace( '/*@noflip*/ ', '', $css );
104 }
105
106 /**
107 * What happens when you mix @embed and @noflip?
108 * This really is an integration test, but oh well.
109 *
110 * @covers ResourceLoaderFileModule::getStyles
111 * @covers ResourceLoaderFileModule::getStyleFiles
112 */
113 public function testMixedCssAnnotations( ) {
114 $basePath = __DIR__ . '/../../data/css';
115 $testModule = new ResourceLoaderFileModule( array(
116 'localBasePath' => $basePath,
117 'styles' => array( 'test.css' ),
118 ) );
119 $expectedModule = new ResourceLoaderFileModule( array(
120 'localBasePath' => $basePath,
121 'styles' => array( 'expected.css' ),
122 ) );
123
124 $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
125 $contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' );
126
127 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
128 // the @noflip annotations are always preserved, we need to strip them first.
129 $this->assertEquals(
130 $expectedModule->getStyles( $contextLtr ),
131 self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
132 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
133 );
134 $this->assertEquals(
135 $expectedModule->getStyles( $contextLtr ),
136 self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
137 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
138 );
139 }
140
141 public static function providerGetTemplates() {
142 $modules = self::getModules();
143
144 return array(
145 array(
146 $modules['noTemplateModule'],
147 array(),
148 ),
149 array(
150 $modules['templateModuleHandlebars'],
151 array(
152 'templates/template_awesome.handlebars' => "wow\n",
153 ),
154 ),
155 array(
156 $modules['htmlTemplateModule'],
157 array(
158 'templates/template.html' => "<strong>hello</strong>\n",
159 'templates/template2.html' => "<div>goodbye</div>\n",
160 ),
161 ),
162 array(
163 $modules['aliasedHtmlTemplateModule'],
164 array(
165 'foo.html' => "<strong>hello</strong>\n",
166 'bar.html' => "<div>goodbye</div>\n",
167 ),
168 ),
169 );
170 }
171
172 /**
173 * @dataProvider providerGetTemplates
174 * @covers ResourceLoaderFileModule::getTemplates
175 */
176 public function testGetTemplates( $module, $expected ) {
177 $rl = new ResourceLoaderFileModule( $module );
178
179 $this->assertEquals( $rl->getTemplates(), $expected );
180 }
181
182 public static function providerGetModifiedTime() {
183 $modules = self::getModules();
184
185 return array(
186 // Check the default value when no templates present in module is 1
187 array( $modules['noTemplateModule'], 1 ),
188 );
189 }
190
191 /**
192 * @dataProvider providerGetModifiedTime
193 * @covers ResourceLoaderFileModule::getModifiedTime
194 */
195 public function testGetModifiedTime( $module, $expected ) {
196 $rl = new ResourceLoaderFileModule( $module );
197 $ts = $rl->getModifiedTime( $this->getResourceLoaderContext() );
198 $this->assertEquals( $ts, $expected );
199 }
200 }