class FieldStorageConfigAccessControlHandlerTest
Same name in other branches
- 9 core/modules/field/tests/src/Unit/FieldStorageConfigAccessControlHandlerTest.php \Drupal\Tests\field\Unit\FieldStorageConfigAccessControlHandlerTest
- 8.9.x core/modules/field/tests/src/Unit/FieldStorageConfigAccessControlHandlerTest.php \Drupal\Tests\field\Unit\FieldStorageConfigAccessControlHandlerTest
- 10 core/modules/field/tests/src/Unit/FieldStorageConfigAccessControlHandlerTest.php \Drupal\Tests\field\Unit\FieldStorageConfigAccessControlHandlerTest
Tests the field storage config access controller.
@group field
@coversDefaultClass \Drupal\field\FieldStorageConfigAccessControlHandler
Hierarchy
- class \Drupal\Tests\field\Unit\FieldStorageConfigAccessControlHandlerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of FieldStorageConfigAccessControlHandlerTest
File
-
core/
modules/ field/ tests/ src/ Unit/ FieldStorageConfigAccessControlHandlerTest.php, line 26
Namespace
Drupal\Tests\field\UnitView source
class FieldStorageConfigAccessControlHandlerTest extends UnitTestCase {
/**
* The field storage config access controller to test.
*
* @var \Drupal\field\FieldStorageConfigAccessControlHandler
*/
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
*/
protected $anon;
/**
* The mock account with field storage config access.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $member;
/**
* The FieldStorageConfig entity used for testing.
*
* @var \Drupal\field\FieldStorageConfigInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->anon = $this->createMock(AccountInterface::class);
$this->anon
->expects($this->any())
->method('hasPermission')
->willReturn(FALSE);
$this->anon
->expects($this->any())
->method('id')
->willReturn(0);
$this->member = $this->createMock(AccountInterface::class);
$this->member
->expects($this->any())
->method('hasPermission')
->willReturnMap([
[
'administer node fields',
TRUE,
],
]);
$this->member
->expects($this->any())
->method('id')
->willReturn(2);
$storageType = $this->createMock(ConfigEntityTypeInterface::class);
$storageType->expects($this->any())
->method('getProvider')
->willReturn('field');
$storageType->expects($this->any())
->method('getConfigPrefix')
->willReturn('field.storage');
$entityType = $this->createMock(ConfigEntityTypeInterface::class);
$entityType->expects($this->any())
->method('getProvider')
->willReturn('node');
$entityType->expects($this->any())
->method('getConfigPrefix')
->willReturn('node');
$this->moduleHandler = $this->createMock(ModuleHandlerInterface::class);
$this->moduleHandler
->expects($this->any())
->method('invokeAll')
->willReturn([]);
$storage_access_control_handler = new FieldStorageConfigAccessControlHandler($storageType);
$storage_access_control_handler->setModuleHandler($this->moduleHandler);
$entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
$entity_type_manager->expects($this->any())
->method('getDefinition')
->willReturnMap([
[
'field_storage_config',
TRUE,
$storageType,
],
[
'node',
TRUE,
$entityType,
],
]);
$entity_type_manager->expects($this->any())
->method('getStorage')
->willReturnMap([
[
'field_storage_config',
$this->createMock(EntityStorageInterface::class),
],
]);
$entity_type_manager->expects($this->any())
->method('getAccessControlHandler')
->willReturnMap([
[
'field_storage_config',
$storage_access_control_handler,
],
]);
$container = new Container();
$container->set('entity_type.manager', $entity_type_manager);
$container->set('uuid', $this->createMock(UuidInterface::class));
$container->set('cache_contexts_manager', $this->prophesize(CacheContextsManager::class));
\Drupal::setContainer($container);
$this->entity = new FieldStorageConfig([
'field_name' => 'test_field',
'entity_type' => 'node',
'type' => 'boolean',
'id' => 'node.test_field',
'uuid' => '6f2f259a-f3c7-42ea-bdd5-111ad1f85ed1',
]);
$this->accessControlHandler = $storage_access_control_handler;
}
/**
* 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.
*
* @internal
*/
public function assertAllowOperations(array $allow_operations, AccountInterface $user) : void {
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 testAccess() : void {
$this->assertAllowOperations([], $this->anon);
$this->assertAllowOperations([
'view',
'update',
'delete',
], $this->member);
$this->entity
->setLocked(TRUE)
->save();
// Unfortunately, EntityAccessControlHandler has a static cache, which we
// therefore must reset manually.
$this->accessControlHandler
->resetCache();
$this->assertAllowOperations([], $this->anon);
$this->assertAllowOperations([
'view',
'update',
], $this->member);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | ||
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | ||
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | ||
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | ||
FieldStorageConfigAccessControlHandlerTest::$accessControlHandler | protected | property | The field storage config access controller to test. | ||
FieldStorageConfigAccessControlHandlerTest::$anon | protected | property | The mock account without field storage config access. | ||
FieldStorageConfigAccessControlHandlerTest::$entity | protected | property | The FieldStorageConfig entity used for testing. | ||
FieldStorageConfigAccessControlHandlerTest::$member | protected | property | The mock account with field storage config access. | ||
FieldStorageConfigAccessControlHandlerTest::$moduleHandler | protected | property | The mock module handler. | ||
FieldStorageConfigAccessControlHandlerTest::assertAllowOperations | public | function | Assert method to verify the access by operations. | ||
FieldStorageConfigAccessControlHandlerTest::setUp | protected | function | Overrides UnitTestCase::setUp | 1 | |
FieldStorageConfigAccessControlHandlerTest::testAccess | public | function | Ensures field storage config access is working properly. | 1 | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | ||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | ||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | ||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | ||
UnitTestCase::$root | protected | property | The app root. | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | ||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | ||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | ||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | ||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.