View source
<?php
class AJAXTestCase extends DrupalWebTestCase {
function setUp() {
parent::setUp('ajax_test');
}
function drupalGetAJAX($path, $query = array()) {
$this
->drupalGet($path, array(
'query' => $query,
));
return json_decode($this->content, TRUE);
}
}
class AJAXFrameworkTestCase extends AJAXTestCase {
function getInfo() {
return array(
'name' => 'AJAX framework',
'description' => 'Performs tests on AJAX framework functions.',
'group' => 'AJAX',
);
}
function testAJAXRender() {
$result = $this
->drupalGetAJAX('ajax-test/render');
$this
->assertIdentical($result[0]['command'], 'settings', t('drupal_add_js() settings are contained first.'));
$this
->assertEqual($result[0]['settings']['basePath'], base_path(), t('Base path is contained in JavaScript settings.'));
}
function testAJAXRenderError() {
$result = $this
->drupalGetAJAX('ajax-test/render-error');
$this
->assertEqual($result[0]['command'], 'alert', t('ajax_render_error() invokes alert command.'));
$this
->assertEqual($result[0]['text'], t('An error occurred while handling the request: The server received invalid input.'), t('Default error message is output.'));
$edit = array(
'message' => 'Custom error message.',
);
$result = $this
->drupalGetAJAX('ajax-test/render-error', $edit);
$this
->assertEqual($result[0]['text'], $edit['message'], t('Custom error message is output.'));
}
}
class AJAXCommandsTestCase extends AJAXTestCase {
function getInfo() {
return array(
'name' => 'AJAX commands',
'description' => 'Performs tests on AJAX framework commands.',
'group' => 'AJAX',
);
}
function testAJAXRender() {
$commands = array();
$commands[] = ajax_command_settings(array(
'foo' => 42,
));
$result = $this
->drupalGetAJAX('ajax-test/render', array(
'commands' => $commands,
));
$this
->assertIdentical($result[0]['command'], 'settings', t('drupal_add_js() settings are contained first.'));
$this
->assertEqual($result[1]['settings']['foo'], 42, t('Custom setting is output.'));
}
}