JsOptimizerUnitTest.php 3.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

namespace Drupal\Tests\Core\Asset;

use Drupal\Core\Asset\JsOptimizer;
use Drupal\Tests\UnitTestCase;

/**
 * Tests the JS asset optimizer.
 *
 * @group Asset
 */
class JsOptimizerUnitTest extends UnitTestCase {

  /**
   * A JS asset optimizer.
   *
18
   * @var \Drupal\Core\Asset\JsOptimizer
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   */
  protected $optimizer;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->optimizer = new JsOptimizer();
  }

  /**
   * Provides data for the JS asset cleaning test.
   *
34
   * @see \Drupal\Core\Asset\JsOptimizer::clean()
35 36 37 38
   *
   * @returns array
   *   An array of test data.
   */
39
  public function providerTestClean() {
40
    $path = dirname(__FILE__) . '/js_test_files/';
41
    return [
42 43
      // File. Tests:
      // - Stripped sourceMappingURL with comment # syntax.
44
      0 => [
45 46
        file_get_contents($path . 'source_mapping_url.min.js'),
        file_get_contents($path . 'source_mapping_url.min.js.optimized.js'),
47
      ],
48 49
      // File. Tests:
      // - Stripped sourceMappingURL with comment @ syntax.
50
      1 => [
51 52
        file_get_contents($path . 'source_mapping_url_old.min.js'),
        file_get_contents($path . 'source_mapping_url_old.min.js.optimized.js'),
53
      ],
54 55
      // File. Tests:
      // - Stripped sourceURL with comment # syntax.
56
      2 => [
57 58
        file_get_contents($path . 'source_url.min.js'),
        file_get_contents($path . 'source_url.min.js.optimized.js'),
59
      ],
60 61
      // File. Tests:
      // - Stripped sourceURL with comment @ syntax.
62
      3 => [
63 64
        file_get_contents($path . 'source_url_old.min.js'),
        file_get_contents($path . 'source_url_old.min.js.optimized.js'),
65 66
      ],
    ];
67 68 69 70 71 72 73
  }

  /**
   * Tests cleaning of a JS asset group containing 'type' => 'file'.
   *
   * @dataProvider providerTestClean
   */
74
  public function testClean($js_asset, $expected) {
75 76 77
    $this->assertEquals($expected, $this->optimizer->clean($js_asset));
  }

78 79 80
  /**
   * Provides data for the JS asset optimize test.
   *
81
   * @see \Drupal\Core\Asset\JsOptimizer::optimize()
82 83 84 85
   *
   * @returns array
   *   An array of test data.
   */
86
  public function providerTestOptimize() {
87
    $path = dirname(__FILE__) . '/js_test_files/';
88 89 90
    return [
      0 => [
        [
91 92 93
          'type' => 'file',
          'preprocess' => TRUE,
          'data' => $path . 'utf8_bom.js',
94
        ],
95
        file_get_contents($path . 'utf8_bom.js.optimized.js'),
96 97 98
      ],
      1 => [
        [
99 100 101
          'type' => 'file',
          'preprocess' => TRUE,
          'data' => $path . 'utf16_bom.js',
102
        ],
103
        file_get_contents($path . 'utf16_bom.js.optimized.js'),
104 105 106
      ],
      2 => [
        [
107 108 109
          'type' => 'file',
          'preprocess' => TRUE,
          'data' => $path . 'latin_9.js',
110 111
          'attributes' => ['charset' => 'ISO-8859-15'],
        ],
112
        file_get_contents($path . 'latin_9.js.optimized.js'),
113 114
      ],
    ];
115 116 117 118 119 120 121
  }

  /**
   * Tests cleaning of a JS asset group containing 'type' => 'file'.
   *
   * @dataProvider providerTestOptimize
   */
122
  public function testOptimize($js_asset, $expected) {
123 124 125
    $this->assertEquals($expected, $this->optimizer->optimize($js_asset));
  }

126
}