function JavaScriptTest::testHeaderSetting

Tests drupal_get_js() for JavaScript settings.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php, line 141
Definition of Drupal\system\Tests\Common\JavaScriptTest.

Class

JavaScriptTest
Tests the JavaScript system.

Namespace

Drupal\system\Tests\Common

Code

function testHeaderSetting() {
  drupal_add_library('system', 'drupalSettings');
  $javascript = drupal_get_js('header');
  $this
    ->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.');
  $this
    ->assertTrue(strpos($javascript, 'scriptPath') > 0, 'Rendered JavaScript header returns scriptPath setting.');
  $this
    ->assertTrue(strpos($javascript, 'pathPrefix') > 0, 'Rendered JavaScript header returns pathPrefix setting.');
  $this
    ->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.');

  // Only the second of these two entries should appear in Drupal.settings.
  drupal_add_js(array(
    'commonTest' => 'commonTestShouldNotAppear',
  ), 'setting');
  drupal_add_js(array(
    'commonTest' => 'commonTestShouldAppear',
  ), 'setting');

  // All three of these entries should appear in Drupal.settings.
  drupal_add_js(array(
    'commonTestArray' => array(
      'commonTestValue0',
    ),
  ), 'setting');
  drupal_add_js(array(
    'commonTestArray' => array(
      'commonTestValue1',
    ),
  ), 'setting');
  drupal_add_js(array(
    'commonTestArray' => array(
      'commonTestValue2',
    ),
  ), 'setting');

  // Only the second of these two entries should appear in Drupal.settings.
  drupal_add_js(array(
    'commonTestArray' => array(
      'key' => 'commonTestOldValue',
    ),
  ), 'setting');
  drupal_add_js(array(
    'commonTestArray' => array(
      'key' => 'commonTestNewValue',
    ),
  ), 'setting');
  $javascript = drupal_get_js('header');

  // Test whether drupal_add_js can be used to override a previous setting.
  $this
    ->assertTrue(strpos($javascript, 'commonTestShouldAppear') > 0, 'Rendered JavaScript header returns custom setting.');
  $this
    ->assertTrue(strpos($javascript, 'commonTestShouldNotAppear') === FALSE, 'drupal_add_js() correctly overrides a custom setting.');

  // Test whether drupal_add_js can be used to add numerically indexed values
  // to an array.
  $array_values_appear = strpos($javascript, 'commonTestValue0') > 0 && strpos($javascript, 'commonTestValue1') > 0 && strpos($javascript, 'commonTestValue2') > 0;
  $this
    ->assertTrue($array_values_appear, 'drupal_add_js() correctly adds settings to the end of an indexed array.');

  // Test whether drupal_add_js can be used to override the entry for an
  // existing key in an associative array.
  $associative_array_override = strpos($javascript, 'commonTestNewValue') > 0 && strpos($javascript, 'commonTestOldValue') === FALSE;
  $this
    ->assertTrue($associative_array_override, 'drupal_add_js() correctly overrides settings within an associative array.');

  // Check in a rendered page.
  $this
    ->drupalGet('common-test/query-string');
  $this
    ->assertPattern('@<script>.+drupalSettings.+"currentPath":"common-test\\\\/query-string"@s', 'currentPath is in the JS settings');
  $path = array(
    'source' => 'common-test/query-string',
    'alias' => 'common-test/currentpath-check',
  );
  drupal_container()
    ->get('path.crud')
    ->save($path['source'], $path['alias']);
  $this
    ->drupalGet('common-test/currentpath-check');
  $this
    ->assertPattern('@<script>.+drupalSettings.+"currentPath":"common-test\\\\/query-string"@s', 'currentPath is in the JS settings for an aliased path');
}