You are here

public function QuickEditLoadingTest::testUserPermissions in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Tests 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
    ->assertSession()
    ->responseNotContains('core/modules/quickedit/js/quickedit.js');
  $this
    ->assertSession()
    ->responseNotContains('core/modules/quickedit/js/editors/formEditor.js');

  // HTML annotation and title class do not exist for users without
  // permission to in-place edit.
  $this
    ->assertSession()
    ->responseNotContains('data-quickedit-entity-id="node/1"');
  $this
    ->assertSession()
    ->responseNotContains('data-quickedit-field-id="node/1/body/en/full"');
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//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();

  // 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
    ->waitForElementRemoved('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]');

  // 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);

  // Ensure that a new revision has not been created.
  $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.');
}