function AttachedAssetsTest::testHeaderHTML

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testHeaderHTML()
  2. 8.9.x 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 237

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testHeaderHTML() : void {
    $build['#attached']['library'][] = 'common_test/js-header';
    $assets = AttachedAssets::createFromRenderArray($build);
    $js = $this->assetResolver
        ->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage())[0];
    $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
    $rendered_js = (string) $this->renderer
        ->renderInIsolation($js_render_array);
    $query_string = $this->container
        ->get('asset.query_string')
        ->get();
    $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.