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