You are here

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

Tests adding JavaScript within conditional comments.

See also

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

File

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

Class

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

Namespace

Drupal\system\Tests\Common

Code

function testBrowserConditionalComments() {
  $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="' . file_create_url('core/modules/system/tests/modules/common_test/old-ie.js') . '?' . $default_query_string . '"></script>' . "\n<![endif]-->";
  $expected_2 = "<!--[if !IE]><!-->\n" . '<script src="' . file_create_url('core/modules/system/tests/modules/common_test/no-ie.js') . '?' . $default_query_string . '"></script>' . "\n<!--<![endif]-->";
  $this
    ->assertNotIdentical(strpos($rendered_js, $expected_1), FALSE, 'Rendered JavaScript within downlevel-hidden conditional comments.');
  $this
    ->assertNotIdentical(strpos($rendered_js, $expected_2), FALSE, 'Rendered JavaScript within downlevel-revealed conditional comments.');
}