You are here

public function FilterNodeAccessTest::testFilterNodeAccess in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/Views/FilterNodeAccessTest.php \Drupal\Tests\node\Functional\Views\FilterNodeAccessTest::testFilterNodeAccess()

Tests the node access filter.

File

core/modules/node/tests/src/Functional/Views/FilterNodeAccessTest.php, line 86

Class

FilterNodeAccessTest
Tests the node_access filter handler.

Namespace

Drupal\Tests\node\Functional\Views

Code

public function testFilterNodeAccess() {
  $this
    ->drupalLogin($this->users[0]);
  $this
    ->drupalGet('test_filter_node_access');

  // Test that the private node of the current user is shown.
  $this
    ->assertSession()
    ->pageTextContains('Private Article created by ' . $this->users[0]
    ->getAccountName());

  // Test that the private node of the other use isn't shown.
  $this
    ->assertSession()
    ->pageTextNotContains('Private Article created by ' . $this->users[1]
    ->getAccountName());

  // Test that both public nodes are shown.
  $this
    ->assertSession()
    ->pageTextContains('Public Article created by ' . $this->users[0]
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextContains('Public Article created by ' . $this->users[1]
    ->getAccountName());

  // Switch users and test the other private node is shown.
  $this
    ->drupalLogin($this->users[1]);
  $this
    ->drupalGet('test_filter_node_access');

  // Test that the private node of the current user is shown.
  $this
    ->assertSession()
    ->pageTextContains('Private Article created by ' . $this->users[1]
    ->getAccountName());

  // Test that the private node of the other use isn't shown.
  $this
    ->assertSession()
    ->pageTextNotContains('Private Article created by ' . $this->users[0]
    ->getAccountName());

  // Test that a user with administer nodes permission can't see all nodes.
  $administer_nodes_user = $this
    ->drupalCreateUser([
    'access content',
    'administer nodes',
  ]);
  $this
    ->drupalLogin($administer_nodes_user);
  $this
    ->drupalGet('test_filter_node_access');
  $this
    ->assertSession()
    ->pageTextNotContains('Private Article created by ' . $this->users[0]
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextNotContains('Private Article created by ' . $this->users[1]
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextContains('Public Article created by ' . $this->users[0]
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextContains('Public Article created by ' . $this->users[1]
    ->getAccountName());

  // Test that a user with bypass node access can see all nodes.
  $bypass_access_user = $this
    ->drupalCreateUser([
    'access content',
    'bypass node access',
  ]);
  $this
    ->drupalLogin($bypass_access_user);
  $this
    ->drupalGet('test_filter_node_access');
  $this
    ->assertSession()
    ->pageTextContains('Private Article created by ' . $this->users[0]
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextContains('Private Article created by ' . $this->users[1]
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextContains('Public Article created by ' . $this->users[0]
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextContains('Public Article created by ' . $this->users[1]
    ->getAccountName());
}