View source
<?php
namespace Drupal\FunctionalJavascriptTests\Ajax;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class AjaxTest extends WebDriverTestBase {
public static $modules = [
'ajax_test',
];
protected $defaultTheme = 'stark';
public function testAjaxWithAdminRoute() {
\Drupal::service('theme_installer')
->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);
$this
->drupalGet('admin/ajax-test/theme');
$assert = $this
->assertSession();
$assert
->pageTextContains('Current theme: seven');
$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');
}
public function testDrupalSettingsCachingRegression() {
$this
->drupalGet('ajax-test/dialog');
$assert = $this
->assertSession();
$session = $this
->getSession();
$fake_library = 'fakeLibrary/fakeLibrary';
$session
->evaluateScript("drupalSettings.ajaxPageState.libraries = drupalSettings.ajaxPageState.libraries + ',{$fake_library}';");
$libraries = $session
->evaluateScript('drupalSettings.ajaxPageState.libraries');
$this
->assertStringContainsString($fake_library, $libraries);
$this
->clickLink('Link 8 (ajax)');
$assert
->assertWaitOnAjaxRequest();
$libraries = $session
->evaluateScript('drupalSettings.ajaxPageState.libraries');
$this
->assertStringContainsString($fake_library, $libraries);
$this
->drupalGet('ajax-test/dialog');
$libraries = $session
->evaluateScript('drupalSettings.ajaxPageState.libraries');
$this
->assertStringNotContainsString($fake_library, $libraries);
$this
->clickLink('Link 8 (ajax)');
$assert
->assertWaitOnAjaxRequest();
$libraries = $session
->evaluateScript('drupalSettings.ajaxPageState.libraries');
$this
->assertStringNotContainsString($fake_library, $libraries);
}
public function testInsertAjaxResponse() {
$render_single_root = [
'pre-wrapped-div' => '<div class="pre-wrapped">pre-wrapped<script> var test;</script></div>',
'pre-wrapped-span' => '<span class="pre-wrapped">pre-wrapped<script> var test;</script></span>',
'pre-wrapped-whitespace' => ' <div class="pre-wrapped-whitespace">pre-wrapped-whitespace</div>' . "\n",
'not-wrapped' => 'not-wrapped',
'comment-string-not-wrapped' => '<!-- COMMENT -->comment-string-not-wrapped',
'comment-not-wrapped' => '<!-- COMMENT --><div class="comment-not-wrapped">comment-not-wrapped</div>',
'svg' => '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect x="0" y="0" height="10" width="10" fill="green"/></svg>',
'empty' => '',
];
$render_multiple_root_unwrapper = [
'mixed' => ' foo <!-- COMMENT --> foo bar<div class="a class"><p>some string</p></div> additional not wrapped strings, <!-- ANOTHER COMMENT --> <p>final string</p>',
'top-level-only' => '<div>element #1</div><div>element #2</div>',
'top-level-only-pre-whitespace' => ' <div>element #1</div><div>element #2</div> ',
'top-level-only-middle-whitespace-span' => '<span>element #1</span> <span>element #2</span>',
'top-level-only-middle-whitespace-div' => '<div>element #1</div> <div>element #2</div>',
];
$render_multiple_root_wrapper = [];
foreach ($render_multiple_root_unwrapper as $key => $render) {
$render_multiple_root_wrapper["{$key}--effect"] = '<div>' . $render . '</div>';
}
$expected_renders = array_merge($render_single_root, $render_multiple_root_wrapper, $render_multiple_root_unwrapper);
foreach ($expected_renders as $render_type => $expected) {
$this
->assertInsert($render_type, $expected);
}
$custom_wrapper_multiple_root = <<<JS
(function(\$, Drupal){
Drupal.theme.ajaxWrapperMultipleRootElements = function (elements) {
return \$('<div class="my-favorite-div"></div>').append(elements);
};
}(jQuery, Drupal));
JS;
$expected = '<div class="my-favorite-div"><span>element #1</span> <span>element #2</span></div>';
$this
->assertInsert('top-level-only-middle-whitespace-span--effect', $expected, $custom_wrapper_multiple_root);
$custom_wrapper_new_content = <<<JS
(function(\$, Drupal){
Drupal.theme.ajaxWrapperNewContent = function (elements) {
return \$('<div class="div-wrapper-forever"></div>').append(elements);
};
}(jQuery, Drupal));
JS;
$expected = '<div class="div-wrapper-forever"></div>';
$this
->assertInsert('empty', $expected, $custom_wrapper_new_content);
}
public function assertInsert($render_type, $expected, $script = '') {
$this
->drupalGet('ajax-test/insert-block-wrapper');
$this
->getSession()
->executeScript($script);
$this
->clickLink("Link html {$render_type}");
$this
->assertWaitPageContains('<div class="ajax-target-wrapper"><div id="ajax-target">' . $expected . '</div></div>');
$this
->drupalGet('ajax-test/insert-block-wrapper');
$this
->getSession()
->executeScript($script);
$this
->clickLink("Link replaceWith {$render_type}");
$this
->assertWaitPageContains('<div class="ajax-target-wrapper">' . $expected . '</div>');
$this
->drupalGet('ajax-test/insert-inline-wrapper');
$this
->getSession()
->executeScript($script);
$this
->clickLink("Link html {$render_type}");
$this
->assertWaitPageContains('<div class="ajax-target-wrapper"><span id="ajax-target-inline">' . $expected . '</span></div>');
$this
->drupalGet('ajax-test/insert-inline-wrapper');
$this
->getSession()
->executeScript($script);
$this
->clickLink("Link replaceWith {$render_type}");
$this
->assertWaitPageContains('<div class="ajax-target-wrapper">' . $expected . '</div>');
}
protected function assertWaitPageContains($expected) {
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->waitFor(10, function () use ($page, $expected) {
$content = str_replace([
' class="processed"',
' processed',
' style=""',
], '', $page
->getContent());
return stripos($content, $expected) !== FALSE;
}), "Page contains expected value: {$expected}");
}
}