You are here

public function QuickEditLoadingTest::testUserPermissions in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditLoadingTest.php \Drupal\Tests\quickedit\FunctionalJavascript\QuickEditLoadingTest::testUserPermissions()
  2. 10 core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditLoadingTest.php \Drupal\Tests\quickedit\FunctionalJavascript\QuickEditLoadingTest::testUserPermissions()

Test the loading of Quick Edit with different permissions.

File

core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditLoadingTest.php, line 125

Class

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

Namespace

Drupal\Tests\quickedit\FunctionalJavascript

Code

public function testUserPermissions() {
  $assert = $this
    ->assertSession();
  $this
    ->drupalLogin($this->authorUser);
  $this
    ->drupalGet('node/1');

  // Library and in-place editors.
  $this
    ->assertNoRaw('core/modules/quickedit/js/quickedit.js', 'Quick Edit library not loaded.');
  $this
    ->assertNoRaw('core/modules/quickedit/js/editors/formEditor.js', "'form' in-place editor not loaded.");

  // HTML annotation and title class does not exist for users without
  // permission to in-place edit.
  $this
    ->assertNoRaw('data-quickedit-entity-id="node/1"');
  $this
    ->assertNoRaw('data-quickedit-field-id="node/1/body/en/full"');
  $this
    ->assertNoFieldByXPath('//h1[contains(@class, "js-quickedit-page-title")]');
  $assert
    ->linkNotExists('Quick edit');

  // Tests the loading of Quick Edit when a user does have access to it.
  // Also ensures lazy loading of in-place editors works.
  $nid = $this->testNode
    ->id();

  // There should be only one revision so far.
  $node = Node::load($nid);
  $vids = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->revisionIds($node);
  $this
    ->assertCount(1, $vids, 'The node has only one revision.');
  $original_log = $node->revision_log->value;
  $this
    ->drupalLogin($this->editorUser);
  $this
    ->drupalGet('node/' . $nid);
  $page = $this
    ->getSession()
    ->getPage();

  // Wait "Quick edit" button for node.
  $assert
    ->waitForElement('css', '[data-quickedit-entity-id="node/' . $nid . '"] .contextual .quickedit');

  // Click by "Quick edit".
  $this
    ->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');

  // Switch to body field.
  $page
    ->find('css', '[data-quickedit-field-id="node/' . $nid . '/body/en/full"]')
    ->click();
  $assert
    ->assertWaitOnAjaxRequest();

  // Wait and update body field.
  $body_field_locator = '[name="body[0][value]"]';
  $body_text = 'Fine thanks.';
  $assert
    ->waitForElementVisible('css', $body_field_locator)
    ->setValue('<p>' . $body_text . '</p>');

  // Wait and click by "Save" button after body field was changed.
  $assert
    ->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')
    ->click();
  $assert
    ->assertWaitOnAjaxRequest();
  $node = Node::load($nid);
  $vids = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->revisionIds($node);
  $this
    ->assertCount(1, $vids, 'The node has only one revision.');
  $this
    ->assertSame($original_log, $node->revision_log->value, 'The revision log message is unchanged.');

  // Ensure that the changes take effect.
  $assert
    ->responseMatches("|\\s*{$body_text}\\s*|");

  // Reload the page and check for updated body.
  $this
    ->drupalGet('node/' . $nid);
  $assert
    ->pageTextContains($body_text);
}