4ed751233305ebb7ddb107a3117d9d3c4549bc08
[yaffs-website] / web / core / modules / field / tests / src / Unit / FieldStorageConfigAccessControlHandlerTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Unit;
4
5 use Drupal\Component\Uuid\UuidInterface;
6 use Drupal\Core\Cache\Context\CacheContextsManager;
7 use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
8 use Drupal\Core\DependencyInjection\Container;
9 use Drupal\Core\Entity\EntityManagerInterface;
10 use Drupal\Core\Entity\EntityStorageInterface;
11 use Drupal\Core\Extension\ModuleHandlerInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Drupal\field\Entity\FieldStorageConfig;
14 use Drupal\field\FieldStorageConfigAccessControlHandler;
15 use Drupal\Tests\UnitTestCase;
16
17 /**
18  * Tests the field storage config access controller.
19  *
20  * @group field
21  *
22  * @coversDefaultClass \Drupal\field\FieldStorageConfigAccessControlHandler
23  */
24 class FieldStorageConfigAccessControlHandlerTest extends UnitTestCase {
25
26   /**
27    * The field storage config access controller to test.
28    *
29    * @var \Drupal\field\FieldStorageConfigAccessControlHandler
30    */
31   protected $accessControlHandler;
32
33   /**
34    * The mock module handler.
35    *
36    * @var \Drupal\Core\Extension\ModuleHandlerInterface
37    */
38   protected $moduleHandler;
39
40   /**
41    * The mock account without field storage config access.
42    *
43    * @var \Drupal\Core\Session\AccountInterface
44    */
45   protected $anon;
46
47   /**
48    * The mock account with field storage config access.
49    *
50    * @var \Drupal\Core\Session\AccountInterface
51    */
52   protected $member;
53
54   /**
55    * The mocked test field storage config.
56    *
57    * @var \Drupal\field\FieldStorageConfigInterface
58    */
59   protected $fieldStorage;
60
61   /**
62    * The main entity used for testing.
63    *
64    * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
65    */
66   protected $entity;
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function setUp() {
72     parent::setUp();
73
74     $this->anon = $this->getMock(AccountInterface::class);
75     $this->anon
76       ->expects($this->any())
77       ->method('hasPermission')
78       ->will($this->returnValue(FALSE));
79     $this->anon
80       ->expects($this->any())
81       ->method('id')
82       ->will($this->returnValue(0));
83
84     $this->member = $this->getMock(AccountInterface::class);
85     $this->member
86       ->expects($this->any())
87       ->method('hasPermission')
88       ->will($this->returnValueMap([
89         ['administer node fields', TRUE],
90       ]));
91     $this->member
92       ->expects($this->any())
93       ->method('id')
94       ->will($this->returnValue(2));
95
96     $storageType = $this->getMock(ConfigEntityTypeInterface::class);
97     $storageType
98       ->expects($this->any())
99       ->method('getProvider')
100       ->will($this->returnValue('field'));
101     $storageType
102       ->expects($this->any())
103       ->method('getConfigPrefix')
104       ->will($this->returnValue('field.storage'));
105
106     $entityType = $this->getMock(ConfigEntityTypeInterface::class);
107     $entityType
108       ->expects($this->any())
109       ->method('getProvider')
110       ->will($this->returnValue('node'));
111     $entityType
112       ->expects($this->any())
113       ->method('getConfigPrefix')
114       ->willReturn('node');
115
116     $this->moduleHandler = $this->getMock(ModuleHandlerInterface::class);
117     $this->moduleHandler
118       ->expects($this->any())
119       ->method('getImplementations')
120       ->will($this->returnValue([]));
121     $this->moduleHandler
122       ->expects($this->any())
123       ->method('invokeAll')
124       ->will($this->returnValue([]));
125
126     $storage_access_control_handler = new FieldStorageConfigAccessControlHandler($storageType);
127     $storage_access_control_handler->setModuleHandler($this->moduleHandler);
128
129     $entityManager = $this->getMock(EntityManagerInterface::class);
130     $entityManager
131       ->expects($this->any())
132       ->method('getDefinition')
133       ->willReturnMap([
134         ['field_storage_config', TRUE, $storageType],
135         ['node', TRUE, $entityType],
136       ]);
137     $entityManager
138       ->expects($this->any())
139       ->method('getStorage')
140       ->willReturnMap([
141         ['field_storage_config', $this->getMock(EntityStorageInterface::class)],
142       ]);
143     $entityManager
144       ->expects($this->any())
145       ->method('getAccessControlHandler')
146       ->willReturnMap([
147         ['field_storage_config', $storage_access_control_handler],
148       ]);
149
150     $container = new Container();
151     $container->set('entity.manager', $entityManager);
152     $container->set('uuid', $this->getMock(UuidInterface::class));
153     $container->set('cache_contexts_manager', $this->prophesize(CacheContextsManager::class));
154     \Drupal::setContainer($container);
155
156     $this->fieldStorage = new FieldStorageConfig([
157       'field_name' => 'test_field',
158       'entity_type' => 'node',
159       'type' => 'boolean',
160       'id' => 'node.test_field',
161       'uuid' => '6f2f259a-f3c7-42ea-bdd5-111ad1f85ed1',
162     ]);
163
164     $this->entity = $this->fieldStorage;
165     $this->accessControlHandler = $storage_access_control_handler;
166   }
167
168   /**
169    * Assert method to verify the access by operations.
170    *
171    * @param array $allow_operations
172    *   A list of allowed operations.
173    * @param \Drupal\Core\Session\AccountInterface $user
174    *   The account to use for get access.
175    */
176   public function assertAllowOperations(array $allow_operations, AccountInterface $user) {
177     foreach (['view', 'update', 'delete'] as $operation) {
178       $expected = in_array($operation, $allow_operations);
179       $actual = $this->accessControlHandler->access($this->entity, $operation, $user);
180       $this->assertSame($expected, $actual, "Access problem with '$operation' operation.");
181     }
182   }
183
184   /**
185    * Ensures field storage config access is working properly.
186    */
187   public function testAccess() {
188     $this->assertAllowOperations([], $this->anon);
189     $this->assertAllowOperations(['view', 'update', 'delete'], $this->member);
190
191     $this->fieldStorage->setLocked(TRUE)->save();
192     // Unfortunately, EntityAccessControlHandler has a static cache, which we
193     // therefore must reset manually.
194     $this->accessControlHandler->resetCache();
195
196     $this->assertAllowOperations([], $this->anon);
197     $this->assertAllowOperations(['view', 'update'], $this->member);
198   }
199
200 }