getSession();
$page = $this->getSession()->getPage();
$form_path = 'ajax_forms_test_ajax_commands_form';
$web_user = $this->drupalCreateUser(['access content']);
$this->drupalLogin($web_user);
$this->drupalGet($form_path);
// Tests the 'add_css' command.
$page->pressButton("AJAX 'add_css' command");
$this->assertWaitPageContains('my/file.css');
$this->assertSession()->elementExists('css', 'link[href="my/file.css"]');
$this->assertSession()->elementExists('css', 'link[href="https://example.com/css?family=Open+Sans"]');
// Tests the 'after' command.
$page->pressButton("AJAX 'After': Click to put something after the div");
$this->assertWaitPageContains('
Default announcement.
');
$this->drupalGet($form_path);
$page->pressButton("AJAX 'Announce': Click to announce with 'polite' priority");
$this->assertWaitPageContains('Polite announcement.
');
$this->drupalGet($form_path);
$page->pressButton("AJAX 'Announce': Click to announce with 'assertive' priority");
$this->assertWaitPageContains('Assertive announcement.
');
$this->drupalGet($form_path);
$page->pressButton("AJAX 'Announce': Click to announce twice");
$this->assertWaitPageContains('Assertive announcement.' . "\nAnother announcement.
");
// Tests the 'append' command.
$page->pressButton("AJAX 'Append': Click to append something");
$this->assertWaitPageContains('Insert something before this.
');
// Tests the 'changed' command.
$page->pressButton("AJAX changed: Click to mark div changed.");
$this->assertWaitPageContains('');
// Tests the 'changed' command using the second argument.
// Refresh page for testing 'changed' command to same element again.
$this->drupalGet($form_path);
$page->pressButton("AJAX changed: Click to mark div changed with asterisk.");
$this->assertWaitPageContains('
This div can be marked as changed or not.
* ');
// Tests the 'css' command.
$page->pressButton("Set the '#box' div to be blue.");
$this->assertWaitPageContains('
');
// Tests the 'data' command.
$page->pressButton("AJAX data command: Issue command.");
$this->assertTrue($page->waitFor(10, function () use ($session) {
return 'test_value' === $session->evaluateScript('window.jQuery("#data_div").data("test_key")');
}));
// Tests the 'html' command.
$page->pressButton("AJAX html: Replace the HTML in a selector.");
$this->assertWaitPageContains('
replacement text
');
// Tests the 'insert' command.
$page->pressButton("AJAX insert: Let client insert based on #ajax['method'].");
$this->assertWaitPageContains('
insert replacement textOriginal contents
');
// Tests the 'invoke' command.
$page->pressButton("AJAX invoke command: Invoke addClass() method.");
$this->assertWaitPageContains('
Original contents
');
// Tests the 'prepend' command.
$page->pressButton("AJAX 'prepend': Click to prepend something");
$this->assertWaitPageContains('
prepended textSomething will be prepended to this div.
');
// Tests the 'remove' command.
$page->pressButton("AJAX 'remove': Click to remove text");
$this->assertWaitPageContains('
');
// Tests the 'restripe' command.
$page->pressButton("AJAX 'restripe' command");
$this->assertWaitPageContains('
first row |
');
$this->assertWaitPageContains('
second row |
');
// Tests the 'settings' command.
$test_settings_command = <<
' + settings.ajax_forms_test.foo + '');
}
};
JS;
$session->executeScript($test_settings_command);
// @todo Replace after https://www.drupal.org/project/drupal/issues/2616184
$session->executeScript('window.jQuery("#edit-settings-command-example").mousedown();');
$this->assertWaitPageContains('
42
');
}
/**
* Asserts that page contains a text after waiting.
*
* @param string $text
* A needle text.
*
* @internal
*/
protected function assertWaitPageContains(string $text): void {
$page = $this->getSession()->getPage();
$page->waitFor(10, function () use ($page, $text) {
return stripos($page->getContent(), $text) !== FALSE;
});
$this->assertStringContainsString($text, $page->getContent());
}
}