function BlockContentAccessHandlerTest::testAccess
Same name in other branches
- 9 core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php \Drupal\Tests\block_content\Kernel\BlockContentAccessHandlerTest::testAccess()
- 8.9.x core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php \Drupal\Tests\block_content\Kernel\BlockContentAccessHandlerTest::testAccess()
- 10 core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php \Drupal\Tests\block_content\Kernel\BlockContentAccessHandlerTest::testAccess()
Test block content entity access.
@covers ::checkAccess
@dataProvider providerTestAccess
@phpstan-param class-string<\Drupal\Core\Access\AccessResultInterface>|null $parent_access @phpstan-param class-string<\Drupal\Core\Access\AccessResultInterface> $expected_access
Parameters
string $operation: The entity operation to test.
bool $published: Whether the latest revision should be published.
bool $reusable: Whether the block content should be reusable. Non-reusable blocks are typically used in Layout Builder.
array $permissions: Permissions to grant to the test user.
bool $isLatest: Whether the block content should be the latest revision when checking access. If FALSE, multiple revisions will be created, and an older revision will be loaded before checking access.
string|null $parent_access: Whether the test user has access to the parent entity, valid values are class names of classes implementing AccessResultInterface. Set to NULL to assert parent will not be called.
string $expected_access: The expected access for the user and block content. Valid values are class names of classes implementing AccessResultInterface
string|null $expected_access_message: The expected access message.
File
-
core/
modules/ block_content/ tests/ src/ Kernel/ BlockContentAccessHandlerTest.php, line 139
Class
- BlockContentAccessHandlerTest
- Tests the block content entity access handler.
Namespace
Drupal\Tests\block_content\KernelCode
public function testAccess(string $operation, bool $published, bool $reusable, array $permissions, bool $isLatest, ?string $parent_access, string $expected_access, ?string $expected_access_message = NULL) : void {
/** @var \Drupal\Core\Entity\RevisionableStorageInterface $entityStorage */
$entityStorage = \Drupal::entityTypeManager()->getStorage('block_content');
$loadRevisionId = NULL;
if (!$isLatest) {
// Save a historical revision, then setup for a new revision to be saved.
$this->blockEntity
->save();
$loadRevisionId = $this->blockEntity
->getRevisionId();
$this->blockEntity = $entityStorage->createRevision($this->blockEntity);
}
$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())
->save();
if ($parent_access !== NULL) {
$parent_entity = $this->prophesize(AccessibleInterface::class);
$expected_parent_result = new $parent_access();
$parent_entity->access($operation, $user, TRUE)
->willReturn($expected_parent_result)
->shouldBeCalled();
$this->blockEntity
->setAccessDependency($parent_entity->reveal());
}
$this->blockEntity
->save();
// Reload a previous revision.
if ($loadRevisionId !== NULL) {
$this->blockEntity = $entityStorage->loadRevision($loadRevisionId);
}
$result = $this->accessControlHandler
->access($this->blockEntity, $operation, $user, TRUE);
$this->assertInstanceOf($expected_access, $result);
if ($expected_access_message !== NULL) {
$this->assertInstanceOf(AccessResultReasonInterface::class, $result);
$this->assertEquals($expected_access_message, $result->getReason());
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.