You are here

public function ContentAccessModuleTest::testViewAccess in Content Access 8

Test for viewing nodes.

File

tests/src/Functional/ContentAccessModuleTest.php, line 98

Class

ContentAccessModuleTest
Automated BrowserTest Case for content access module.

Namespace

Drupal\Tests\content_access\Functional

Code

public function testViewAccess() {

  // Restrict access to the content type.
  $accessPermissions = [
    'view[anonymous]' => FALSE,
    'view[authenticated]' => FALSE,
  ];
  $this
    ->changeAccessContentType($accessPermissions);

  // Logout admin and try to access the node anonymously.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('node/' . $this->node1
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));

  // Login test user, view node, access must be denied.
  $this
    ->drupalLogin($this->testUser);
  $this
    ->drupalGet('node/' . $this->node1
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));

  // Login admin and grant access for viewing to the test user.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->changeAccessContentTypeKeyword('view');

  // Logout admin and try to access the node anonymously
  // access must be denied again.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('node/' . $this->node1
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));

  // Login test user, view node, access must be granted.
  $this
    ->drupalLogin($this->testUser);
  $this
    ->drupalGet('node/' . $this->node1
    ->id());
  $this
    ->assertSession()
    ->pageTextNotContains($this
    ->t('Access denied'));

  // Login admin and enable per node access.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->changeAccessPerNode();

  // Restrict access on node2 for the test user role.
  $this
    ->changeAccessNodeKeyword($this->node2, 'view', FALSE);

  // Logout admin and try to access both nodes anonymously.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('node/' . $this->node1
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));
  $this
    ->drupalGet('node/' . $this->node2
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));

  // Login test user, view node1, access must be granted.
  $this
    ->drupalLogin($this->testUser);
  $this
    ->drupalGet('node/' . $this->node1
    ->id());
  $this
    ->assertSession()
    ->pageTextNotContains($this
    ->t('Access denied'));

  // View node2, access must be denied.
  $this
    ->drupalGet('node/' . $this->node2
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));

  // Login admin, swap permissions between content type and node2.
  $this
    ->drupalLogin($this->adminUser);

  // Restrict access to content type.
  $this
    ->changeAccessContentTypeKeyword('view', FALSE);

  // Grant access to node2.
  $this
    ->changeAccessNodeKeyword($this->node2, 'view');

  // Logout admin and try to access both nodes anonymously.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('node/' . $this->node1
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));
  $this
    ->drupalGet('node/' . $this->node2
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));

  // Login test user, view node1, access must be denied.
  $this
    ->drupalLogin($this->testUser);
  $this
    ->drupalGet('node/' . $this->node1
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Access denied'));

  // View node2, access must be granted.
  $this
    ->drupalGet('node/' . $this->node2
    ->id());
  $this
    ->assertSession()
    ->pageTextNotContains($this
    ->t('Access denied'));
}