You are here

public function ContentAccessModuleTest::testOwnViewAccess in Content Access 8

Test own view access.

File

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

Class

ContentAccessModuleTest
Automated BrowserTest Case for content access module.

Namespace

Drupal\Tests\content_access\Functional

Code

public function testOwnViewAccess() {

  // Setup 2 test users.
  $testUser1 = $this->testUser;
  $testUser2 = $this
    ->drupalCreateUser();

  // Change ownership of test nodes to test users.
  $this->node1
    ->setOwner($testUser1);
  $this->node1
    ->save();
  $this->node2
    ->setOwner($testUser2);
  $this->node2
    ->save();

  // Remove all view permissions for this content type.
  $accessPermissions = [
    'view[anonymous]' => FALSE,
    'view[authenticated]' => FALSE,
    'view_own[anonymous]' => FALSE,
    'view_own[authenticated]' => FALSE,
  ];
  $this
    ->changeAccessContentType($accessPermissions);

  // Allow view own content for test user 1 and 2 roles.
  $this
    ->changeAccessContentTypeKeyword('view_own', TRUE, $testUser1);
  $this
    ->changeAccessContentTypeKeyword('view_own', TRUE, $testUser2);

  // 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 1, view node1, access must be granted.
  $this
    ->drupalLogin($testUser1);
  $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 test user 2, view node1, access must be denied.
  $this
    ->drupalLogin($testUser2);
  $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'));
}