You are here

public function EntityCreateAccessTest::testViewPermissionDoesNotGrantCreateAccess in Organic groups 8

Tests that users that can only view cannot access the entity creation form.

File

tests/src/Kernel/Entity/EntityCreateAccessTest.php, line 87

Class

EntityCreateAccessTest
Tests access to the create entity form through the user interface.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testViewPermissionDoesNotGrantCreateAccess() {

  // Create test user.
  $user = User::create([
    'name' => $this
      ->randomString(),
  ]);
  $user
    ->save();

  // Create a group.
  Node::create([
    'title' => $this
      ->randomString(),
    'type' => 'group',
    'uid' => $user
      ->id(),
  ])
    ->save();

  // Make sure the anonymous user exists. This normally is created in the
  // install hook of the User module, but this doesn't run in a KernelTest.
  // @see user_install()
  \Drupal::entityTypeManager()
    ->getStorage('user')
    ->create([
    'uid' => 0,
    'status' => 0,
    'name' => '',
  ])
    ->save();

  // Grant the anonymous user permission to view published content.

  /** @var \Drupal\user\Entity\Role $role */
  $role = Role::create([
    'id' => Role::ANONYMOUS_ID,
    'label' => 'anonymous user',
  ])
    ->grantPermission('access content');
  $role
    ->save();

  // Verify that the user does not have access to the entity create form of
  // the group content type.

  /** @var \Drupal\node\Access\NodeAddAccessCheck $node_access_check */
  $node_access_check = $this->container
    ->get('access_check.node.add');
  $result = $node_access_check
    ->access(User::getAnonymousUser(), $this->groupContentType);
  $this
    ->assertNotInstanceOf('\\Drupal\\Core\\Access\\AccessResultAllowed', $result);

  // Test that the user can access the entity create form when the permission
  // to create group content is granted. Note that node access control is
  // cached, so we need to reset it when we change permissions.
  $this->container
    ->get('entity_type.manager')
    ->getAccessControlHandler('node')
    ->resetCache();
  $role
    ->grantPermission('create post content')
    ->trustData()
    ->save();
  $result = $node_access_check
    ->access(User::getAnonymousUser(), $this->groupContentType);
  $this
    ->assertInstanceOf('\\Drupal\\Core\\Access\\AccessResultAllowed', $result);
}