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

namespace Drupal\system\Tests\Ajax;

5 6
use Drupal\Core\Ajax\AddCssCommand;
use Drupal\Core\Ajax\AfterCommand;
7
use Drupal\Core\Ajax\AjaxResponse;
8 9 10 11 12 13 14 15 16 17 18 19 20
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;
21
use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
22
use Symfony\Component\HttpFoundation\Request;
23 24
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
25

26
/**
27 28 29
 * Performs tests on AJAX framework commands.
 *
 * @group Ajax
30 31
 */
class CommandsTest extends AjaxTestBase {
32

33
  /**
34
   * Tests the various Ajax Commands.
35
   */
36
  public function testAjaxCommands() {
37
    $form_path = 'ajax_forms_test_ajax_commands_form';
38
    $web_user = $this->drupalCreateUser(['access content']);
39 40
    $this->drupalLogin($web_user);

41
    $edit = [];
42

43
    // Tests the 'add_css' command.
44
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'add_css' command")]);
45 46 47
    $expected = new AddCssCommand('my/file.css');
    $this->assertCommand($commands, $expected->render(), "'add_css' AJAX command issued with correct data.");

48
    // Tests the 'after' command.
49
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'After': Click to put something after the div")]);
50 51
    $expected = new AfterCommand('#after_div', 'This will be placed after');
    $this->assertCommand($commands, $expected->render(), "'after' AJAX command issued with correct data.");
52 53

    // Tests the 'alert' command.
54
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'Alert': Click to alert")]);
55 56
    $expected = new AlertCommand(t('Alert'));
    $this->assertCommand($commands, $expected->render(), "'alert' AJAX Command issued with correct text.");
57 58

    // Tests the 'append' command.
59
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'Append': Click to append something")]);
60 61
    $expected = new AppendCommand('#append_div', 'Appended text');
    $this->assertCommand($commands, $expected->render(), "'append' AJAX command issued with correct data.");
62 63

    // Tests the 'before' command.
64
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'before': Click to put something before the div")]);
65 66
    $expected = new BeforeCommand('#before_div', 'Before text');
    $this->assertCommand($commands, $expected->render(), "'before' AJAX command issued with correct data.");
67 68

    // Tests the 'changed' command.
69
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX changed: Click to mark div changed.")]);
70 71
    $expected = new ChangedCommand('#changed_div');
    $this->assertCommand($commands, $expected->render(), "'changed' AJAX command issued with correct selector.");
72 73

    // Tests the 'changed' command using the second argument.
74
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX changed: Click to mark div changed with asterisk.")]);
75 76
    $expected = new ChangedCommand('#changed_div', '#changed_div_mark_this');
    $this->assertCommand($commands, $expected->render(), "'changed' AJAX command (with asterisk) issued with correct selector.");
77 78

    // Tests the 'css' command.
79 80
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("Set the '#box' div to be blue.")]);
    $expected = new CssCommand('#css_div', ['background-color' => 'blue']);
81
    $this->assertCommand($commands, $expected->render(), "'css' AJAX command issued with correct selector.");
82 83

    // Tests the 'data' command.
84
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX data command: Issue command.")]);
85 86
    $expected = new DataCommand('#data_div', 'testkey', 'testvalue');
    $this->assertCommand($commands, $expected->render(), "'data' AJAX command issued with correct key and value.");
87 88

    // Tests the 'html' command.
89
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX html: Replace the HTML in a selector.")]);
90 91
    $expected = new HtmlCommand('#html_div', 'replacement text');
    $this->assertCommand($commands, $expected->render(), "'html' AJAX command issued with correct data.");
92 93

    // Tests the 'insert' command.
94
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX insert: Let client insert based on #ajax['method'].")]);
95 96 97 98
    $expected = new InsertCommand('#insert_div', 'insert replacement text');
    $this->assertCommand($commands, $expected->render(), "'insert' AJAX command issued with correct data.");

    // Tests the 'invoke' command.
99 100
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX invoke command: Invoke addClass() method.")]);
    $expected = new InvokeCommand('#invoke_div', 'addClass', ['error']);
101
    $this->assertCommand($commands, $expected->render(), "'invoke' AJAX command issued with correct method and argument.");
102 103

    // Tests the 'prepend' command.
104
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'prepend': Click to prepend something")]);
105 106
    $expected = new PrependCommand('#prepend_div', 'prepended text');
    $this->assertCommand($commands, $expected->render(), "'prepend' AJAX command issued with correct data.");
107 108

    // Tests the 'remove' command.
109
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'remove': Click to remove text")]);
110 111
    $expected = new RemoveCommand('#remove_text');
    $this->assertCommand($commands, $expected->render(), "'remove' AJAX command issued with correct command and selector.");
112 113

    // Tests the 'restripe' command.
114
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'restripe' command")]);
115 116
    $expected = new RestripeCommand('#restripe_table');
    $this->assertCommand($commands, $expected->render(), "'restripe' AJAX command issued with correct selector.");
117 118

    // Tests the 'settings' command.
119 120
    $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'settings' command")]);
    $expected = new SettingsCommand(['ajax_forms_test' => ['foo' => 42]]);
121
    $this->assertCommand($commands, $expected->render(), "'settings' AJAX command issued with correct data.");
122
  }
123

124 125 126 127
  /**
   * Regression test: Settings command exists regardless of JS aggregation.
   */
  public function testAttachedSettings() {
128
    $assert = function ($message) {
129 130 131 132 133 134
      $response = new AjaxResponse();
      $response->setAttachments([
        'library' => ['core/drupalSettings'],
        'drupalSettings' => ['foo' => 'bar'],
      ]);

135 136 137 138 139 140 141 142 143
      $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
      $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
      $event = new FilterResponseEvent(
        \Drupal::service('http_kernel'),
        new Request(),
        HttpKernelInterface::MASTER_REQUEST,
        $response
      );
      $subscriber->onResponse($event);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      $expected = [
        'command' => 'settings',
      ];
      $this->assertCommand($response->getCommands(), $expected, $message);
    };

    $config = $this->config('system.performance');

    $config->set('js.preprocess', FALSE)->save();
    $assert('Settings command exists when JS aggregation is disabled.');

    $config->set('js.preprocess', TRUE)->save();
    $assert('Settings command exists when JS aggregation is enabled.');
  }

159
}