summaryrefslogtreecommitdiffstatshomepage
path: root/core/tests/Drupal/FunctionalJavascriptTests/JavascriptGetDrupalSettingsTest.php
blob: 31ec8c91675b49a4d0947ac13bf614bfe906525f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

declare(strict_types=1);

namespace Drupal\FunctionalJavascriptTests;

/**
 * Tests Drupal settings retrieval in WebDriverTestBase tests.
 *
 * @group javascript
 */
class JavascriptGetDrupalSettingsTest extends WebDriverTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['test_page_test'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests retrieval of Drupal settings.
   *
   * @see \Drupal\FunctionalJavascriptTests\WebDriverTestBase::getDrupalSettings()
   */
  public function testGetDrupalSettings(): void {
    $this->drupalLogin($this->drupalCreateUser());
    $this->drupalGet('test-page');

    // Check that we can read the JS settings.
    $js_settings = $this->getDrupalSettings();
    $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);

    // Dynamically change the setting using JavaScript.
    $script = <<<EndOfScript
(function () {
  drupalSettings['test-setting'] = 'foo';
})();
EndOfScript;

    $this->getSession()->evaluateScript($script);

    // Check that the setting has been changed.
    $js_settings = $this->getDrupalSettings();
    $this->assertSame('foo', $js_settings['test-setting']);
  }

}