function BlockContentAccessHandlerTest::testAccess

Same name in other branches
  1. 8.9.x core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php \Drupal\Tests\block_content\Kernel\BlockContentAccessHandlerTest::testAccess()
  2. 10 core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php \Drupal\Tests\block_content\Kernel\BlockContentAccessHandlerTest::testAccess()
  3. 11.x core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php \Drupal\Tests\block_content\Kernel\BlockContentAccessHandlerTest::testAccess()

@covers ::checkAccess

@dataProvider providerTestAccess

File

core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php, line 96

Class

BlockContentAccessHandlerTest
Tests the block content entity access handler.

Namespace

Drupal\Tests\block_content\Kernel

Code

public function testAccess($operation, $published, $reusable, $permissions, $parent_access, $expected_access) {
    $published ? $this->blockEntity
        ->setPublished() : $this->blockEntity
        ->setUnpublished();
    $reusable ? $this->blockEntity
        ->setReusable() : $this->blockEntity
        ->setNonReusable();
    $user = User::create([
        'name' => 'Someone',
        'mail' => 'hi@example.com',
    ]);
    if ($permissions) {
        foreach ($permissions as $permission) {
            $this->role
                ->grantPermission($permission);
        }
        $this->role
            ->save();
    }
    $user->addRole($this->role
        ->id());
    $user->save();
    if ($parent_access) {
        $parent_entity = $this->prophesize(AccessibleInterface::class);
        $expected_parent_result = NULL;
        switch ($parent_access) {
            case 'allowed':
                $expected_parent_result = AccessResult::allowed();
                break;
            case 'neutral':
                $expected_parent_result = AccessResult::neutral();
                break;
            case 'forbidden':
                $expected_parent_result = AccessResult::forbidden();
                break;
        }
        $parent_entity->access($operation, $user, TRUE)
            ->willReturn($expected_parent_result)
            ->shouldBeCalled();
        $this->blockEntity
            ->setAccessDependency($parent_entity->reveal());
    }
    $this->blockEntity
        ->save();
    $result = $this->accessControlHandler
        ->access($this->blockEntity, $operation, $user, TRUE);
    switch ($expected_access) {
        case 'allowed':
            $this->assertTrue($result->isAllowed());
            break;
        case 'forbidden':
            $this->assertTrue($result->isForbidden());
            break;
        case 'neutral':
            $this->assertTrue($result->isNeutral());
            break;
        default:
            $this->fail('Unexpected access type');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.