DialogTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\ajax_test\Controller\AjaxTestController;
  4. use Drupal\Component\Render\FormattableMarkup;
  5. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  6. /**
  7. * Performs tests on opening and manipulating dialogs via AJAX commands.
  8. *
  9. * @group Ajax
  10. */
  11. class DialogTest extends WebDriverTestBase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. protected static $modules = ['ajax_test', 'ajax_forms_test', 'contact'];
  16. /**
  17. * Test sending non-JS and AJAX requests to open and manipulate modals.
  18. */
  19. public function testDialog() {
  20. $this->drupalLogin($this->drupalCreateUser(['administer contact forms']));
  21. // Ensure the elements render without notices or exceptions.
  22. $this->drupalGet('ajax-test/dialog');
  23. // Set up variables for this test.
  24. $dialog_renderable = AjaxTestController::dialogContents();
  25. $dialog_contents = \Drupal::service('renderer')->renderRoot($dialog_renderable);
  26. // Check that requesting a modal dialog without JS goes to a page.
  27. $this->drupalGet('ajax-test/dialog-contents');
  28. $this->assertSession()->responseContains($dialog_contents);
  29. // Visit the page containing the many test dialog links.
  30. $this->drupalGet('ajax-test/dialog');
  31. // Tests a basic modal dialog by verifying the contents of the dialog are as
  32. // expected.
  33. $this->getSession()->getPage()->clickLink('Link 1 (modal)');
  34. // Clicking the link triggers a AJAX request/response.
  35. // Opens a Dialog panel.
  36. $link1_dialog_div = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
  37. $this->assertNotNull($link1_dialog_div, 'Link was used to open a dialog ( modal )');
  38. $link1_modal = $link1_dialog_div->find('css', '#drupal-modal');
  39. $this->assertNotNull($link1_modal, 'Link was used to open a dialog ( non-modal )');
  40. $this->assertSession()->responseContains($dialog_contents);
  41. $dialog_title = $link1_dialog_div->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')");
  42. $this->assertNotNull($dialog_title);
  43. $dialog_title_amp = $link1_dialog_div->find('css', "span.ui-dialog-title:contains('AJAX Dialog &amp; contents')");
  44. $this->assertNull($dialog_title_amp);
  45. // Close open dialog, return to the dialog links page.
  46. $close_button = $link1_dialog_div->findButton('Close');
  47. $this->assertNotNull($close_button);
  48. $close_button->press();
  49. // Tests a modal with a dialog-option.
  50. // Link 2 is similar to Link 1, except it submits additional width
  51. // information which must be echoed in the resulting DOM update.
  52. $this->getSession()->getPage()->clickLink('Link 2 (modal)');
  53. $dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
  54. $this->assertNotNull($dialog, 'Link was used to open a dialog ( non-modal, with options )');
  55. $style = $dialog->getAttribute('style');
  56. $this->assertContains('width: 400px;', $style, new FormattableMarkup('Modal respected the dialog-options width parameter. Style = style', ['%style' => $style]));
  57. // Reset: Return to the dialog links page.
  58. $this->drupalGet('ajax-test/dialog');
  59. // Test a non-modal dialog ( with target ).
  60. $this->clickLink('Link 3 (non-modal)');
  61. $non_modal_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
  62. $this->assertNotNull($non_modal_dialog, 'Link opens a non-modal dialog.');
  63. // Tests the dialog contains a target element specified in the AJAX request.
  64. $non_modal_dialog->find('css', 'div#ajax-test-dialog-wrapper-1');
  65. $this->assertSession()->responseContains($dialog_contents);
  66. // Reset: Return to the dialog links page.
  67. $this->drupalGet('ajax-test/dialog');
  68. // Tests a non-modal dialog ( without target ).
  69. $this->clickLink('Link 7 (non-modal, no target)');
  70. $no_target_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
  71. $this->assertNotNull($no_target_dialog, 'Link opens a non-modal dialog.');
  72. $contents_no_target = $no_target_dialog->find('css', 'div.ui-dialog-content');
  73. $this->assertNotNull($contents_no_target, 'non-modal dialog opens ( no target ). ');
  74. $id = $contents_no_target->getAttribute('id');
  75. $partial_match = strpos($id, 'drupal-dialog-ajax-testdialog-contents') === 0;
  76. $this->assertTrue($partial_match, 'The non-modal ID has the expected prefix.');
  77. $no_target_button = $no_target_dialog->findButton('Close');
  78. $this->assertNotNull($no_target_button, 'Link dialog has a close button');
  79. $no_target_button->press();
  80. $this->getSession()->getPage()->findButton('Button 1 (modal)')->press();
  81. $button1_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
  82. $this->assertNotNull($button1_dialog, 'Button opens a modal dialog.');
  83. $button1_dialog_content = $button1_dialog->find('css', 'div.ui-dialog-content');
  84. $this->assertNotNull($button1_dialog_content, 'Button opens a modal dialog.');
  85. // Test the HTML escaping of & character.
  86. $button1_dialog_title = $button1_dialog->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')");
  87. $this->assertNotNull($button1_dialog_title);
  88. $button1_dialog_title_amp = $button1_dialog->find('css', "span.ui-dialog-title:contains('AJAX Dialog &amp; contents')");
  89. $this->assertNull($button1_dialog_title_amp);
  90. // Reset: Close the dialog.
  91. $button1_dialog->findButton('Close')->press();
  92. // Abbreviated test for "normal" dialogs, testing only the difference.
  93. $this->getSession()->getPage()->findButton('Button 2 (non-modal)')->press();
  94. $button2_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog-content');
  95. $this->assertNotNull($button2_dialog, 'Non-modal content displays as expected.');
  96. // Use a link to close the pagnel opened by button 2.
  97. $this->getSession()->getPage()->clickLink('Link 4 (close non-modal if open)');
  98. // Form modal.
  99. $this->clickLink('Link 5 (form)');
  100. // Two links have been clicked in succession - This time wait for a change
  101. // in the title as the previous closing dialog may temporarily be open.
  102. $form_dialog_title = $this->assertSession()->waitForElementVisible('css', "span.ui-dialog-title:contains('Ajax Form contents')");
  103. $this->assertNotNull($form_dialog_title, 'Dialog form has the expected title.');
  104. // Locate the newly opened dialog.
  105. $form_dialog = $this->getSession()->getPage()->find('css', 'div.ui-dialog');
  106. $this->assertNotNull($form_dialog, 'Form dialog is visible');
  107. $form_contents = $form_dialog->find('css', "p:contains('Ajax Form contents description.')");
  108. $this->assertNotNull($form_contents, 'For has the expected text.');
  109. $do_it = $form_dialog->findButton('Do it');
  110. $this->assertNotNull($do_it, 'The dialog has a "Do it" button.');
  111. $preview = $form_dialog->findButton('Preview');
  112. $this->assertNotNull($preview, 'The dialog contains a "Preview" button.');
  113. // Reset: close the form.
  114. $form_dialog->findButton('Close')->press();
  115. // Non AJAX version of Link 6.
  116. $this->drupalGet('admin/structure/contact/add');
  117. // Check we get a chunk of the code, we can't test the whole form as form
  118. // build id and token with be different.
  119. $contact_form = $this->xpath("//form[@id='contact-form-add-form']");
  120. $this->assertTrue(!empty($contact_form), 'Non-JS entity form page present.');
  121. // Reset: Return to the dialog links page.
  122. $this->drupalGet('ajax-test/dialog');
  123. $this->clickLink('Link 6 (entity form)');
  124. $dialog_add = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
  125. $this->assertNotNull($dialog_add, 'Form dialog is visible');
  126. $form_add = $dialog_add->find('css', 'form.contact-form-add-form');
  127. $this->assertNotNull($form_add, 'Modal dialog JSON contains entity form.');
  128. $form_title = $dialog_add->find('css', "span.ui-dialog-title:contains('Add contact form')");
  129. $this->assertNotNull($form_title, 'The add form title is as expected.');
  130. }
  131. }