Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Asset / CssOptimizerUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Asset;
4
5 use Drupal\Core\Asset\CssOptimizer;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests the CSS asset optimizer.
10  *
11  * @group Asset
12  */
13 class CssOptimizerUnitTest extends UnitTestCase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected $backupGlobals = FALSE;
19
20   /**
21    * A CSS asset optimizer.
22    *
23    * @var \Drupal\Core\Asset\CssOptimizer object.
24    */
25   protected $optimizer;
26
27   protected function setUp() {
28     parent::setUp();
29
30     $this->optimizer = new CssOptimizer();
31   }
32
33   /**
34    * Provides data for the CSS asset optimizing test.
35    */
36   public function providerTestOptimize() {
37     $path = 'core/tests/Drupal/Tests/Core/Asset/css_test_files/';
38     $absolute_path = dirname(__FILE__) . '/css_test_files/';
39     return [
40       // File. Tests:
41       // - Stripped comments and white-space.
42       // - Retain white-space in selectors. (https://www.drupal.org/node/472820)
43       // - Retain pseudo-selectors. (https://www.drupal.org/node/460448)
44       [
45         [
46           'group' => -100,
47           'type' => 'file',
48           'weight' => 0.012,
49           'media' => 'all',
50           'preprocess' => TRUE,
51           'data' => $path . 'css_input_without_import.css',
52           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
53           'basename' => 'css_input_without_import.css',
54         ],
55         file_get_contents($absolute_path . 'css_input_without_import.css.optimized.css'),
56       ],
57       // File. Tests:
58       // - Proper URLs in imported files. (https://www.drupal.org/node/265719)
59       // - A background image with relative paths, which must be rewritten.
60       // - The rewritten background image path must also be passed through
61       //   file_create_url(). (https://www.drupal.org/node/1961340)
62       // - Imported files that are external (protocol-relative URL or not)
63       //   should not be expanded. (https://www.drupal.org/node/2014851)
64       [
65         [
66           'group' => -100,
67           'type' => 'file',
68           'weight' => 0.013,
69           'media' => 'all',
70           'preprocess' => TRUE,
71           'data' => $path . 'css_input_with_import.css',
72           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
73           'basename' => 'css_input_with_import.css',
74         ],
75         str_replace('url(images/icon.png)', 'url(' . file_url_transform_relative(file_create_url($path . 'images/icon.png')) . ')', file_get_contents($absolute_path . 'css_input_with_import.css.optimized.css')),
76       ],
77       // File. Tests:
78       // - Retain comment hacks.
79       [
80         [
81           'group' => -100,
82           'type' => 'file',
83           'weight' => 0.013,
84           'media' => 'all',
85           'preprocess' => TRUE,
86           'data' => $path . 'comment_hacks.css',
87           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
88           'basename' => 'comment_hacks.css',
89         ],
90         file_get_contents($absolute_path . 'comment_hacks.css.optimized.css'),
91       ],
92       // File in subfolder. Tests:
93       // - CSS import path is properly interpreted.
94       //   (https://www.drupal.org/node/1198904)
95       // - Don't adjust data URIs (https://www.drupal.org/node/2142441)
96       [
97         [
98           'group' => -100,
99           'type' => 'file',
100           'weight' => 0.013,
101           'media' => 'all',
102           'preprocess' => TRUE,
103           'data' => $path . 'css_subfolder/css_input_with_import.css',
104           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
105           'basename' => 'css_input_with_import.css',
106         ],
107         str_replace('url(../images/icon.png)', 'url(' . file_url_transform_relative(file_create_url($path . 'images/icon.png')) . ')', file_get_contents($absolute_path . 'css_subfolder/css_input_with_import.css.optimized.css')),
108       ],
109       // File. Tests:
110       // - Any @charaset declaration at the beginning of a file should be
111       //   removed without breaking subsequent CSS.
112       [
113         [
114           'group' => -100,
115           'type' => 'file',
116           'weight' => 0.013,
117           'media' => 'all',
118           'preprocess' => TRUE,
119           'data' => $path . 'charset_sameline.css',
120           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
121           'basename' => 'charset_sameline.css',
122         ],
123         file_get_contents($absolute_path . 'charset.css.optimized.css'),
124       ],
125       [
126         [
127           'group' => -100,
128           'type' => 'file',
129           'weight' => 0.013,
130           'media' => 'all',
131           'preprocess' => TRUE,
132           'data' => $path . 'charset_newline.css',
133           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
134           'basename' => 'charset_newline.css',
135         ],
136         file_get_contents($absolute_path . 'charset.css.optimized.css'),
137       ],
138       [
139         [
140           'group' => -100,
141           'type' => 'file',
142           'weight' => 0.013,
143           'media' => 'all',
144           'preprocess' => TRUE,
145           'data' => $path . 'css_input_with_bom.css',
146           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
147           'basename' => 'css_input_with_bom.css',
148         ],
149         '.byte-order-mark-test{content:"☃";}' . "\n",
150       ],
151       [
152         [
153           'group' => -100,
154           'type' => 'file',
155           'weight' => 0.013,
156           'media' => 'all',
157           'preprocess' => TRUE,
158           'data' => $path . 'css_input_with_charset.css',
159           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
160           'basename' => 'css_input_with_charset.css',
161         ],
162         '.charset-test{content:"€";}' . "\n",
163       ],
164       [
165         [
166           'group' => -100,
167           'type' => 'file',
168           'weight' => 0.013,
169           'media' => 'all',
170           'preprocess' => TRUE,
171           'data' => $path . 'css_input_with_bom_and_charset.css',
172           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
173           'basename' => 'css_input_with_bom_and_charset.css',
174         ],
175         '.byte-order-mark-charset-test{content:"☃";}' . "\n",
176       ],
177       [
178         [
179           'group' => -100,
180           'type' => 'file',
181           'weight' => 0.013,
182           'media' => 'all',
183           'preprocess' => TRUE,
184           'data' => $path . 'css_input_with_utf16_bom.css',
185           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
186           'basename' => 'css_input_with_utf16_bom.css',
187         ],
188         '.utf16-byte-order-mark-test{content:"☃";}' . "\n",
189       ],
190       [
191         [
192           'group' => -100,
193           'type' => 'file',
194           'weight' => 0.013,
195           'media' => 'all',
196           'preprocess' => TRUE,
197           'data' => $path . 'quotes.css',
198           'browsers' => ['IE' => TRUE, '!IE' => TRUE],
199           'basename' => 'quotes.css',
200         ],
201         file_get_contents($absolute_path . 'quotes.css.optimized.css'),
202       ],
203     ];
204   }
205
206   /**
207    * Tests optimizing a CSS asset group containing 'type' => 'file'.
208    *
209    * @dataProvider providerTestOptimize
210    */
211   public function testOptimize($css_asset, $expected) {
212     global $base_path;
213     $original_base_path = $base_path;
214     $base_path = '/';
215
216     // \Drupal\Core\Asset\CssOptimizer::loadFile() relies on the current working
217     // directory being the one that is used when index.php is the entry point.
218     // Note: PHPUnit automatically restores the original working directory.
219     chdir(realpath(__DIR__ . '/../../../../../../'));
220
221     $this->assertEquals($expected, $this->optimizer->optimize($css_asset), 'Group of file CSS assets optimized correctly.');
222
223     $base_path = $original_base_path;
224   }
225
226   /**
227    * Tests a file CSS asset with preprocessing disabled.
228    */
229   public function testTypeFilePreprocessingDisabled() {
230     $this->setExpectedException('Exception', 'Only file CSS assets with preprocessing enabled can be optimized.');
231
232     $css_asset = [
233       'group' => -100,
234       'type' => 'file',
235       'weight' => 0.012,
236       'media' => 'all',
237       // Preprocessing disabled.
238       'preprocess' => FALSE,
239       'data' => 'tests/Drupal/Tests/Core/Asset/foo.css',
240       'browsers' => ['IE' => TRUE, '!IE' => TRUE],
241       'basename' => 'foo.css',
242     ];
243     $this->optimizer->optimize($css_asset);
244   }
245
246   /**
247    * Tests a CSS asset with 'type' => 'external'.
248    */
249   public function testTypeExternal() {
250     $this->setExpectedException('Exception', 'Only file CSS assets can be optimized.');
251
252     $css_asset = [
253       'group' => -100,
254       // Type external.
255       'type' => 'external',
256       'weight' => 0.012,
257       'media' => 'all',
258       'preprocess' => TRUE,
259       'data' => 'http://example.com/foo.js',
260       'browsers' => ['IE' => TRUE, '!IE' => TRUE],
261     ];
262     $this->optimizer->optimize($css_asset);
263   }
264
265 }
266
267 /**
268  * Temporary mock for file_create_url(), until that is moved into
269  * Component/Utility.
270  */
271 if (!function_exists('Drupal\Tests\Core\Asset\file_create_url')) {
272   function file_create_url($uri) {
273     return 'file_create_url:' . $uri;
274   }
275 }
276
277 /**
278  * Temporary mock of file_url_transform_relative, until that is moved into
279  * Component/Utility.
280  */
281 if (!function_exists('Drupal\Tests\Core\Asset\file_url_transform_relative')) {
282   function file_url_transform_relative($uri) {
283     return 'file_url_transform_relative:' . $uri;
284   }
285 }
286
287 /**
288  * CssCollectionRenderer uses file_create_url() & file_url_transform_relative(),
289  * which *are* available when using the Simpletest test runner, but not when
290  * using the PHPUnit test runner; hence this hack.
291  */
292 namespace Drupal\Core\Asset;
293
294 if (!function_exists('Drupal\Core\Asset\file_create_url')) {
295
296   /**
297    * Temporary mock for file_create_url(), until that is moved into
298    * Component/Utility.
299    */
300   function file_create_url($uri) {
301     return \Drupal\Tests\Core\Asset\file_create_url($uri);
302   }
303
304 }
305 if (!function_exists('Drupal\Core\Asset\file_url_transform_relative')) {
306
307   /**
308    * Temporary mock of file_url_transform_relative, until that is moved into
309    * Component/Utility.
310    */
311   function file_url_transform_relative($uri) {
312     return \Drupal\Tests\Core\Asset\file_url_transform_relative($uri);
313   }
314
315 }
316 if (!function_exists('Drupal\Core\Asset\file_uri_scheme')) {
317
318   function file_uri_scheme($uri) {
319     return FALSE;
320   }
321
322 }