3 namespace Drupal\Tests\field\Unit;
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;
18 * Tests the field storage config access controller.
22 * @coversDefaultClass \Drupal\field\FieldStorageConfigAccessControlHandler
24 class FieldStorageConfigAccessControlHandlerTest extends UnitTestCase {
27 * The field storage config access controller to test.
29 * @var \Drupal\field\FieldStorageConfigAccessControlHandler
31 protected $accessControlHandler;
34 * The mock module handler.
36 * @var \Drupal\Core\Extension\ModuleHandlerInterface
38 protected $moduleHandler;
41 * The mock account without field storage config access.
43 * @var \Drupal\Core\Session\AccountInterface
48 * The mock account with field storage config access.
50 * @var \Drupal\Core\Session\AccountInterface
55 * The mocked test field storage config.
57 * @var \Drupal\field\FieldStorageConfigInterface
59 protected $fieldStorage;
62 * The main entity used for testing.
64 * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
71 protected function setUp() {
74 $this->anon = $this->getMock(AccountInterface::class);
76 ->expects($this->any())
77 ->method('hasPermission')
78 ->will($this->returnValue(FALSE));
80 ->expects($this->any())
82 ->will($this->returnValue(0));
84 $this->member = $this->getMock(AccountInterface::class);
86 ->expects($this->any())
87 ->method('hasPermission')
88 ->will($this->returnValueMap([
89 ['administer node fields', TRUE],
92 ->expects($this->any())
94 ->will($this->returnValue(2));
96 $storageType = $this->getMock(ConfigEntityTypeInterface::class);
98 ->expects($this->any())
99 ->method('getProvider')
100 ->will($this->returnValue('field'));
102 ->expects($this->any())
103 ->method('getConfigPrefix')
104 ->will($this->returnValue('field.storage'));
106 $entityType = $this->getMock(ConfigEntityTypeInterface::class);
108 ->expects($this->any())
109 ->method('getProvider')
110 ->will($this->returnValue('node'));
112 ->expects($this->any())
113 ->method('getConfigPrefix')
114 ->willReturn('node');
116 $this->moduleHandler = $this->getMock(ModuleHandlerInterface::class);
118 ->expects($this->any())
119 ->method('getImplementations')
120 ->will($this->returnValue([]));
122 ->expects($this->any())
123 ->method('invokeAll')
124 ->will($this->returnValue([]));
126 $storage_access_control_handler = new FieldStorageConfigAccessControlHandler($storageType);
127 $storage_access_control_handler->setModuleHandler($this->moduleHandler);
129 $entityManager = $this->getMock(EntityManagerInterface::class);
131 ->expects($this->any())
132 ->method('getDefinition')
134 ['field_storage_config', TRUE, $storageType],
135 ['node', TRUE, $entityType],
138 ->expects($this->any())
139 ->method('getStorage')
141 ['field_storage_config', $this->getMock(EntityStorageInterface::class)],
144 ->expects($this->any())
145 ->method('getAccessControlHandler')
147 ['field_storage_config', $storage_access_control_handler],
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);
156 $this->fieldStorage = new FieldStorageConfig([
157 'field_name' => 'test_field',
158 'entity_type' => 'node',
160 'id' => 'node.test_field',
161 'uuid' => '6f2f259a-f3c7-42ea-bdd5-111ad1f85ed1',
164 $this->entity = $this->fieldStorage;
165 $this->accessControlHandler = $storage_access_control_handler;
169 * Assert method to verify the access by operations.
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.
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.");
185 * Ensures field storage config access is working properly.
187 public function testAccess() {
188 $this->assertAllowOperations([], $this->anon);
189 $this->assertAllowOperations(['view', 'update', 'delete'], $this->member);
191 $this->fieldStorage->setLocked(TRUE)->save();
192 // Unfortunately, EntityAccessControlHandler has a static cache, which we
193 // therefore must reset manually.
194 $this->accessControlHandler->resetCache();
196 $this->assertAllowOperations([], $this->anon);
197 $this->assertAllowOperations(['view', 'update'], $this->member);