function AttachedAssetsTest::testAddFiles

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

Tests adding a CSS and a JavaScript file.

File

core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php, line 87

Class

AttachedAssetsTest
Tests #attached assets: attached asset libraries and JavaScript settings.

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testAddFiles() : void {
    $build['#attached']['library'][] = 'common_test/files';
    $assets = AttachedAssets::createFromRenderArray($build);
    $css = $this->assetResolver
        ->getCssAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage());
    $js = $this->assetResolver
        ->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage())[1];
    $this->assertArrayHasKey('core/modules/system/tests/modules/common_test/bar.css', $css);
    $this->assertArrayHasKey('core/modules/system/tests/modules/common_test/foo.js', $js);
    $css_render_array = \Drupal::service('asset.css.collection_renderer')->render($css);
    $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
    $rendered_css = (string) $this->renderer
        ->renderInIsolation($css_render_array);
    $rendered_js = (string) $this->renderer
        ->renderInIsolation($js_render_array);
    $query_string = $this->container
        ->get('asset.query_string')
        ->get();
    $this->assertStringContainsString('<link rel="stylesheet" media="all" href="' . $this->fileUrlGenerator
        ->generateString('core/modules/system/tests/modules/common_test/bar.css') . '?' . $query_string . '" />', $rendered_css, 'Rendering an external CSS file.');
    $this->assertStringContainsString('<script src="' . $this->fileUrlGenerator
        ->generateString('core/modules/system/tests/modules/common_test/foo.js') . '?' . $query_string . '"></script>', $rendered_js, 'Rendering an external JavaScript file.');
}

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