updated core and modules
This commit is contained in:
+3
-3
@@ -2,8 +2,8 @@ name: Entity test
|
||||
type: module
|
||||
# core: 8.x
|
||||
|
||||
# Information added by Drupal.org packaging script on 2016-06-23
|
||||
version: '8.x-1.0-alpha3+2-dev'
|
||||
# Information added by Drupal.org packaging script on 2016-12-08
|
||||
version: '8.x-1.0-alpha4'
|
||||
core: '8.x'
|
||||
project: 'entity'
|
||||
datestamp: 1466693538
|
||||
datestamp: 1481194986
|
||||
|
||||
-4
@@ -1,7 +1,3 @@
|
||||
'administer entity_test_enhanced':
|
||||
title: 'Administer entity_test_enhanced'
|
||||
'restrict access': TRUE
|
||||
|
||||
'view all entity_test_enhanced revisions':
|
||||
title: 'View all entity_test_enhanced revisions'
|
||||
'restrict access': TRUE
|
||||
|
||||
+3
@@ -14,6 +14,8 @@ use Drupal\entity\Revision\RevisionableContentEntityBase;
|
||||
* label = @Translation("Entity test with enhancements"),
|
||||
* handlers = {
|
||||
* "storage" = "\Drupal\Core\Entity\Sql\SqlContentEntityStorage",
|
||||
* "access" = "\Drupal\entity\EntityAccessControlHandler",
|
||||
* "permission_provider" = "\Drupal\entity\EntityPermissionProvider",
|
||||
* "form" = {
|
||||
* "add" = "\Drupal\entity\Form\RevisionableContentEntityForm",
|
||||
* "edit" = "\Drupal\entity\Form\RevisionableContentEntityForm",
|
||||
@@ -33,6 +35,7 @@ use Drupal\entity\Revision\RevisionableContentEntityBase;
|
||||
* translatable = TRUE,
|
||||
* revisionable = TRUE,
|
||||
* admin_permission = "administer entity_test_enhanced",
|
||||
* permission_granularity = "bundle",
|
||||
* entity_keys = {
|
||||
* "id" = "id",
|
||||
* "bundle" = "type",
|
||||
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\entity\Unit;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Cache\Context\CacheContextsManager;
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\Core\Entity\ContentEntityTypeInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Language\Language;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\entity\EntityAccessControlHandler;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\user\EntityOwnerInterface;
|
||||
use Prophecy\Argument;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\entity\EntityAccessControlHandler
|
||||
* @group entity
|
||||
*/
|
||||
class EntityAccessControlHandlerTest 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
|
||||
* @covers ::checkEntityPermissions
|
||||
* @covers ::checkEntityOwnerPermissions
|
||||
* @covers ::checkCreateAccess
|
||||
*
|
||||
* @dataProvider accessProvider
|
||||
*/
|
||||
public function testAccess(EntityInterface $entity, $operation, $account, $allowed) {
|
||||
$handler = new EntityAccessControlHandler($entity->getEntityType());
|
||||
$handler->setStringTranslation($this->getStringTranslationStub());
|
||||
$result = $handler->access($entity, $operation, $account);
|
||||
$this->assertEquals($allowed, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::checkCreateAccess
|
||||
*
|
||||
* @dataProvider createAccessProvider
|
||||
*/
|
||||
public function testCreateAccess(EntityTypeInterface $entity_type, $bundle, $account, $allowed) {
|
||||
$handler = new EntityAccessControlHandler($entity_type);
|
||||
$handler->setStringTranslation($this->getStringTranslationStub());
|
||||
$result = $handler->createAccess($bundle, $account);
|
||||
$this->assertEquals($allowed, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testAccess().
|
||||
*
|
||||
* @return array
|
||||
* A list of testAccess method arguments.
|
||||
*/
|
||||
public function accessProvider() {
|
||||
$data = [];
|
||||
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->id()->willReturn('green_entity');
|
||||
$entity_type->getAdminPermission()->willReturn('administer green_entity');
|
||||
|
||||
// User with the admin permission can do anything.
|
||||
$entity = $this->buildMockEntity($entity_type->reveal());
|
||||
$account = $this->prophesize(AccountInterface::class);
|
||||
$account->id()->willReturn(6);
|
||||
$account->hasPermission('administer green_entity')->willReturn(TRUE);
|
||||
$data[] = [$entity->reveal(), 'view', $account->reveal(), TRUE];
|
||||
$data[] = [$entity->reveal(), 'update', $account->reveal(), TRUE];
|
||||
$data[] = [$entity->reveal(), 'delete', $account->reveal(), TRUE];
|
||||
|
||||
// Entity with no owner.
|
||||
$entity = $this->buildMockEntity($entity_type->reveal());
|
||||
// User who has access.
|
||||
$first_account = $this->prophesize(AccountInterface::class);
|
||||
$first_account->id()->willReturn(6);
|
||||
$first_account->hasPermission('view green_entity')->willReturn(TRUE);
|
||||
$first_account->hasPermission(Argument::any())->willReturn(FALSE);
|
||||
// User who doesn't have access.
|
||||
$second_account = $this->prophesize(AccountInterface::class);
|
||||
$second_account->id()->willReturn(7);
|
||||
$second_account->hasPermission('view green_entity')->willReturn(FALSE);
|
||||
$second_account->hasPermission(Argument::any())->willReturn(FALSE);
|
||||
$data[] = [$entity->reveal(), 'view', $first_account->reveal(), TRUE];
|
||||
$data[] = [$entity->reveal(), 'view', $second_account->reveal(), FALSE];
|
||||
|
||||
// Entity with owner.
|
||||
$entity = $this->buildMockEntity($entity_type->reveal(), 6);
|
||||
// Owner.
|
||||
$first_account = $this->prophesize(AccountInterface::class);
|
||||
$first_account->id()->willReturn(6);
|
||||
$first_account->hasPermission('update own green_entity')->willReturn(TRUE);
|
||||
$first_account->hasPermission(Argument::any())->willReturn(FALSE);
|
||||
// Non-owner.
|
||||
$second_account = $this->prophesize(AccountInterface::class);
|
||||
$second_account->id()->willReturn(7);
|
||||
$second_account->hasPermission('update own green_entity')->willReturn(TRUE);
|
||||
$second_account->hasPermission(Argument::any())->willReturn(FALSE);
|
||||
// User who can update any.
|
||||
$third_account = $this->prophesize(AccountInterface::class);
|
||||
$third_account->id()->willReturn(8);
|
||||
$third_account->hasPermission('update any green_entity')->willReturn(TRUE);
|
||||
$third_account->hasPermission(Argument::any())->willReturn(FALSE);
|
||||
$data[] = [$entity->reveal(), 'update', $first_account->reveal(), TRUE];
|
||||
$data[] = [$entity->reveal(), 'update', $second_account->reveal(), FALSE];
|
||||
$data[] = [$entity->reveal(), 'update', $third_account->reveal(), TRUE];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testCreateAccess().
|
||||
*
|
||||
* @return array
|
||||
* A list of testCreateAccess method arguments.
|
||||
*/
|
||||
public function createAccessProvider() {
|
||||
$data = [];
|
||||
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->id()->willReturn('green_entity');
|
||||
$entity_type->getAdminPermission()->willReturn('administer green_entity');
|
||||
|
||||
// User with the admin permission.
|
||||
$account = $this->prophesize(AccountInterface::class);
|
||||
$account->id()->willReturn(6);
|
||||
$account->hasPermission('administer green_entity')->willReturn(TRUE);
|
||||
$data[] = [$entity_type->reveal(), NULL, $account->reveal(), TRUE];
|
||||
|
||||
// Ordinary user.
|
||||
$account = $this->prophesize(AccountInterface::class);
|
||||
$account->id()->willReturn(6);
|
||||
$account->hasPermission('create green_entity')->willReturn(TRUE);
|
||||
$account->hasPermission(Argument::any())->willReturn(FALSE);
|
||||
$data[] = [$entity_type->reveal(), NULL, $account->reveal(), TRUE];
|
||||
|
||||
// Ordinary user, entity with a bundle.
|
||||
$account = $this->prophesize(AccountInterface::class);
|
||||
$account->id()->willReturn(6);
|
||||
$account->hasPermission('create first_bundle green_entity')->willReturn(TRUE);
|
||||
$account->hasPermission(Argument::any())->willReturn(FALSE);
|
||||
$data[] = [$entity_type->reveal(), 'first_bundle', $account->reveal(), TRUE];
|
||||
|
||||
// User with no permissions.
|
||||
$account = $this->prophesize(AccountInterface::class);
|
||||
$account->id()->willReturn(6);
|
||||
$account->hasPermission(Argument::any())->willReturn(FALSE);
|
||||
$data[] = [$entity_type->reveal(), NULL, $account->reveal(), FALSE];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a mock entity.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
||||
* The entity type.
|
||||
* @param string $owner_id
|
||||
* The owner ID.
|
||||
*
|
||||
* @return \Prophecy\Prophecy\ObjectProphecy
|
||||
* The entity mock.
|
||||
*/
|
||||
protected function buildMockEntity(EntityTypeInterface $entity_type, $owner_id = NULL) {
|
||||
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
|
||||
$entity = $this->prophesize(ContentEntityInterface::class);
|
||||
if ($owner_id) {
|
||||
$entity->willImplement(EntityOwnerInterface::class);
|
||||
$entity->getOwnerId()->willReturn($owner_id);
|
||||
}
|
||||
$entity->bundle()->willReturn($entity_type->id());
|
||||
$entity->isNew()->willReturn(FALSE);
|
||||
$entity->uuid()->willReturn('fake uuid');
|
||||
$entity->language()->willReturn(new Language(['id' => $langcode]));
|
||||
$entity->getEntityTypeId()->willReturn($entity_type->id());
|
||||
$entity->getEntityType()->willReturn($entity_type);
|
||||
$entity->getCacheContexts()->willReturn([]);
|
||||
$entity->getCacheTags()->willReturn([]);
|
||||
$entity->getCacheMaxAge()->willReturn(Cache::PERMANENT);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\entity\Unit;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
|
||||
use Drupal\Core\Entity\ContentEntityTypeInterface;
|
||||
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\entity\EntityPermissionProvider;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\user\EntityOwnerInterface;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\entity\EntityPermissionProvider
|
||||
* @group entity
|
||||
*/
|
||||
class EntityPermissionProviderTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The entity permission provider.
|
||||
*
|
||||
* @var \Drupal\entity\EntityPermissionProviderInterface
|
||||
*/
|
||||
protected $permissionProvider;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$entity_type_bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class);
|
||||
$entity_type_bundle_info->getBundleInfo('white_entity')->willReturn([
|
||||
'first' => ['label' => 'First'],
|
||||
'second' => ['label' => 'Second'],
|
||||
]);
|
||||
$entity_type_bundle_info->getBundleInfo('black_entity')->willReturn([
|
||||
'third' => ['label' => 'Third'],
|
||||
]);
|
||||
$this->permissionProvider = new EntityPermissionProvider($entity_type_bundle_info->reveal());
|
||||
$this->permissionProvider->setStringTranslation($this->getStringTranslationStub());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildPermissions
|
||||
*
|
||||
* @dataProvider entityTypeProvider
|
||||
*/
|
||||
public function testBuildPermissions(EntityTypeInterface $entity_type, array $expected_permissions) {
|
||||
$permissions = $this->permissionProvider->buildPermissions($entity_type);
|
||||
$this->assertEquals(array_keys($expected_permissions), array_keys($permissions));
|
||||
foreach ($permissions as $name => $permission) {
|
||||
$this->assertEquals('entity_module_test', $permission['provider']);
|
||||
$this->assertEquals($expected_permissions[$name], $permission['title']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testBuildPermissions().
|
||||
*
|
||||
* @return array
|
||||
* A list of testBuildPermissions method arguments.
|
||||
*/
|
||||
public function entityTypeProvider() {
|
||||
$data = [];
|
||||
// Content entity type.
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->getProvider()->willReturn('entity_module_test');
|
||||
$entity_type->id()->willReturn('green_entity');
|
||||
$entity_type->getSingularLabel()->willReturn('green entity');
|
||||
$entity_type->getPluralLabel()->willReturn('green entities');
|
||||
$entity_type->isSubclassOf(EntityOwnerInterface::class)->willReturn(FALSE);
|
||||
$entity_type->getPermissionGranularity()->willReturn('entity_type');
|
||||
$expected_permissions = [
|
||||
'administer green_entity' => 'Administer green entities',
|
||||
'access green_entity overview' => 'Access the green entities overview page',
|
||||
'view green_entity' => 'View green entities',
|
||||
'create green_entity' => 'Create green entities',
|
||||
'update green_entity' => 'Update green entities',
|
||||
'delete green_entity' => 'Delete green entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
// Content entity type with owner.
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->getProvider()->willReturn('entity_module_test');
|
||||
$entity_type->id()->willReturn('blue_entity');
|
||||
$entity_type->getSingularLabel()->willReturn('blue entity');
|
||||
$entity_type->getPluralLabel()->willReturn('blue entities');
|
||||
$entity_type->isSubclassOf(EntityOwnerInterface::class)->willReturn(TRUE);
|
||||
$entity_type->getPermissionGranularity()->willReturn('entity_type');
|
||||
$expected_permissions = [
|
||||
'administer blue_entity' => 'Administer blue entities',
|
||||
'access blue_entity overview' => 'Access the blue entities overview page',
|
||||
'view any blue_entity' => 'View any blue entity',
|
||||
'view own blue_entity' => 'View own blue entities',
|
||||
'create blue_entity' => 'Create blue entities',
|
||||
'update any blue_entity' => 'Update any blue entity',
|
||||
'update own blue_entity' => 'Update own blue entities',
|
||||
'delete any blue_entity' => 'Delete any blue entity',
|
||||
'delete own blue_entity' => 'Delete own blue entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
// Content entity type with bundles.
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->getProvider()->willReturn('entity_module_test');
|
||||
$entity_type->id()->willReturn('white_entity');
|
||||
$entity_type->getSingularLabel()->willReturn('white entity');
|
||||
$entity_type->getPluralLabel()->willReturn('white entities');
|
||||
$entity_type->isSubclassOf(EntityOwnerInterface::class)->willReturn(FALSE);
|
||||
$entity_type->getPermissionGranularity()->willReturn('bundle');
|
||||
$expected_permissions = [
|
||||
'administer white_entity' => 'Administer white entities',
|
||||
'access white_entity overview' => 'Access the white entities overview page',
|
||||
'view white_entity' => 'View white entities',
|
||||
'create first white_entity' => 'First: Create white entities',
|
||||
'update first white_entity' => 'First: Update white entities',
|
||||
'delete first white_entity' => 'First: Delete white entities',
|
||||
'create second white_entity' => 'Second: Create white entities',
|
||||
'update second white_entity' => 'Second: Update white entities',
|
||||
'delete second white_entity' => 'Second: Delete white entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
// Content entity type with bundles and owner.
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->getProvider()->willReturn('entity_module_test');
|
||||
$entity_type->id()->willReturn('black_entity');
|
||||
$entity_type->getSingularLabel()->willReturn('black entity');
|
||||
$entity_type->getPluralLabel()->willReturn('black entities');
|
||||
$entity_type->isSubclassOf(EntityOwnerInterface::class)->willReturn(TRUE);
|
||||
$entity_type->getPermissionGranularity()->willReturn('bundle');
|
||||
$expected_permissions = [
|
||||
'administer black_entity' => 'Administer black entities',
|
||||
'access black_entity overview' => 'Access the black entities overview page',
|
||||
'view any black_entity' => 'View any black entity',
|
||||
'view own black_entity' => 'View own black entities',
|
||||
'create third black_entity' => 'Third: Create black entities',
|
||||
'update any third black_entity' => 'Third: Update any black entity',
|
||||
'update own third black_entity' => 'Third: Update own black entities',
|
||||
'delete any third black_entity' => 'Third: Delete any black entity',
|
||||
'delete own third black_entity' => 'Third: Delete own black entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user