install(['stable', 'seven']); $theme_config = \Drupal::configFactory()->getEditable('system.theme'); $theme_config->set('admin', 'seven'); $theme_config->set('default', 'stable'); $theme_config->save(); $account = $this->drupalCreateUser(['view the administration theme']); $this->drupalLogin($account); // First visit the site directly via the URL. This should render it in the // admin theme. $this->drupalGet('admin/ajax-test/theme'); $assert = $this->assertSession(); $assert->pageTextContains('Current theme: seven'); // Now click the modal, which should also use the admin theme. $this->drupalGet('ajax-test/dialog'); $assert->pageTextNotContains('Current theme: stable'); $this->clickLink('Link 8 (ajax)'); $assert->assertWaitOnAjaxRequest(); $assert->pageTextContains('Current theme: stable'); $assert->pageTextNotContains('Current theme: seven'); } /** * Test that AJAX loaded libraries are not retained between requests. * * @see https://www.drupal.org/node/2647916 */ public function testDrupalSettingsCachingRegression() { $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'; $session->evaluateScript("drupalSettings.ajaxPageState.libraries = drupalSettings.ajaxPageState.libraries + ',$fake_library';"); $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries'); // Test that the fake library is set. $this->assertContains($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. $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries'); $this->assertContains($fake_library, $libraries); // Reload the page, this should reset the loaded libraries and remove the // fake library. $this->drupalGet('ajax-test/dialog'); $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries'); $this->assertNotContains($fake_library, $libraries); // Click on the AJAX link again, and the libraries should still not contain // the fake library. $this->clickLink('Link 8 (ajax)'); $assert->assertWaitOnAjaxRequest(); $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries'); $this->assertNotContains($fake_library, $libraries); } /** * Tests that various AJAX responses with DOM elements are correctly inserted. * * After inserting DOM elements, Drupal JavaScript behaviors should be * reattached and all top-level elements of type Node.ELEMENT_NODE need to be * part of the context. */ public function testInsertAjaxResponse() { $render_single_root = [ 'pre-wrapped-div' => '
pre-wrapped
', 'pre-wrapped-span' => 'pre-wrapped', 'pre-wrapped-whitespace' => '
pre-wrapped-whitespace
' . "\n", 'not-wrapped' => 'not-wrapped', 'comment-string-not-wrapped' => 'comment-string-not-wrapped', 'comment-not-wrapped' => '
comment-not-wrapped
', 'svg' => '', 'empty' => '', ]; $render_multiple_root_unwrapper = [ 'mixed' => ' foo foo bar

some string

additional not wrapped strings,

final string

', 'top-level-only' => '
element #1
element #2
', 'top-level-only-pre-whitespace' => '
element #1
element #2
', 'top-level-only-middle-whitespace-span' => 'element #1 element #2', 'top-level-only-middle-whitespace-div' => '
element #1
element #2
', ]; // This is temporary behavior for BC reason. $render_multiple_root_wrapper = []; foreach ($render_multiple_root_unwrapper as $key => $render) { $render_multiple_root_wrapper["$key--effect"] = '
' . $render . '
'; } $expected_renders = array_merge( $render_single_root, $render_multiple_root_wrapper, $render_multiple_root_unwrapper ); // Checking default process of wrapping Ajax content. foreach ($expected_renders as $render_type => $expected) { $this->assertInsert($render_type, $expected); } // Checking custom ajaxWrapperMultipleRootElements wrapping. $custom_wrapper_multiple_root = <<').append(elements); }; }(jQuery, Drupal)); JS; $expected = '
element #1 element #2
'; $this->assertInsert('top-level-only-middle-whitespace-span--effect', $expected, $custom_wrapper_multiple_root); // Checking custom ajaxWrapperNewContent wrapping. $custom_wrapper_new_content = <<').append(elements); }; }(jQuery, Drupal)); JS; $expected = '
'; $this->assertInsert('empty', $expected, $custom_wrapper_new_content); } /** * Assert insert. * * @param string $render_type * Render type. * @param string $expected * Expected result. * @param string $script * Script for additional theming. */ public function assertInsert($render_type, $expected, $script = '') { // Check insert to block element. $this->drupalGet('ajax-test/insert-block-wrapper'); $this->getSession()->executeScript($script); $this->clickLink("Link html $render_type"); $this->assertWaitPageContains('
' . $expected . '
'); $this->drupalGet('ajax-test/insert-block-wrapper'); $this->getSession()->executeScript($script); $this->clickLink("Link replaceWith $render_type"); $this->assertWaitPageContains('
' . $expected . '
'); // Check insert to inline element. $this->drupalGet('ajax-test/insert-inline-wrapper'); $this->getSession()->executeScript($script); $this->clickLink("Link html $render_type"); $this->assertWaitPageContains('
' . $expected . '
'); $this->drupalGet('ajax-test/insert-inline-wrapper'); $this->getSession()->executeScript($script); $this->clickLink("Link replaceWith $render_type"); $this->assertWaitPageContains('
' . $expected . '
'); } /** * Asserts that page contains an expected value after waiting. * * @param string $expected * A needle text. */ protected function assertWaitPageContains($expected) { $page = $this->getSession()->getPage(); $this->assertTrue($page->waitFor(10, function () use ($page, $expected) { // Clear content from empty styles and "processed" classes after effect. $content = str_replace([' class="processed"', ' processed', ' style=""'], '', $page->getContent()); return stripos($content, $expected) !== FALSE; }), "Page contains expected value: $expected"); } }