You are here

public function AttachedAssetsTest::testHeaderHTML in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testHeaderHTML()
  2. 9 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.');
}