summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/ajax.test
blob: 3a55dbb971451ab3ccc6ea5ded00cc4ab49787ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
// $Id$

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);
  }
}

/**
 * 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.',
    );
    $result = $this->drupalGetAJAX('ajax-test/render-error', $edit);
    $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));
    $result = $this->drupalGetAJAX('ajax-test/render', array('commands' => $commands));
    // 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.'));
  }
}