annotate core/modules/system/src/Tests/Ajax/AjaxTestBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\Ajax;
Chris@0 4
Chris@0 5 use Drupal\simpletest\WebTestBase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Provides a base class for Ajax tests.
Chris@0 9 */
Chris@0 10 abstract class AjaxTestBase extends WebTestBase {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Modules to enable.
Chris@0 14 *
Chris@0 15 * @var array
Chris@0 16 */
Chris@0 17 public static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Asserts the array of Ajax commands contains the searched command.
Chris@0 21 *
Chris@0 22 * An AjaxResponse object stores an array of Ajax commands. This array
Chris@0 23 * sometimes includes commands automatically provided by the framework in
Chris@0 24 * addition to commands returned by a particular controller. During testing,
Chris@0 25 * we're usually interested that a particular command is present, and don't
Chris@0 26 * care whether other commands precede or follow the one we're interested in.
Chris@0 27 * Additionally, the command we're interested in may include additional data
Chris@0 28 * that we're not interested in. Therefore, this function simply asserts that
Chris@0 29 * one of the commands in $haystack contains all of the keys and values in
Chris@0 30 * $needle. Furthermore, if $needle contains a 'settings' key with an array
Chris@0 31 * value, we simply assert that all keys and values within that array are
Chris@0 32 * present in the command we're checking, and do not consider it a failure if
Chris@0 33 * the actual command contains additional settings that aren't part of
Chris@0 34 * $needle.
Chris@0 35 *
Chris@0 36 * @param $haystack
Chris@0 37 * An array of rendered Ajax commands returned by the server.
Chris@0 38 * @param $needle
Chris@0 39 * Array of info we're expecting in one of those commands.
Chris@0 40 * @param $message
Chris@0 41 * An assertion message.
Chris@0 42 */
Chris@0 43 protected function assertCommand($haystack, $needle, $message) {
Chris@0 44 $found = FALSE;
Chris@0 45 foreach ($haystack as $command) {
Chris@0 46 // If the command has additional settings that we're not testing for, do
Chris@0 47 // not consider that a failure.
Chris@0 48 if (isset($command['settings']) && is_array($command['settings']) && isset($needle['settings']) && is_array($needle['settings'])) {
Chris@0 49 $command['settings'] = array_intersect_key($command['settings'], $needle['settings']);
Chris@0 50 }
Chris@0 51 // If the command has additional data that we're not testing for, do not
Chris@0 52 // consider that a failure. Also, == instead of ===, because we don't
Chris@0 53 // require the key/value pairs to be in any particular order
Chris@0 54 // (http://php.net/manual/language.operators.array.php).
Chris@0 55 if (array_intersect_key($command, $needle) == $needle) {
Chris@0 56 $found = TRUE;
Chris@0 57 break;
Chris@0 58 }
Chris@0 59 }
Chris@0 60 $this->assertTrue($found, $message);
Chris@0 61 }
Chris@0 62
Chris@0 63 }