You are here

public function EntityBlockTest::testAccessControl in Lightning Layout 8

Tests that entity blocks do proper access control.

File

tests/src/Functional/EntityBlockTest.php, line 51

Class

EntityBlockTest
@group lightning_layout

Namespace

Drupal\Tests\lightning_layout\Functional

Code

public function testAccessControl() {
  $node_type = $this
    ->drupalCreateContentType();
  $node = Node::create([
    'type' => $node_type
      ->id(),
    'title' => $this
      ->randomString(),
  ]);
  $node
    ->setUnpublished();
  $node
    ->save();
  $block = $this
    ->drupalPlaceBlock('entity_block:node', [
    'entity' => $node
      ->id(),
  ]);
  $account = $this->container
    ->get('current_user');
  $this
    ->assertTrue($account
    ->isAnonymous());
  user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
    'access content',
  ]);

  // The node is unpublished, so the anonymous user cannot access the block.
  $this
    ->assertEmpty($block
    ->getPlugin()
    ->build());

  // Publish the node, reset static caches, and ensure that the anonymous
  // user can now access the block.
  $node
    ->setPublished();
  $node
    ->save();
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  $entity_type_manager
    ->getStorage('node')
    ->resetCache();
  $entity_type_manager
    ->getAccessControlHandler('node')
    ->resetCache();
  $this
    ->assertNotEmpty($block
    ->getPlugin()
    ->build());
}