class CssCollectionOptimizerUnitTest

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

Tests the CSS asset optimizer.

@group Asset

Hierarchy

Expanded class hierarchy of CssCollectionOptimizerUnitTest

File

core/tests/Drupal/Tests/Core/Asset/CssCollectionOptimizerUnitTest.php, line 20

Namespace

Drupal\Tests\Core\Asset
View source
class CssCollectionOptimizerUnitTest extends UnitTestCase {
    
    /**
     * The data from the dumper.
     *
     * @var string
     */
    protected $dumperData;
    
    /**
     * A CSS Collection optimizer.
     *
     * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
     */
    protected $optimizer;
    
    /**
     * Tests that CSS imports with strange letters do not destroy the CSS output.
     *
     * @group legacy
     */
    public function testCssImport() {
        $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_dumper = $this->createMock(AssetDumperInterface::class);
        $mock_dumper->method('dump')
            ->willReturnCallback(function ($css) {
            $this->dumperData = $css;
        });
        $mock_state = $this->createMock(StateInterface::class);
        $mock_file_system = $this->createMock(FileSystemInterface::class);
        $this->optimizer = new CssCollectionOptimizer($mock_grouper, $mock_optimizer, $mock_dumper, $mock_state, $mock_file_system);
        $gpl_license = [
            'name' => 'GNU-GPL-2.0-or-later',
            'url' => 'https://www.drupal.org/licensing/faq',
            'gpl-compatible' => TRUE,
        ];
        $this->optimizer
            ->optimize([
            '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::assertEquals(file_get_contents(__DIR__ . '/css_test_files/css_input_with_import.css.optimized.aggregated.css'), $this->dumperData);
    }
    
    /**
     * Tests that CSS imports with strange letters do not destroy the CSS output.
     *
     * Checks that license information is added only once when several files
     * have the same license. Checks that multiple licenses are added properly.
     *
     * @group legacy
     */
    public function testCssLicenseAggregation() {
        $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_dumper = $this->createMock(AssetDumperInterface::class);
        $mock_dumper->method('dump')
            ->willReturnCallback(function ($css) {
            $this->dumperData = $css;
        });
        $mock_state = $this->createMock(StateInterface::class);
        $mock_file_system = $this->createMock(FileSystemInterface::class);
        $this->optimizer = new CssCollectionOptimizer($mock_grouper, $mock_optimizer, $mock_dumper, $mock_state, $mock_file_system);
        $gpl_license = [
            'name' => 'GNU-GPL-2.0-or-later',
            'url' => 'https://www.drupal.org/licensing/faq',
            'gpl-compatible' => TRUE,
        ];
        $this->optimizer
            ->optimize([
            '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::assertEquals(file_get_contents(__DIR__ . '/css_test_files/css_license.css.optimized.aggregated.css'), $this->dumperData);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
CssCollectionOptimizerUnitTest::$dumperData protected property The data from the dumper.
CssCollectionOptimizerUnitTest::$optimizer protected property A CSS Collection optimizer.
CssCollectionOptimizerUnitTest::testCssImport public function Tests that CSS imports with strange letters do not destroy the CSS output.
CssCollectionOptimizerUnitTest::testCssLicenseAggregation public function Tests that CSS imports with strange letters do not destroy the CSS output.
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::setUp protected function 351
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.