You are here

public function QuickEditLoadingTest::testTitleBaseField in Quick Edit 7

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

File

./quickedit.test, line 325
Tests loading of Quick Edit and lazy-loading of in-place editors.

Class

QuickEditLoadingTest
Tests loading of Quick Edit and lazy-loading of in-place editors.

Code

public function testTitleBaseField() {
  $this
    ->drupalLogin($this->editor_user);

  // First, ensure quickedit_preprocess_node()'s wrapping of the 'title'
  // template variable does not result in an XSS. We cannot check this on the
  // full node page, because there the 'node' template's 'title' variable is
  // not printed: there, the actual
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('<script>alert("EVIL!")</script>');

  // Next, try in-place editing the node on its full node page. Also ensure
  // quickedit_preprocess_page()'s wrapping of the 'title' template variable
  // does not result in an XSS.
  $this
    ->drupalGet('node/1');
  $this
    ->assertNoRaw('<script>alert("EVIL!")</script>');

  // Ensure that the full page title is actually in-place editable
  $node = node_load(1);
  $elements = $this
    ->xpath('//h1/div[@data-quickedit-field-id="node/1/title/und/full" and normalize-space(text())=:title]', array(
    ':title' => $node->title,
  ));
  $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(
    'entities[0]' => 'node/1',
    'fields[0]' => 'node/1/title/und/full',
  );
  $response = $this
    ->drupalPostCustom('quickedit/metadata', 'application/json', $post);
  $this
    ->assertResponse(200);
  $expected = array(
    // The label should be check_plain()'d.
    'node/1' => array(
      'label' => '&lt;script&gt;alert(&quot;EVIL!&quot;)&lt;/script&gt;',
    ),
    'node/1/title/und/full' => array(
      'label' => 'Title',
      'access' => TRUE,
      'editor' => 'plain_text',
      'aria' => 'Entity node 1, field Title',
    ),
  );
  $this
    ->assertIdentical(drupal_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 an quickeditFieldForm command.
  $post = array(
    'nocssjs' => 'true',
    'reset' => 'true',
  ) + $this
    ->getAjaxPageStatePostData();
  $response = $this
    ->drupalPostCustom('quickedit/form/' . 'node/1/title/und/full', 'application/vnd.drupal-ajax', $post);
  $this
    ->assertResponse(200);
  $ajax_commands = drupal_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 an quickeditFieldForm command.');
  $this
    ->assertIdentical('<form ', drupal_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' => '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 TempStore on the server.
    $response = $this
      ->drupalPostCustom('quickedit/form/' . 'node/1/title/und/full', 'application/vnd.drupal-ajax', $post);
    $this
      ->assertResponse(200);
    $ajax_commands = drupal_json_decode($response);
    $this
      ->assertIdentical(2, count($ajax_commands), 'The field form HTTP request results in two AJAX commands.');
    $this
      ->assertIdentical('settings', $ajax_commands[0]['command'], 'The first AJAX command is a settings command.');
    $this
      ->assertIdentical('quickeditFieldFormSaved', $ajax_commands[1]['command'], 'The second AJAX command is an quickeditFieldFormSaved command.');
    $this
      ->assertTrue(strpos($ajax_commands[1]['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',
    );
    $response = $this
      ->drupalPostCustom('quickedit/entity/' . 'node/1', 'application/json', $post);
    $this
      ->assertResponse(200);
    $ajax_commands = drupal_json_decode($response);
    $this
      ->assertIdentical(2, count($ajax_commands), 'The entity submission HTTP request results in two AJAX commands.');
    $this
      ->assertIdentical('settings', $ajax_commands[0]['command'], 'The first AJAX command is a settings command.');
    $this
      ->assertIdentical('quickeditEntitySaved', $ajax_commands[1]['command'], 'The second AJAX command is an quickeditEntitySaved command.');
    $this
      ->assertIdentical($ajax_commands[1]['data']['entity_type'], 'node', 'Saved entity is of type node.');
    $this
      ->assertIdentical($ajax_commands[1]['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');
  }
}