You are here

public function EntityAccessComplexTest::testAnonymousViewAnyUnpublishedAccess in Group 2.0.x

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

Tests the viewing of any unpublished grouped entities for anonymous.

File

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

Class

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

Namespace

Drupal\Tests\group\Kernel

Code

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

  // Sanity check: Verify that we don't touch published nodes.
  $node_4 = $this
    ->createNode([
    'type' => 'page',
  ]);
  $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 unpublished grouped nodes without permission.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_2, 'view'), 'Anonymous cannot see unpublished grouped nodes without permission.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_3, 'view'), 'Anonymous cannot see unpublished ungrouped nodes.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_4, 'view'), 'The published grouped node cannot be viewed.');
  $this->groupTypeA
    ->getAnonymousRole()
    ->grantPermission('view any unpublished node_as_content:page entity')
    ->save();
  $this->groupTypeB
    ->getAnonymousRole()
    ->grantPermission('view any unpublished node_as_content:page entity')
    ->save();
  $this->accessControlHandler
    ->resetCache();
  $this
    ->assertTrue($this->accessControlHandler
    ->access($node_1, 'view'), 'Anonymous can see any unpublished grouped nodes.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($node_2, 'view'), 'Anonymous can see any unpublished grouped nodes.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_3, 'view'), 'Anonymous cannot see unpublished ungrouped nodes.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($node_4, 'view'), 'The published grouped node cannot be viewed.');
}