You are here

class CssOptimizerUnitTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php \Drupal\Tests\Core\Asset\CssOptimizerUnitTest

Tests the CSS asset optimizer.

@group Asset

Hierarchy

Expanded class hierarchy of CssOptimizerUnitTest

File

core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php, line 49
Contains \Drupal\Tests\Core\Asset\CssOptimizerUnitTest.

Namespace

Drupal\Tests\Core\Asset
View source
class CssOptimizerUnitTest extends UnitTestCase {

  /**
   * A CSS asset optimizer.
   *
   * @var \Drupal\Core\Asset\CssOptimizer object.
   */
  protected $optimizer;
  protected function setUp() {
    parent::setUp();
    $this->optimizer = new CssOptimizer();
  }

  /**
   * Provides data for the CSS asset optimizing test.
   */
  function providerTestOptimize() {
    $path = dirname(__FILE__) . '/css_test_files/';
    return array(
      // File. Tests:
      // - Stripped comments and white-space.
      // - Retain white-space in selectors. (https://www.drupal.org/node/472820)
      // - Retain pseudo-selectors. (https://www.drupal.org/node/460448)
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.012,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'css_input_without_import.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'css_input_without_import.css',
        ),
        file_get_contents($path . 'css_input_without_import.css.optimized.css'),
      ),
      // File. Tests:
      // - Proper URLs in imported files. (https://www.drupal.org/node/265719)
      // - A background image with relative paths, which must be rewritten.
      // - The rewritten background image path must also be passed through
      //   file_create_url(). (https://www.drupal.org/node/1961340)
      // - Imported files that are external (protocol-relative URL or not)
      //   should not be expanded. (https://www.drupal.org/node/2014851)
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'css_input_with_import.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'css_input_with_import.css',
        ),
        str_replace('url(images/icon.png)', 'url(' . file_create_url($path . 'images/icon.png') . ')', file_get_contents($path . 'css_input_with_import.css.optimized.css')),
      ),
      // File. Tests:
      // - Retain comment hacks.
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'comment_hacks.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'comment_hacks.css',
        ),
        file_get_contents($path . 'comment_hacks.css.optimized.css'),
      ),
      // File in subfolder. Tests:
      // - CSS import path is properly interpreted.
      //   (https://www.drupal.org/node/1198904)
      // - Don't adjust data URIs (https://www.drupal.org/node/2142441)
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'css_subfolder/css_input_with_import.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'css_input_with_import.css',
        ),
        str_replace('url(../images/icon.png)', 'url(' . file_create_url($path . 'images/icon.png') . ')', file_get_contents($path . 'css_subfolder/css_input_with_import.css.optimized.css')),
      ),
      // File. Tests:
      // - Any @charaset declaration at the beginning of a file should be
      //   removed without breaking subsequent CSS.
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'charset_sameline.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'charset_sameline.css',
        ),
        file_get_contents($path . 'charset.css.optimized.css'),
      ),
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'charset_newline.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'charset_newline.css',
        ),
        file_get_contents($path . 'charset.css.optimized.css'),
      ),
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'css_input_with_bom.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'css_input_with_bom.css',
        ),
        '.byte-order-mark-test{content:"☃";}' . "\n",
      ),
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'css_input_with_charset.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'css_input_with_charset.css',
        ),
        '.charset-test{content:"€";}' . "\n",
      ),
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'css_input_with_bom_and_charset.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'css_input_with_bom_and_charset.css',
        ),
        '.byte-order-mark-charset-test{content:"☃";}' . "\n",
      ),
      array(
        array(
          'group' => -100,
          'type' => 'file',
          'weight' => 0.013,
          'media' => 'all',
          'preprocess' => TRUE,
          'data' => $path . 'css_input_with_utf16_bom.css',
          'browsers' => array(
            'IE' => TRUE,
            '!IE' => TRUE,
          ),
          'basename' => 'css_input_with_utf16_bom.css',
        ),
        '.utf16-byte-order-mark-test{content:"☃";}' . "\n",
      ),
    );
  }

  /**
   * Tests optimizing a CSS asset group containing 'type' => 'file'.
   *
   * @dataProvider providerTestOptimize
   */
  function testOptimize($css_asset, $expected) {
    $this
      ->assertEquals($expected, $this->optimizer
      ->optimize($css_asset), 'Group of file CSS assets optimized correctly.');
  }

  /**
   * Tests a file CSS asset with preprocessing disabled.
   */
  function testTypeFilePreprocessingDisabled() {
    $this
      ->setExpectedException('Exception', 'Only file CSS assets with preprocessing enabled can be optimized.');
    $css_asset = array(
      'group' => -100,
      'type' => 'file',
      'weight' => 0.012,
      'media' => 'all',
      // Preprocessing disabled.
      'preprocess' => FALSE,
      'data' => 'tests/Drupal/Tests/Core/Asset/foo.css',
      'browsers' => array(
        'IE' => TRUE,
        '!IE' => TRUE,
      ),
      'basename' => 'foo.css',
    );
    $this->optimizer
      ->optimize($css_asset);
  }

  /**
   * Tests a CSS asset with 'type' => 'external'.
   */
  function testTypeExternal() {
    $this
      ->setExpectedException('Exception', 'Only file or inline CSS assets can be optimized.');
    $css_asset = array(
      'group' => -100,
      // Type external.
      'type' => 'external',
      'weight' => 0.012,
      'media' => 'all',
      'preprocess' => TRUE,
      'data' => 'http://example.com/foo.js',
      'browsers' => array(
        'IE' => TRUE,
        '!IE' => TRUE,
      ),
    );
    $this->optimizer
      ->optimize($css_asset);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CssOptimizerUnitTest::$optimizer protected property A CSS asset optimizer.
CssOptimizerUnitTest::providerTestOptimize function Provides data for the CSS asset optimizing test.
CssOptimizerUnitTest::setUp protected function Overrides UnitTestCase::setUp
CssOptimizerUnitTest::testOptimize function Tests optimizing a CSS asset group containing 'type' => 'file'.
CssOptimizerUnitTest::testTypeExternal function Tests a CSS asset with 'type' => 'external'.
CssOptimizerUnitTest::testTypeFilePreprocessingDisabled function Tests a file CSS asset with preprocessing disabled.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.