You are here

public function QuickEditLoadingTest::testUserWithPermission in Quick Edit 7

Tests the loading of Quick Edit when a user does have access to it.

Also ensures lazy loading of in-place editors works.

File

./quickedit.test, line 150
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 testUserWithPermission() {
  $this
    ->drupalLogin($this->editor_user);
  $this
    ->drupalGet('node/1');

  // Library and in-place editors.
  $settings = $this
    ->drupalGetSettings();
  $module_path = drupal_get_path('module', 'quickedit');
  $this
    ->assertTrue(isset($settings['ajaxPageState']['js'][$module_path . '/js/quickedit.js']), 'Quick Edit library loaded.');
  $this
    ->assertFalse(isset($settings['ajaxPageState']['js'][$module_path . '/js/editors/formEditor.js']), "'form' in-place editor not loaded.");

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

  // There should be only one revision so far.
  $revisions = node_revision_list(node_load(1));
  $this
    ->assertIdentical(1, count($revisions), 'The node has only one revision.');
  $original_log = $revisions[1]->log;

  // Retrieving the metadata should result in a 200 JSON response.
  $htmlPageDrupalSettings = $this->drupalSettings;
  $post = array(
    'fields[0]' => 'node/1/body/und/full',
  );
  $response = $this
    ->drupalPostCustom('quickedit/metadata', 'application/json', $post);
  $this
    ->assertResponse(200);
  $expected = array(
    'node/1/body/und/full' => array(
      'label' => 'Body',
      'access' => TRUE,
      'editor' => 'form',
      'aria' => 'Entity node 1, field Body',
    ),
  );
  $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 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
  $post = array(
    'editors[0]' => 'form',
  ) + $this
    ->getAjaxPageStatePostData();
  $response = $this
    ->drupalPostCustom('quickedit/attachments', 'application/vnd.drupal-ajax', $post);
  $ajax_commands = drupal_json_decode($response);
  $this
    ->assertIdentical(2, count($ajax_commands), 'The attachments HTTP request results in two AJAX commands.');

  // 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.');
  $expected = array(
    'command' => 'insert',
    'method' => 'append',
    'selector' => 'body',
    'data' => '<script type="text/javascript" src="' . file_create_url($module_path . '/js/editors/formEditor.js') . '?v=' . VERSION . '"></script>' . "\n",
    'settings' => NULL,
  );
  $this
    ->assertIdentical($expected, $ajax_commands[1], 'The append command contains the expected data.');

  // 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/body/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 editFieldForm 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(
      'body[und][0][summary]' => '',
      'body[und][0][value]' => '<p>Fine thanks.</p>',
      'body[und][0][format]' => 'filtered_html',
      '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/body/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'], 'Fine thanks.'), 'Form value saved and printed back.');
    $this
      ->assertIdentical($ajax_commands[1]['other_view_modes'], array(), 'Field was not rendered in any other view mode.');

    // 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.
    $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('Fine thanks.');

    // Ensure no new revision was created and the log message is unchanged.
    $revisions = node_revision_list(node_load(1));
    $this
      ->assertIdentical(1, count($revisions), 'The node has only one revision.');
    $this
      ->assertIdentical($original_log, $revisions[1]->log, 'The revision log message is unchanged.');

    // 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.
    variable_set('node_options_article', array(
      'status',
      'revision',
    ));

    // Retrieve field form.
    $post = array(
      'nocssjs' => 'true',
      'reset' => 'true',
    );
    $response = $this
      ->drupalPostCustom('quickedit/form/' . 'node/1/body/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.');

    // 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[und][0][value]'] = '<p>kthxbye</p>';
    $post = array(
      'form_id' => 'quickedit_field_form',
      'form_token' => $token_match[1],
      'form_build_id' => $build_id_match[1],
    );
    $post += $edit + $this
      ->getAjaxPageStatePostData();
    $response = $this
      ->drupalPostCustom('quickedit/form/' . 'node/1/body/und/full', 'application/vnd.drupal-ajax', $post);

    // @todo Uncomment the below once https://drupal.org/node/2063303 is fixed.
    // $this->assertIdentical('[]', $response);
    $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'], 'kthxbye'), 'Form value saved and printed back.');

    // Save the entity.
    $post = array(
      'nocssjs' => 'true',
    );
    $response = $this
      ->drupalPostCustom('quickedit/entity/' . 'node/1', 'application/json', $post);

    // @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.
    $revisions = node_revision_list(node_load(1));
    $this
      ->assertIdentical(2, count($revisions), 'The node has two revisions.');
    $this
      ->assertIdentical($original_log, $revisions[1]->log, 'The first revision log message is unchanged.');
    $this
      ->assertIdentical('Updated the <em class="placeholder">Body</em> field through in-place editing.', $revisions[2]->log, 'The second revision log message was correctly generated by Edit module.');
  }
}