function AjaxTest::testDrupalSettingsCachingRegression

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxTest::testDrupalSettingsCachingRegression()
  2. 8.9.x core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxTest::testDrupalSettingsCachingRegression()
  3. 10 core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxTest::testDrupalSettingsCachingRegression()

Tests that AJAX loaded libraries are not retained between requests.

See also

https://www.drupal.org/node/2647916

File

core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php, line 58

Class

AjaxTest
Tests AJAX responses.

Namespace

Drupal\FunctionalJavascriptTests\Ajax

Code

public function testDrupalSettingsCachingRegression() : void {
    $this->drupalGet('ajax-test/dialog');
    $assert = $this->assertSession();
    $session = $this->getSession();
    // Insert a fake library into the already loaded library settings.
    $fake_library = 'fakeLibrary/fakeLibrary';
    $libraries = $session->evaluateScript("drupalSettings.ajaxPageState.libraries");
    $libraries = UrlHelper::compressQueryParameter(UrlHelper::uncompressQueryParameter($libraries) . ',' . $fake_library);
    $session->evaluateScript("drupalSettings.ajaxPageState.libraries = '{$libraries}';");
    $ajax_page_state = $session->evaluateScript("drupalSettings.ajaxPageState");
    $libraries = UrlHelper::uncompressQueryParameter($ajax_page_state['libraries']);
    // Test that the fake library is set.
    $this->assertStringContainsString($fake_library, $libraries);
    // Click on the AJAX link.
    $this->clickLink('Link 8 (ajax)');
    $assert->assertWaitOnAjaxRequest();
    // Test that the fake library is still set after the AJAX call.
    $ajax_page_state = $session->evaluateScript("drupalSettings.ajaxPageState");
    // Test that the fake library is set.
    $this->assertStringContainsString($fake_library, UrlHelper::uncompressQueryParameter($ajax_page_state['libraries']));
    // Reload the page, this should reset the loaded libraries and remove the
    // fake library.
    $this->drupalGet('ajax-test/dialog');
    $ajax_page_state = $session->evaluateScript("drupalSettings.ajaxPageState");
    $this->assertStringNotContainsString($fake_library, UrlHelper::uncompressQueryParameter($ajax_page_state['libraries']));
    // Click on the AJAX link again, and the libraries should still not contain
    // the fake library.
    $this->clickLink('Link 8 (ajax)');
    $assert->assertWaitOnAjaxRequest();
    $ajax_page_state = $session->evaluateScript("drupalSettings.ajaxPageState");
    $this->assertStringNotContainsString($fake_library, UrlHelper::uncompressQueryParameter($ajax_page_state['libraries']));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.