You are here

public function QuickEditLoadingTest::testTitleBaseField in Zircon Profile 8.0

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

Tests the loading of Quick Edit for the title base field.

File

core/modules/quickedit/src/Tests/QuickEditLoadingTest.php, line 316
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 testTitleBaseField() {
  $this
    ->drupalLogin($this->editorUser);
  $this
    ->drupalGet('node/1');

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

  // Retrieving the metadata should result in a 200 JSON response.
  $htmlPageDrupalSettings = $this->drupalSettings;
  $post = array(
    'fields[0]' => 'node/1/title/en/full',
  );
  $response = $this
    ->drupalPostWithFormat('quickedit/metadata', 'json', $post);
  $this
    ->assertResponse(200);
  $expected = array(
    'node/1/title/en/full' => array(
      'label' => 'Title',
      'access' => TRUE,
      'editor' => 'plain_text',
    ),
  );
  $this
    ->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');

  // 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,
  // containing only a quickeditFieldForm command.
  $post = array(
    'nocssjs' => 'true',
    'reset' => 'true',
  ) + $this
    ->getAjaxPageStatePostData();
  $response = $this
    ->drupalPost('quickedit/form/' . 'node/1/title/en/full', 'application/vnd.drupal-ajax', $post);
  $this
    ->assertResponse(200);
  $ajax_commands = Json::decode($response);
  $this
    ->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
  $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.');

  // 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(
      'form_id' => 'quickedit_field_form',
      '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 PrivateTempStore on the server.
    $response = $this
      ->drupalPost('quickedit/form/' . 'node/1/title/en/full', 'application/vnd.drupal-ajax', $post);
    $this
      ->assertResponse(200);
    $ajax_commands = Json::decode($response);
    $this
      ->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
    $this
      ->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
    $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 PrivateTempStore values to entity storage.
    $post = array(
      'nocssjs' => 'true',
    );
    $response = $this
      ->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
    $this
      ->assertResponse(200);
    $ajax_commands = Json::decode($response);
    $this
      ->assertIdentical(1, count($ajax_commands), 'The entity submission HTTP request results in one AJAX command.');
    $this
      ->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is n quickeditEntitySaved command.');
    $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');
  }
}