function AttachedAssetsTest::testHeaderHTML

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

Tests JS assets depending on the 'core/<head>' virtual library.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testHeaderHTML() {
    $build['#attached']['library'][] = 'common_test/js-header';
    $assets = AttachedAssets::createFromRenderArray($build);
    $js = $this->assetResolver
        ->getJsAssets($assets, FALSE)[0];
    $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
    $rendered_js = $this->renderer
        ->renderPlain($js_render_array);
    $query_string = $this->container
        ->get('state')
        ->get('system.css_js_query_string') ?: '0';
    $this->assertStringContainsString('<script src="' . $this->fileUrlGenerator
        ->generateString('core/modules/system/tests/modules/common_test/header.js') . '?' . $query_string . '"></script>', $rendered_js, 'The JS asset in common_test/js-header appears in the header.');
    $this->assertStringContainsString('<script src="' . $this->fileUrlGenerator
        ->generateString('core/misc/drupal.js'), $rendered_js, 'The JS asset of the direct dependency (core/drupal) of common_test/js-header appears in the header.');
    $this->assertStringContainsString('<script src="' . $this->fileUrlGenerator
        ->generateString('core/misc/drupalSettingsLoader.js'), $rendered_js, 'The JS asset of the indirect dependency (core/drupalSettings) of common_test/js-header appears in the header.');
}

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