Chris@0: drupalLogin($this->drupalCreateUser(['administer contact forms'])); Chris@0: // Ensure the elements render without notices or exceptions. Chris@0: $this->drupalGet('ajax-test/dialog'); Chris@0: Chris@0: // Set up variables for this test. Chris@0: $dialog_renderable = AjaxTestController::dialogContents(); Chris@0: $dialog_contents = \Drupal::service('renderer')->renderRoot($dialog_renderable); Chris@0: $modal_expected_response = [ Chris@0: 'command' => 'openDialog', Chris@0: 'selector' => '#drupal-modal', Chris@0: 'settings' => NULL, Chris@0: 'data' => $dialog_contents, Chris@0: 'dialogOptions' => [ Chris@0: 'modal' => TRUE, Chris@0: 'title' => 'AJAX Dialog & contents', Chris@0: ], Chris@0: ]; Chris@0: $form_expected_response = [ Chris@0: 'command' => 'openDialog', Chris@0: 'selector' => '#drupal-modal', Chris@0: 'settings' => NULL, Chris@0: 'dialogOptions' => [ Chris@0: 'modal' => TRUE, Chris@0: 'title' => 'Ajax Form contents', Chris@0: ], Chris@0: ]; Chris@0: $entity_form_expected_response = [ Chris@0: 'command' => 'openDialog', Chris@0: 'selector' => '#drupal-modal', Chris@0: 'settings' => NULL, Chris@0: 'dialogOptions' => [ Chris@0: 'modal' => TRUE, Chris@0: 'title' => 'Add contact form', Chris@0: ], Chris@0: ]; Chris@0: $normal_expected_response = [ Chris@0: 'command' => 'openDialog', Chris@0: 'selector' => '#ajax-test-dialog-wrapper-1', Chris@0: 'settings' => NULL, Chris@0: 'data' => $dialog_contents, Chris@0: 'dialogOptions' => [ Chris@0: 'modal' => FALSE, Chris@0: 'title' => 'AJAX Dialog & contents', Chris@0: ], Chris@0: ]; Chris@0: $no_target_expected_response = [ Chris@0: 'command' => 'openDialog', Chris@0: 'selector' => '#drupal-dialog-ajax-testdialog-contents', Chris@0: 'settings' => NULL, Chris@0: 'data' => $dialog_contents, Chris@0: 'dialogOptions' => [ Chris@0: 'modal' => FALSE, Chris@0: 'title' => 'AJAX Dialog & contents', Chris@0: ], Chris@0: ]; Chris@0: $close_expected_response = [ Chris@0: 'command' => 'closeDialog', Chris@0: 'selector' => '#ajax-test-dialog-wrapper-1', Chris@0: 'persist' => FALSE, Chris@0: ]; Chris@0: Chris@0: // Check that requesting a modal dialog without JS goes to a page. Chris@0: $this->drupalGet('ajax-test/dialog-contents'); Chris@0: $this->assertRaw($dialog_contents, 'Non-JS modal dialog page present.'); Chris@0: Chris@0: // Check that requesting a modal dialog with XMLHttpRequest goes to a page. Chris@0: $this->drupalGetXHR('ajax-test/dialog-contents'); Chris@0: $this->assertRaw($dialog_contents, 'Modal dialog page on XMLHttpRequest present.'); Chris@0: Chris@0: // Emulate going to the JS version of the page and check the JSON response. Chris@0: $ajax_result = $this->drupalGetAjax('ajax-test/dialog-contents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal']]); Chris@0: $this->assertEqual($modal_expected_response, $ajax_result[3], 'Modal dialog JSON response matches.'); Chris@0: // Test the HTML escaping of & character. Chris@0: $this->assertEqual($ajax_result[3]['dialogOptions']['title'], 'AJAX Dialog & contents'); Chris@0: $this->assertNotEqual($ajax_result[3]['dialogOptions']['title'], 'AJAX Dialog & contents'); Chris@0: Chris@0: // Check that requesting a "normal" dialog without JS goes to a page. Chris@0: $this->drupalGet('ajax-test/dialog-contents'); Chris@0: $this->assertRaw($dialog_contents, 'Non-JS normal dialog page present.'); Chris@0: Chris@0: // Emulate going to the JS version of the page and check the JSON response. Chris@0: // This needs to use WebTestBase::drupalPostAjaxForm() so that the correct Chris@0: // dialog options are sent. Chris@0: $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', [ Chris@0: // We have to mock a form element to make drupalPost submit from a link. Chris@0: 'textfield' => 'test', Chris@0: ], [], 'ajax-test/dialog-contents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog']], [], NULL, [ Chris@0: 'submit' => [ Chris@0: 'dialogOptions[target]' => 'ajax-test-dialog-wrapper-1', Chris@0: ] Chris@0: ]); Chris@0: $this->assertEqual($normal_expected_response, $ajax_result[3], 'Normal dialog JSON response matches.'); Chris@0: Chris@0: // Emulate going to the JS version of the page and check the JSON response. Chris@0: // This needs to use WebTestBase::drupalPostAjaxForm() so that the correct Chris@0: // dialog options are sent. Chris@0: $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', [ Chris@0: // We have to mock a form element to make drupalPost submit from a link. Chris@0: 'textfield' => 'test', Chris@0: ], [], 'ajax-test/dialog-contents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog']], [], NULL, [ Chris@0: // Don't send a target. Chris@0: 'submit' => [] Chris@0: ]); Chris@0: // Make sure the selector ID starts with the right string. Chris@0: $this->assert(strpos($ajax_result[3]['selector'], $no_target_expected_response['selector']) === 0, 'Selector starts with right string.'); Chris@0: unset($ajax_result[3]['selector']); Chris@0: unset($no_target_expected_response['selector']); Chris@0: $this->assertEqual($no_target_expected_response, $ajax_result[3], 'Normal dialog with no target JSON response matches.'); Chris@0: Chris@0: // Emulate closing the dialog via an AJAX request. There is no non-JS Chris@0: // version of this test. Chris@0: $ajax_result = $this->drupalGetAjax('ajax-test/dialog-close'); Chris@0: $this->assertEqual($close_expected_response, $ajax_result[0], 'Close dialog JSON response matches.'); Chris@0: Chris@0: // Test submitting via a POST request through the button for modals. This Chris@0: // approach more accurately reflects the real responses by Drupal because Chris@0: // all of the necessary page variables are emulated. Chris@0: $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', [], 'button1'); Chris@0: Chris@0: // Check that CSS and JavaScript are "added" to the page dynamically. Chris@0: $this->assertTrue(in_array('core/drupal.dialog.ajax', explode(',', $ajax_result[0]['settings']['ajaxPageState']['libraries'])), 'core/drupal.dialog.ajax library is added to the page.'); Chris@0: $dialog_css_exists = strpos($ajax_result[1]['data'], 'dialog.css') !== FALSE; Chris@0: $this->assertTrue($dialog_css_exists, 'jQuery UI dialog CSS added to the page.'); Chris@0: $dialog_js_exists = strpos($ajax_result[2]['data'], 'dialog-min.js') !== FALSE; Chris@0: $this->assertTrue($dialog_js_exists, 'jQuery UI dialog JS added to the page.'); Chris@0: $dialog_js_exists = strpos($ajax_result[2]['data'], 'dialog.ajax.js') !== FALSE; Chris@0: $this->assertTrue($dialog_js_exists, 'Drupal dialog JS added to the page.'); Chris@0: Chris@0: // Check that the response matches the expected value. Chris@0: $this->assertEqual($modal_expected_response, $ajax_result[4], 'POST request modal dialog JSON response matches.'); Chris@0: // Test the HTML escaping of & character. Chris@0: $this->assertNotEqual($ajax_result[4]['dialogOptions']['title'], 'AJAX Dialog & contents'); Chris@0: Chris@0: // Abbreviated test for "normal" dialogs, testing only the difference. Chris@0: $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', [], 'button2'); Chris@0: $this->assertEqual($normal_expected_response, $ajax_result[4], 'POST request normal dialog JSON response matches.'); Chris@0: Chris@0: // Check that requesting a form dialog without JS goes to a page. Chris@0: $this->drupalGet('ajax-test/dialog-form'); Chris@0: // Check we get a chunk of the code, we can't test the whole form as form Chris@0: // build id and token with be different. Chris@0: $form = $this->xpath("//form[@id='ajax-test-form']"); Chris@0: $this->assertTrue(!empty($form), 'Non-JS form page present.'); Chris@0: Chris@0: // Emulate going to the JS version of the form and check the JSON response. Chris@0: $ajax_result = $this->drupalGetAjax('ajax-test/dialog-form', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal']]); Chris@0: $expected_ajax_settings = [ Chris@0: 'edit-preview' => [ Chris@0: 'callback' => '::preview', Chris@0: 'event' => 'click', Chris@0: 'url' => Url::fromRoute('ajax_test.dialog_form', [], [ Chris@0: 'query' => [ Chris@0: MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal', Chris@0: FormBuilderInterface::AJAX_FORM_REQUEST => TRUE, Chris@0: ], Chris@0: ])->toString(), Chris@0: 'dialogType' => 'ajax', Chris@0: 'submit' => [ Chris@0: '_triggering_element_name' => 'op', Chris@0: '_triggering_element_value' => 'Preview', Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: $this->assertEqual($expected_ajax_settings, $ajax_result[0]['settings']['ajax']); Chris@0: $this->setRawContent($ajax_result[3]['data']); Chris@0: // Remove the data, the form build id and token will never match. Chris@0: unset($ajax_result[3]['data']); Chris@0: $form = $this->xpath("//form[@id='ajax-test-form']"); Chris@0: $this->assertTrue(!empty($form), 'Modal dialog JSON contains form.'); Chris@0: $this->assertEqual($form_expected_response, $ajax_result[3]); Chris@0: Chris@0: // Check that requesting an entity form dialog without JS goes to a page. Chris@0: $this->drupalGet('admin/structure/contact/add'); Chris@0: // Check we get a chunk of the code, we can't test the whole form as form Chris@0: // build id and token with be different. Chris@0: $form = $this->xpath("//form[@id='contact-form-add-form']"); Chris@0: $this->assertTrue(!empty($form), 'Non-JS entity form page present.'); Chris@0: Chris@0: // Emulate going to the JS version of the form and check the JSON response. Chris@0: $ajax_result = $this->drupalGetAjax('admin/structure/contact/add', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal']]); Chris@0: $this->setRawContent($ajax_result[3]['data']); Chris@0: // Remove the data, the form build id and token will never match. Chris@0: unset($ajax_result[3]['data']); Chris@0: $form = $this->xpath("//form[@id='contact-form-add-form']"); Chris@0: $this->assertTrue(!empty($form), 'Modal dialog JSON contains entity form.'); Chris@0: $this->assertEqual($entity_form_expected_response, $ajax_result[3]); Chris@0: } Chris@0: Chris@0: }