3 namespace Drupal\Tests\entity\Unit;
5 use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
6 use Drupal\Core\Entity\ContentEntityTypeInterface;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\entity\EntityPermissionProvider;
10 use Drupal\Tests\UnitTestCase;
11 use Drupal\user\EntityOwnerInterface;
14 * @coversDefaultClass \Drupal\entity\EntityPermissionProvider
17 class EntityPermissionProviderTest extends UnitTestCase {
20 * The entity permission provider.
22 * @var \Drupal\entity\EntityPermissionProviderInterface
24 protected $permissionProvider;
29 protected function setUp() {
32 $entity_type_bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class);
33 $entity_type_bundle_info->getBundleInfo('white_entity')->willReturn([
34 'first' => ['label' => 'First'],
35 'second' => ['label' => 'Second'],
37 $entity_type_bundle_info->getBundleInfo('black_entity')->willReturn([
38 'third' => ['label' => 'Third'],
40 $this->permissionProvider = new EntityPermissionProvider($entity_type_bundle_info->reveal());
41 $this->permissionProvider->setStringTranslation($this->getStringTranslationStub());
45 * @covers ::buildPermissions
47 * @dataProvider entityTypeProvider
49 public function testBuildPermissions(EntityTypeInterface $entity_type, array $expected_permissions) {
50 $permissions = $this->permissionProvider->buildPermissions($entity_type);
51 $this->assertEquals(array_keys($expected_permissions), array_keys($permissions));
52 foreach ($permissions as $name => $permission) {
53 $this->assertEquals('entity_module_test', $permission['provider']);
54 $this->assertEquals($expected_permissions[$name], $permission['title']);
59 * Data provider for testBuildPermissions().
62 * A list of testBuildPermissions method arguments.
64 public function entityTypeProvider() {
66 // Content entity type.
67 $entity_type = $this->prophesize(ContentEntityTypeInterface::class);
68 $entity_type->getProvider()->willReturn('entity_module_test');
69 $entity_type->id()->willReturn('green_entity');
70 $entity_type->getSingularLabel()->willReturn('green entity');
71 $entity_type->getPluralLabel()->willReturn('green entities');
72 $entity_type->isSubclassOf(EntityOwnerInterface::class)->willReturn(FALSE);
73 $entity_type->getPermissionGranularity()->willReturn('entity_type');
74 $expected_permissions = [
75 'administer green_entity' => 'Administer green entities',
76 'access green_entity overview' => 'Access the green entities overview page',
77 'view green_entity' => 'View green entities',
78 'create green_entity' => 'Create green entities',
79 'update green_entity' => 'Update green entities',
80 'delete green_entity' => 'Delete green entities',
82 $data[] = [$entity_type->reveal(), $expected_permissions];
84 // Content entity type with owner.
85 $entity_type = $this->prophesize(ContentEntityTypeInterface::class);
86 $entity_type->getProvider()->willReturn('entity_module_test');
87 $entity_type->id()->willReturn('blue_entity');
88 $entity_type->getSingularLabel()->willReturn('blue entity');
89 $entity_type->getPluralLabel()->willReturn('blue entities');
90 $entity_type->isSubclassOf(EntityOwnerInterface::class)->willReturn(TRUE);
91 $entity_type->getPermissionGranularity()->willReturn('entity_type');
92 $expected_permissions = [
93 'administer blue_entity' => 'Administer blue entities',
94 'access blue_entity overview' => 'Access the blue entities overview page',
95 'view any blue_entity' => 'View any blue entity',
96 'view own blue_entity' => 'View own blue entities',
97 'create blue_entity' => 'Create blue entities',
98 'update any blue_entity' => 'Update any blue entity',
99 'update own blue_entity' => 'Update own blue entities',
100 'delete any blue_entity' => 'Delete any blue entity',
101 'delete own blue_entity' => 'Delete own blue entities',
103 $data[] = [$entity_type->reveal(), $expected_permissions];
105 // Content entity type with bundles.
106 $entity_type = $this->prophesize(ContentEntityTypeInterface::class);
107 $entity_type->getProvider()->willReturn('entity_module_test');
108 $entity_type->id()->willReturn('white_entity');
109 $entity_type->getSingularLabel()->willReturn('white entity');
110 $entity_type->getPluralLabel()->willReturn('white entities');
111 $entity_type->isSubclassOf(EntityOwnerInterface::class)->willReturn(FALSE);
112 $entity_type->getPermissionGranularity()->willReturn('bundle');
113 $expected_permissions = [
114 'administer white_entity' => 'Administer white entities',
115 'access white_entity overview' => 'Access the white entities overview page',
116 'view white_entity' => 'View white entities',
117 'create first white_entity' => 'First: Create white entities',
118 'update first white_entity' => 'First: Update white entities',
119 'delete first white_entity' => 'First: Delete white entities',
120 'create second white_entity' => 'Second: Create white entities',
121 'update second white_entity' => 'Second: Update white entities',
122 'delete second white_entity' => 'Second: Delete white entities',
124 $data[] = [$entity_type->reveal(), $expected_permissions];
126 // Content entity type with bundles and owner.
127 $entity_type = $this->prophesize(ContentEntityTypeInterface::class);
128 $entity_type->getProvider()->willReturn('entity_module_test');
129 $entity_type->id()->willReturn('black_entity');
130 $entity_type->getSingularLabel()->willReturn('black entity');
131 $entity_type->getPluralLabel()->willReturn('black entities');
132 $entity_type->isSubclassOf(EntityOwnerInterface::class)->willReturn(TRUE);
133 $entity_type->getPermissionGranularity()->willReturn('bundle');
134 $expected_permissions = [
135 'administer black_entity' => 'Administer black entities',
136 'access black_entity overview' => 'Access the black entities overview page',
137 'view any black_entity' => 'View any black entity',
138 'view own black_entity' => 'View own black entities',
139 'create third black_entity' => 'Third: Create black entities',
140 'update any third black_entity' => 'Third: Update any black entity',
141 'update own third black_entity' => 'Third: Update own black entities',
142 'delete any third black_entity' => 'Third: Delete any black entity',
143 'delete own third black_entity' => 'Third: Delete own black entities',
145 $data[] = [$entity_type->reveal(), $expected_permissions];