Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Ajax / AjaxCommandsTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Ajax;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\Ajax\AddCssCommand;
7 use Drupal\Core\Ajax\AfterCommand;
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\InsertCommand;
16 use Drupal\Core\Ajax\InvokeCommand;
17 use Drupal\Core\Ajax\PrependCommand;
18 use Drupal\Core\Ajax\RemoveCommand;
19 use Drupal\Core\Ajax\ReplaceCommand;
20 use Drupal\Core\Ajax\RestripeCommand;
21 use Drupal\Core\Ajax\SettingsCommand;
22 use Drupal\Core\Ajax\CloseDialogCommand;
23 use Drupal\Core\Ajax\CloseModalDialogCommand;
24 use Drupal\Core\Ajax\SetDialogOptionCommand;
25 use Drupal\Core\Ajax\SetDialogTitleCommand;
26 use Drupal\Core\Ajax\RedirectCommand;
27 use Drupal\Core\Ajax\UpdateBuildIdCommand;
28 use Drupal\Core\Ajax\OpenDialogCommand;
29
30 /**
31  * Test coverage for various classes in the \Drupal\Core\Ajax namespace.
32  *
33  * @group Ajax
34  */
35 class AjaxCommandsTest extends UnitTestCase {
36
37   /**
38    * @covers \Drupal\Core\Ajax\AddCssCommand
39    */
40   public function testAddCssCommand() {
41     $command = new AddCssCommand('p{ text-decoration:blink; }');
42
43     $expected = [
44       'command' => 'add_css',
45       'data' => 'p{ text-decoration:blink; }',
46     ];
47
48     $this->assertEquals($expected, $command->render());
49   }
50
51   /**
52    * @covers \Drupal\Core\Ajax\AfterCommand
53    */
54   public function testAfterCommand() {
55     $command = new AfterCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);
56
57     $expected = [
58       'command' => 'insert',
59       'method' => 'after',
60       'selector' => '#page-title',
61       'data' => '<p>New Text!</p>',
62       'settings' => ['my-setting' => 'setting'],
63     ];
64
65     $this->assertEquals($expected, $command->render());
66   }
67
68   /**
69    * @covers \Drupal\Core\Ajax\AlertCommand
70    */
71   public function testAlertCommand() {
72     $command = new AlertCommand('Set condition 1 throughout the ship!');
73     $expected = [
74       'command' => 'alert',
75       'text' => 'Set condition 1 throughout the ship!',
76     ];
77
78     $this->assertEquals($expected, $command->render());
79   }
80
81   /**
82    * @covers \Drupal\Core\Ajax\AppendCommand
83    */
84   public function testAppendCommand() {
85     $command = new AppendCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);
86
87     $expected = [
88       'command' => 'insert',
89       'method' => 'append',
90       'selector' => '#page-title',
91       'data' => '<p>New Text!</p>',
92       'settings' => ['my-setting' => 'setting'],
93     ];
94
95     $this->assertEquals($expected, $command->render());
96   }
97
98   /**
99    * @covers \Drupal\Core\Ajax\BeforeCommand
100    */
101   public function testBeforeCommand() {
102     $command = new BeforeCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);
103
104     $expected = [
105       'command' => 'insert',
106       'method' => 'before',
107       'selector' => '#page-title',
108       'data' => '<p>New Text!</p>',
109       'settings' => ['my-setting' => 'setting'],
110     ];
111
112     $this->assertEquals($expected, $command->render());
113   }
114
115   /**
116    * @covers \Drupal\Core\Ajax\ChangedCommand
117    */
118   public function testChangedCommand() {
119     $command = new ChangedCommand('#page-title', '#page-title-changed');
120
121     $expected = [
122       'command' => 'changed',
123       'selector' => '#page-title',
124       'asterisk' => '#page-title-changed',
125     ];
126
127     $this->assertEquals($expected, $command->render());
128   }
129
130   /**
131    * @covers \Drupal\Core\Ajax\CssCommand
132    */
133   public function testCssCommand() {
134     $command = new CssCommand('#page-title', ['text-decoration' => 'blink']);
135     $command->setProperty('font-size', '40px')->setProperty('font-weight', 'bold');
136
137     $expected = [
138       'command' => 'css',
139       'selector' => '#page-title',
140       'argument' => [
141         'text-decoration' => 'blink',
142         'font-size' => '40px',
143         'font-weight' => 'bold',
144       ],
145     ];
146
147     $this->assertEquals($expected, $command->render());
148   }
149
150   /**
151    * @covers \Drupal\Core\Ajax\DataCommand
152    */
153   public function testDataCommand() {
154     $command = new DataCommand('#page-title', 'my-data', ['key' => 'value']);
155
156     $expected = [
157       'command' => 'data',
158       'selector' => '#page-title',
159       'name' => 'my-data',
160       'value' => ['key' => 'value'],
161     ];
162
163     $this->assertEquals($expected, $command->render());
164   }
165
166   /**
167    * @covers \Drupal\Core\Ajax\HtmlCommand
168    */
169   public function testHtmlCommand() {
170     $command = new HtmlCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);
171
172     $expected = [
173       'command' => 'insert',
174       'method' => 'html',
175       'selector' => '#page-title',
176       'data' => '<p>New Text!</p>',
177       'settings' => ['my-setting' => 'setting'],
178     ];
179
180     $this->assertEquals($expected, $command->render());
181   }
182
183   /**
184    * @covers \Drupal\Core\Ajax\InsertCommand
185    */
186   public function testInsertCommand() {
187     $command = new InsertCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);
188
189     $expected = [
190       'command' => 'insert',
191       'method' => NULL,
192       'selector' => '#page-title',
193       'data' => '<p>New Text!</p>',
194       'settings' => ['my-setting' => 'setting'],
195     ];
196
197     $this->assertEquals($expected, $command->render());
198   }
199
200   /**
201    * @covers \Drupal\Core\Ajax\InvokeCommand
202    */
203   public function testInvokeCommand() {
204     $command = new InvokeCommand('#page-title', 'myMethod', ['var1', 'var2']);
205
206     $expected = [
207       'command' => 'invoke',
208       'selector' => '#page-title',
209       'method' => 'myMethod',
210       'args' => ['var1', 'var2'],
211     ];
212
213     $this->assertEquals($expected, $command->render());
214   }
215
216   /**
217    * @covers \Drupal\Core\Ajax\PrependCommand
218    */
219   public function testPrependCommand() {
220     $command = new PrependCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);
221
222     $expected = [
223       'command' => 'insert',
224       'method' => 'prepend',
225       'selector' => '#page-title',
226       'data' => '<p>New Text!</p>',
227       'settings' => ['my-setting' => 'setting'],
228     ];
229
230     $this->assertEquals($expected, $command->render());
231   }
232
233   /**
234    * @covers \Drupal\Core\Ajax\RemoveCommand
235    */
236   public function testRemoveCommand() {
237     $command = new RemoveCommand('#page-title');
238
239     $expected = [
240       'command' => 'remove',
241       'selector' => '#page-title',
242     ];
243
244     $this->assertEquals($expected, $command->render());
245   }
246
247   /**
248    * @covers \Drupal\Core\Ajax\ReplaceCommand
249    */
250   public function testReplaceCommand() {
251     $command = new ReplaceCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);
252
253     $expected = [
254       'command' => 'insert',
255       'method' => 'replaceWith',
256       'selector' => '#page-title',
257       'data' => '<p>New Text!</p>',
258       'settings' => ['my-setting' => 'setting'],
259     ];
260
261     $this->assertEquals($expected, $command->render());
262   }
263
264   /**
265    * @covers \Drupal\Core\Ajax\RestripeCommand
266    */
267   public function testRestripeCommand() {
268     $command = new RestripeCommand('#page-title');
269
270     $expected = [
271       'command' => 'restripe',
272       'selector' => '#page-title',
273     ];
274
275     $this->assertEquals($expected, $command->render());
276   }
277
278   /**
279    * @covers \Drupal\Core\Ajax\SettingsCommand
280    */
281   public function testSettingsCommand() {
282     $command = new SettingsCommand(['key' => 'value'], TRUE);
283
284     $expected = [
285       'command' => 'settings',
286       'settings' => ['key' => 'value'],
287       'merge' => TRUE,
288     ];
289
290     $this->assertEquals($expected, $command->render());
291   }
292
293   /**
294    * @covers \Drupal\Core\Ajax\OpenDialogCommand
295    */
296   public function testOpenDialogCommand() {
297     $command = new OpenDialogCommand('#some-dialog', 'Title', '<p>Text!</p>', [
298       'url' => FALSE,
299       'width' => 500,
300     ]);
301
302     $expected = [
303       'command' => 'openDialog',
304       'selector' => '#some-dialog',
305       'settings' => NULL,
306       'data' => '<p>Text!</p>',
307       'dialogOptions' => [
308         'url' => FALSE,
309         'width' => 500,
310         'title' => 'Title',
311         'modal' => FALSE,
312       ],
313     ];
314     $this->assertEquals($expected, $command->render());
315
316     $command->setDialogTitle('New title');
317     $expected['dialogOptions']['title'] = 'New title';
318     $this->assertEquals($expected, $command->render());
319   }
320
321   /**
322    * @covers \Drupal\Core\Ajax\OpenModalDialogCommand
323    */
324   public function testOpenModalDialogCommand() {
325     $command = $this->getMockBuilder('Drupal\Core\Ajax\OpenModalDialogCommand')
326       ->setConstructorArgs([
327         'Title', '<p>Text!</p>', [
328           'url' => 'example',
329           'width' => 500,
330         ],
331       ])
332       ->setMethods(['getRenderedContent'])
333       ->getMock();
334
335     // This method calls the render service, which isn't available. We want it
336     // to do nothing so we mock it to return a known value.
337     $command->expects($this->once())
338       ->method('getRenderedContent')
339       ->willReturn('rendered content');
340
341     $expected = [
342       'command' => 'openDialog',
343       'selector' => '#drupal-modal',
344       'settings' => NULL,
345       'data' => 'rendered content',
346       'dialogOptions' => [
347         'url' => 'example',
348         'width' => 500,
349         'title' => 'Title',
350         'modal' => TRUE,
351       ],
352     ];
353     $this->assertEquals($expected, $command->render());
354   }
355
356   /**
357    * @covers \Drupal\Core\Ajax\CloseModalDialogCommand
358    */
359   public function testCloseModalDialogCommand() {
360     $command = new CloseModalDialogCommand();
361     $expected = [
362       'command' => 'closeDialog',
363       'selector' => '#drupal-modal',
364       'persist' => FALSE,
365     ];
366
367     $this->assertEquals($expected, $command->render());
368   }
369
370   /**
371    * @covers \Drupal\Core\Ajax\CloseDialogCommand
372    */
373   public function testCloseDialogCommand() {
374     $command = new CloseDialogCommand('#some-dialog', TRUE);
375     $expected = [
376       'command' => 'closeDialog',
377       'selector' => '#some-dialog',
378       'persist' => TRUE,
379     ];
380
381     $this->assertEquals($expected, $command->render());
382   }
383
384   /**
385    * @covers \Drupal\Core\Ajax\SetDialogOptionCommand
386    */
387   public function testSetDialogOptionCommand() {
388     $command = new SetDialogOptionCommand('#some-dialog', 'width', '500');
389     $expected = [
390       'command' => 'setDialogOption',
391       'selector' => '#some-dialog',
392       'optionName' => 'width',
393       'optionValue' => '500',
394     ];
395
396     $this->assertEquals($expected, $command->render());
397   }
398
399   /**
400    * @covers \Drupal\Core\Ajax\SetDialogTitleCommand
401    */
402   public function testSetDialogTitleCommand() {
403     $command = new SetDialogTitleCommand('#some-dialog', 'Example');
404     $expected = [
405       'command' => 'setDialogOption',
406       'selector' => '#some-dialog',
407       'optionName' => 'title',
408       'optionValue' => 'Example',
409     ];
410
411     $this->assertEquals($expected, $command->render());
412   }
413
414   /**
415    * @covers \Drupal\Core\Ajax\RedirectCommand
416    */
417   public function testRedirectCommand() {
418     $command = new RedirectCommand('http://example.com');
419     $expected = [
420       'command' => 'redirect',
421       'url' => 'http://example.com',
422     ];
423
424     $this->assertEquals($expected, $command->render());
425   }
426
427   /**
428    * @covers \Drupal\Core\Ajax\UpdateBuildIdCommand
429    */
430   public function testUpdateBuildIdCommand() {
431     $old = 'ThisStringisOld';
432     $new = 'ThisStringIsNew';
433     $command = new UpdateBuildIdCommand($old, $new);
434     $expected = [
435       'command' => 'update_build_id',
436       'old' => $old,
437       'new' => $new,
438     ];
439
440     $this->assertEquals($expected, $command->render());
441   }
442
443 }