Chris@17: drupalLogin($this->drupalCreateUser(['administer contact forms'])); Chris@17: // Ensure the elements render without notices or exceptions. Chris@17: $this->drupalGet('ajax-test/dialog'); Chris@17: Chris@17: // Set up variables for this test. Chris@17: $dialog_renderable = AjaxTestController::dialogContents(); Chris@17: $dialog_contents = \Drupal::service('renderer')->renderRoot($dialog_renderable); Chris@17: Chris@17: // Check that requesting a modal dialog without JS goes to a page. Chris@17: $this->drupalGet('ajax-test/dialog-contents'); Chris@17: $this->assertSession()->responseContains($dialog_contents); Chris@17: Chris@17: // Visit the page containing the many test dialog links. Chris@17: $this->drupalGet('ajax-test/dialog'); Chris@17: Chris@17: // Tests a basic modal dialog by verifying the contents of the dialog are as Chris@17: // expected. Chris@17: $this->getSession()->getPage()->clickLink('Link 1 (modal)'); Chris@17: Chris@17: // Clicking the link triggers a AJAX request/response. Chris@17: // Opens a Dialog panel. Chris@17: $link1_dialog_div = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog'); Chris@17: $this->assertNotNull($link1_dialog_div, 'Link was used to open a dialog ( modal )'); Chris@17: Chris@17: $link1_modal = $link1_dialog_div->find('css', '#drupal-modal'); Chris@17: $this->assertNotNull($link1_modal, 'Link was used to open a dialog ( non-modal )'); Chris@17: $this->assertSession()->responseContains($dialog_contents); Chris@17: Chris@17: $dialog_title = $link1_dialog_div->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')"); Chris@17: $this->assertNotNull($dialog_title); Chris@17: $dialog_title_amp = $link1_dialog_div->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')"); Chris@17: $this->assertNull($dialog_title_amp); Chris@17: Chris@17: // Close open dialog, return to the dialog links page. Chris@17: $close_button = $link1_dialog_div->findButton('Close'); Chris@17: $this->assertNotNull($close_button); Chris@17: $close_button->press(); Chris@17: Chris@17: // Tests a modal with a dialog-option. Chris@17: // Link 2 is similar to Link 1, except it submits additional width Chris@17: // information which must be echoed in the resulting DOM update. Chris@17: $this->getSession()->getPage()->clickLink('Link 2 (modal)'); Chris@17: $dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog'); Chris@17: $this->assertNotNull($dialog, 'Link was used to open a dialog ( non-modal, with options )'); Chris@17: $style = $dialog->getAttribute('style'); Chris@17: $this->assertContains('width: 400px;', $style, new FormattableMarkup('Modal respected the dialog-options width parameter. Style = style', ['%style' => $style])); Chris@17: Chris@17: // Reset: Return to the dialog links page. Chris@17: $this->drupalGet('ajax-test/dialog'); Chris@17: Chris@17: // Test a non-modal dialog ( with target ). Chris@17: $this->clickLink('Link 3 (non-modal)'); Chris@17: $non_modal_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog'); Chris@17: $this->assertNotNull($non_modal_dialog, 'Link opens a non-modal dialog.'); Chris@17: Chris@17: // Tests the dialog contains a target element specified in the AJAX request. Chris@17: $non_modal_dialog->find('css', 'div#ajax-test-dialog-wrapper-1'); Chris@17: $this->assertSession()->responseContains($dialog_contents); Chris@17: Chris@17: // Reset: Return to the dialog links page. Chris@17: $this->drupalGet('ajax-test/dialog'); Chris@17: Chris@17: // Tests a non-modal dialog ( without target ). Chris@17: $this->clickLink('Link 7 (non-modal, no target)'); Chris@17: $no_target_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog'); Chris@17: $this->assertNotNull($no_target_dialog, 'Link opens a non-modal dialog.'); Chris@17: Chris@17: $contents_no_target = $no_target_dialog->find('css', 'div.ui-dialog-content'); Chris@17: $this->assertNotNull($contents_no_target, 'non-modal dialog opens ( no target ). '); Chris@17: $id = $contents_no_target->getAttribute('id'); Chris@17: $partial_match = strpos($id, 'drupal-dialog-ajax-testdialog-contents') === 0; Chris@17: $this->assertTrue($partial_match, 'The non-modal ID has the expected prefix.'); Chris@17: Chris@17: $no_target_button = $no_target_dialog->findButton('Close'); Chris@17: $this->assertNotNull($no_target_button, 'Link dialog has a close button'); Chris@17: $no_target_button->press(); Chris@17: Chris@17: $this->getSession()->getPage()->findButton('Button 1 (modal)')->press(); Chris@17: $button1_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog'); Chris@17: $this->assertNotNull($button1_dialog, 'Button opens a modal dialog.'); Chris@17: Chris@17: $button1_dialog_content = $button1_dialog->find('css', 'div.ui-dialog-content'); Chris@17: $this->assertNotNull($button1_dialog_content, 'Button opens a modal dialog.'); Chris@17: Chris@17: // Test the HTML escaping of & character. Chris@17: $button1_dialog_title = $button1_dialog->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')"); Chris@17: $this->assertNotNull($button1_dialog_title); Chris@17: $button1_dialog_title_amp = $button1_dialog->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')"); Chris@17: $this->assertNull($button1_dialog_title_amp); Chris@17: Chris@17: // Reset: Close the dialog. Chris@17: $button1_dialog->findButton('Close')->press(); Chris@17: Chris@17: // Abbreviated test for "normal" dialogs, testing only the difference. Chris@17: $this->getSession()->getPage()->findButton('Button 2 (non-modal)')->press(); Chris@17: $button2_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog-content'); Chris@17: $this->assertNotNull($button2_dialog, 'Non-modal content displays as expected.'); Chris@17: Chris@17: // Use a link to close the pagnel opened by button 2. Chris@17: $this->getSession()->getPage()->clickLink('Link 4 (close non-modal if open)'); Chris@17: Chris@17: // Form modal. Chris@17: $this->clickLink('Link 5 (form)'); Chris@17: // Two links have been clicked in succession - This time wait for a change Chris@17: // in the title as the previous closing dialog may temporarily be open. Chris@17: $form_dialog_title = $this->assertSession()->waitForElementVisible('css', "span.ui-dialog-title:contains('Ajax Form contents')"); Chris@17: $this->assertNotNull($form_dialog_title, 'Dialog form has the expected title.'); Chris@17: // Locate the newly opened dialog. Chris@17: $form_dialog = $this->getSession()->getPage()->find('css', 'div.ui-dialog'); Chris@17: $this->assertNotNull($form_dialog, 'Form dialog is visible'); Chris@17: Chris@17: $form_contents = $form_dialog->find('css', "p:contains('Ajax Form contents description.')"); Chris@17: $this->assertNotNull($form_contents, 'For has the expected text.'); Chris@17: $do_it = $form_dialog->findButton('Do it'); Chris@17: $this->assertNotNull($do_it, 'The dialog has a "Do it" button.'); Chris@17: $preview = $form_dialog->findButton('Preview'); Chris@17: $this->assertNotNull($preview, 'The dialog contains a "Preview" button.'); Chris@17: Chris@17: // Reset: close the form. Chris@17: $form_dialog->findButton('Close')->press(); Chris@17: Chris@17: // Non AJAX version of Link 6. Chris@17: $this->drupalGet('admin/structure/contact/add'); Chris@17: // Check we get a chunk of the code, we can't test the whole form as form Chris@17: // build id and token with be different. Chris@17: $contact_form = $this->xpath("//form[@id='contact-form-add-form']"); Chris@17: $this->assertTrue(!empty($contact_form), 'Non-JS entity form page present.'); Chris@17: Chris@17: // Reset: Return to the dialog links page. Chris@17: $this->drupalGet('ajax-test/dialog'); Chris@17: Chris@17: $this->clickLink('Link 6 (entity form)'); Chris@17: $dialog_add = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog'); Chris@17: $this->assertNotNull($dialog_add, 'Form dialog is visible'); Chris@17: Chris@17: $form_add = $dialog_add->find('css', 'form.contact-form-add-form'); Chris@17: $this->assertNotNull($form_add, 'Modal dialog JSON contains entity form.'); Chris@17: Chris@17: $form_title = $dialog_add->find('css', "span.ui-dialog-title:contains('Add contact form')"); Chris@17: $this->assertNotNull($form_title, 'The add form title is as expected.'); Chris@17: } Chris@17: Chris@17: }