ajax.test 8.78 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

class AJAXTestCase extends DrupalWebTestCase {
  function setUp() {
6
    parent::setUp('ajax_test', 'ajax_forms_test');
7
  }
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  /**
   * Returns the passed-in commands array without the initial settings command.
   *
   * Depending on factors that may be irrelevant to a particular test,
   * ajax_render() may prepend a settings command. This function allows the test
   * to only have to concern itself with the commands that were passed to
   * ajax_render().
   */
  protected function discardSettings($commands) {
    if ($commands[0]['command'] == 'settings') {
      array_shift($commands);
    }
    return $commands;
  }
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
}

/**
 * Tests primary AJAX framework functions.
 */
class AJAXFrameworkTestCase extends AJAXTestCase {
  function getInfo() {
    return array(
      'name' => 'AJAX framework',
      'description' => 'Performs tests on AJAX framework functions.',
      'group' => 'AJAX',
    );
  }

  /**
   * Test proper passing of JavaScript settings via ajax_render().
   */
  function testAJAXRender() {
    $result = $this->drupalGetAJAX('ajax-test/render');
    // Verify that JavaScript settings are contained (always first).
    $this->assertIdentical($result[0]['command'], 'settings', t('drupal_add_js() settings are contained first.'));
    // Verify that basePath is contained in JavaScript settings.
    $this->assertEqual($result[0]['settings']['basePath'], base_path(), t('Base path is contained in JavaScript settings.'));
  }

  /**
   * Test behavior of ajax_render_error().
   */
  function testAJAXRenderError() {
    $result = $this->drupalGetAJAX('ajax-test/render-error');
    // Verify default error message.
    $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.'));
    // Verify custom error message.
    $edit = array(
      'message' => 'Custom error message.',
    );
60
    $result = $this->drupalGetAJAX('ajax-test/render-error', array('query' => $edit));
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    $this->assertEqual($result[0]['text'], $edit['message'], t('Custom error message is output.'));
  }
}

/**
 * Tests AJAX framework commands.
 */
class AJAXCommandsTestCase extends AJAXTestCase {
  function getInfo() {
    return array(
      'name' => 'AJAX commands',
      'description' => 'Performs tests on AJAX framework commands.',
      'group' => 'AJAX',
    );
  }

  /**
   * Test ajax_command_settings().
   */
  function testAJAXRender() {
    $commands = array();
    $commands[] = ajax_command_settings(array('foo' => 42));
83
    $result = $this->drupalGetAJAX('ajax-test/render', array('query' => array('commands' => $commands)));
84 85 86 87 88 89
    // Verify that JavaScript settings are contained (always first).
    $this->assertIdentical($result[0]['command'], 'settings', t('drupal_add_js() settings are contained first.'));
    // Verify that the custom setting is contained.
    $this->assertEqual($result[1]['settings']['foo'], 42, t('Custom setting is output.'));
  }

90 91 92 93 94 95 96 97 98 99 100
  /**
   * Test the various AJAX Commands.
   */
  function testAJAXCommands() {
    $form_path = 'ajax_forms_test_ajax_commands_form';
    $web_user = $this->drupalCreateUser(array('access content'));
    $this->drupalLogin($web_user);

    $edit = array();

    // Tests the 'after' command.
101 102
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'after_command_example'));
    $command = $commands[0];
103 104 105
    $this->assertTrue($command['command'] == 'insert' && $command['method'] == 'after' && $command['data'] == 'This will be placed after', "'after' AJAX command issued with correct data");

    // Tests the 'alert' command.
106 107
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'alert_command_example'));
    $command = $commands[0];
108 109 110
    $this->assertTrue($command['command'] == 'alert' && $command['text'] == 'Alert', "'alert' AJAX Command issued with correct text");

    // Tests the 'append' command.
111 112
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'append_command_example'));
    $command = $commands[0];
113 114 115
    $this->assertTrue($command['command'] == 'insert' && $command['method'] == 'append' && $command['data'] == 'Appended text', "'append' AJAX command issued with correct data");

    // Tests the 'before' command.
116 117
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'before_command_example'));
    $command = $commands[0];
118 119 120
    $this->assertTrue($command['command'] == 'insert' && $command['method'] == 'before' && $command['data'] == 'Before text', "'before' AJAX command issued with correct data");

    // Tests the 'changed' command.
121 122
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'changed_command_example'));
    $command = $commands[0];
123 124
    $this->assertTrue($command['command'] == 'changed' && $command['selector'] == '#changed_div', "'changed' AJAX command issued with correct selector");

125
    // Tests the 'changed' command using the second argument.
126 127
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'changed_command_asterisk_example'));
    $command = $commands[0];
128 129
    $this->assertTrue($command['command'] == 'changed' && $command['selector'] == '#changed_div' && $command['asterisk'] == '#changed_div_mark_this', "'changed' AJAX command (with asterisk) issued with correct selector");

130
    // Tests the 'css' command.
131 132
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'css_command_example'));
    $command = $commands[0];
133
    $this->assertTrue($command['command'] == 'css' && $command['selector'] == '#css_div' && $command['argument']['background-color'] == 'blue', "'css' AJAX command issued with correct selector");
134 135

    // Tests the 'data' command.
136 137
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'data_command_example'));
    $command = $commands[0];
138 139 140
    $this->assertTrue($command['command'] == 'data' && $command['name'] == 'testkey' && $command['value'] == 'testvalue', "'data' AJAX command issued with correct key and value");

    // Tests the 'html' command.
141 142
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'html_command_example'));
    $command = $commands[0];
143 144 145
    $this->assertTrue($command['command'] == 'insert' && $command['method'] == 'html' && $command['data'] == 'replacement text', "'html' AJAX command issued with correct data");

    // Tests the 'prepend' command.
146 147
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'prepend_command_example'));
    $command = $commands[0];
148 149 150
    $this->assertTrue($command['command'] == 'insert' && $command['method'] == 'prepend' && $command['data'] == 'prepended text', "'prepend' AJAX command issued with correct data");

    // Tests the 'remove' command.
151 152
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'remove_command_example'));
    $command = $commands[0];
153 154 155
    $this->assertTrue($command['command'] == 'remove' && $command['selector'] == '#remove_text', "'remove' AJAX command issued with correct command and selector");

    // Tests the 'restripe' command.
156 157
    $commands = $this->discardSettings($this->drupalPostAJAX($form_path, $edit, 'restripe_command_example'));
    $command = $commands[0];
158 159 160
    $this->assertTrue($command['command'] == 'restripe' && $command['selector'] == '#restripe_table', "'restripe' AJAX command issued with correct selector");
  }
}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

/**
 * Test that $form_state['values'] is properly delivered to $ajax['callback'].
 */
class AJAXFormValuesTestCase extends AJAXTestCase {
  public static function getInfo() {
    return array(
      'name' => 'AJAX command form values',
      'description' => 'Tests that form values are properly delivered to AJAX callbacks.',
      'group' => 'AJAX',
    );
  }

  function setUp() {
    parent::setUp();

    $this->web_user = $this->drupalCreateUser(array('access content'));
    $this->drupalLogin($this->web_user);
  }

  /**
   * Create a simple form, then POST to system/ajax to change to it.
   */
  function testSimpleAJAXFormValue() {
    // Verify form values of a select element.
    foreach(array('red', 'green', 'blue') as $item) {
      $edit = array(
        'select' => $item,
      );
190 191
      $commands = $this->discardSettings($this->drupalPostAJAX('ajax_forms_test_get_form', $edit, 'select'));
      $data_command = $commands[1];
192 193 194 195 196 197 198 199
      $this->assertEqual($data_command['value'], $item);
    }

    // Verify form values of a checkbox element.
    foreach(array(FALSE, TRUE) as $item) {
      $edit = array(
        'checkbox' => $item,
      );
200 201
      $commands = $this->discardSettings($this->drupalPostAJAX('ajax_forms_test_get_form', $edit, 'checkbox'));
      $data_command = $commands[1];
202 203 204 205
      $this->assertEqual((int) $data_command['value'], (int) $item);
    }
  }
}