class BundleEntityAccessControlHandlerTest in Entity API 8
@coversDefaultClass \Drupal\entity\BundleEntityAccessControlHandler @group entity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\entity\Unit\BundleEntityAccessControlHandlerTest
Expanded class hierarchy of BundleEntityAccessControlHandlerTest
File
- tests/
src/ Unit/ BundleEntityAccessControlHandlerTest.php, line 22
Namespace
Drupal\Tests\entity\UnitView source
class BundleEntityAccessControlHandlerTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->invokeAll(Argument::any(), Argument::any())
->willReturn([]);
$cache_contexts_manager = $this
->prophesize(CacheContextsManager::class);
$cache_contexts_manager
->assertValidTokens(Argument::any())
->willReturn(TRUE);
$container = new ContainerBuilder();
$container
->set('module_handler', $module_handler
->reveal());
$container
->set('cache_contexts_manager', $cache_contexts_manager
->reveal());
\Drupal::setContainer($container);
}
/**
* @covers ::checkAccess
*
* @dataProvider accessProvider
*/
public function testAccess(EntityInterface $entity, $operation, $account, $allowed) {
$handler = new BundleEntityAccessControlHandler($entity
->getEntityType());
$handler
->setStringTranslation($this
->getStringTranslationStub());
$result = $handler
->access($entity, $operation, $account);
$this
->assertEquals($allowed, $result);
}
/**
* Data provider for testAccess().
*
* @return array
* A list of testAccess method arguments.
*/
public function accessProvider() {
$entity_type = $this
->prophesize(ContentEntityTypeInterface::class);
$entity_type
->id()
->willReturn('green_entity_bundle');
$entity_type
->getBundleOf()
->willReturn('green_entity');
$entity_type
->getAdminPermission()
->willReturn('administer green_entity');
$entity_type = $entity_type
->reveal();
$entity = $this
->prophesize(ConfigEntityInterface::class);
$entity
->getEntityType()
->willReturn($entity_type);
$entity
->getEntityTypeId()
->willReturn('green_entity_bundle');
$entity
->id()
->willReturn('default');
$entity
->uuid()
->willReturn('fake uuid');
$entity
->language()
->willReturn(new Language([
'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]));
// User with no access.
$user = $this
->buildMockUser(1, 'access content');
$data[] = [
$entity
->reveal(),
'view label',
$user
->reveal(),
FALSE,
];
// Permissions which grant "view label" access.
$permissions = [
'administer green_entity',
'view green_entity',
'view default green_entity',
'view own green_entity',
'view any green_entity',
'view own default green_entity',
'view any default green_entity',
];
foreach ($permissions as $index => $permission) {
$user = $this
->buildMockUser(10 + $index, $permission);
$data[] = [
$entity
->reveal(),
'view label',
$user
->reveal(),
TRUE,
];
}
return $data;
}
/**
* Builds a mock user.
*
* @param int $uid
* The user ID.
* @param string $permission
* The permission to grant.
*
* @return \Prophecy\Prophecy\ObjectProphecy
* The user mock.
*/
protected function buildMockUser($uid, $permission) {
$account = $this
->prophesize(AccountInterface::class);
$account
->id()
->willReturn($uid);
$account
->hasPermission($permission)
->willReturn(TRUE);
$account
->hasPermission(Argument::any())
->willReturn(FALSE);
return $account;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BundleEntityAccessControlHandlerTest:: |
public | function | Data provider for testAccess(). | |
BundleEntityAccessControlHandlerTest:: |
protected | function | Builds a mock user. | |
BundleEntityAccessControlHandlerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
BundleEntityAccessControlHandlerTest:: |
public | function | @covers ::checkAccess | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |