class CssOptimizerUnitTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php \Drupal\Tests\Core\Asset\CssOptimizerUnitTest
- 8.9.x core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php \Drupal\Tests\Core\Asset\CssOptimizerUnitTest
- 11.x core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php \Drupal\Tests\Core\Asset\CssOptimizerUnitTest
Tests the CSS asset optimizer.
@group Asset
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Asset\CssOptimizerUnitTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of CssOptimizerUnitTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Asset/ CssOptimizerUnitTest.php, line 16
Namespace
Drupal\Tests\Core\AssetView source
class CssOptimizerUnitTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected $backupGlobals = FALSE;
/**
* A CSS asset optimizer.
*
* @var \Drupal\Core\Asset\CssOptimizer
*/
protected $optimizer;
/**
* The file URL generator mock.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $fileUrlGenerator;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->fileUrlGenerator = $this->createMock(FileUrlGeneratorInterface::class);
$this->fileUrlGenerator
->expects($this->any())
->method('generateString')
->with($this->isType('string'))
->willReturnCallback(function ($uri) {
return 'generated-relative-url:' . $uri;
});
$this->optimizer = new CssOptimizer($this->fileUrlGenerator);
}
/**
* Provides data for the CSS asset optimizing test.
*/
public static function providerTestOptimize() {
$path = 'core/tests/Drupal/Tests/Core/Asset/css_test_files/';
$absolute_path = dirname(__FILE__) . '/css_test_files/';
return [
// 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)
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.012,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_without_import.css',
'basename' => 'css_input_without_import.css',
],
file_get_contents($absolute_path . 'css_input_without_import.css.optimized.css'),
],
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.012,
'media' => 'screen',
'preprocess' => TRUE,
'data' => $path . 'css_input_simple.css',
'browsers' => [
'IE' => TRUE,
'!IE' => TRUE,
],
'basename' => 'css_input_simple.css',
],
file_get_contents($absolute_path . 'css_input_simple_with_media.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
// FileUrlGeneratorInterface::generate().
// (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)
// Potential forms of @import might also include media queries.
// (https://developer.mozilla.org/en-US/docs/Web/CSS/@import)
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_import.css',
'basename' => 'css_input_with_import.css',
],
str_replace("url('import1.css')", 'url(generated-relative-url:' . $path . 'import1.css)', str_replace('url(images/icon.png)', 'url(generated-relative-url:' . $path . 'images/icon.png)', file_get_contents($absolute_path . 'css_input_with_import.css.optimized.css'))),
],
// File. Tests:
// - Retain comment hacks.
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'comment_hacks.css',
'basename' => 'comment_hacks.css',
],
file_get_contents($absolute_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)
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_subfolder/css_input_with_import.css',
'basename' => 'css_input_with_import.css',
],
str_replace('url(../images/icon.png)', 'url(generated-relative-url:' . $path . 'images/icon.png)', file_get_contents($absolute_path . 'css_subfolder/css_input_with_import.css.optimized.css')),
],
// File. Tests:
// - Any @charset declaration at the beginning of a file should be
// removed without breaking subsequent CSS.
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'charset_sameline.css',
'basename' => 'charset_sameline.css',
],
file_get_contents($absolute_path . 'charset.css.optimized.css'),
],
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'charset_newline.css',
'basename' => 'charset_newline.css',
],
file_get_contents($absolute_path . 'charset.css.optimized.css'),
],
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_bom.css',
'basename' => 'css_input_with_bom.css',
],
'.byte-order-mark-test{content:"☃";}' . "\n",
],
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_charset.css',
'basename' => 'css_input_with_charset.css',
],
'.charset-test{content:"€";}' . "\n",
],
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_bom_and_charset.css',
'basename' => 'css_input_with_bom_and_charset.css',
],
'.byte-order-mark-charset-test{content:"☃";}' . "\n",
],
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_utf16_bom.css',
'basename' => 'css_input_with_utf16_bom.css',
],
'.utf16-byte-order-mark-test{content:"☃";}' . "\n",
],
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'quotes.css',
'basename' => 'quotes.css',
],
file_get_contents($absolute_path . 'quotes.css.optimized.css'),
],
[
[
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'import3.css',
'basename' => 'import3.css',
],
file_get_contents($absolute_path . 'import3.css.optimized.css'),
],
];
}
/**
* Tests optimizing a CSS asset group containing 'type' => 'file'.
*
* @dataProvider providerTestOptimize
*/
public function testOptimize($css_asset, $expected) : void {
global $base_path;
$original_base_path = $base_path;
$base_path = '/';
$this->assertEquals($expected, $this->optimizer
->optimize($css_asset), 'Group of file CSS assets optimized correctly.');
$base_path = $original_base_path;
}
/**
* Tests a file CSS asset with preprocessing disabled.
*/
public function testTypeFilePreprocessingDisabled() : void {
$this->expectException('Exception');
$this->expectExceptionMessage('Only file CSS assets with preprocessing enabled can be optimized.');
$css_asset = [
'group' => -100,
'type' => 'file',
'weight' => 0.012,
'media' => 'all',
// Preprocessing disabled.
'preprocess' => FALSE,
'data' => 'tests/Drupal/Tests/Core/Asset/foo.css',
'basename' => 'foo.css',
];
$this->optimizer
->optimize($css_asset);
}
/**
* Tests a CSS asset with 'type' => 'external'.
*/
public function testTypeExternal() : void {
$this->expectException('Exception');
$this->expectExceptionMessage('Only file CSS assets can be optimized.');
$css_asset = [
'group' => -100,
// Type external.
'type' => 'external',
'weight' => 0.012,
'media' => 'all',
'preprocess' => TRUE,
'data' => 'http://example.com/foo.js',
];
$this->optimizer
->optimize($css_asset);
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
CssOptimizerUnitTest::$backupGlobals | protected | property | ||||
CssOptimizerUnitTest::$fileUrlGenerator | protected | property | The file URL generator mock. | |||
CssOptimizerUnitTest::$optimizer | protected | property | A CSS asset optimizer. | |||
CssOptimizerUnitTest::providerTestOptimize | public static | function | Provides data for the CSS asset optimizing test. | |||
CssOptimizerUnitTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
CssOptimizerUnitTest::testOptimize | public | function | Tests optimizing a CSS asset group containing 'type' => 'file'. | |||
CssOptimizerUnitTest::testTypeExternal | public | function | Tests a CSS asset with 'type' => 'external'. | |||
CssOptimizerUnitTest::testTypeFilePreprocessingDisabled | public | function | Tests a file CSS asset with preprocessing disabled. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed 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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.