DialogTest.php 9.45 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\system\Tests\Ajax;

5
use Drupal\ajax_test\Controller\AjaxTestController;
6
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
7
use Drupal\Core\Form\FormBuilderInterface;
8 9
use Drupal\Core\Url;

10
/**
11 12 13
 * Performs tests on opening and manipulating dialogs via AJAX commands.
 *
 * @group Ajax
14 15
 */
class DialogTest extends AjaxTestBase {
16

17 18 19 20 21 22 23
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('ajax_test', 'ajax_forms_test', 'contact');

24
  /**
25
   * Test sending non-JS and AJAX requests to open and manipulate modals.
26
   */
27
  public function testDialog() {
28
    $this->drupalLogin($this->drupalCreateUser(array('administer contact forms')));
29 30 31
    // Ensure the elements render without notices or exceptions.
    $this->drupalGet('ajax-test/dialog');

32
    // Set up variables for this test.
33
    $dialog_renderable = AjaxTestController::dialogContents();
34
    $dialog_contents = \Drupal::service('renderer')->renderRoot($dialog_renderable);
35 36 37 38 39 40
    $modal_expected_response = array(
      'command' => 'openDialog',
      'selector' => '#drupal-modal',
      'settings' => NULL,
      'data' => $dialog_contents,
      'dialogOptions' => array(
41 42
        'modal' => TRUE,
        'title' => 'AJAX Dialog contents',
43 44
      ),
    );
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    $form_expected_response = array(
      'command' => 'openDialog',
      'selector' => '#drupal-modal',
      'settings' => NULL,
      'dialogOptions' => array(
        'modal' => TRUE,
        'title' => 'Ajax Form contents',
      ),
    );
    $entity_form_expected_response = array(
      'command' => 'openDialog',
      'selector' => '#drupal-modal',
      'settings' => NULL,
      'dialogOptions' => array(
        'modal' => TRUE,
60
        'title' => 'Add contact form',
61 62
      ),
    );
63 64 65 66 67 68
    $normal_expected_response = array(
      'command' => 'openDialog',
      'selector' => '#ajax-test-dialog-wrapper-1',
      'settings' => NULL,
      'data' => $dialog_contents,
      'dialogOptions' => array(
69 70
        'modal' => FALSE,
        'title' => 'AJAX Dialog contents',
71 72
      ),
    );
73 74
    $no_target_expected_response = array(
      'command' => 'openDialog',
75
      'selector' => '#drupal-dialog-ajax-testdialog-contents',
76 77 78 79 80 81 82
      'settings' => NULL,
      'data' => $dialog_contents,
      'dialogOptions' => array(
        'modal' => FALSE,
        'title' => 'AJAX Dialog contents',
      ),
    );
83 84 85
    $close_expected_response = array(
      'command' => 'closeDialog',
      'selector' => '#ajax-test-dialog-wrapper-1',
86
      'persist' => FALSE,
87 88 89
    );

    // Check that requesting a modal dialog without JS goes to a page.
90
    $this->drupalGet('ajax-test/dialog-contents');
91 92
    $this->assertRaw($dialog_contents, 'Non-JS modal dialog page present.');

93 94 95 96
    // Check that requesting a modal dialog with XMLHttpRequest goes to a page.
    $this->drupalGetXHR('ajax-test/dialog-contents');
    $this->assertRaw($dialog_contents, 'Modal dialog page on XMLHttpRequest present.');

97
    // Emulate going to the JS version of the page and check the JSON response.
98
    $ajax_result = $this->drupalGetAjax('ajax-test/dialog-contents', array('query' => array(MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal')));
99
    $this->assertEqual($modal_expected_response, $ajax_result[3], 'Modal dialog JSON response matches.');
100 101

    // Check that requesting a "normal" dialog without JS goes to a page.
102
    $this->drupalGet('ajax-test/dialog-contents');
103 104 105
    $this->assertRaw($dialog_contents, 'Non-JS normal dialog page present.');

    // Emulate going to the JS version of the page and check the JSON response.
106
    // This needs to use WebTestBase::drupalPostAjaxForm() so that the correct
107
    // dialog options are sent.
108
    $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', array(
109 110
        // We have to mock a form element to make drupalPost submit from a link.
        'textfield' => 'test',
111
      ), array(), 'ajax-test/dialog-contents', array('query' => array(MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog')), array(), NULL, array(
112 113 114 115 116
      'submit' => array(
        'dialogOptions[target]' => 'ajax-test-dialog-wrapper-1',
      )
    ));
    $this->assertEqual($normal_expected_response, $ajax_result[3], 'Normal dialog JSON response matches.');
117

118
    // Emulate going to the JS version of the page and check the JSON response.
119
    // This needs to use WebTestBase::drupalPostAjaxForm() so that the correct
120
    // dialog options are sent.
121
    $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', array(
122 123
        // We have to mock a form element to make drupalPost submit from a link.
        'textfield' => 'test',
124
      ), array(), 'ajax-test/dialog-contents', array('query' => array(MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog')), array(), NULL, array(
125 126 127
      // Don't send a target.
      'submit' => array()
    ));
128 129 130 131
    // Make sure the selector ID starts with the right string.
    $this->assert(strpos($ajax_result[3]['selector'], $no_target_expected_response['selector']) === 0, 'Selector starts with right string.');
    unset($ajax_result[3]['selector']);
    unset($no_target_expected_response['selector']);
132 133
    $this->assertEqual($no_target_expected_response, $ajax_result[3], 'Normal dialog with no target JSON response matches.');

134 135
    // Emulate closing the dialog via an AJAX request. There is no non-JS
    // version of this test.
136
    $ajax_result = $this->drupalGetAjax('ajax-test/dialog-close');
137 138 139 140 141
    $this->assertEqual($close_expected_response, $ajax_result[0], 'Close dialog JSON response matches.');

    // Test submitting via a POST request through the button for modals. This
    // approach more accurately reflects the real responses by Drupal because
    // all of the necessary page variables are emulated.
142
    $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', array(), 'button1');
143 144

    // Check that CSS and JavaScript are "added" to the page dynamically.
145 146 147 148 149 150 151
    $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.');
    $dialog_css_exists = strpos($ajax_result[1]['data'], 'dialog.css') !== FALSE;
    $this->assertTrue($dialog_css_exists, 'jQuery UI dialog CSS added to the page.');
    $dialog_js_exists = strpos($ajax_result[2]['data'], 'dialog-min.js') !== FALSE;
    $this->assertTrue($dialog_js_exists, 'jQuery UI dialog JS added to the page.');
    $dialog_js_exists = strpos($ajax_result[2]['data'], 'dialog.ajax.js') !== FALSE;
    $this->assertTrue($dialog_js_exists, 'Drupal dialog JS added to the page.');
152 153

    // Check that the response matches the expected value.
154
    $this->assertEqual($modal_expected_response, $ajax_result[4], 'POST request modal dialog JSON response matches.');
155 156

    // Abbreviated test for "normal" dialogs, testing only the difference.
157
    $ajax_result = $this->drupalPostAjaxForm('ajax-test/dialog', array(), 'button2');
158
    $this->assertEqual($normal_expected_response, $ajax_result[4], 'POST request normal dialog JSON response matches.');
159 160 161 162 163 164 165 166 167

    // Check that requesting a form dialog without JS goes to a page.
    $this->drupalGet('ajax-test/dialog-form');
    // Check we get a chunk of the code, we can't test the whole form as form
    // build id and token with be different.
    $form = $this->xpath("//form[@id='ajax-test-form']");
    $this->assertTrue(!empty($form), 'Non-JS form page present.');

    // Emulate going to the JS version of the form and check the JSON response.
168
    $ajax_result = $this->drupalGetAjax('ajax-test/dialog-form', array('query' => array(MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal')));
169 170 171 172
    $expected_ajax_settings = [
      'edit-preview' => [
        'callback' => '::preview',
        'event' => 'click',
173 174 175 176
        'url' => Url::fromRoute('ajax_test.dialog_form', [], ['query' => [
            MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal',
            FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
          ]])->toString(),
177
        'dialogType' => 'ajax',
178 179 180 181 182 183 184
        'submit' => [
          '_triggering_element_name' => 'op',
          '_triggering_element_value' => 'Preview',
        ],
      ],
    ];
    $this->assertEqual($expected_ajax_settings, $ajax_result[0]['settings']['ajax']);
185
    $this->setRawContent($ajax_result[3]['data']);
186
    // Remove the data, the form build id and token will never match.
187
    unset($ajax_result[3]['data']);
188 189
    $form = $this->xpath("//form[@id='ajax-test-form']");
    $this->assertTrue(!empty($form), 'Modal dialog JSON contains form.');
190
    $this->assertEqual($form_expected_response, $ajax_result[3]);
191 192 193 194 195

    // Check that requesting an entity form dialog without JS goes to a page.
    $this->drupalGet('admin/structure/contact/add');
    // Check we get a chunk of the code, we can't test the whole form as form
    // build id and token with be different.
196
    $form = $this->xpath("//form[@id='contact-form-add-form']");
197 198 199
    $this->assertTrue(!empty($form), 'Non-JS entity form page present.');

    // Emulate going to the JS version of the form and check the JSON response.
200
    $ajax_result = $this->drupalGetAjax('admin/structure/contact/add', array('query' => array(MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal')));
201
    $this->setRawContent($ajax_result[3]['data']);
202
    // Remove the data, the form build id and token will never match.
203
    unset($ajax_result[3]['data']);
204
    $form = $this->xpath("//form[@id='contact-form-add-form']");
205
    $this->assertTrue(!empty($form), 'Modal dialog JSON contains entity form.');
206
    $this->assertEqual($entity_form_expected_response, $ajax_result[3]);
207 208 209
  }

}