function AttachedAssetsTest::testBrowserConditionalComments

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php \Drupal\KernelTests\Core\Asset\AttachedAssetsTest::testBrowserConditionalComments()

Tests adding JavaScript within conditional comments.

@group legacy

See also

\Drupal\Core\Render\Element\HtmlTag::preRenderConditionalComments()

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testBrowserConditionalComments() {
    $this->expectDeprecation('Support for IE Conditional Comments is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/3102997');
    $default_query_string = $this->container
        ->get('state')
        ->get('system.css_js_query_string') ?: '0';
    $build['#attached']['library'][] = 'common_test/browsers';
    $assets = AttachedAssets::createFromRenderArray($build);
    $js = $this->assetResolver
        ->getJsAssets($assets, FALSE)[1];
    $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
    $rendered_js = $this->renderer
        ->renderPlain($js_render_array);
    $expected_1 = "<!--[if lte IE 8]>\n" . '<script src="' . $this->fileUrlGenerator
        ->generateString('core/modules/system/tests/modules/common_test/old-ie.js') . '?' . $default_query_string . '"></script>' . "\n<![endif]-->";
    $expected_2 = "<!--[if !IE]><!-->\n" . '<script src="' . $this->fileUrlGenerator
        ->generateString('core/modules/system/tests/modules/common_test/no-ie.js') . '?' . $default_query_string . '"></script>' . "\n<!--<![endif]-->";
    $this->assertStringContainsString($expected_1, $rendered_js, 'Rendered JavaScript within downlevel-hidden conditional comments.');
    $this->assertStringContainsString($expected_2, $rendered_js, 'Rendered JavaScript within downlevel-revealed conditional comments.');
}

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