function FilterNodeAccessTest::testFilterNodeAccess

Same name in other branches
  1. 9 core/modules/node/tests/src/Functional/Views/FilterNodeAccessTest.php \Drupal\Tests\node\Functional\Views\FilterNodeAccessTest::testFilterNodeAccess()
  2. 8.9.x core/modules/node/tests/src/Functional/Views/FilterNodeAccessTest.php \Drupal\Tests\node\Functional\Views\FilterNodeAccessTest::testFilterNodeAccess()
  3. 10 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 88

Class

FilterNodeAccessTest
Tests the node_access filter handler.

Namespace

Drupal\Tests\node\Functional\Views

Code

public function testFilterNodeAccess() : void {
    $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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.