3 namespace Drupal\FunctionalJavascriptTests\Ajax;
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
8 * Tests AJAX responses.
12 class AjaxTest extends JavascriptTestBase {
17 public static $modules = ['ajax_test'];
19 public function testAjaxWithAdminRoute() {
20 \Drupal::service('theme_installer')->install(['stable', 'seven']);
21 $theme_config = \Drupal::configFactory()->getEditable('system.theme');
22 $theme_config->set('admin', 'seven');
23 $theme_config->set('default', 'stable');
24 $theme_config->save();
26 $account = $this->drupalCreateUser(['view the administration theme']);
27 $this->drupalLogin($account);
29 // First visit the site directly via the URL. This should render it in the
31 $this->drupalGet('admin/ajax-test/theme');
32 $assert = $this->assertSession();
33 $assert->pageTextContains('Current theme: seven');
35 // Now click the modal, which should also use the admin theme.
36 $this->drupalGet('ajax-test/dialog');
37 $assert->pageTextNotContains('Current theme: stable');
38 $this->clickLink('Link 8 (ajax)');
39 $assert->assertWaitOnAjaxRequest();
41 $assert->pageTextContains('Current theme: stable');
42 $assert->pageTextNotContains('Current theme: seven');
46 * Test that AJAX loaded libraries are not retained between requests.
48 * @see https://www.drupal.org/node/2647916
50 public function testDrupalSettingsCachingRegression() {
51 $this->drupalGet('ajax-test/dialog');
52 $assert = $this->assertSession();
53 $session = $this->getSession();
55 // Insert a fake library into the already loaded library settings.
56 $fake_library = 'fakeLibrary/fakeLibrary';
57 $session->evaluateScript("drupalSettings.ajaxPageState.libraries = drupalSettings.ajaxPageState.libraries + ',$fake_library';");
59 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
60 // Test that the fake library is set.
61 $this->assertContains($fake_library, $libraries);
63 // Click on the AJAX link.
64 $this->clickLink('Link 8 (ajax)');
65 $assert->assertWaitOnAjaxRequest();
67 // Test that the fake library is still set after the AJAX call.
68 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
69 $this->assertContains($fake_library, $libraries);
71 // Reload the page, this should reset the loaded libraries and remove the
73 $this->drupalGet('ajax-test/dialog');
74 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
75 $this->assertNotContains($fake_library, $libraries);
77 // Click on the AJAX link again, and the libraries should still not contain
79 $this->clickLink('Link 8 (ajax)');
80 $assert->assertWaitOnAjaxRequest();
81 $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
82 $this->assertNotContains($fake_library, $libraries);