0db5e5f74e4deb5b3dd69ab133be555741c454f6
[yaffs-website] / web / core / tests / Drupal / FunctionalJavascriptTests / Ajax / CommandsTest.php
1 <?php
2
3 namespace Drupal\FunctionalJavascriptTests\Ajax;
4
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6
7 /**
8  * Performs tests on AJAX framework commands.
9  *
10  * @group Ajax
11  */
12 class CommandsTest extends WebDriverTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
18
19   /**
20    * Tests the various Ajax Commands.
21    */
22   public function testAjaxCommands() {
23     $session = $this->getSession();
24     $page = $this->getSession()->getPage();
25
26     $form_path = 'ajax_forms_test_ajax_commands_form';
27     $web_user = $this->drupalCreateUser(['access content']);
28     $this->drupalLogin($web_user);
29     $this->drupalGet($form_path);
30
31     // Tests the 'add_css' command.
32     $page->pressButton("AJAX 'add_css' command");
33     $this->assertWaitPageContains('my/file.css');
34
35     // Tests the 'after' command.
36     $page->pressButton("AJAX 'After': Click to put something after the div");
37     $this->assertWaitPageContains('<div id="after_div">Something can be inserted after this</div>This will be placed after');
38
39     // Tests the 'alert' command.
40     $test_alert_command = <<<JS
41 window.alert = function() {
42   document.body.innerHTML += '<div class="alert-command">Alert</div>';
43 };
44 JS;
45     $session->executeScript($test_alert_command);
46     $page->pressButton("AJAX 'Alert': Click to alert");
47     $this->assertWaitPageContains('<div class="alert-command">Alert</div>');
48
49     // Tests the 'append' command.
50     $page->pressButton("AJAX 'Append': Click to append something");
51     $this->assertWaitPageContains('<div id="append_div">Append inside this divAppended text</div>');
52
53     // Tests the 'before' command.
54     $page->pressButton("AJAX 'before': Click to put something before the div");
55     $this->assertWaitPageContains('Before text<div id="before_div">Insert something before this.</div>');
56
57     // Tests the 'changed' command.
58     $page->pressButton("AJAX changed: Click to mark div changed.");
59     $this->assertWaitPageContains('<div id="changed_div" class="ajax-changed">');
60
61     // Tests the 'changed' command using the second argument.
62     // Refresh page for testing 'changed' command to same element again.
63     $this->drupalGet($form_path);
64     $page->pressButton("AJAX changed: Click to mark div changed with asterisk.");
65     $this->assertWaitPageContains('<div id="changed_div" class="ajax-changed"> <div id="changed_div_mark_this">This div can be marked as changed or not. <abbr class="ajax-changed" title="Changed">*</abbr> </div></div>');
66
67     // Tests the 'css' command.
68     $page->pressButton("Set the '#box' div to be blue.");
69     $this->assertWaitPageContains('<div id="css_div" style="background-color: blue;">');
70
71     // Tests the 'data' command.
72     $page->pressButton("AJAX data command: Issue command.");
73     $this->assertTrue($page->waitFor(10, function () use ($session) {
74       return 'testvalue' === $session->evaluateScript('window.jQuery("#data_div").data("testkey")');
75     }));
76
77     // Tests the 'html' command.
78     $page->pressButton("AJAX html: Replace the HTML in a selector.");
79     $this->assertWaitPageContains('<div id="html_div">replacement text</div>');
80
81     // Tests the 'insert' command.
82     $page->pressButton("AJAX insert: Let client insert based on #ajax['method'].");
83     $this->assertWaitPageContains('<div id="insert_div">insert replacement textOriginal contents</div>');
84
85     // Tests the 'invoke' command.
86     $page->pressButton("AJAX invoke command: Invoke addClass() method.");
87     $this->assertWaitPageContains('<div id="invoke_div" class="error">Original contents</div>');
88
89     // Tests the 'prepend' command.
90     $page->pressButton("AJAX 'prepend': Click to prepend something");
91     $this->assertWaitPageContains('<div id="prepend_div">prepended textSomething will be prepended to this div. </div>');
92
93     // Tests the 'remove' command.
94     $page->pressButton("AJAX 'remove': Click to remove text");
95     $this->assertWaitPageContains('<div id="remove_div"></div>');
96
97     // Tests the 'restripe' command.
98     $page->pressButton("AJAX 'restripe' command");
99     $this->assertWaitPageContains('<tr id="table-first" class="odd"><td>first row</td></tr>');
100     $this->assertWaitPageContains('<tr class="even"><td>second row</td></tr>');
101
102     // Tests the 'settings' command.
103     $test_settings_command = <<<JS
104 Drupal.behaviors.testSettingsCommand = {
105   attach: function (context, settings) {
106     window.jQuery('body').append('<div class="test-settings-command">' + settings.ajax_forms_test.foo + '</div>');
107   }
108 };
109 JS;
110     $session->executeScript($test_settings_command);
111     // @todo: Replace after https://www.drupal.org/project/drupal/issues/2616184
112     $session->executeScript('window.jQuery("#edit-settings-command-example").mousedown();');
113     $this->assertWaitPageContains('<div class="test-settings-command">42</div>');
114   }
115
116   /**
117    * Asserts that page contains a text after waiting.
118    *
119    * @param string $text
120    *   A needle text.
121    */
122   protected function assertWaitPageContains($text) {
123     $page = $this->getSession()->getPage();
124     $page->waitFor(10, function () use ($page, $text) {
125       return stripos($page->getContent(), $text) !== FALSE;
126     });
127     $this->assertContains($text, $page->getContent());
128   }
129
130 }