summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php
blob: 9d97b28270c893087f71cc6e224caa7d9cc03e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php

/**
 * @group Database
 * @group ResourceLoader
 */
class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {

	protected function setUp() {
		parent::setUp();

		// The return value of the closure shouldn't matter since this test should
		// never call it
		SkinFactory::getDefaultInstance()->register(
			'fakeskin',
			'FakeSkin',
			function () {
			}
		);
	}

	private static function getModules() {
		$base = array(
			'localBasePath' => realpath( dirname( __FILE__ ) ),
		);

		return array(
			'noTemplateModule' => array(),

			'htmlTemplateModule' => $base + array(
				'templates' => array(
					'templates/template.html',
					'templates/template2.html',
				)
			),

			'aliasedHtmlTemplateModule' => $base + array(
				'templates' => array(
					'foo.html' => 'templates/template.html',
					'bar.html' => 'templates/template2.html',
				)
			),

			'templateModuleHandlebars' => $base + array(
				'templates' => array(
					'templates/template_awesome.handlebars',
				),
			),

			'aliasFooFromBar' => $base + array(
				'templates' => array(
					'foo.foo' => 'templates/template.bar',
				),
			),
		);
	}

	public static function providerTemplateDependencies() {
		$modules = self::getModules();

		return array(
			array(
				$modules['noTemplateModule'],
				array(),
			),
			array(
				$modules['htmlTemplateModule'],
				array(
					'mediawiki.template',
				),
			),
			array(
				$modules['templateModuleHandlebars'],
				array(
					'mediawiki.template',
					'mediawiki.template.handlebars',
				),
			),
			array(
				$modules['aliasFooFromBar'],
				array(
					'mediawiki.template',
					'mediawiki.template.foo',
				),
			),
		);
	}

	/**
	 * @dataProvider providerTemplateDependencies
	 * @covers ResourceLoaderFileModule::__construct
	 * @covers ResourceLoaderFileModule::getDependencies
	 */
	public function testTemplateDependencies( $module, $expected ) {
		$rl = new ResourceLoaderFileModule( $module );
		$this->assertEquals( $rl->getDependencies(), $expected );
	}

	/**
	 * @covers ResourceLoaderFileModule::getAllStyleFiles
	 * @covers ResourceLoaderFileModule::getAllSkinStyleFiles
	 * @covers ResourceLoaderFileModule::getSkinStyleFiles
	 */
	public function testGetAllSkinStyleFiles() {
		$baseParams = array(
			'scripts' => array(
				'foo.js',
				'bar.js',
			),
			'styles' => array(
				'foo.css',
				'bar.css' => array( 'media' => 'print' ),
				'screen.less' => array( 'media' => 'screen' ),
				'screen-query.css' => array( 'media' => 'screen and (min-width: 400px)' ),
			),
			'skinStyles' => array(
				'default' => 'quux-fallback.less',
				'fakeskin' => array(
					'baz-vector.css',
					'quux-vector.less',
				),
			),
			'messages' => array(
				'hello',
				'world',
			),
		);

		$module = new ResourceLoaderFileModule( $baseParams );

		$this->assertEquals(
			array(
				'foo.css',
				'baz-vector.css',
				'quux-vector.less',
				'quux-fallback.less',
				'bar.css',
				'screen.less',
				'screen-query.css',
			),
			array_map( 'basename', $module->getAllStyleFiles() )
		);
	}

	/**
	 * Strip @noflip annotations from CSS code.
	 * @param string $css
	 * @return string
	 */
	private static function stripNoflip( $css ) {
		return str_replace( '/*@noflip*/ ', '', $css );
	}

	/**
	 * What happens when you mix @embed and @noflip?
	 * This really is an integration test, but oh well.
	 *
	 * @covers ResourceLoaderFileModule::getStyles
	 * @covers ResourceLoaderFileModule::getStyleFiles
	 */
	public function testMixedCssAnnotations() {
		$basePath = __DIR__ . '/../../data/css';
		$testModule = new ResourceLoaderFileModule( array(
			'localBasePath' => $basePath,
			'styles' => array( 'test.css' ),
		) );
		$expectedModule = new ResourceLoaderFileModule( array(
			'localBasePath' => $basePath,
			'styles' => array( 'expected.css' ),
		) );

		$contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
		$contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' );

		// Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
		// the @noflip annotations are always preserved, we need to strip them first.
		$this->assertEquals(
			$expectedModule->getStyles( $contextLtr ),
			self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
			"/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
		);
		$this->assertEquals(
			$expectedModule->getStyles( $contextLtr ),
			self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
			"/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
		);
	}

	public static function providerGetTemplates() {
		$modules = self::getModules();

		return array(
			array(
				$modules['noTemplateModule'],
				array(),
			),
			array(
				$modules['templateModuleHandlebars'],
				array(
					'templates/template_awesome.handlebars' => "wow\n",
				),
			),
			array(
				$modules['htmlTemplateModule'],
				array(
					'templates/template.html' => "<strong>hello</strong>\n",
					'templates/template2.html' => "<div>goodbye</div>\n",
				),
			),
			array(
				$modules['aliasedHtmlTemplateModule'],
				array(
					'foo.html' => "<strong>hello</strong>\n",
					'bar.html' => "<div>goodbye</div>\n",
				),
			),
		);
	}

	/**
	 * @dataProvider providerGetTemplates
	 * @covers ResourceLoaderFileModule::getTemplates
	 */
	public function testGetTemplates( $module, $expected ) {
		$rl = new ResourceLoaderFileModule( $module );

		$this->assertEquals( $rl->getTemplates(), $expected );
	}
}