Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / tests / src / Unit / BundleEntityAccessControlHandlerTest.php
1 <?php
2
3 namespace Drupal\Tests\entity\Unit;
4
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;
17
18 /**
19  * @coversDefaultClass \Drupal\entity\BundleEntityAccessControlHandler
20  * @group entity
21  */
22 class BundleEntityAccessControlHandlerTest extends UnitTestCase {
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
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);
34
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);
39   }
40
41   /**
42    * @covers ::checkAccess
43    *
44    * @dataProvider accessProvider
45    */
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);
51   }
52
53   /**
54    * Data provider for testAccess().
55    *
56    * @return array
57    *   A list of testAccess method arguments.
58    */
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();
65
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]));
72
73     // User with no access.
74     $user = $this->buildMockUser(1, 'access content');
75     $data[] = [$entity->reveal(), 'view label', $user->reveal(), FALSE];
76
77     // Permissions which grant "view label" access.
78     $permissions = [
79       'administer green_entity',
80       'view 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',
86     ];
87     foreach ($permissions as $index => $permission) {
88       $user = $this->buildMockUser(10 + $index, $permission);
89       $data[] = [$entity->reveal(), 'view label', $user->reveal(), TRUE];
90     }
91
92     return $data;
93   }
94
95   /**
96    * Builds a mock user.
97    *
98    * @param int $uid
99    *   The user ID.
100    * @param string $permission
101    *   The permission to grant.
102    *
103    * @return \Prophecy\Prophecy\ObjectProphecy
104    *   The user mock.
105    */
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);
111
112     return $account;
113   }
114
115 }