You are here

public function EntityAccessComplexTest::testAnonymousViewAnyPublishedAccess in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Kernel/EntityAccessComplexTest.php \Drupal\Tests\group\Kernel\EntityAccessComplexTest::testAnonymousViewAnyPublishedAccess()

Tests the viewing of any published grouped entities for anonymous.

File

tests/src/Kernel/EntityAccessComplexTest.php, line 262

Class

EntityAccessComplexTest
Tests that Group properly checks access for "complex" grouped entities.

Namespace

Drupal\Tests\group\Kernel

Code

public function testAnonymousViewAnyPublishedAccess() {
  $this->entityTypeManager
    ->getStorage('user_role')
    ->load('anonymous')
    ->grantPermission('access content')
    ->save();
  $node_1 = $this
    ->createNode([
    'type' => 'page',
  ]);
  $node_2 = $this
    ->createNode([
    'type' => 'page',
  ]);
  $node_3 = $this
    ->createNode([
    'type' => 'page',
  ]);

  // Sanity check: Verify that we don't touch unpublished nodes.
  $node_4 = $this
    ->createNode([
    'type' => 'page',
    'status' => 0,
  ]);
  $group_a = $this
    ->createGroup([
    'type' => $this->groupTypeA
      ->id(),
  ]);
  $group_a
    ->addContent($node_1, 'node_as_content:page');
  $group_a
    ->addContent($node_4, 'node_as_content:page');
  $group_b = $this
    ->createGroup([
    'type' => $this->groupTypeB
      ->id(),
  ]);
  $group_b
    ->addContent($node_2, 'node_as_content:page');
  $this
    ->setCurrentUser(new AnonymousUserSession());
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_1, 'view'), 'Anonymous cannot see published grouped nodes without permission.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_2, 'view'), 'Anonymous cannot see published grouped nodes without permission.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($node_3, 'view'), 'Anonymous can see published ungrouped nodes.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_4, 'view'), 'The unpublished grouped node cannot be viewed.');
  $this->groupTypeA
    ->getAnonymousRole()
    ->grantPermission('view any node_as_content:page entity')
    ->save();
  $this->groupTypeB
    ->getAnonymousRole()
    ->grantPermission('view any node_as_content:page entity')
    ->save();
  $this->accessControlHandler
    ->resetCache();
  $this
    ->assertTrue($this->accessControlHandler
    ->access($node_1, 'view'), 'Anonymous can see any published grouped nodes.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($node_2, 'view'), 'Anonymous can see any published grouped nodes.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($node_3, 'view'), 'Anonymous can see published ungrouped nodes.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_4, 'view'), 'The unpublished grouped node cannot be viewed.');
}