function LibraryDiscoveryIntegrationTest::testLibrariesExtend

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php \Drupal\KernelTests\Core\Asset\LibraryDiscoveryIntegrationTest::testLibrariesExtend()
  2. 10 core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php \Drupal\KernelTests\Core\Asset\LibraryDiscoveryIntegrationTest::testLibrariesExtend()
  3. 11.x core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php \Drupal\KernelTests\Core\Asset\LibraryDiscoveryIntegrationTest::testLibrariesExtend()

Tests libraries-extend.

File

core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php, line 175

Class

LibraryDiscoveryIntegrationTest
Tests the library discovery and library discovery parser.

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testLibrariesExtend() {
    // Simulate starterkit_theme defining the book-navigation library.
    // @see theme_test_library_info_alter()
    $this->container
        ->get('state')
        ->set('theme_test_library_info_alter starterkit_theme', [
        'book-navigation' => [
            'css' => [
                'component' => [
                    'css/components/book-navigation.css' => [],
                ],
            ],
        ],
    ]);
    // Activate starterkit_theme and verify the libraries are not extended.
    $this->activateTheme('starterkit_theme');
    $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_1.css', 'starterkit_theme', 'book-navigation', 'css');
    $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/js/extend_1.js', 'starterkit_theme', 'book-navigation', 'js');
    $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_2.css', 'starterkit_theme', 'book-navigation', 'css');
    // Activate the theme that extends the book-navigation library in
    // starterkit_theme.
    $this->activateTheme('test_theme_libraries_extend');
    $this->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_1.css', 'starterkit_theme', 'book-navigation', 'css');
    $this->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/js/extend_1.js', 'starterkit_theme', 'book-navigation', 'js');
    $this->assertAssetInLibrary('core/modules/system/tests/themes/test_theme_libraries_extend/css/extend_2.css', 'starterkit_theme', 'book-navigation', 'css');
    // Activate a sub theme and confirm that it inherits the library assets
    // extended in the base theme as well as its own.
    $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/base-libraries-extend.css', 'starterkit_theme', 'base', 'css');
    $this->assertNoAssetInLibrary('core/modules/system/tests/themes/test_subtheme/css/sub-libraries-extend.css', 'starterkit_theme', 'base', 'css');
    $this->activateTheme('test_subtheme');
    $this->assertAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/base-libraries-extend.css', 'starterkit_theme', 'base', 'css');
    $this->assertAssetInLibrary('core/modules/system/tests/themes/test_subtheme/css/sub-libraries-extend.css', 'starterkit_theme', 'base', 'css');
    // Activate test theme that extends with a non-existent library. An
    // exception should be thrown.
    $this->activateTheme('test_theme_libraries_extend');
    try {
        $this->libraryDiscovery
            ->getLibraryByName('core', 'drupal.dialog');
        $this->fail('Throw Exception when specifying non-existent libraries-extend.');
    } catch (InvalidLibrariesExtendSpecificationException $e) {
        $expected_message = 'The specified library "test_theme_libraries_extend/non_existent_library" does not exist.';
        $this->assertEquals($expected_message, $e->getMessage(), 'Throw Exception when specifying non-existent libraries-extend.');
    }
    // Also, test non-string libraries-extend. An exception should be thrown.
    $this->container
        ->get('theme_installer')
        ->install([
        'test_theme',
    ]);
    try {
        $this->libraryDiscovery
            ->getLibraryByName('test_theme', 'collapse');
        $this->fail('Throw Exception when specifying non-string libraries-extend.');
    } catch (InvalidLibrariesExtendSpecificationException $e) {
        $expected_message = 'The libraries-extend specification for each library must be a list of strings.';
        $this->assertEquals($expected_message, $e->getMessage(), 'Throw Exception when specifying non-string libraries-extend.');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.