You are here

function AttachedAssetsTest::testAddExternalFiles in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Common/AttachedAssetsTest.php \Drupal\system\Tests\Common\AttachedAssetsTest::testAddExternalFiles()

Tests adding external CSS and JavaScript files.

File

core/modules/system/src/Tests/Common/AttachedAssetsTest.php, line 125
Contains \Drupal\system\Tests\Common\AttachedAssetsTest.

Class

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

Namespace

Drupal\system\Tests\Common

Code

function testAddExternalFiles() {
  $build['#attached']['library'][] = 'common_test/external';
  $assets = AttachedAssets::createFromRenderArray($build);
  $css = $this->assetResolver
    ->getCssAssets($assets, FALSE);
  $js = $this->assetResolver
    ->getJsAssets($assets, FALSE)[1];
  $this
    ->assertTrue(array_key_exists('http://example.com/stylesheet.css', $css), 'External CSS files are correctly added.');
  $this
    ->assertTrue(array_key_exists('http://example.com/script.js', $js), 'External JavaScript files are correctly added.');
  $css_render_array = \Drupal::service('asset.css.collection_renderer')
    ->render($css);
  $js_render_array = \Drupal::service('asset.js.collection_renderer')
    ->render($js);
  $rendered_css = $this->renderer
    ->renderPlain($css_render_array);
  $rendered_js = $this->renderer
    ->renderPlain($js_render_array);
  $this
    ->assertNotIdentical(strpos($rendered_css, '<link rel="stylesheet" href="http://example.com/stylesheet.css" media="all" />'), FALSE, 'Rendering an external CSS file.');
  $this
    ->assertNotIdentical(strpos($rendered_js, '<script src="http://example.com/script.js"></script>'), FALSE, 'Rendering an external JavaScript file.');
}