annotate core/tests/Drupal/FunctionalJavascriptTests/Ajax/DialogTest.php @ 19:fa3358dc1485 tip

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