comparison core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
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 $page->pressButton("AJAX 'Alert': Click to alert");
41 // Wait for the alert to appear.
42 $page->waitFor(10, function () use ($session) {
43 try {
44 $session->getDriver()->getWebDriverSession()->getAlert_text();
45 return TRUE;
46 }
47 catch (\Exception $e) {
48 return FALSE;
49 }
50 });
51 $alert_text = $this->getSession()->getDriver()->getWebDriverSession()->getAlert_text();
52 $this->assertEquals('Alert', $alert_text);
53 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
54
55 // Tests the 'append' command.
56 $page->pressButton("AJAX 'Append': Click to append something");
57 $this->assertWaitPageContains('<div id="append_div">Append inside this divAppended text</div>');
58
59 // Tests the 'before' command.
60 $page->pressButton("AJAX 'before': Click to put something before the div");
61 $this->assertWaitPageContains('Before text<div id="before_div">Insert something before this.</div>');
62
63 // Tests the 'changed' command.
64 $page->pressButton("AJAX changed: Click to mark div changed.");
65 $this->assertWaitPageContains('<div id="changed_div" class="ajax-changed">');
66
67 // Tests the 'changed' command using the second argument.
68 // Refresh page for testing 'changed' command to same element again.
69 $this->drupalGet($form_path);
70 $page->pressButton("AJAX changed: Click to mark div changed with asterisk.");
71 $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>');
72
73 // Tests the 'css' command.
74 $page->pressButton("Set the '#box' div to be blue.");
75 $this->assertWaitPageContains('<div id="css_div" style="background-color: blue;">');
76
77 // Tests the 'data' command.
78 $page->pressButton("AJAX data command: Issue command.");
79 $this->assertTrue($page->waitFor(10, function () use ($session) {
80 return 'testvalue' === $session->evaluateScript('window.jQuery("#data_div").data("testkey")');
81 }));
82
83 // Tests the 'html' command.
84 $page->pressButton("AJAX html: Replace the HTML in a selector.");
85 $this->assertWaitPageContains('<div id="html_div">replacement text</div>');
86
87 // Tests the 'insert' command.
88 $page->pressButton("AJAX insert: Let client insert based on #ajax['method'].");
89 $this->assertWaitPageContains('<div id="insert_div">insert replacement textOriginal contents</div>');
90
91 // Tests the 'invoke' command.
92 $page->pressButton("AJAX invoke command: Invoke addClass() method.");
93 $this->assertWaitPageContains('<div id="invoke_div" class="error">Original contents</div>');
94
95 // Tests the 'prepend' command.
96 $page->pressButton("AJAX 'prepend': Click to prepend something");
97 $this->assertWaitPageContains('<div id="prepend_div">prepended textSomething will be prepended to this div. </div>');
98
99 // Tests the 'remove' command.
100 $page->pressButton("AJAX 'remove': Click to remove text");
101 $this->assertWaitPageContains('<div id="remove_div"></div>');
102
103 // Tests the 'restripe' command.
104 $page->pressButton("AJAX 'restripe' command");
105 $this->assertWaitPageContains('<tr id="table-first" class="odd"><td>first row</td></tr>');
106 $this->assertWaitPageContains('<tr class="even"><td>second row</td></tr>');
107
108 // Tests the 'settings' command.
109 $test_settings_command = <<<JS
110 Drupal.behaviors.testSettingsCommand = {
111 attach: function (context, settings) {
112 window.jQuery('body').append('<div class="test-settings-command">' + settings.ajax_forms_test.foo + '</div>');
113 }
114 };
115 JS;
116 $session->executeScript($test_settings_command);
117 // @todo: Replace after https://www.drupal.org/project/drupal/issues/2616184
118 $session->executeScript('window.jQuery("#edit-settings-command-example").mousedown();');
119 $this->assertWaitPageContains('<div class="test-settings-command">42</div>');
120 }
121
122 /**
123 * Asserts that page contains a text after waiting.
124 *
125 * @param string $text
126 * A needle text.
127 */
128 protected function assertWaitPageContains($text) {
129 $page = $this->getSession()->getPage();
130 $page->waitFor(10, function () use ($page, $text) {
131 return stripos($page->getContent(), $text) !== FALSE;
132 });
133 $this->assertContains($text, $page->getContent());
134 }
135
136 }