function AttachedAssetsTest::testAggregation

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

Integration test for CSS/JS aggregation.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testAggregation() : void {
  $build['#attached']['library'][] = 'core/drupal.timezone';
  $build['#attached']['library'][] = 'core/drupal.vertical-tabs';
  $assets = AttachedAssets::createFromRenderArray($build);
  $this->assertCount(1, $this->assetResolver
    ->getCssAssets($assets, TRUE, \Drupal::languageManager()->getCurrentLanguage()), 'There is a sole aggregated CSS asset.');
  [
    $header_js,
    $footer_js,
  ] = $this->assetResolver
    ->getJsAssets($assets, TRUE, \Drupal::languageManager()->getCurrentLanguage());
  $this->assertEquals([], \Drupal::service('asset.js.collection_renderer')->render($header_js), 'There are 0 JavaScript assets in the header.');
  $rendered_footer_js = \Drupal::service('asset.js.collection_renderer')->render($footer_js);
  $this->assertCount(3, $rendered_footer_js, 'There are 3 JavaScript assets in the footer.');
  $this->assertEquals('drupal-settings-json', $rendered_footer_js[0]['#attributes']['data-drupal-selector'], 'The first of the two JavaScript assets in the footer has drupal settings.');
  $this->assertStringContainsString('jquery.min.js', $rendered_footer_js[1]['#attributes']['src'], 'The second of the two JavaScript assets in the footer is jquery.min.js.');
  $this->assertStringStartsWith(base_path(), $rendered_footer_js[2]['#attributes']['src'], 'The third of the two JavaScript assets in the footer has the sole aggregated JavaScript asset.');
}

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