3 namespace Drupal\Tests\Core\Asset;
5 use Drupal\Core\Asset\JsOptimizer;
6 use Drupal\Tests\UnitTestCase;
9 * Tests the JS asset optimizer.
13 class JsOptimizerUnitTest extends UnitTestCase {
16 * A JS asset optimizer.
18 * @var \Drupal\Core\Asset\JsOptimizer object.
25 protected function setUp() {
28 $this->optimizer = new JsOptimizer();
32 * Provides data for the JS asset cleaning test.
34 * @see \Drupal\Core\Asset\JsOptimizer::clean()
37 * An array of test data.
39 public function providerTestClean() {
40 $path = dirname(__FILE__) . '/js_test_files/';
43 // - Stripped sourceMappingURL with comment # syntax.
45 file_get_contents($path . 'source_mapping_url.min.js'),
46 file_get_contents($path . 'source_mapping_url.min.js.optimized.js'),
49 // - Stripped sourceMappingURL with comment @ syntax.
51 file_get_contents($path . 'source_mapping_url_old.min.js'),
52 file_get_contents($path . 'source_mapping_url_old.min.js.optimized.js'),
55 // - Stripped sourceURL with comment # syntax.
57 file_get_contents($path . 'source_url.min.js'),
58 file_get_contents($path . 'source_url.min.js.optimized.js'),
61 // - Stripped sourceURL with comment @ syntax.
63 file_get_contents($path . 'source_url_old.min.js'),
64 file_get_contents($path . 'source_url_old.min.js.optimized.js'),
70 * Tests cleaning of a JS asset group containing 'type' => 'file'.
72 * @dataProvider providerTestClean
74 public function testClean($js_asset, $expected) {
75 $this->assertEquals($expected, $this->optimizer->clean($js_asset));
79 * Provides data for the JS asset optimize test.
81 * @see \Drupal\Core\Asset\JsOptimizer::optimize()
84 * An array of test data.
86 public function providerTestOptimize() {
87 $path = dirname(__FILE__) . '/js_test_files/';
93 'data' => $path . 'utf8_bom.js',
95 file_get_contents($path . 'utf8_bom.js.optimized.js'),
100 'preprocess' => TRUE,
101 'data' => $path . 'utf16_bom.js',
103 file_get_contents($path . 'utf16_bom.js.optimized.js'),
108 'preprocess' => TRUE,
109 'data' => $path . 'latin_9.js',
110 'attributes' => ['charset' => 'ISO-8859-15'],
112 file_get_contents($path . 'latin_9.js.optimized.js'),
118 * Tests cleaning of a JS asset group containing 'type' => 'file'.
120 * @dataProvider providerTestOptimize
122 public function testOptimize($js_asset, $expected) {
123 $this->assertEquals($expected, $this->optimizer->optimize($js_asset));