3 namespace Drupal\Tests\entity\Unit;
5 use Drupal\Core\Cache\Context\CacheContextsManager;
6 use Drupal\Core\Config\Entity\ConfigEntityInterface;
7 use Drupal\Core\DependencyInjection\ContainerBuilder;
8 use Drupal\Core\Entity\ContentEntityTypeInterface;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Extension\ModuleHandlerInterface;
11 use Drupal\Core\Language\Language;
12 use Drupal\Core\Language\LanguageInterface;
13 use Drupal\Core\Session\AccountInterface;
14 use Drupal\entity\BundleEntityAccessControlHandler;
15 use Drupal\Tests\UnitTestCase;
16 use Prophecy\Argument;
19 * @coversDefaultClass \Drupal\entity\BundleEntityAccessControlHandler
22 class BundleEntityAccessControlHandlerTest extends UnitTestCase {
27 protected function setUp() {
30 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
31 $module_handler->invokeAll(Argument::any(), Argument::any())->willReturn([]);
32 $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
33 $cache_contexts_manager->assertValidTokens(Argument::any())->willReturn(TRUE);
35 $container = new ContainerBuilder();
36 $container->set('module_handler', $module_handler->reveal());
37 $container->set('cache_contexts_manager', $cache_contexts_manager->reveal());
38 \Drupal::setContainer($container);
42 * @covers ::checkAccess
44 * @dataProvider accessProvider
46 public function testAccess(EntityInterface $entity, $operation, $account, $allowed) {
47 $handler = new BundleEntityAccessControlHandler($entity->getEntityType());
48 $handler->setStringTranslation($this->getStringTranslationStub());
49 $result = $handler->access($entity, $operation, $account);
50 $this->assertEquals($allowed, $result);
54 * Data provider for testAccess().
57 * A list of testAccess method arguments.
59 public function accessProvider() {
60 $entity_type = $this->prophesize(ContentEntityTypeInterface::class);
61 $entity_type->id()->willReturn('green_entity_bundle');
62 $entity_type->getBundleOf()->willReturn('green_entity');
63 $entity_type->getAdminPermission()->willReturn('administer green_entity');
64 $entity_type = $entity_type->reveal();
66 $entity = $this->prophesize(ConfigEntityInterface::class);
67 $entity->getEntityType()->willReturn($entity_type);
68 $entity->getEntityTypeId()->willReturn('green_entity_bundle');
69 $entity->id()->willReturn('default');
70 $entity->uuid()->willReturn('fake uuid');
71 $entity->language()->willReturn(new Language(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED]));
73 // User with no access.
74 $user = $this->buildMockUser(1, 'access content');
75 $data[] = [$entity->reveal(), 'view label', $user->reveal(), FALSE];
77 // Permissions which grant "view label" access.
79 'administer green_entity',
81 'view default green_entity',
82 'view own green_entity',
83 'view any green_entity',
84 'view own default green_entity',
85 'view any default green_entity',
87 foreach ($permissions as $index => $permission) {
88 $user = $this->buildMockUser(10 + $index, $permission);
89 $data[] = [$entity->reveal(), 'view label', $user->reveal(), TRUE];
100 * @param string $permission
101 * The permission to grant.
103 * @return \Prophecy\Prophecy\ObjectProphecy
106 protected function buildMockUser($uid, $permission) {
107 $account = $this->prophesize(AccountInterface::class);
108 $account->id()->willReturn($uid);
109 $account->hasPermission($permission)->willReturn(TRUE);
110 $account->hasPermission(Argument::any())->willReturn(FALSE);