function AttachedAssetsTest::testAddJsFileWithQueryString

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

Tests JavaScript files that have query strings attached get added right.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testAddJsFileWithQueryString() : void {
    $build['#attached']['library'][] = 'common_test/querystring';
    $assets = AttachedAssets::createFromRenderArray($build);
    $css = $this->assetResolver
        ->getCssAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage());
    $js = $this->assetResolver
        ->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage())[1];
    $this->assertArrayHasKey('core/modules/system/tests/modules/common_test/querystring.css?arg1=value1&arg2=value2', $css);
    $this->assertArrayHasKey('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2', $js);
    $css_render_array = \Drupal::service('asset.css.collection_renderer')->render($css);
    $rendered_css = (string) $this->renderer
        ->renderInIsolation($css_render_array);
    $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
    $rendered_js = (string) $this->renderer
        ->renderInIsolation($js_render_array);
    $query_string = $this->container
        ->get('asset.query_string')
        ->get();
    $this->assertStringContainsString('<link rel="stylesheet" media="all" href="' . str_replace('&', '&amp;', $this->fileUrlGenerator
        ->generateString('core/modules/system/tests/modules/common_test/querystring.css?arg1=value1&arg2=value2')) . '&amp;' . $query_string . '" />', $rendered_css, 'CSS file with query string gets version query string correctly appended..');
    $this->assertStringContainsString('<script src="' . str_replace('&', '&amp;', $this->fileUrlGenerator
        ->generateString('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2')) . '&amp;' . $query_string . '"></script>', $rendered_js, 'JavaScript file with query string gets version query string correctly appended.');
}

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