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