You are here

public function QuickEditLoadingTest::testConcurrentEdit in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/quickedit/src/Tests/QuickEditLoadingTest.php \Drupal\quickedit\Tests\QuickEditLoadingTest::testConcurrentEdit()

Tests Quick Edit on a node that was concurrently edited on the full node form.

File

core/modules/quickedit/src/Tests/QuickEditLoadingTest.php, line 464
Contains \Drupal\quickedit\Tests\QuickEditLoadingTest.

Class

QuickEditLoadingTest
Tests loading of in-place editing functionality and lazy loading of its in-place editors.

Namespace

Drupal\quickedit\Tests

Code

public function testConcurrentEdit() {
  $this
    ->drupalLogin($this->editorUser);
  $post = array(
    'nocssjs' => 'true',
  ) + $this
    ->getAjaxPageStatePostData();
  $response = $this
    ->drupalPost('quickedit/form/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', $post);
  $this
    ->assertResponse(200);
  $ajax_commands = Json::decode($response);

  // 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(
      'nocssjs' => 'true',
      'form_id' => 'quickedit_field_form',
      '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.
    $response = $this
      ->drupalPost('quickedit/form/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', $post);
    $this
      ->assertResponse(200);
    $ajax_commands = Json::decode($response);
    $this
      ->assertIdentical(2, count($ajax_commands), 'The field form HTTP request results in two AJAX commands.');
    $this
      ->assertIdentical('quickeditFieldFormValidationErrors', $ajax_commands[1]['command'], 'The second AJAX command is a quickeditFieldFormValidationErrors command.');
    $this
      ->assertTrue(strpos($ajax_commands[1]['data'], '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.');
  }
}