You are here

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

Tests adding JavaScript settings.

File

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

Class

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

Namespace

Drupal\system\Tests\Common

Code

function testAddJsSettings() {

  // Add a file in order to test default settings.
  $build['#attached']['library'][] = 'core/drupalSettings';
  $assets = AttachedAssets::createFromRenderArray($build);
  $this
    ->assertEqual([], $assets
    ->getSettings(), 'JavaScript settings on $assets are empty.');
  $javascript = $this->assetResolver
    ->getJsAssets($assets, FALSE)[1];
  $this
    ->assertTrue(array_key_exists('currentPath', $javascript['drupalSettings']['data']['path']), 'The current path JavaScript setting is set correctly.');
  $this
    ->assertTrue(array_key_exists('currentPath', $assets
    ->getSettings()['path']), 'JavaScript settings on $assets are resolved after retrieving JavaScript assets, and are equal to the returned JavaScript settings.');
  $assets
    ->setSettings([
    'drupal' => 'rocks',
    'dries' => 280342800,
  ]);
  $javascript = $this->assetResolver
    ->getJsAssets($assets, FALSE)[1];
  $this
    ->assertEqual(280342800, $javascript['drupalSettings']['data']['dries'], 'JavaScript setting is set correctly.');
  $this
    ->assertEqual('rocks', $javascript['drupalSettings']['data']['drupal'], 'The other JavaScript setting is set correctly.');
}