QuickEditLoadingTest.php 25.6 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\quickedit\Tests\QuickEditLoadingTest.
6 7
 */

8
namespace Drupal\quickedit\Tests;
9

10
use Drupal\Component\Serialization\Json;
11
use Drupal\simpletest\WebTestBase;
12
use Drupal\quickedit\Ajax\MetadataCommand;
13
use Drupal\Core\Ajax\AppendCommand;
14
use Drupal\Component\Utility\Unicode;
15 16

/**
17
 * Tests loading of Quick Edit and lazy-loading of in-place editors.
18
 */
19
class QuickEditLoadingTest extends WebTestBase {
20 21 22 23 24 25

  /**
   * Modules to enable.
   *
   * @var array
   */
26
  public static $modules = array('contextual', 'quickedit', 'filter', 'node');
27 28 29 30 31

  public static function getInfo() {
    return array(
      'name' => 'In-place editing loading',
      'description' => 'Tests loading of in-place editing functionality and lazy loading of its in-place editors.',
32
      'group' => 'Quick Edit',
33 34 35
    );
  }

36
  protected function setUp() {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    parent::setUp();

    // Create a text format.
    $filtered_html_format = entity_create('filter_format', array(
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => array(),
    ));
    $filtered_html_format->save();

    // Create a node type.
    $this->drupalCreateContentType(array(
      'type' => 'article',
      'name' => 'Article',
    ));

    // Create one node of the above node type using the above text format.
    $this->drupalCreateNode(array(
      'type' => 'article',
      'body' => array(
        0 => array(
          'value' => '<p>How are you?</p>',
          'format' => 'filtered_html',
        )
62
      ),
63
      'revision_log' => $this->randomString(),
64 65 66 67 68 69 70 71 72 73
    ));

    // Create 2 users, the only difference being the ability to use in-place
    // editing
    $basic_permissions = array('access content', 'create article content', 'edit any article content', 'use text format filtered_html', 'access contextual links');
    $this->author_user = $this->drupalCreateUser($basic_permissions);
    $this->editor_user = $this->drupalCreateUser(array_merge($basic_permissions, array('access in-place editing')));
  }

  /**
74
   * Test the loading of Quick Edit when a user doesn't have access to it.
75
   */
76
  public function testUserWithoutPermission() {
77 78 79
    $this->drupalLogin($this->author_user);
    $this->drupalGet('node/1');

80
    // Library and in-place editors.
81
    $settings = $this->drupalGetSettings();
82 83
    $this->assertFalse(isset($settings['ajaxPageState']['js']['core/modules/quickedit/js/quickedit.js']), 'Quick Edit library not loaded.');
    $this->assertFalse(isset($settings['ajaxPageState']['js']['core/modules/quickedit/js/editors/formEditor.js']), "'form' in-place editor not loaded.");
84 85

    // HTML annotation must always exist (to not break the render cache).
86 87
    $this->assertRaw('data-quickedit-entity-id="node/1"');
    $this->assertRaw('data-quickedit-field-id="node/1/body/und/full"');
88 89

    // Retrieving the metadata should result in an empty 403 response.
90
    $post = array('fields[0]' => 'node/1/body/und/full');
91
    $response = $this->drupalPost('quickedit/metadata', 'application/json', $post);
92 93
    $this->assertIdentical('{}', $response);
    $this->assertResponse(403);
94

95 96 97
    // Quick Edit's JavaScript would never hit these endpoints if the metadata
    // was empty as above, but we need to make sure that malicious users aren't
    // able to use any of the other endpoints either.
98
    $post = array('editors[0]' => 'form') + $this->getAjaxPageStatePostData();
99
    $response = $this->drupalPost('quickedit/attachments', 'application/vnd.drupal-ajax', $post);
100 101 102
    // @todo Uncomment the below once https://drupal.org/node/2063303 is fixed.
    // $this->assertIdentical('[]', $response);
    $this->assertResponse(403);
103
    $post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData();
104
    $response = $this->drupalPost('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $post);
105 106 107 108
    // @todo Uncomment the below once https://drupal.org/node/2063303 is fixed.
    // $this->assertIdentical('[]', $response);
    $this->assertResponse(403);
    $edit = array();
109
    $edit['form_id'] = 'quickedit_field_form';
110 111
    $edit['form_token'] = 'xIOzMjuc-PULKsRn_KxFn7xzNk5Bx7XKXLfQfw1qOnA';
    $edit['form_build_id'] = 'form-kVmovBpyX-SJfTT5kY0pjTV35TV-znor--a64dEnMR8';
112 113 114
    $edit['body[0][summary]'] = '';
    $edit['body[0][value]'] = '<p>Malicious content.</p>';
    $edit['body[0][format]'] = 'filtered_html';
115
    $edit['op'] = t('Save');
116
    $response = $this->drupalPost('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $edit);
117 118 119
    // @todo Uncomment the below once https://drupal.org/node/2063303 is fixed.
    // $this->assertIdentical('[]', $response);
    $this->assertResponse(403);
120
    $post = array('nocssjs' => 'true');
121
    $response = $this->drupalPost('quickedit/entity/' . 'node/1', 'application/json', $post);
122 123 124
    // @todo Uncomment the below once https://drupal.org/node/2063303 is fixed.
    // $this->assertIdentical('[]', $response);
    $this->assertResponse(403);
125 126 127
  }

  /**
128
   * Tests the loading of Quick Edit when a user does have access to it.
129 130 131
   *
   * Also ensures lazy loading of in-place editors works.
   */
132
  public function testUserWithPermission() {
133 134 135
    $this->drupalLogin($this->editor_user);
    $this->drupalGet('node/1');

136
    // Library and in-place editors.
137
    $settings = $this->drupalGetSettings();
138 139
    $this->assertTrue(isset($settings['ajaxPageState']['js']['core/modules/quickedit/js/quickedit.js']), 'Quick Edit library loaded.');
    $this->assertFalse(isset($settings['ajaxPageState']['js']['core/modules/quickedit/js/editors/formEditor.js']), "'form' in-place editor not loaded.");
140 141

    // HTML annotation must always exist (to not break the render cache).
142 143
    $this->assertRaw('data-quickedit-entity-id="node/1"');
    $this->assertRaw('data-quickedit-field-id="node/1/body/und/full"');
144

145
    // There should be only one revision so far.
146 147 148
    $node = node_load(1);
    $vids = \Drupal::entityManager()->getStorage('node')->revisionIds($node);
    $this->assertIdentical(1, count($vids), 'The node has only one revision.');
149
    $original_log = $node->revision_log->value;
150

151 152
    // Retrieving the metadata should result in a 200 JSON response.
    $htmlPageDrupalSettings = $this->drupalSettings;
153
    $post = array('fields[0]' => 'node/1/body/und/full');
154
    $response = $this->drupalPost('quickedit/metadata', 'application/json', $post);
155
    $this->assertResponse(200);
156 157 158 159 160 161 162 163
    $expected = array(
      'node/1/body/und/full' => array(
        'label' => 'Body',
        'access' => TRUE,
        'editor' => 'form',
        'aria' => 'Entity node 1, field Body',
      )
    );
164
    $this->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');
165 166 167
    // Restore drupalSettings to build the next requests; simpletest wipes them
    // after a JSON response.
    $this->drupalSettings = $htmlPageDrupalSettings;
168

169 170 171
    // Retrieving the attachments should result in a 200 response, containing:
    //  1. a settings command with useless metadata: AjaxController is dumb
    //  2. an insert command that loads the required in-place editors
172
    $post = array('editors[0]' => 'form') + $this->getAjaxPageStatePostData();
173
    $response = $this->drupalPost('quickedit/attachments', 'application/vnd.drupal-ajax', $post);
174
    $ajax_commands = Json::decode($response);
175
    $this->assertIdentical(2, count($ajax_commands), 'The attachments HTTP request results in two AJAX commands.');
176 177 178 179
    // First command: settings.
    $this->assertIdentical('settings', $ajax_commands[0]['command'], 'The first AJAX command is a settings command.');
    // Second command: insert libraries into DOM.
    $this->assertIdentical('insert', $ajax_commands[1]['command'], 'The second AJAX command is an append command.');
180
    $this->assertTrue(in_array('core/modules/quickedit/js/editors/formEditor.js', array_keys($ajax_commands[0]['settings']['ajaxPageState']['js'])), 'The quickedit.inPlaceEditor.form library is loaded.');
181

182
    // Retrieving the form for this field should result in a 200 response,
183
    // containing only a quickeditFieldForm command.
184
    $post = array('nocssjs' => 'true', 'reset' => 'true') + $this->getAjaxPageStatePostData();
185
    $response = $this->drupalPost('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $post);
186
    $this->assertResponse(200);
187
    $ajax_commands = Json::decode($response);
188
    $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
189 190
    $this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
    $this->assertIdentical('<form ', Unicode::substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
191

192
    // Prepare form values for submission. drupalPostAjaxForm() is not suitable
193
    // for handling pages with JSON responses, so we need our own solution here.
194 195 196 197
    $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
    $this->assertTrue($form_tokens_found, 'Form tokens found in output.');

    if ($form_tokens_found) {
198
      $edit = array(
199 200 201
        'body[0][summary]' => '',
        'body[0][value]' => '<p>Fine thanks.</p>',
        'body[0][format]' => 'filtered_html',
202 203
        'op' => t('Save'),
      );
204
      $post = array(
205
        'form_id' => 'quickedit_field_form',
206 207 208 209
        'form_token' => $token_match[1],
        'form_build_id' => $build_id_match[1],
      );
      $post += $edit + $this->getAjaxPageStatePostData();
210

211 212
      // Submit field form and check response. This should store the updated
      // entity in TempStore on the server.
213
      $response = $this->drupalPost('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $post);
214
      $this->assertResponse(200);
215
      $ajax_commands = Json::decode($response);
216
      $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
217
      $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
218
      $this->assertTrue(strpos($ajax_commands[0]['data'], 'Fine thanks.'), 'Form value saved and printed back.');
219
      $this->assertIdentical($ajax_commands[0]['other_view_modes'], array(), 'Field was not rendered in any other view mode.');
220 221 222 223 224 225

      // Ensure the text on the original node did not change yet.
      $this->drupalGet('node/1');
      $this->assertText('How are you?');

      // Save the entity by moving the TempStore values to entity storage.
226
      $post = array('nocssjs' => 'true');
227
      $response = $this->drupalPost('quickedit/entity/' . 'node/1', 'application/json', $post);
228
      $this->assertResponse(200);
229
      $ajax_commands = Json::decode($response);
230
      $this->assertIdentical(1, count($ajax_commands), 'The entity submission HTTP request results in one AJAX command.');
231
      $this->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditEntitySaved command.');
232 233 234 235 236 237
      $this->assertIdentical($ajax_commands[0]['data']['entity_type'], 'node', 'Saved entity is of type node.');
      $this->assertIdentical($ajax_commands[0]['data']['entity_id'], '1', 'Entity id is 1.');

      // Ensure the text on the original node did change.
      $this->drupalGet('node/1');
      $this->assertText('Fine thanks.');
238 239

      // Ensure no new revision was created and the log message is unchanged.
240 241 242
      $node = node_load(1);
      $vids = \Drupal::entityManager()->getStorage('node')->revisionIds($node);
      $this->assertIdentical(1, count($vids), 'The node has only one revision.');
243
      $this->assertIdentical($original_log, $node->revision_log->value, 'The revision log message is unchanged.');
244 245 246 247 248

      // Now configure this node type to create new revisions automatically,
      // then again retrieve the field form, fill it, submit it (so it ends up
      // in TempStore) and then save the entity. Now there should be two
      // revisions.
249 250 251
      $node_type = entity_load('node_type', 'article');
      $node_type->settings['node']['options']['revision'] = TRUE;
      $node_type->save();
252 253 254

      // Retrieve field form.
      $post = array('nocssjs' => 'true', 'reset' => 'true');
255
      $response = $this->drupalPost('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $post);
256
      $this->assertResponse(200);
257
      $ajax_commands = Json::decode($response);
258
      $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
259 260
      $this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
      $this->assertIdentical('<form ', Unicode::substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
261 262 263 264 265 266

      // Submit field form.
      preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match);
      preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
      $edit['body[0][value]'] = '<p>kthxbye</p>';
      $post = array(
267
        'form_id' => 'quickedit_field_form',
268 269 270 271
        'form_token' => $token_match[1],
        'form_build_id' => $build_id_match[1],
      );
      $post += $edit + $this->getAjaxPageStatePostData();
272
      $response = $this->drupalPost('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $post);
273 274 275
      // @todo Uncomment the below once https://drupal.org/node/2063303 is fixed.
      // $this->assertIdentical('[]', $response);
      $this->assertResponse(200);
276
      $ajax_commands = Json::decode($response);
277
      $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
278
      $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is an quickeditFieldFormSaved command.');
279 280 281 282
      $this->assertTrue(strpos($ajax_commands[0]['data'], 'kthxbye'), 'Form value saved and printed back.');

      // Save the entity.
      $post = array('nocssjs' => 'true');
283
      $response = $this->drupalPost('quickedit/entity/' . 'node/1', 'application/json', $post);
284 285 286 287 288
      // @todo Uncomment the below once https://drupal.org/node/2063303 is fixed.
      // $this->assertIdentical('[]', $response);
      $this->assertResponse(200);

      // Test that a revision was created with the correct log message.
289 290 291
      $vids = \Drupal::entityManager()->getStorage('node')->revisionIds(node_load(1));
      $this->assertIdentical(2, count($vids), 'The node has two revisions.');
      $revision = node_revision_load($vids[0]);
292
      $this->assertIdentical($original_log, $revision->revision_log->value, 'The first revision log message is unchanged.');
293
      $revision = node_revision_load($vids[1]);
294
      $this->assertIdentical('Updated the <em class="placeholder">Body</em> field through in-place editing.', $revision->revision_log->value, 'The second revision log message was correctly generated by Quick Edit module.');
295
    }
296 297
  }

298
  /**
299
   * Tests the loading of Quick Edit for the title base field.
300 301 302 303 304
   */
  public function testTitleBaseField() {
    $this->drupalLogin($this->editor_user);
    $this->drupalGet('node/1');

305 306 307 308 309
    // Ensure that the full page title is actually in-place editable
    $node = entity_load('node', 1);
    $elements = $this->xpath('//h1/span[@data-quickedit-field-id="node/1/title/und/full" and normalize-space(text())=:title]', array(':title' => $node->label()));
    $this->assertTrue(!empty($elements), 'Title with data-quickedit-field-id attribute found.');

310 311 312
    // Retrieving the metadata should result in a 200 JSON response.
    $htmlPageDrupalSettings = $this->drupalSettings;
    $post = array('fields[0]' => 'node/1/title/und/full');
313
    $response = $this->drupalPost('quickedit/metadata', 'application/json', $post);
314 315 316 317 318 319 320 321 322
    $this->assertResponse(200);
    $expected = array(
      'node/1/title/und/full' => array(
        'label' => 'Title',
        'access' => TRUE,
        'editor' => 'plain_text',
        'aria' => 'Entity node 1, field Title',
      )
    );
323
    $this->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');
324 325 326 327 328
    // Restore drupalSettings to build the next requests; simpletest wipes them
    // after a JSON response.
    $this->drupalSettings = $htmlPageDrupalSettings;

    // Retrieving the form for this field should result in a 200 response,
329
    // containing only a quickeditFieldForm command.
330
    $post = array('nocssjs' => 'true', 'reset' => 'true') + $this->getAjaxPageStatePostData();
331
    $response = $this->drupalPost('quickedit/form/' . 'node/1/title/und/full', 'application/vnd.drupal-ajax', $post);
332
    $this->assertResponse(200);
333
    $ajax_commands = Json::decode($response);
334
    $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
335 336
    $this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
    $this->assertIdentical('<form ', Unicode::substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
337 338 339 340 341 342 343 344 345 346 347 348 349

    // Prepare form values for submission. drupalPostAjaxForm() is not suitable
    // for handling pages with JSON responses, so we need our own solution
    // here.
    $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
    $this->assertTrue($form_tokens_found, 'Form tokens found in output.');

    if ($form_tokens_found) {
      $edit = array(
        'title[0][value]' => 'Obligatory question',
        'op' => t('Save'),
      );
      $post = array(
350
        'form_id' => 'quickedit_field_form',
351 352 353 354 355 356 357
        'form_token' => $token_match[1],
        'form_build_id' => $build_id_match[1],
      );
      $post += $edit + $this->getAjaxPageStatePostData();

      // Submit field form and check response. This should store the
      // updated entity in TempStore on the server.
358
      $response = $this->drupalPost('quickedit/form/' . 'node/1/title/und/full', 'application/vnd.drupal-ajax', $post);
359
      $this->assertResponse(200);
360
      $ajax_commands = Json::decode($response);
361
      $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
362
      $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
363 364 365 366 367 368 369 370
      $this->assertTrue(strpos($ajax_commands[0]['data'], 'Obligatory question'), 'Form value saved and printed back.');

      // Ensure the text on the original node did not change yet.
      $this->drupalGet('node/1');
      $this->assertNoText('Obligatory question');

      // Save the entity by moving the TempStore values to entity storage.
      $post = array('nocssjs' => 'true');
371
      $response = $this->drupalPost('quickedit/entity/' . 'node/1', 'application/json', $post);
372
      $this->assertResponse(200);
373
      $ajax_commands = Json::decode($response);
374
      $this->assertIdentical(1, count($ajax_commands), 'The entity submission HTTP request results in one AJAX command.');
375
      $this->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is n quickeditEntitySaved command.');
376 377 378 379 380 381 382 383 384
      $this->assertIdentical($ajax_commands[0]['data']['entity_type'], 'node', 'Saved entity is of type node.');
      $this->assertIdentical($ajax_commands[0]['data']['entity_id'], '1', 'Entity id is 1.');

      // Ensure the text on the original node did change.
      $this->drupalGet('node/1');
      $this->assertText('Obligatory question');
    }
  }

385
  /**
386 387
   * Tests that Quick Edit doesn't make pseudo fields or computed fields
   * editable.
388
   */
389
  public function testPseudoFields() {
390
    \Drupal::moduleHandler()->install(array('quickedit_test'));
391 392 393 394 395

    $this->drupalLogin($this->author_user);
    $this->drupalGet('node/1');

    // Check that the data- attribute is not added.
396
    $this->assertNoRaw('data-quickedit-field-id="node/1/quickedit_test_pseudo_field/und/default"');
397 398
  }

399
  /**
400 401
   * Tests that Quick Edit doesn't make fields rendered with display options
   * editable.
402 403 404 405 406 407
   */
  public function testDisplayOptions() {
    $node = entity_load('node', '1');
    $display_settings = array(
      'label' => 'inline',
    );
408
    $build = $node->body->view($display_settings);
409
    $output = drupal_render($build);
410
    $this->assertFalse(strpos($output, 'data-quickedit-field-id'), 'data-quickedit-field-id attribute not added when rendering field using dynamic display options.');
411 412
  }

413
  /**
414
   * Tests that Quick Edit works with custom render pipelines.
415 416
   */
  public function testCustomPipeline() {
417
    \Drupal::moduleHandler()->install(array('quickedit_test'));
418

419
    $custom_render_url = 'quickedit/form/node/1/body/und/quickedit_test-custom-render-data';
420 421 422 423 424
    $this->drupalLogin($this->editor_user);

    // Request editing to render results with the custom render pipeline.
    $post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData();
    $response = $this->drupalPost($custom_render_url, 'application/vnd.drupal-ajax', $post);
425
    $ajax_commands = Json::decode($response);
426 427 428 429 430 431 432 433

    // Prepare form values for submission. drupalPostAJAX() is not suitable for
    // handling pages with JSON responses, so we need our own solution here.
    $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
    $this->assertTrue($form_tokens_found, 'Form tokens found in output.');

    if ($form_tokens_found) {
      $post = array(
434
        'form_id' => 'quickedit_field_form',
435 436 437 438 439 440 441
        'form_token' => $token_match[1],
        'form_build_id' => $build_id_match[1],
        'body[0][summary]' => '',
        'body[0][value]' => '<p>Fine thanks.</p>',
        'body[0][format]' => 'filtered_html',
        'op' => t('Save'),
      );
442 443 444
      // Assume there is another field on this page, which doesn't use a custom
      // render pipeline, but the default one, and it uses the "full" view mode.
      $post += array('other_view_modes[]' => 'full');
445 446 447 448 449

      // Submit field form and check response. Should render with the custom
      // render pipeline.
      $response = $this->drupalPost($custom_render_url, 'application/vnd.drupal-ajax', $post);
      $this->assertResponse(200);
450
      $ajax_commands = Json::decode($response);
451
      $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
452
      $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
453
      $this->assertTrue(strpos($ajax_commands[0]['data'], 'Fine thanks.'), 'Form value saved and printed back.');
454
      $this->assertTrue(strpos($ajax_commands[0]['data'], '<div class="quickedit-test-wrapper">') !== FALSE, 'Custom render pipeline used to render the value.');
455 456
      $this->assertIdentical(array_keys($ajax_commands[0]['other_view_modes']), array('full'), 'Field was also rendered in the "full" view mode.');
      $this->assertTrue(strpos($ajax_commands[0]['other_view_modes']['full'], 'Fine thanks.'), '"full" version of field contains the form value.');
457 458 459
    }
  }

460
  /**
461 462
   * Tests Quick Edit on a node that was concurrently edited on the full node
   * form.
463 464 465 466 467
   */
  public function testConcurrentEdit() {
    $this->drupalLogin($this->editor_user);

    $post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData();
468
    $response = $this->drupalPost('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $post);
469
    $this->assertResponse(200);
470
    $ajax_commands = Json::decode($response);
471 472 473 474 475 476 477 478

    // Prepare form values for submission. drupalPostAJAX() is not suitable for
    // handling pages with JSON responses, so we need our own solution here.
    $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
    $this->assertTrue($form_tokens_found, 'Form tokens found in output.');

    if ($form_tokens_found) {
      $post = array(
479
        'form_id' => 'quickedit_field_form',
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
        'form_token' => $token_match[1],
        'form_build_id' => $build_id_match[1],
        'body[0][summary]' => '',
        'body[0][value]' => '<p>Fine thanks.</p>',
        'body[0][format]' => 'filtered_html',
        'op' => t('Save'),
      );

      // Save the node on the regular node edit form.
      $this->drupalPostForm('node/1/edit', array(), t('Save'));
      // Ensure different save timestamps for field editing.
      sleep(2);

      // Submit field form and check response. Should throw a validation error
      // because the node was changed in the meantime.
495
      $response = $this->drupalPost('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $post);
496
      $this->assertResponse(200);
497
      $ajax_commands = Json::decode($response);
498
      $this->assertIdentical(2, count($ajax_commands), 'The field form HTTP request results in two AJAX commands.');
499
      $this->assertIdentical('quickeditFieldFormValidationErrors', $ajax_commands[1]['command'], 'The second AJAX command is a quickeditFieldFormValidationErrors command.');
500 501 502 503
      $this->assertTrue(strpos($ajax_commands[1]['data'], t('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.')), 'Error message returned to user.');
    }
  }

504
}