diff --git a/modules/refreshless_turbo/tests/src/FunctionalJavascript/DrupalSettingsTest.php b/modules/refreshless_turbo/tests/src/FunctionalJavascript/DrupalSettingsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c23e357b4097b82cf587b8e49620ae5638c8c743 --- /dev/null +++ b/modules/refreshless_turbo/tests/src/FunctionalJavascript/DrupalSettingsTest.php @@ -0,0 +1,115 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\refreshless_turbo\FunctionalJavascript; + +use Drupal\Core\Url; +use Drupal\FunctionalJavascriptTests\WebDriverTestBase; +use Drupal\Tests\refreshless_turbo\FunctionalJavascript\TurboAssertTrait; +use Drupal\Tests\refreshless_turbo\FunctionalJavascript\TurboHtmlDebugTrait; + +/** + * drupalSettings updater tests. + * + * @group refreshless + * + * @group refreshless_turbo + */ +class DrupalSettingsTest extends WebDriverTestBase { + + use TurboAssertTrait; + + use TurboHtmlDebugTrait; + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * {@inheritdoc} + */ + protected static $modules = ['block', 'refreshless_turbo', 'system']; + + /** + * Assert that a drupalSettings property equals an expected value. + * + * @param string $propertyName + * The property name. Note that 'drupalSettings.' is prepended automatically + * and so should not be included. + * + * @param string $expectedValue + * The expected value of the property. + */ + protected function assertDrupalSettingsValue( + string $propertyName, string $expectedValue, + ): void { + + $actualValue = $this->getSession()->evaluateScript(<<<JS + return drupalSettings.{$propertyName}; + JS); + + $this->assertEquals( + $expectedValue, $actualValue, + "drupalSettings.$propertyName was expected to equal \"$expectedValue\" but got \"$actualValue\"!", + ); + + } + + /** + * Test that drupalSettings updates are merged when navigating to new pages. + */ + public function testMerging(): void { + + $this->drupalPlaceBlock('local_tasks_block', [ + 'region' => 'content', 'id' => 'local-tasks-block', + ]); + + $loginUrl = Url::fromRoute('user.login'); + + $registerUrl = Url::fromRoute('user.register'); + + $resetPasswordUrl = Url::fromRoute('user.pass'); + + $this->drupalGet($loginUrl); + + $this->assertSession()->assertTurboIsPresent(); + + $this->assertSession()->startTurboPersist(); + + $this->assertDrupalSettingsValue( + 'path.currentPath', $loginUrl->getInternalPath(), + ); + + $this->click('[data-drupal-link-system-path="' . + $registerUrl->getInternalPath() . + '"]'); + + $this->assertSession()->assertWaitOnTurboRequest(); + + $this->assertSession()->assertTurboPersisted(); + + $this->assertDrupalSettingsValue( + 'path.currentPath', $registerUrl->getInternalPath(), + ); + + $this->turboHtmlOutput(); + + $this->click('[data-drupal-link-system-path="' . + $resetPasswordUrl->getInternalPath() . + '"]'); + + $this->assertSession()->assertWaitOnTurboRequest(); + + $this->assertSession()->assertTurboPersisted(); + + $this->assertDrupalSettingsValue( + 'path.currentPath', $resetPasswordUrl->getInternalPath(), + ); + + $this->turboHtmlOutput(); + + } + +}