6ae97ae1341aad9dcf403595bbadced297d25632
[yaffs-website] / web / core / modules / system / src / Tests / Ajax / CommandsTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Ajax;
4
5 use Drupal\Core\Ajax\AddCssCommand;
6 use Drupal\Core\Ajax\AfterCommand;
7 use Drupal\Core\Ajax\AjaxResponse;
8 use Drupal\Core\Ajax\AlertCommand;
9 use Drupal\Core\Ajax\AppendCommand;
10 use Drupal\Core\Ajax\BeforeCommand;
11 use Drupal\Core\Ajax\ChangedCommand;
12 use Drupal\Core\Ajax\CssCommand;
13 use Drupal\Core\Ajax\DataCommand;
14 use Drupal\Core\Ajax\HtmlCommand;
15 use Drupal\Core\Ajax\InvokeCommand;
16 use Drupal\Core\Ajax\InsertCommand;
17 use Drupal\Core\Ajax\PrependCommand;
18 use Drupal\Core\Ajax\RemoveCommand;
19 use Drupal\Core\Ajax\RestripeCommand;
20 use Drupal\Core\Ajax\SettingsCommand;
21 use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
22 use Symfony\Component\HttpFoundation\Request;
23 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
24 use Symfony\Component\HttpKernel\HttpKernelInterface;
25
26 /**
27  * Performs tests on AJAX framework commands.
28  *
29  * @group Ajax
30  */
31 class CommandsTest extends AjaxTestBase {
32   /**
33    * Tests the various Ajax Commands.
34    */
35   public function testAjaxCommands() {
36     $form_path = 'ajax_forms_test_ajax_commands_form';
37     $web_user = $this->drupalCreateUser(['access content']);
38     $this->drupalLogin($web_user);
39
40     $edit = [];
41
42     // Tests the 'add_css' command.
43     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'add_css' command")]);
44     $expected = new AddCssCommand('my/file.css');
45     $this->assertCommand($commands, $expected->render(), "'add_css' AJAX command issued with correct data.");
46
47     // Tests the 'after' command.
48     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'After': Click to put something after the div")]);
49     $expected = new AfterCommand('#after_div', 'This will be placed after');
50     $this->assertCommand($commands, $expected->render(), "'after' AJAX command issued with correct data.");
51
52     // Tests the 'alert' command.
53     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'Alert': Click to alert")]);
54     $expected = new AlertCommand(t('Alert'));
55     $this->assertCommand($commands, $expected->render(), "'alert' AJAX Command issued with correct text.");
56
57     // Tests the 'append' command.
58     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'Append': Click to append something")]);
59     $expected = new AppendCommand('#append_div', 'Appended text');
60     $this->assertCommand($commands, $expected->render(), "'append' AJAX command issued with correct data.");
61
62     // Tests the 'before' command.
63     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'before': Click to put something before the div")]);
64     $expected = new BeforeCommand('#before_div', 'Before text');
65     $this->assertCommand($commands, $expected->render(), "'before' AJAX command issued with correct data.");
66
67     // Tests the 'changed' command.
68     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX changed: Click to mark div changed.")]);
69     $expected = new ChangedCommand('#changed_div');
70     $this->assertCommand($commands, $expected->render(), "'changed' AJAX command issued with correct selector.");
71
72     // Tests the 'changed' command using the second argument.
73     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX changed: Click to mark div changed with asterisk.")]);
74     $expected = new ChangedCommand('#changed_div', '#changed_div_mark_this');
75     $this->assertCommand($commands, $expected->render(), "'changed' AJAX command (with asterisk) issued with correct selector.");
76
77     // Tests the 'css' command.
78     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("Set the '#box' div to be blue.")]);
79     $expected = new CssCommand('#css_div', ['background-color' => 'blue']);
80     $this->assertCommand($commands, $expected->render(), "'css' AJAX command issued with correct selector.");
81
82     // Tests the 'data' command.
83     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX data command: Issue command.")]);
84     $expected = new DataCommand('#data_div', 'testkey', 'testvalue');
85     $this->assertCommand($commands, $expected->render(), "'data' AJAX command issued with correct key and value.");
86
87     // Tests the 'html' command.
88     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX html: Replace the HTML in a selector.")]);
89     $expected = new HtmlCommand('#html_div', 'replacement text');
90     $this->assertCommand($commands, $expected->render(), "'html' AJAX command issued with correct data.");
91
92     // Tests the 'insert' command.
93     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX insert: Let client insert based on #ajax['method'].")]);
94     $expected = new InsertCommand('#insert_div', 'insert replacement text');
95     $this->assertCommand($commands, $expected->render(), "'insert' AJAX command issued with correct data.");
96
97     // Tests the 'invoke' command.
98     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX invoke command: Invoke addClass() method.")]);
99     $expected = new InvokeCommand('#invoke_div', 'addClass', ['error']);
100     $this->assertCommand($commands, $expected->render(), "'invoke' AJAX command issued with correct method and argument.");
101
102     // Tests the 'prepend' command.
103     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'prepend': Click to prepend something")]);
104     $expected = new PrependCommand('#prepend_div', 'prepended text');
105     $this->assertCommand($commands, $expected->render(), "'prepend' AJAX command issued with correct data.");
106
107     // Tests the 'remove' command.
108     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'remove': Click to remove text")]);
109     $expected = new RemoveCommand('#remove_text');
110     $this->assertCommand($commands, $expected->render(), "'remove' AJAX command issued with correct command and selector.");
111
112     // Tests the 'restripe' command.
113     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'restripe' command")]);
114     $expected = new RestripeCommand('#restripe_table');
115     $this->assertCommand($commands, $expected->render(), "'restripe' AJAX command issued with correct selector.");
116
117     // Tests the 'settings' command.
118     $commands = $this->drupalPostAjaxForm($form_path, $edit, ['op' => t("AJAX 'settings' command")]);
119     $expected = new SettingsCommand(['ajax_forms_test' => ['foo' => 42]]);
120     $this->assertCommand($commands, $expected->render(), "'settings' AJAX command issued with correct data.");
121   }
122
123   /**
124    * Regression test: Settings command exists regardless of JS aggregation.
125    */
126   public function testAttachedSettings() {
127     $assert = function ($message) {
128       $response = new AjaxResponse();
129       $response->setAttachments([
130         'library' => ['core/drupalSettings'],
131         'drupalSettings' => ['foo' => 'bar'],
132       ]);
133
134       $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
135       $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
136       $event = new FilterResponseEvent(
137         \Drupal::service('http_kernel'),
138         new Request(),
139         HttpKernelInterface::MASTER_REQUEST,
140         $response
141       );
142       $subscriber->onResponse($event);
143       $expected = [
144         'command' => 'settings',
145       ];
146       $this->assertCommand($response->getCommands(), $expected, $message);
147     };
148
149     $config = $this->config('system.performance');
150
151     $config->set('js.preprocess', FALSE)->save();
152     $assert('Settings command exists when JS aggregation is disabled.');
153
154     $config->set('js.preprocess', TRUE)->save();
155     $assert('Settings command exists when JS aggregation is enabled.');
156   }
157
158 }