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

some string

additional not wrapped strings,

final string

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