CommandsTest.php 6.81 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\system\Tests\Ajax\CommandsTest.
6 7 8 9
 */

namespace Drupal\system\Tests\Ajax;

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use Drupal\Core\Ajax\AddCssCommand;
use Drupal\Core\Ajax\AfterCommand;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\BeforeCommand;
use Drupal\Core\Ajax\ChangedCommand;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\DataCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\InsertCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\RestripeCommand;
use Drupal\Core\Ajax\SettingsCommand;

26 27 28 29 30 31 32 33 34 35 36 37 38
/**
 * Tests Ajax framework commands.
 */
class CommandsTest extends AjaxTestBase {
  public static function getInfo() {
    return array(
      'name' => 'AJAX commands',
      'description' => 'Performs tests on AJAX framework commands.',
      'group' => 'AJAX',
    );
  }

  /**
39
   * Tests the various Ajax Commands.
40 41 42 43 44 45 46 47
   */
  function testAjaxCommands() {
    $form_path = 'ajax_forms_test_ajax_commands_form';
    $web_user = $this->drupalCreateUser(array('access content'));
    $this->drupalLogin($web_user);

    $edit = array();

48 49 50 51 52
    // Tests the 'add_css' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'add_css' command")));
    $expected = new AddCssCommand('my/file.css');
    $this->assertCommand($commands, $expected->render(), "'add_css' AJAX command issued with correct data.");

53 54
    // Tests the 'after' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'After': Click to put something after the div")));
55 56
    $expected = new AfterCommand('#after_div', 'This will be placed after');
    $this->assertCommand($commands, $expected->render(), "'after' AJAX command issued with correct data.");
57 58 59

    // Tests the 'alert' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'Alert': Click to alert")));
60 61
    $expected = new AlertCommand(t('Alert'));
    $this->assertCommand($commands, $expected->render(), "'alert' AJAX Command issued with correct text.");
62 63 64

    // Tests the 'append' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'Append': Click to append something")));
65 66
    $expected = new AppendCommand('#append_div', 'Appended text');
    $this->assertCommand($commands, $expected->render(), "'append' AJAX command issued with correct data.");
67 68 69

    // Tests the 'before' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'before': Click to put something before the div")));
70 71
    $expected = new BeforeCommand('#before_div', 'Before text');
    $this->assertCommand($commands, $expected->render(), "'before' AJAX command issued with correct data.");
72 73 74

    // Tests the 'changed' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX changed: Click to mark div changed.")));
75 76
    $expected = new ChangedCommand('#changed_div');
    $this->assertCommand($commands, $expected->render(), "'changed' AJAX command issued with correct selector.");
77 78 79

    // Tests the 'changed' command using the second argument.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX changed: Click to mark div changed with asterisk.")));
80 81
    $expected = new ChangedCommand('#changed_div', '#changed_div_mark_this');
    $this->assertCommand($commands, $expected->render(), "'changed' AJAX command (with asterisk) issued with correct selector.");
82 83 84

    // Tests the 'css' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("Set the the '#box' div to be blue.")));
85 86
    $expected = new CssCommand('#css_div', array('background-color' => 'blue'));
    $this->assertCommand($commands, $expected->render(), "'css' AJAX command issued with correct selector.");
87 88 89

    // Tests the 'data' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX data command: Issue command.")));
90 91
    $expected = new DataCommand('#data_div', 'testkey', 'testvalue');
    $this->assertCommand($commands, $expected->render(), "'data' AJAX command issued with correct key and value.");
92 93 94

    // Tests the 'html' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX html: Replace the HTML in a selector.")));
95 96
    $expected = new HtmlCommand('#html_div', 'replacement text');
    $this->assertCommand($commands, $expected->render(), "'html' AJAX command issued with correct data.");
97 98 99

    // Tests the 'insert' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX insert: Let client insert based on #ajax['method'].")));
100 101 102 103 104 105 106
    $expected = new InsertCommand('#insert_div', 'insert replacement text');
    $this->assertCommand($commands, $expected->render(), "'insert' AJAX command issued with correct data.");

    // Tests the 'invoke' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX invoke command: Invoke addClass() method.")));
    $expected = new InvokeCommand('#invoke_div', 'addClass', array('error'));
    $this->assertCommand($commands, $expected->render(), "'invoke' AJAX command issued with correct method and argument.");
107 108 109

    // Tests the 'prepend' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'prepend': Click to prepend something")));
110 111
    $expected = new PrependCommand('#prepend_div', 'prepended text');
    $this->assertCommand($commands, $expected->render(), "'prepend' AJAX command issued with correct data.");
112 113 114

    // Tests the 'remove' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'remove': Click to remove text")));
115 116
    $expected = new RemoveCommand('#remove_text');
    $this->assertCommand($commands, $expected->render(), "'remove' AJAX command issued with correct command and selector.");
117 118 119

    // Tests the 'restripe' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'restripe' command")));
120 121
    $expected = new RestripeCommand('#restripe_table');
    $this->assertCommand($commands, $expected->render(), "'restripe' AJAX command issued with correct selector.");
122 123 124

    // Tests the 'settings' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'settings' command")));
125 126
    $expected = new SettingsCommand(array('ajax_forms_test' => array('foo' => 42)));
    $this->assertCommand($commands, $expected->render(), "'settings' AJAX command issued with correct data.");
127

128 129 130 131
    // Test that the settings command merges settings properly.
    $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX 'settings' command with setting merging")));
    $expected = new SettingsCommand(array('ajax_forms_test' => array('foo' => 9001)), TRUE);
    $this->assertCommand($commands, $expected->render(), "'settings' AJAX command with setting merging.");
132 133
  }
}