You are here

function AttachedAssetsTest::testAddFiles in Zircon Profile 8

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

Tests adding a CSS and a JavaScript file.

File

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

Class

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

Namespace

Drupal\system\Tests\Common

Code

function testAddFiles() {
  $build['#attached']['library'][] = 'common_test/files';
  $assets = AttachedAssets::createFromRenderArray($build);
  $css = $this->assetResolver
    ->getCssAssets($assets, FALSE);
  $js = $this->assetResolver
    ->getJsAssets($assets, FALSE)[1];
  $this
    ->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/bar.css', $css), 'CSS files are correctly added.');
  $this
    ->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/foo.js', $js), '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);
  $query_string = $this->container
    ->get('state')
    ->get('system.css_js_query_string') ?: '0';
  $this
    ->assertNotIdentical(strpos($rendered_css, '<link rel="stylesheet" href="' . file_create_url('core/modules/system/tests/modules/common_test/bar.css') . '?' . $query_string . '" media="all" />'), FALSE, 'Rendering an external CSS file.');
  $this
    ->assertNotIdentical(strpos($rendered_js, '<script src="' . file_create_url('core/modules/system/tests/modules/common_test/foo.js') . '?' . $query_string . '"></script>'), FALSE, 'Rendering an external JavaScript file.');
}