class CssCollectionOptimizerLazyUnitTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/Asset/CssCollectionOptimizerLazyUnitTest.php \Drupal\Tests\Core\Asset\CssCollectionOptimizerLazyUnitTest
- 10 core/tests/Drupal/Tests/Core/Asset/CssCollectionOptimizerLazyUnitTest.php \Drupal\Tests\Core\Asset\CssCollectionOptimizerLazyUnitTest
Tests the CSS asset optimizer.
Attributes
#[Group('Asset')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Asset\CssCollectionOptimizerLazyUnitTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of CssCollectionOptimizerLazyUnitTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Asset/ CssCollectionOptimizerLazyUnitTest.php, line 26
Namespace
Drupal\Tests\Core\AssetView source
class CssCollectionOptimizerLazyUnitTest extends UnitTestCase {
/**
* A CSS asset optimizer.
*/
protected CssOptimizer $optimizer;
/**
* The file URL generator mock.
*/
protected FileUrlGeneratorInterface|MockObject $fileUrlGenerator;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->fileUrlGenerator = $this->createMock(FileUrlGeneratorInterface::class);
$this->fileUrlGenerator
->expects($this->any())
->method('generateString')
->with($this->isString())
->willReturnCallback(function ($uri) {
return 'generated-relative-url:' . $uri;
});
$this->optimizer = new CssOptimizer($this->fileUrlGenerator);
}
/**
* Tests that CSS imports with strange letters do not destroy the CSS output.
*/
public function testCssImport() : void {
$mock_grouper = $this->createMock(AssetCollectionGrouperInterface::class);
$mock_grouper->method('group')
->willReturnCallback(function ($assets) {
return [
[
'items' => $assets,
'type' => 'file',
'preprocess' => TRUE,
],
];
});
$mock_optimizer = $this->createMock(AssetOptimizerInterface::class);
$mock_optimizer->method('optimize')
->willReturn(file_get_contents(__DIR__ . '/css_test_files/css_input_with_import.css.optimized.css'), file_get_contents(__DIR__ . '/css_test_files/css_subfolder/css_input_with_import.css.optimized.css'));
$mock_theme_manager = $this->createMock(ThemeManagerInterface::class);
$mock_dependency_resolver = $this->createMock(LibraryDependencyResolverInterface::class);
$mock_file_system = $this->createMock(FileSystemInterface::class);
$mock_config_factory = $this->createMock(ConfigFactoryInterface::class);
$mock_file_url_generator = $this->createMock(FileUrlGeneratorInterface::class);
$mock_time = $this->createMock(TimeInterface::class);
$mock_language = $this->createMock(LanguageManagerInterface::class);
$optimizer = new CssCollectionOptimizerLazy($mock_grouper, $mock_optimizer, $mock_theme_manager, $mock_dependency_resolver, new RequestStack(), $mock_file_system, $mock_config_factory, $mock_file_url_generator, $mock_time, $mock_language);
$gpl_license = [
'name' => 'GPL-2.0-or-later',
'url' => 'https://www.drupal.org/licensing/faq',
'gpl-compatible' => TRUE,
];
$aggregate = $optimizer->optimizeGroup([
'items' => [
'core/modules/system/tests/modules/common_test/common_test_css_import.css' => [
'type' => 'file',
'data' => 'core/modules/system/tests/modules/common_test/common_test_css_import.css',
'preprocess' => TRUE,
'license' => $gpl_license,
],
'core/modules/system/tests/modules/common_test/common_test_css_import_not_preprocessed.css' => [
'type' => 'file',
'data' => 'core/modules/system/tests/modules/common_test/common_test_css_import.css',
'preprocess' => TRUE,
'license' => $gpl_license,
],
],
]);
self::assertStringEqualsFile(__DIR__ . '/css_test_files/css_input_with_import.css.optimized.aggregated.css', $aggregate);
}
/**
* Test that license information is added correctly to aggregated CSS.
*
* Checks that license information is added only once when several files
* have the same license. Checks that multiple licenses are added properly.
*/
public function testCssLicenseAggregation() : void {
$mock_grouper = $this->createMock(AssetCollectionGrouperInterface::class);
$mock_grouper->method('group')
->willReturnCallback(function ($assets) {
return [
[
'items' => $assets,
'type' => 'file',
'preprocess' => TRUE,
],
];
});
$mock_optimizer = $this->createMock(AssetOptimizerInterface::class);
$mock_optimizer->method('optimize')
->willReturn(file_get_contents(__DIR__ . '/css_test_files/css_input_with_import.css.optimized.css'), file_get_contents(__DIR__ . '/css_test_files/css_subfolder/css_input_with_import.css.optimized.css'), file_get_contents(__DIR__ . '/css_test_files/css_input_without_import.css.optimized.css'));
$mock_theme_manager = $this->createMock(ThemeManagerInterface::class);
$mock_dependency_resolver = $this->createMock(LibraryDependencyResolverInterface::class);
$mock_file_system = $this->createMock(FileSystemInterface::class);
$mock_config_factory = $this->createMock(ConfigFactoryInterface::class);
$mock_file_url_generator = $this->createMock(FileUrlGeneratorInterface::class);
$mock_time = $this->createMock(TimeInterface::class);
$mock_language = $this->createMock(LanguageManagerInterface::class);
$optimizer = new CssCollectionOptimizerLazy($mock_grouper, $mock_optimizer, $mock_theme_manager, $mock_dependency_resolver, new RequestStack(), $mock_file_system, $mock_config_factory, $mock_file_url_generator, $mock_time, $mock_language);
$gpl_license = [
'name' => 'GPL-2.0-or-later',
'url' => 'https://www.drupal.org/licensing/faq',
'gpl-compatible' => TRUE,
];
$aggregate = $optimizer->optimizeGroup([
'items' => [
'core/modules/system/tests/modules/common_test/common_test_css_import.css' => [
'type' => 'file',
'data' => 'core/modules/system/tests/modules/common_test/common_test_css_import.css',
'preprocess' => TRUE,
'license' => $gpl_license,
],
'core/modules/system/tests/modules/common_test/common_test_css_import_not_preprocessed.css' => [
'type' => 'file',
'data' => 'core/modules/system/tests/modules/common_test/common_test_css_import.css',
'preprocess' => TRUE,
'license' => $gpl_license,
],
'core/modules/system/tests/modules/common_test/css_input_without_import.css' => [
'type' => 'file',
'data' => 'core/modules/system/tests/modules/common_test/css_input_without_import.css',
'preprocess' => TRUE,
'license' => [
'name' => 'MIT',
'url' => 'https://opensource.org/licenses/MIT',
'gpl-compatible' => TRUE,
],
],
],
]);
self::assertStringEqualsFile(__DIR__ . '/css_test_files/css_license.css.optimized.aggregated.css', $aggregate);
}
/**
* Test that external minified CSS assets do not trigger optimization.
*
* This ensures that fully external asset groups do not result in a
* CssOptimizer exception and are safely ignored.
*/
public function testExternalMinifiedCssAssetOptimizationIsSkipped() : void {
$mock_grouper = $this->createMock(AssetCollectionGrouperInterface::class);
$mock_optimizer = $this->createMock(AssetOptimizerInterface::class);
// The expectation is to never call optimize on minified external assets.
$mock_optimizer->expects($this->never())
->method('optimize');
$optimizer = new CssCollectionOptimizerLazy($mock_grouper, $mock_optimizer, $this->createMock(ThemeManagerInterface::class), $this->createMock(LibraryDependencyResolverInterface::class), new RequestStack(), $this->createMock(FileSystemInterface::class), $this->createMock(ConfigFactoryInterface::class), $this->createMock(FileUrlGeneratorInterface::class), $this->createMock(TimeInterface::class), $this->createMock(LanguageManagerInterface::class));
$optimizer->optimizeGroup([
'items' => [
[
'type' => 'external',
'data' => 'core/tests/Drupal/Tests/Core/Asset/css_test_files/css_external.optimized.aggregated.css',
'license' => FALSE,
'preprocess' => TRUE,
'minified' => TRUE,
],
],
]);
}
/**
* Test that local minified CSS assets still trigger optimization.
*
* This ensures that local minified assets are optimized to correct relative
* paths.
*/
public function testLocalMinifiedCssAssetOptimizationIsNotSkipped() : void {
$mock_grouper = $this->createMock(AssetCollectionGrouperInterface::class);
$mock_optimizer = $this->createMock(AssetOptimizerInterface::class);
$mock_optimizer->expects($this->once())
->method('optimize');
$optimizer = new CssCollectionOptimizerLazy($mock_grouper, $mock_optimizer, $this->createMock(ThemeManagerInterface::class), $this->createMock(LibraryDependencyResolverInterface::class), new RequestStack(), $this->createMock(FileSystemInterface::class), $this->createMock(ConfigFactoryInterface::class), $this->createMock(FileUrlGeneratorInterface::class), $this->createMock(TimeInterface::class), $this->createMock(LanguageManagerInterface::class));
$optimizer->optimizeGroup([
'items' => [
[
'type' => 'file',
'data' => 'core/tests/Drupal/Tests/Core/Asset/css_test_files/css_input_with_import.css',
'license' => FALSE,
'preprocess' => TRUE,
'minified' => TRUE,
],
],
]);
}
/**
* Test that relative paths in local minified CSS files are updated.
*
* This ensures that local minified assets have their relative paths correctly
* rewritten during optimization.
*/
public function testRelativePathsInLocalMinifiedCssAssets() : void {
$mock_grouper = $this->createMock(AssetCollectionGrouperInterface::class);
$optimizer = new CssCollectionOptimizerLazy($mock_grouper, $this->optimizer, $this->createMock(ThemeManagerInterface::class), $this->createMock(LibraryDependencyResolverInterface::class), new RequestStack(), $this->createMock(FileSystemInterface::class), $this->createMock(ConfigFactoryInterface::class), $this->createMock(FileUrlGeneratorInterface::class), $this->createMock(TimeInterface::class), $this->createMock(LanguageManagerInterface::class));
$result = $optimizer->optimizeGroup([
'items' => [
[
'type' => 'file',
'data' => 'core/tests/Drupal/Tests/Core/Asset/css_test_files/css_minified_with_relative_paths.css',
'media' => 'all',
'license' => FALSE,
'preprocess' => TRUE,
'minified' => TRUE,
],
],
]);
$expected = file_get_contents(__DIR__ . '/css_test_files/css_minified_with_relative_paths.css.optimized.css');
self::assertEquals($expected, $result, 'Relative paths in local minified CSS assets are correctly replaced.');
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|---|
| CssCollectionOptimizerLazyUnitTest::$fileUrlGenerator | protected | property | The file URL generator mock. | ||
| CssCollectionOptimizerLazyUnitTest::$optimizer | protected | property | A CSS asset optimizer. | ||
| CssCollectionOptimizerLazyUnitTest::setUp | protected | function | Overrides UnitTestCase::setUp | ||
| CssCollectionOptimizerLazyUnitTest::testCssImport | public | function | Tests that CSS imports with strange letters do not destroy the CSS output. | ||
| CssCollectionOptimizerLazyUnitTest::testCssLicenseAggregation | public | function | Test that license information is added correctly to aggregated CSS. | ||
| CssCollectionOptimizerLazyUnitTest::testExternalMinifiedCssAssetOptimizationIsSkipped | public | function | Test that external minified CSS assets do not trigger optimization. | ||
| CssCollectionOptimizerLazyUnitTest::testLocalMinifiedCssAssetOptimizationIsNotSkipped | public | function | Test that local minified CSS assets still trigger optimization. | ||
| CssCollectionOptimizerLazyUnitTest::testRelativePathsInLocalMinifiedCssAssets | public | function | Test that relative paths in local minified CSS files are updated. | ||
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | ||
| ExpectDeprecationTrait::expectDeprecation | Deprecated | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | |||
| 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. | ||
| UnitTestCase::$root | protected | property | The app root. | ||
| 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::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::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | ||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.