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