CommandsTest.php 5.1 KB

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