function AttachedAssetsTest::testSettings

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

Tests JavaScript settings.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Asset

Code

public function testSettings() : void {
  $build = [];
  $build['#attached']['library'][] = 'core/drupalSettings';
  // Nonsensical value to verify if it's possible to override path settings.
  $build['#attached']['drupalSettings']['path']['pathPrefix'] = 'does_not_exist';
  $assets = AttachedAssets::createFromRenderArray($build);
  $js = $this->assetResolver
    ->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage())[1];
  $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
  // Cast to string since this returns a \Drupal\Core\Render\Markup object.
  $rendered_js = (string) $this->renderer
    ->renderInIsolation($js_render_array);
  // Parse the generated drupalSettings <script> back to a PHP representation.
  $startToken = '{';
  $endToken = '}';
  $start = strpos($rendered_js, $startToken);
  $end = strrpos($rendered_js, $endToken);
  // Convert to a string, as $renderer_js is a \Drupal\Core\Render\Markup
  // object.
  $json = mb_substr($rendered_js, $start, $end - $start + 1);
  $parsed_settings = Json::decode($json);
  // Test whether the settings for core/drupalSettings are available.
  $this->assertTrue(isset($parsed_settings['path']['baseUrl']), 'drupalSettings.path.baseUrl is present.');
  $this->assertSame('does_not_exist', $parsed_settings['path']['pathPrefix'], 'drupalSettings.path.pathPrefix is present and has the correct (overridden) value.');
  $this->assertSame('', $parsed_settings['path']['currentPath'], 'drupalSettings.path.currentPath is present and has the correct value.');
  $this->assertFalse($parsed_settings['path']['currentPathIsAdmin'], 'drupalSettings.path.currentPathIsAdmin is present and has the correct value.');
  $this->assertFalse($parsed_settings['path']['isFront'], 'drupalSettings.path.isFront is present and has the correct value.');
  $this->assertSame('en', $parsed_settings['path']['currentLanguage'], 'drupalSettings.path.currentLanguage is present and has the correct value.');
  // Tests whether altering JavaScript settings via hook_js_settings_alter()
  // is working as expected.
  // @see common_test_js_settings_alter()
  $this->assertSame('☃', $parsed_settings['pluralDelimiter']);
  $this->assertSame('bar', $parsed_settings['foo']);
}

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