function AttachedAssetsTest::testAttributes
Tests adding JavaScript files with additional attributes.
File
- 
              core/
tests/ Drupal/ KernelTests/ Core/ Asset/ AttachedAssetsTest.php, line 147  
Class
- AttachedAssetsTest
 - Tests #attached assets: attached asset libraries and JavaScript settings.
 
Namespace
Drupal\KernelTests\Core\AssetCode
public function testAttributes() : void {
  $build['#attached']['library'][] = 'common_test/js-attributes';
  $assets = AttachedAssets::createFromRenderArray($build);
  $js = $this->assetResolver
    ->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage())[1];
  $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
  $rendered_js = (string) $this->renderer
    ->renderInIsolation($js_render_array);
  $expected_1 = '<script src="http://example.com/deferred-external.js" foo="bar" defer></script>';
  $expected_2 = '<script src="' . $this->fileUrlGenerator
    ->generateString('core/modules/system/tests/modules/common_test/deferred-internal.js') . '?v=1" defer bar="foo"></script>';
  $this->assertStringContainsString($expected_1, $rendered_js, 'Rendered external JavaScript with correct defer and random attributes.');
  $this->assertStringContainsString($expected_2, $rendered_js, 'Rendered internal JavaScript with correct defer and random attributes.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.