function EditLoadingTest::testUserWithPermission

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

Also ensures lazy loading of in-place editors works.

File

drupal/core/modules/edit/lib/Drupal/edit/Tests/EditLoadingTest.php, line 99
Contains \Drupal\edit\Tests\EditLoadingTest.

Class

EditLoadingTest
Tests loading of Edit and lazy-loading of in-place editors.

Namespace

Drupal\edit\Tests

Code

function testUserWithPermission() {
  $this
    ->drupalLogin($this->editor_user);
  $this
    ->drupalGet('node/1');

  // Settings, library and in-place editors.
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertTrue(isset($settings['edit']), 'Edit settings exist.');
  $this
    ->assertTrue(isset($settings['ajaxPageState']['js']['core/modules/edit/js/edit.js']), 'Edit library loaded.');
  $this
    ->assertFalse(isset($settings['ajaxPageState']['js']['core/modules/edit/js/createjs/editingWidgets/formwidget.js']), "'form' in-place editor not loaded.");

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

  // Retrieving the metadata should result in a 200 JSON response.
  $htmlPageDrupalSettings = $this->drupalSettings;
  $response = $this
    ->retrieveMetadata(array(
    'node/1/body/und/full',
  ));
  $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
  $response = $this
    ->retrieveAttachments(array(
    'form',
  ));
  $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.');
  $command = new AppendCommand('body', '<script src="' . file_create_url('core/modules/edit/js/editors/formEditor.js') . '?v=' . VERSION . '"></script>' . "\n");
  $this
    ->assertIdentical($command
    ->render(), $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 editFieldForm command.
  $response = $this
    ->retrieveFieldForm('node/1/body/und/full');
  $this
    ->assertResponse(200);
  $ajax_commands = drupal_json_decode($response);
  $this
    ->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in three AJAX commands.');
  $this
    ->assertIdentical('editFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is an editFieldForm command.');
  $this
    ->assertIdentical('<form ', Unicode::substr($ajax_commands[0]['data'], 0, 6), 'The editFieldForm command contains a form.');
}