protected function AJAXTestCase::assertCommand in Drupal 7
Assert that a command with the required properties exists within the array of Ajax commands returned by the server.
The Ajax framework, via the ajax_deliver() and ajax_render() functions, returns an array of commands. This array sometimes includes commands automatically provided by the framework in addition to commands returned by a particular page callback. 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.
Parameters
$haystack: An array of Ajax commands returned by the server.
$needle: Array of info we're expecting in one of those commands.
$message: An assertion message.
6 calls to AJAXTestCase::assertCommand()
- AJAXCommandsTestCase::testAJAXCommands in modules/
simpletest/ tests/ ajax.test - Test the various Ajax Commands.
- AJAXFormPageCacheTestCase::testSimpleAJAXFormValue in modules/
simpletest/ tests/ ajax.test - Create a simple form, then POST to system/ajax to change to it.
- AJAXFormValuesTestCase::testSimpleAJAXFormValue in modules/
simpletest/ tests/ ajax.test - Create a simple form, then POST to system/ajax to change to it.
- AJAXFrameworkTestCase::testAJAXRender in modules/
simpletest/ tests/ ajax.test - Test that ajax_render() returns JavaScript settings generated during the page request.
- AJAXFrameworkTestCase::testAJAXRenderError in modules/
simpletest/ tests/ ajax.test - Test behavior of ajax_render_error().
File
- modules/
simpletest/ tests/ ajax.test, line 36
Class
Code
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://www.php.net/manual/en/language.operators.array.php).
if (array_intersect_key($command, $needle) == $needle) {
$found = TRUE;
break;
}
}
$this
->assertTrue($found, $message);
}