diff --git a/core/modules/field/tests/src/Unit/FieldConfigAccessControlHandlerTest.php b/core/modules/field/tests/src/Unit/FieldConfigAccessControlHandlerTest.php new file mode 100644 index 0000000..0ad8265 --- /dev/null +++ b/core/modules/field/tests/src/Unit/FieldConfigAccessControlHandlerTest.php @@ -0,0 +1,43 @@ +entity = new FieldConfig([ + 'field_name' => $this->fieldStorage->getName(), + 'entity_type' => 'node', + 'fieldStorage' => $this->fieldStorage, + 'bundle' => 'test_bundle', + 'field_type' => 'test_field', + ], 'node'); + + $this->accessControlHandler = new FieldConfigAccessControlHandler($this->entity->getEntityType()); + $this->accessControlHandler->setModuleHandler($this->moduleHandler); + } + + /** + * Ensures field config access is working properly. + */ + public function testAccess() { + $this->assertAllowOperations([], $this->anon); + $this->assertAllowOperations(['view', 'update', 'delete'], $this->member); + } + +} diff --git a/core/modules/field/tests/src/Unit/FieldStorageConfigAccessControlHandlerTest.php b/core/modules/field/tests/src/Unit/FieldStorageConfigAccessControlHandlerTest.php index 99a64ba..4ed7512 100644 --- a/core/modules/field/tests/src/Unit/FieldStorageConfigAccessControlHandlerTest.php +++ b/core/modules/field/tests/src/Unit/FieldStorageConfigAccessControlHandlerTest.php @@ -31,6 +31,13 @@ class FieldStorageConfigAccessControlHandlerTest extends UnitTestCase { protected $accessControlHandler; /** + * The mock module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** * The mock account without field storage config access. * * @var \Drupal\Core\Session\AccountInterface @@ -52,6 +59,13 @@ class FieldStorageConfigAccessControlHandlerTest extends UnitTestCase { protected $fieldStorage; /** + * The main entity used for testing. + * + * @var \Drupal\Core\Config\Entity\ConfigEntityInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUp() { @@ -99,6 +113,19 @@ protected function setUp() { ->method('getConfigPrefix') ->willReturn('node'); + $this->moduleHandler = $this->getMock(ModuleHandlerInterface::class); + $this->moduleHandler + ->expects($this->any()) + ->method('getImplementations') + ->will($this->returnValue([])); + $this->moduleHandler + ->expects($this->any()) + ->method('invokeAll') + ->will($this->returnValue([])); + + $storage_access_control_handler = new FieldStorageConfigAccessControlHandler($storageType); + $storage_access_control_handler->setModuleHandler($this->moduleHandler); + $entityManager = $this->getMock(EntityManagerInterface::class); $entityManager ->expects($this->any()) @@ -113,19 +140,12 @@ protected function setUp() { ->willReturnMap([ ['field_storage_config', $this->getMock(EntityStorageInterface::class)], ]); - - $module_handler = $this->getMock(ModuleHandlerInterface::class); - $module_handler - ->expects($this->any()) - ->method('getImplementations') - ->will($this->returnValue([])); - $module_handler + $entityManager ->expects($this->any()) - ->method('invokeAll') - ->will($this->returnValue([])); - - $this->accessControlHandler = new FieldStorageConfigAccessControlHandler($storageType); - $this->accessControlHandler->setModuleHandler($module_handler); + ->method('getAccessControlHandler') + ->willReturnMap([ + ['field_storage_config', $storage_access_control_handler], + ]); $container = new Container(); $container->set('entity.manager', $entityManager); @@ -140,35 +160,41 @@ protected function setUp() { 'id' => 'node.test_field', 'uuid' => '6f2f259a-f3c7-42ea-bdd5-111ad1f85ed1', ]); + + $this->entity = $this->fieldStorage; + $this->accessControlHandler = $storage_access_control_handler; } /** - * Asserts correct field storage config access grants. + * Assert method to verify the access by operations. + * + * @param array $allow_operations + * A list of allowed operations. + * @param \Drupal\Core\Session\AccountInterface $user + * The account to use for get access. */ - public function checkAccess($operation, $viewer) { - return $this->accessControlHandler->access($this->fieldStorage, $operation, $viewer); + public function assertAllowOperations(array $allow_operations, AccountInterface $user) { + foreach (['view', 'update', 'delete'] as $operation) { + $expected = in_array($operation, $allow_operations); + $actual = $this->accessControlHandler->access($this->entity, $operation, $user); + $this->assertSame($expected, $actual, "Access problem with '$operation' operation."); + } } /** * Ensures field storage config access is working properly. */ - public function testFieldStorageConfigAccess() { - $this->assertFalse($this->checkAccess('view', $this->anon)); - $this->assertFalse($this->checkAccess('update', $this->anon)); - $this->assertFalse($this->checkAccess('delete', $this->anon)); - - $this->assertTrue($this->checkAccess('view', $this->member)); - $this->assertTrue($this->checkAccess('update', $this->member)); - $this->assertTrue($this->checkAccess('delete', $this->member)); + public function testAccess() { + $this->assertAllowOperations([], $this->anon); + $this->assertAllowOperations(['view', 'update', 'delete'], $this->member); $this->fieldStorage->setLocked(TRUE)->save(); // Unfortunately, EntityAccessControlHandler has a static cache, which we // therefore must reset manually. $this->accessControlHandler->resetCache(); - $this->assertTrue($this->checkAccess('view', $this->member)); - $this->assertTrue($this->checkAccess('update', $this->member)); - $this->assertFalse($this->checkAccess('delete', $this->member)); + $this->assertAllowOperations([], $this->anon); + $this->assertAllowOperations(['view', 'update'], $this->member); } }