Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Asset / JsOptimizerUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Asset;
4
5 use Drupal\Core\Asset\JsOptimizer;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests the JS asset optimizer.
10  *
11  * @group Asset
12  */
13 class JsOptimizerUnitTest extends UnitTestCase {
14
15   /**
16    * A JS asset optimizer.
17    *
18    * @var \Drupal\Core\Asset\JsOptimizer object.
19    */
20   protected $optimizer;
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
28     $this->optimizer = new JsOptimizer();
29   }
30
31   /**
32    * Provides data for the JS asset cleaning test.
33    *
34    * @see \Drupal\Core\Asset\JsOptimizer::clean()
35    *
36    * @returns array
37    *   An array of test data.
38    */
39   public function providerTestClean() {
40     $path = dirname(__FILE__) . '/js_test_files/';
41     return [
42       // File. Tests:
43       // - Stripped sourceMappingURL with comment # syntax.
44       0 => [
45         file_get_contents($path . 'source_mapping_url.min.js'),
46         file_get_contents($path . 'source_mapping_url.min.js.optimized.js'),
47       ],
48       // File. Tests:
49       // - Stripped sourceMappingURL with comment @ syntax.
50       1 => [
51         file_get_contents($path . 'source_mapping_url_old.min.js'),
52         file_get_contents($path . 'source_mapping_url_old.min.js.optimized.js'),
53       ],
54       // File. Tests:
55       // - Stripped sourceURL with comment # syntax.
56       2 => [
57         file_get_contents($path . 'source_url.min.js'),
58         file_get_contents($path . 'source_url.min.js.optimized.js'),
59       ],
60       // File. Tests:
61       // - Stripped sourceURL with comment @ syntax.
62       3 => [
63         file_get_contents($path . 'source_url_old.min.js'),
64         file_get_contents($path . 'source_url_old.min.js.optimized.js'),
65       ],
66     ];
67   }
68
69   /**
70    * Tests cleaning of a JS asset group containing 'type' => 'file'.
71    *
72    * @dataProvider providerTestClean
73    */
74   public function testClean($js_asset, $expected) {
75     $this->assertEquals($expected, $this->optimizer->clean($js_asset));
76   }
77
78   /**
79    * Provides data for the JS asset optimize test.
80    *
81    * @see \Drupal\Core\Asset\JsOptimizer::optimize()
82    *
83    * @returns array
84    *   An array of test data.
85    */
86   public function providerTestOptimize() {
87     $path = dirname(__FILE__) . '/js_test_files/';
88     return [
89       0 => [
90         [
91           'type' => 'file',
92           'preprocess' => TRUE,
93           'data' => $path . 'utf8_bom.js',
94         ],
95         file_get_contents($path . 'utf8_bom.js.optimized.js'),
96       ],
97       1 => [
98         [
99           'type' => 'file',
100           'preprocess' => TRUE,
101           'data' => $path . 'utf16_bom.js',
102         ],
103         file_get_contents($path . 'utf16_bom.js.optimized.js'),
104       ],
105       2 => [
106         [
107           'type' => 'file',
108           'preprocess' => TRUE,
109           'data' => $path . 'latin_9.js',
110           'attributes' => ['charset' => 'ISO-8859-15'],
111         ],
112         file_get_contents($path . 'latin_9.js.optimized.js'),
113       ],
114     ];
115   }
116
117   /**
118    * Tests cleaning of a JS asset group containing 'type' => 'file'.
119    *
120    * @dataProvider providerTestOptimize
121    */
122   public function testOptimize($js_asset, $expected) {
123     $this->assertEquals($expected, $this->optimizer->optimize($js_asset));
124   }
125
126 }