diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTestBase.php deleted file mode 100644 index 4d7750945a..0000000000 --- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTestBase.php +++ /dev/null @@ -1,23 +0,0 @@ -getHtml() === 'This will be placed after'; })); + // Tests the 'alert' command. + $test_alert_command = <<executeScript($test_alert_command); + $page->pressButton("AJAX 'Alert': Click to alert"); + $this->assertWaitPageContains('
Alert
'); + // Tests the 'append' command. $page->pressButton("AJAX 'Append': Click to append something"); $this->assertWaitPageContains('
Append inside this div
Appended text
'); @@ -95,7 +119,7 @@ public function testAjaxCommands() { }; JS; $session->executeScript($test_settings_command); - // 'Click' not works, need 'mousedown'. See more in misc/ajax.es6.js. + // @todo: Replace after https://www.drupal.org/project/drupal/issues/2616184 $session->executeScript('window.jQuery("#edit-settings-command-example").mousedown();'); $this->assertWaitPageContains('
42
'); } diff --git a/core/tests/Drupal/KernelTests/Core/Ajax/AjaxTestBase.php b/core/tests/Drupal/KernelTests/Core/Ajax/AjaxTestBase.php deleted file mode 100644 index f12bfaad66..0000000000 --- a/core/tests/Drupal/KernelTests/Core/Ajax/AjaxTestBase.php +++ /dev/null @@ -1,61 +0,0 @@ -assertTrue($found, $message); - } - -} diff --git a/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php b/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php index 2747a9f834..a6bbe0b270 100644 --- a/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\EventSubscriber\AjaxResponseSubscriber; +use Drupal\KernelTests\KernelTestBase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -13,7 +14,12 @@ * * @group Ajax */ -class CommandsTest extends AjaxTestBase { +class CommandsTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['system', 'node', 'ajax_test', 'ajax_forms_test']; /** * {@inheritdoc} @@ -58,4 +64,48 @@ public function testAttachedSettings() { $assert('Settings command exists when JS aggregation is enabled.'); } + /** + * Asserts the array of Ajax commands contains the searched command. + * + * An AjaxResponse object stores an array of Ajax commands. This array + * sometimes includes commands automatically provided by the framework in + * addition to commands returned by a particular controller. During testing, + * we're usually interested that a particular command is present, and don't + * care whether other commands precede or follow the one we're interested in. + * Additionally, the command we're interested in may include additional data + * that we're not interested in. Therefore, this function simply asserts that + * one of the commands in $haystack contains all of the keys and values in + * $needle. Furthermore, if $needle contains a 'settings' key with an array + * value, we simply assert that all keys and values within that array are + * present in the command we're checking, and do not consider it a failure if + * the actual command contains additional settings that aren't part of + * $needle. + * + * @param $haystack + * An array of rendered Ajax commands returned by the server. + * @param $needle + * Array of info we're expecting in one of those commands. + * @param $message + * An assertion message. + */ + protected function assertCommand($haystack, $needle, $message) { + $found = FALSE; + foreach ($haystack as $command) { + // If the command has additional settings that we're not testing for, do + // not consider that a failure. + if (isset($command['settings']) && is_array($command['settings']) && isset($needle['settings']) && is_array($needle['settings'])) { + $command['settings'] = array_intersect_key($command['settings'], $needle['settings']); + } + // If the command has additional data that we're not testing for, do not + // consider that a failure. Also, == instead of ===, because we don't + // require the key/value pairs to be in any particular order + // (http://php.net/manual/language.operators.array.php). + if (array_intersect_key($command, $needle) == $needle) { + $found = TRUE; + break; + } + } + $this->assertTrue($found, $message); + } + }