JavascriptGetDrupalSettingsTest.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests;
  3. /**
  4. * Tests Drupal settings retrieval in JavascriptTestBase tests.
  5. *
  6. * @group javascript
  7. */
  8. class JavascriptGetDrupalSettingsTest extends WebDriverTestBase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. protected static $modules = ['test_page_test'];
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected $defaultTheme = 'stark';
  17. /**
  18. * Tests retrieval of Drupal settings.
  19. *
  20. * @see \Drupal\FunctionalJavascriptTests\WebDriverTestBase::getDrupalSettings()
  21. */
  22. public function testGetDrupalSettings() {
  23. $this->drupalLogin($this->drupalCreateUser());
  24. $this->drupalGet('test-page');
  25. // Check that we can read the JS settings.
  26. $js_settings = $this->getDrupalSettings();
  27. $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);
  28. // Dynamically change the setting using Javascript.
  29. $script = <<<EndOfScript
  30. (function () {
  31. drupalSettings['test-setting'] = 'foo';
  32. })();
  33. EndOfScript;
  34. $this->getSession()->evaluateScript($script);
  35. // Check that the setting has been changed.
  36. $js_settings = $this->getDrupalSettings();
  37. $this->assertSame('foo', $js_settings['test-setting']);
  38. }
  39. }