You are here

function AttachedAssetsTest::testAddJsFileWithQueryString 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::testAddJsFileWithQueryString()

Tests JavaScript files that have querystrings attached get added right.

File

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

Class

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

Namespace

Drupal\system\Tests\Common

Code

function testAddJsFileWithQueryString() {
  $build['#attached']['library'][] = 'common_test/querystring';
  $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/querystring.css?arg1=value1&arg2=value2', $css), 'CSS file with query string is correctly added.');
  $this
    ->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2', $js), 'JavaScript file with query string is correctly added.');
  $css_render_array = \Drupal::service('asset.css.collection_renderer')
    ->render($css);
  $rendered_css = $this->renderer
    ->renderPlain($css_render_array);
  $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
    ->assertNotIdentical(strpos($rendered_css, '<link rel="stylesheet" href="' . str_replace('&', '&amp;', file_create_url('core/modules/system/tests/modules/common_test/querystring.css?arg1=value1&arg2=value2')) . '&amp;' . $query_string . '" media="all" />'), FALSE, 'CSS file with query string gets version query string correctly appended..');
  $this
    ->assertNotIdentical(strpos($rendered_js, '<script src="' . str_replace('&', '&amp;', file_create_url('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2')) . '&amp;' . $query_string . '"></script>'), FALSE, 'JavaScript file with query string gets version query string correctly appended.');
}