updated contrib modules
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
name: 'Entity bundle plugin examples test'
|
||||
type: module
|
||||
description: 'Module for testing bundle plugins.'
|
||||
package: Testing
|
||||
# core: 8.x
|
||||
dependencies:
|
||||
- entity
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-09-20
|
||||
version: '8.x-1.0-beta1'
|
||||
core: '8.x'
|
||||
project: 'entity'
|
||||
datestamp: 1505895847
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\entity_module_bundle_plugin_examples_test\Plugin\BundlePluginTest;
|
||||
|
||||
use Drupal\entity\BundleFieldDefinition;
|
||||
use Drupal\Core\Plugin\PluginBase;
|
||||
use Drupal\entity_module_bundle_plugin_test\Plugin\BundlePluginTest\BundlePluginTestInterface;
|
||||
|
||||
/**
|
||||
* Provides the second bundle plugin.
|
||||
*
|
||||
* @BundlePluginTest(
|
||||
* id = "second",
|
||||
* label = @Translation("Second"),
|
||||
* )
|
||||
*/
|
||||
class Second extends PluginBase implements BundlePluginTestInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildFieldDefinitions() {
|
||||
$fields = [];
|
||||
$fields['second_mail'] = BundleFieldDefinition::create('email')
|
||||
->setLabel(t('Email'))
|
||||
->setRequired(TRUE);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
name: 'Entity bundle plugin test'
|
||||
type: module
|
||||
description: 'Module for testing bundle plugins.'
|
||||
package: Testing
|
||||
# core: 8.x
|
||||
dependencies:
|
||||
- entity
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-09-20
|
||||
version: '8.x-1.0-beta1'
|
||||
core: '8.x'
|
||||
project: 'entity'
|
||||
datestamp: 1505895847
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
services:
|
||||
plugin.manager.bundle_plugin_test:
|
||||
class: Drupal\entity_module_bundle_plugin_test\BundlePluginTestManager
|
||||
parent: default_plugin_manager
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\entity_module_bundle_plugin_test\Annotation;
|
||||
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
|
||||
/**
|
||||
* Defines the BundlePluginTest annotation object.
|
||||
*
|
||||
* Plugin namespace: Plugin\BundlePluginTest.
|
||||
*
|
||||
* @see plugin_api
|
||||
*
|
||||
* @Annotation
|
||||
*/
|
||||
class BundlePluginTest extends Plugin {
|
||||
|
||||
/**
|
||||
* The plugin ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* The plugin label.
|
||||
*
|
||||
* @ingroup plugin_translatable
|
||||
*
|
||||
* @var \Drupal\Core\Annotation\Translation
|
||||
*/
|
||||
public $label;
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\entity_module_bundle_plugin_test;
|
||||
|
||||
use Drupal\Core\Cache\CacheBackendInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Plugin\DefaultPluginManager;
|
||||
|
||||
/**
|
||||
* Manages discovery and instantiation of BundlePluginTest plugins.
|
||||
*
|
||||
* @see \Drupal\entity_module_bundle_plugin_test\Annotation\BundlePluginTest
|
||||
* @see plugin_api
|
||||
*/
|
||||
class BundlePluginTestManager extends DefaultPluginManager {
|
||||
|
||||
/**
|
||||
* Constructs a new BundlePluginTestManager object.
|
||||
*
|
||||
* @param \Traversable $namespaces
|
||||
* An object that implements \Traversable which contains the root paths
|
||||
* keyed by the corresponding namespace to look for plugin implementations.
|
||||
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
|
||||
* The cache backend.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler.
|
||||
*/
|
||||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
|
||||
parent::__construct('Plugin/BundlePluginTest', $namespaces, $module_handler, 'Drupal\entity_module_bundle_plugin_test\Plugin\BundlePluginTest\BundlePluginTestInterface', 'Drupal\entity_module_bundle_plugin_test\Annotation\BundlePluginTest');
|
||||
|
||||
$this->alterInfo('bundle_plugin_test_info');
|
||||
$this->setCacheBackend($cache_backend, 'bundle_plugin_test_plugins');
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\entity_module_bundle_plugin_test\Entity;
|
||||
|
||||
use Drupal\Core\Entity\ContentEntityBase;
|
||||
|
||||
/**
|
||||
* Defines the 'entity_test_bundle_plugin' entity class.
|
||||
*
|
||||
* @ContentEntityType(
|
||||
* id = "entity_test_bundle_plugin",
|
||||
* label = @Translation("Entity test bundle plugin"),
|
||||
* bundle_label = @Translation("Bundle Plugin Test"),
|
||||
* bundle_plugin_type = "bundle_plugin_test",
|
||||
* base_table = "entity_test_bundle_plugin",
|
||||
* admin_permission = "administer content",
|
||||
* fieldable = TRUE,
|
||||
* entity_keys = {
|
||||
* "id" = "method_id",
|
||||
* "uuid" = "uuid",
|
||||
* "bundle" = "type"
|
||||
* },
|
||||
* )
|
||||
*/
|
||||
class EntityTestBundlePlugin extends ContentEntityBase {
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\entity_module_bundle_plugin_test\Plugin\BundlePluginTest;
|
||||
|
||||
use Drupal\entity\BundlePlugin\BundlePluginInterface;
|
||||
|
||||
/**
|
||||
* Defines the interface for BundlePluginTest plugins.
|
||||
*/
|
||||
interface BundlePluginTestInterface extends BundlePluginInterface {
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\entity_module_bundle_plugin_test\Plugin\BundlePluginTest;
|
||||
|
||||
use Drupal\entity\BundleFieldDefinition;
|
||||
use Drupal\Core\Plugin\PluginBase;
|
||||
|
||||
/**
|
||||
* Provides the first bundle plugin.
|
||||
*
|
||||
* @BundlePluginTest(
|
||||
* id = "first",
|
||||
* label = @Translation("First"),
|
||||
* description = @Translation("Some description"),
|
||||
* )
|
||||
*/
|
||||
class First extends PluginBase implements BundlePluginTestInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildFieldDefinitions() {
|
||||
$fields = [];
|
||||
$fields['first_mail'] = BundleFieldDefinition::create('email')
|
||||
->setLabel(t('Email'))
|
||||
->setRequired(TRUE);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
}
|
||||
+4
-3
@@ -1,9 +1,10 @@
|
||||
name: Entity test
|
||||
type: module
|
||||
package: Testing
|
||||
# core: 8.x
|
||||
|
||||
# Information added by Drupal.org packaging script on 2016-12-08
|
||||
version: '8.x-1.0-alpha4'
|
||||
# Information added by Drupal.org packaging script on 2017-09-20
|
||||
version: '8.x-1.0-beta1'
|
||||
core: '8.x'
|
||||
project: 'entity'
|
||||
datestamp: 1481194986
|
||||
datestamp: 1505895847
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ use Drupal\entity\Revision\RevisionableContentEntityBase;
|
||||
* label = @Translation("Entity test with enhancements"),
|
||||
* handlers = {
|
||||
* "storage" = "\Drupal\Core\Entity\Sql\SqlContentEntityStorage",
|
||||
* "access" = "\Drupal\entity\EntityAccessControlHandler",
|
||||
* "access" = "\Drupal\Core\Entity\EntityAccessControlHandler",
|
||||
* "permission_provider" = "\Drupal\entity\EntityPermissionProvider",
|
||||
* "form" = {
|
||||
* "add" = "\Drupal\entity\Form\RevisionableContentEntityForm",
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ namespace Drupal\Tests\entity\Functional;
|
||||
|
||||
use Drupal\entity_module_test\Entity\EnhancedEntity;
|
||||
use Drupal\entity_module_test\Entity\EnhancedEntityBundle;
|
||||
use Drupal\simpletest\BrowserTestBase;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests the delete multiple confirmation form.
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\entity\Kernel;
|
||||
|
||||
use Drupal\entity_module_bundle_plugin_test\Entity\EntityTestBundlePlugin;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests the bundle plugin API.
|
||||
*
|
||||
* @group entity
|
||||
*/
|
||||
class BundlePluginTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = [
|
||||
'system',
|
||||
'entity',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installSchema('system', 'router');
|
||||
|
||||
// Install the modules properly. Putting them into static::$modules doesn't trigger the install
|
||||
// hooks, like hook_modules_installed, so entity_modules_installed is not triggered().
|
||||
/** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
|
||||
$module_installer = \Drupal::service('module_installer');
|
||||
$module_installer->install(['entity_module_bundle_plugin_test', 'entity_module_bundle_plugin_examples_test']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the bundle plugins.
|
||||
*/
|
||||
public function testPluginBundles() {
|
||||
$bundled_entity_types = entity_get_bundle_plugin_entity_types();
|
||||
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
|
||||
$entity_type = $bundled_entity_types['entity_test_bundle_plugin'];
|
||||
$this->assertEquals('entity_test_bundle_plugin', $entity_type->id());
|
||||
$this->assertTrue($entity_type->hasHandlerClass('bundle_plugin'));
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info */
|
||||
$entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
|
||||
$bundle_info = $entity_type_bundle_info->getBundleInfo('entity_test_bundle_plugin');
|
||||
$this->assertEquals(2, count($bundle_info));
|
||||
$this->assertArrayHasKey('first', $bundle_info);
|
||||
$this->assertArrayHasKey('second', $bundle_info);
|
||||
$this->assertEquals('First', $bundle_info['first']['label']);
|
||||
$this->assertEquals('Some description', $bundle_info['first']['description']);
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
|
||||
$entity_field_manager = \Drupal::service('entity_field.manager');
|
||||
$field_storage_definitions = $entity_field_manager->getFieldStorageDefinitions('entity_test_bundle_plugin');
|
||||
$this->assertArrayHasKey('first_mail', $field_storage_definitions);
|
||||
$this->assertArrayHasKey('second_mail', $field_storage_definitions);
|
||||
$first_field_definitions = $entity_field_manager->getFieldDefinitions('entity_test_bundle_plugin', 'first');
|
||||
$this->assertArrayHasKey('first_mail', $first_field_definitions);
|
||||
$this->assertArrayNotHasKey('second_mail', $first_field_definitions);
|
||||
$second_field_definitions = $entity_field_manager->getFieldDefinitions('entity_test_bundle_plugin', 'second');
|
||||
$this->assertArrayNotHasKey('first_mail', $second_field_definitions);
|
||||
$this->assertArrayHasKey('second_mail', $second_field_definitions);
|
||||
|
||||
$first_entity = EntityTestBundlePlugin::create([
|
||||
'type' => 'first',
|
||||
'first_mail' => 'admin@test.com',
|
||||
]);
|
||||
$first_entity->save();
|
||||
$first_entity = EntityTestBundlePlugin::load($first_entity->id());
|
||||
$this->assertEquals('admin@test.com', $first_entity->first_mail->value);
|
||||
|
||||
$second_entity = EntityTestBundlePlugin::create([
|
||||
'type' => 'second',
|
||||
'second_mail' => 'admin@example.com',
|
||||
]);
|
||||
$second_entity->save();
|
||||
$second_entity = EntityTestBundlePlugin::load($second_entity->id());
|
||||
$this->assertEquals('admin@example.com', $second_entity->second_mail->value);
|
||||
|
||||
// Also test entity queries.
|
||||
$result = \Drupal::entityTypeManager()->getStorage('entity_test_bundle_plugin')
|
||||
->getQuery()
|
||||
->condition('second_mail', 'admin@example.com')
|
||||
->execute();
|
||||
$this->assertEquals([$second_entity->id() => $second_entity->id()], $result);
|
||||
|
||||
$result = \Drupal::entityTypeManager()->getStorage('entity_test_bundle_plugin')
|
||||
->getQuery()
|
||||
->condition('type', 'first')
|
||||
->execute();
|
||||
$this->assertEquals([$first_entity->id() => $first_entity->id()], $result);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the uninstallation for a bundle provided by a module.
|
||||
*/
|
||||
public function testBundlePluginModuleUninstallation() {
|
||||
/** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
|
||||
$module_installer = \Drupal::service('module_installer');
|
||||
|
||||
// One should be possible to uninstall without any actual content.
|
||||
$violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
|
||||
$this->assertEmpty($violations);
|
||||
|
||||
$first_entity = EntityTestBundlePlugin::create([
|
||||
'type' => 'first',
|
||||
'first_mail' => 'admin@test.com',
|
||||
]);
|
||||
$first_entity->save();
|
||||
$second_entity = EntityTestBundlePlugin::create([
|
||||
'type' => 'second',
|
||||
'second_mail' => 'admin@example.com',
|
||||
]);
|
||||
$second_entity->save();
|
||||
|
||||
$violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertCount(1, $violations['entity_module_bundle_plugin_examples_test']);
|
||||
$this->assertEquals('There is data for the bundle Second on the entity type Entity test bundle plugin. Please remove all content before uninstalling the module.', $violations['entity_module_bundle_plugin_examples_test'][0]);
|
||||
|
||||
$second_entity->delete();
|
||||
|
||||
// The first entity is defined by entity_module_bundle_plugin_test, so it should be possible
|
||||
// to uninstall the module providing the second bundle plugin.
|
||||
$violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
|
||||
$this->assertEmpty($violations);
|
||||
|
||||
$module_installer->uninstall(['entity_module_bundle_plugin_examples_test']);
|
||||
|
||||
// The first entity is provided by entity_module_bundle_plugin_test which we cannot uninstall,
|
||||
// until all the entities are deleted.
|
||||
$violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_test']);
|
||||
$this->assertNotEmpty($violations);
|
||||
|
||||
$first_entity->delete();
|
||||
$violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_test']);
|
||||
$this->assertEmpty($violations);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -57,22 +57,36 @@ class RevisionBasicUITest extends KernelTestBase {
|
||||
|
||||
/** @var \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel */
|
||||
$http_kernel = \Drupal::service('http_kernel');
|
||||
$request = Request::create($revision->url('version-history'));
|
||||
$request = Request::create($revision->toUrl('version-history')->toString());
|
||||
$response = $http_kernel->handle($request);
|
||||
$this->assertEquals(403, $response->getStatusCode());
|
||||
|
||||
$role_admin = Role::create(['id' => 'test_role_admin']);
|
||||
$role_admin->grantPermission('administer entity_test_enhanced');
|
||||
$role_admin->save();
|
||||
|
||||
$role = Role::create(['id' => 'test_role']);
|
||||
$role->grantPermission('view all entity_test_enhanced revisions');
|
||||
$role->grantPermission('administer entity_test_enhanced');
|
||||
$role->save();
|
||||
|
||||
$user_admin = User::create([
|
||||
'name' => 'Test user admin',
|
||||
]);
|
||||
$user_admin->addRole($role_admin->id());
|
||||
\Drupal::service('account_switcher')->switchTo($user_admin);
|
||||
|
||||
$request = Request::create($revision->toUrl('version-history')->toString());
|
||||
$response = $http_kernel->handle($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
|
||||
$user = User::create([
|
||||
'name' => 'Test user',
|
||||
]);
|
||||
$user->addRole($role->id());
|
||||
\Drupal::service('account_switcher')->switchTo($user);
|
||||
|
||||
$request = Request::create($revision->url('version-history'));
|
||||
$request = Request::create($revision->toUrl('version-history')->toString());
|
||||
$response = $http_kernel->handle($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
|
||||
@@ -87,7 +101,7 @@ class RevisionBasicUITest extends KernelTestBase {
|
||||
$revision->isDefaultRevision(TRUE);
|
||||
$revision->save();
|
||||
|
||||
$request = Request::create($revision->url('version-history'));
|
||||
$request = Request::create($revision->toUrl('version-history')->toString());
|
||||
$response = $http_kernel->handle($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
|
||||
@@ -110,22 +124,36 @@ class RevisionBasicUITest extends KernelTestBase {
|
||||
|
||||
/** @var \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel */
|
||||
$http_kernel = \Drupal::service('http_kernel');
|
||||
$request = Request::create($revision->url('revision'));
|
||||
$request = Request::create($revision->toUrl('revision')->toString());
|
||||
$response = $http_kernel->handle($request);
|
||||
$this->assertEquals(403, $response->getStatusCode());
|
||||
|
||||
$role_admin = Role::create(['id' => 'test_role_admin']);
|
||||
$role_admin->grantPermission('administer entity_test_enhanced');
|
||||
$role_admin->save();
|
||||
|
||||
$role = Role::create(['id' => 'test_role']);
|
||||
$role->grantPermission('view all entity_test_enhanced revisions');
|
||||
$role->grantPermission('administer entity_test_enhanced');
|
||||
$role->save();
|
||||
|
||||
$user_admin = User::create([
|
||||
'name' => 'Test user admin',
|
||||
]);
|
||||
$user_admin->addRole($role_admin->id());
|
||||
\Drupal::service('account_switcher')->switchTo($user_admin);
|
||||
|
||||
$request = Request::create($revision->toUrl('version-history')->toString());
|
||||
$response = $http_kernel->handle($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
|
||||
$user = User::create([
|
||||
'name' => 'Test user',
|
||||
]);
|
||||
$user->addRole($role->id());
|
||||
\Drupal::service('account_switcher')->switchTo($user);
|
||||
|
||||
$request = Request::create($revision->url('revision'));
|
||||
$request = Request::create($revision->toUrl('revision')->toString());
|
||||
$response = $http_kernel->handle($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertNotContains('rev 1', $response->getContent());
|
||||
@@ -156,7 +184,7 @@ class RevisionBasicUITest extends KernelTestBase {
|
||||
|
||||
/** @var \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel */
|
||||
$http_kernel = \Drupal::service('http_kernel');
|
||||
$request = Request::create($entity->url('revision-revert-form'));
|
||||
$request = Request::create($entity->toUrl('revision-revert-form')->toString());
|
||||
$response = $http_kernel->handle($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
+54
-2
@@ -8,12 +8,14 @@ use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\Core\Entity\ContentEntityTypeInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityPublishedInterface;
|
||||
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\entity\EntityPermissionProvider;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\user\EntityOwnerInterface;
|
||||
use Prophecy\Argument;
|
||||
@@ -80,6 +82,8 @@ class EntityAccessControlHandlerTest extends UnitTestCase {
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->id()->willReturn('green_entity');
|
||||
$entity_type->getAdminPermission()->willReturn('administer green_entity');
|
||||
$entity_type->hasHandlerClass('permission_provider')->willReturn(TRUE);
|
||||
$entity_type->getHandlerClass('permission_provider')->willReturn(EntityPermissionProvider::class);
|
||||
|
||||
// User with the admin permission can do anything.
|
||||
$entity = $this->buildMockEntity($entity_type->reveal());
|
||||
@@ -126,6 +130,32 @@ class EntityAccessControlHandlerTest extends UnitTestCase {
|
||||
$data[] = [$entity->reveal(), 'update', $second_account->reveal(), FALSE];
|
||||
$data[] = [$entity->reveal(), 'update', $third_account->reveal(), TRUE];
|
||||
|
||||
// Test the unpublished permissions.
|
||||
$entity_first_other_up = $this->buildMockEntity($entity_type->reveal(), 9999, 'first', FALSE);
|
||||
$entity_first_own_up = $this->buildMockEntity($entity_type->reveal(), 14, 'first', FALSE);
|
||||
$entity_first_own_bundle_up = $this->buildMockEntity($entity_type->reveal(), 15, 'first', FALSE);
|
||||
|
||||
$entity_second_other_up = $this->buildMockEntity($entity_type->reveal(), 9999, 'second', FALSE);
|
||||
$entity_second_own_up = $this->buildMockEntity($entity_type->reveal(), 14, 'second', FALSE);
|
||||
$entity_second_own_bundle_up = $this->buildMockEntity($entity_type->reveal(), 15, 'second', FALSE);
|
||||
|
||||
$user_view_own_up = $this->buildMockUser(14, 'view own unpublished green_entity');
|
||||
$user_view_other = $this->buildMockUser(15, 'view green_entity');
|
||||
|
||||
$data['entity_first_other_up user_view_own_up'] = [$entity_first_other_up->reveal(), 'view', $user_view_own_up->reveal(), FALSE];
|
||||
$data['entity_first_own_up user_view_own_up'] = [$entity_first_own_up->reveal(), 'view', $user_view_own_up->reveal(), TRUE];
|
||||
$data['entity_first_own_bundle_up user_view_own_up'] = [$entity_first_own_bundle_up->reveal(), 'view', $user_view_own_up->reveal(), FALSE];
|
||||
$data['entity_second_other_up user_view_own_up'] = [$entity_second_other_up->reveal(), 'view', $user_view_own_up->reveal(), FALSE];
|
||||
$data['entity_second_own_up user_view_own_up'] = [$entity_second_own_up->reveal(), 'view', $user_view_own_up->reveal(), TRUE];
|
||||
$data['entity_second_own_bundle_up user_view_own_up'] = [$entity_second_own_bundle_up->reveal(), 'view', $user_view_own_up->reveal(), FALSE];
|
||||
|
||||
$data['entity_first_other_up user_view_other'] = [$entity_first_other_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_first_own_up user_view_other'] = [$entity_first_own_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_first_own_bundle_up user_view_other'] = [$entity_first_own_bundle_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_second_other_up user_view_other'] = [$entity_second_other_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_second_own_up user_view_other'] = [$entity_second_own_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_second_own_bundle_up user_view_other'] = [$entity_second_own_bundle_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@@ -141,6 +171,8 @@ class EntityAccessControlHandlerTest extends UnitTestCase {
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->id()->willReturn('green_entity');
|
||||
$entity_type->getAdminPermission()->willReturn('administer green_entity');
|
||||
$entity_type->hasHandlerClass('permission_provider')->willReturn(TRUE);
|
||||
$entity_type->getHandlerClass('permission_provider')->willReturn(EntityPermissionProvider::class);
|
||||
|
||||
// User with the admin permission.
|
||||
$account = $this->prophesize(AccountInterface::class);
|
||||
@@ -182,16 +214,27 @@ class EntityAccessControlHandlerTest extends UnitTestCase {
|
||||
* @return \Prophecy\Prophecy\ObjectProphecy
|
||||
* The entity mock.
|
||||
*/
|
||||
protected function buildMockEntity(EntityTypeInterface $entity_type, $owner_id = NULL) {
|
||||
protected function buildMockEntity(EntityTypeInterface $entity_type, $owner_id = NULL, $bundle = NULL, $published = NULL) {
|
||||
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
|
||||
$entity = $this->prophesize(ContentEntityInterface::class);
|
||||
if (isset($published)) {
|
||||
$entity->willImplement(EntityPublishedInterface::class);
|
||||
}
|
||||
if ($owner_id) {
|
||||
$entity->willImplement(EntityOwnerInterface::class);
|
||||
}
|
||||
if (isset($published)) {
|
||||
$entity->isPublished()->willReturn($published);
|
||||
}
|
||||
if ($owner_id) {
|
||||
$entity->getOwnerId()->willReturn($owner_id);
|
||||
}
|
||||
$entity->bundle()->willReturn($entity_type->id());
|
||||
|
||||
$entity->bundle()->willReturn($bundle ?: $entity_type->id());
|
||||
$entity->isNew()->willReturn(FALSE);
|
||||
$entity->uuid()->willReturn('fake uuid');
|
||||
$entity->id()->willReturn('fake id');
|
||||
$entity->getRevisionId()->willReturn(NULL);
|
||||
$entity->language()->willReturn(new Language(['id' => $langcode]));
|
||||
$entity->getEntityTypeId()->willReturn($entity_type->id());
|
||||
$entity->getEntityType()->willReturn($entity_type);
|
||||
@@ -199,7 +242,16 @@ class EntityAccessControlHandlerTest extends UnitTestCase {
|
||||
$entity->getCacheTags()->willReturn([]);
|
||||
$entity->getCacheMaxAge()->willReturn(Cache::PERMANENT);
|
||||
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+38
-11
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace Drupal\Tests\entity\Unit;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
|
||||
use Drupal\Core\Entity\ContentEntityTypeInterface;
|
||||
use Drupal\Core\Entity\EntityPublishedInterface;
|
||||
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\entity\EntityPermissionProvider;
|
||||
@@ -37,6 +37,9 @@ class EntityPermissionProviderTest extends UnitTestCase {
|
||||
$entity_type_bundle_info->getBundleInfo('black_entity')->willReturn([
|
||||
'third' => ['label' => 'Third'],
|
||||
]);
|
||||
$entity_type_bundle_info->getBundleInfo('pink_entity')->willReturn([
|
||||
'third' => ['label' => 'Third'],
|
||||
]);
|
||||
$this->permissionProvider = new EntityPermissionProvider($entity_type_bundle_info->reveal());
|
||||
$this->permissionProvider->setStringTranslation($this->getStringTranslationStub());
|
||||
}
|
||||
@@ -69,15 +72,16 @@ class EntityPermissionProviderTest extends UnitTestCase {
|
||||
$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->entityClassImplements(EntityOwnerInterface::class)->willReturn(FALSE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::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',
|
||||
'view green_entity' => 'View green entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
@@ -87,18 +91,18 @@ class EntityPermissionProviderTest extends UnitTestCase {
|
||||
$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->entityClassImplements(EntityOwnerInterface::class)->willReturn(TRUE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::class)->willReturn(FALSE);
|
||||
$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',
|
||||
'view blue_entity' => 'View blue entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
@@ -108,18 +112,19 @@ class EntityPermissionProviderTest extends UnitTestCase {
|
||||
$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->entityClassImplements(EntityOwnerInterface::class)->willReturn(FALSE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::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',
|
||||
'view white_entity' => 'View white entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
@@ -129,18 +134,40 @@ class EntityPermissionProviderTest extends UnitTestCase {
|
||||
$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->entityClassImplements(EntityOwnerInterface::class)->willReturn(TRUE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::class)->willReturn(FALSE);
|
||||
$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',
|
||||
'view black_entity' => 'View black entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
// Content entity type with bundles and owner and entity published.
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->getProvider()->willReturn('entity_module_test');
|
||||
$entity_type->id()->willReturn('pink_entity');
|
||||
$entity_type->getSingularLabel()->willReturn('pink entity');
|
||||
$entity_type->getPluralLabel()->willReturn('pink entities');
|
||||
$entity_type->entityClassImplements(EntityOwnerInterface::class)->willReturn(TRUE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::class)->willReturn(TRUE);
|
||||
$entity_type->getPermissionGranularity()->willReturn('bundle');
|
||||
$expected_permissions = [
|
||||
'administer pink_entity' => 'Administer pink entities',
|
||||
'access pink_entity overview' => 'Access the pink entities overview page',
|
||||
'view own unpublished pink_entity' => 'View own unpublished pink entities',
|
||||
'create third pink_entity' => 'Third: Create pink entities',
|
||||
'update any third pink_entity' => 'Third: Update any pink entity',
|
||||
'update own third pink_entity' => 'Third: Update own pink entities',
|
||||
'delete any third pink_entity' => 'Third: Delete any pink entity',
|
||||
'delete own third pink_entity' => 'Third: Delete own pink entities',
|
||||
'view pink_entity' => 'View pink entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
|
||||
+299
@@ -0,0 +1,299 @@
|
||||
<?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\EntityPublishedInterface;
|
||||
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\UncacheableEntityAccessControlHandler;
|
||||
use Drupal\entity\UncacheableEntityPermissionProvider;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\user\EntityOwnerInterface;
|
||||
use Prophecy\Argument;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\entity\EntityAccessControlHandler
|
||||
* @group entity
|
||||
*/
|
||||
class UncacheableEntityAccessControlHandlerTest 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 UncacheableEntityAccessControlHandler($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 UncacheableEntityAccessControlHandler($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');
|
||||
$entity_type->hasHandlerClass('permission_provider')->willReturn(TRUE);
|
||||
$entity_type->getHandlerClass('permission_provider')->willReturn(UncacheableEntityPermissionProvider::class);
|
||||
|
||||
// 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];
|
||||
|
||||
// Per bundle permissions.
|
||||
$entity_first_other = $this->buildMockEntity($entity_type->reveal(), 9999, 'first');
|
||||
$entity_first_own = $this->buildMockEntity($entity_type->reveal(), 10, 'first');
|
||||
$entity_first_own_bundle = $this->buildMockEntity($entity_type->reveal(), 12, 'first');
|
||||
|
||||
$entity_second_other = $this->buildMockEntity($entity_type->reveal(), 9999, 'second');
|
||||
$entity_second_own = $this->buildMockEntity($entity_type->reveal(), 10, 'second');
|
||||
$entity_second_own_bundle = $this->buildMockEntity($entity_type->reveal(), 12, 'second');
|
||||
|
||||
$user_view_any = $this->buildMockUser(9, 'view any green_entity');
|
||||
$user_view_own = $this->buildMockUser(10, 'view own green_entity');
|
||||
$user_view_bundle_any = $this->buildMockUser(11, 'view any first green_entity');
|
||||
$user_view_bundle_own = $this->buildMockUser(12, 'view own first green_entity');
|
||||
|
||||
$data['entity_first_other user_view_any'] = [$entity_first_other->reveal(), 'view', $user_view_any->reveal(), TRUE];
|
||||
$data['entity_first_own user_view_any'] = [$entity_first_own->reveal(), 'view', $user_view_any->reveal(), TRUE];
|
||||
$data['entity_first_own_bundle user_view_any'] = [$entity_first_own_bundle->reveal(), 'view', $user_view_any->reveal(), TRUE];
|
||||
$data['entity_second_other user_view_any'] = [$entity_second_other->reveal(), 'view', $user_view_any->reveal(), TRUE];
|
||||
$data['entity_second_own user_view_any'] = [$entity_second_own->reveal(), 'view', $user_view_any->reveal(), TRUE];
|
||||
$data['entity_second_own_bundle user_view_any'] = [$entity_second_own_bundle->reveal(), 'view', $user_view_any->reveal(), TRUE];
|
||||
|
||||
$data['entity_first_other user_view_own'] = [$entity_first_other->reveal(), 'view', $user_view_own->reveal(), FALSE];
|
||||
$data['entity_first_own user_view_own'] = [$entity_first_own->reveal(), 'view', $user_view_own->reveal(), TRUE];
|
||||
$data['entity_first_own_bundle user_view_own'] = [$entity_first_own_bundle->reveal(), 'view', $user_view_own->reveal(), FALSE];
|
||||
$data['entity_second_other user_view_own'] = [$entity_second_other->reveal(), 'view', $user_view_own->reveal(), FALSE];
|
||||
$data['entity_second_own user_view_own'] = [$entity_second_own->reveal(), 'view', $user_view_own->reveal(), TRUE];
|
||||
$data['entity_second_own_bundle user_view_own'] = [$entity_second_own_bundle->reveal(), 'view', $user_view_own->reveal(), FALSE];
|
||||
|
||||
$data['entity_first_other user_view_bundle_any'] = [$entity_first_other->reveal(), 'view', $user_view_bundle_any->reveal(), TRUE];
|
||||
$data['entity_first_own user_view_bundle_any'] = [$entity_first_own->reveal(), 'view', $user_view_bundle_any->reveal(), TRUE];
|
||||
$data['entity_first_own_bundle user_view_bundle_any'] = [$entity_first_own_bundle->reveal(), 'view', $user_view_bundle_any->reveal(), TRUE];
|
||||
$data['entity_second_other user_view_bundle_any'] = [$entity_second_other->reveal(), 'view', $user_view_bundle_any->reveal(), FALSE];
|
||||
$data['entity_second_own user_view_bundle_any'] = [$entity_second_own->reveal(), 'view', $user_view_bundle_any->reveal(), FALSE];
|
||||
$data['entity_second_own_bundle user_view_bundle_any'] = [$entity_second_own_bundle->reveal(), 'view', $user_view_bundle_any->reveal(), FALSE];
|
||||
|
||||
$data['entity_first_other user_view_bundle_any'] = [$entity_first_other->reveal(), 'view', $user_view_bundle_own->reveal(), FALSE];
|
||||
$data['entity_first_own user_view_bundle_any'] = [$entity_first_own->reveal(), 'view', $user_view_bundle_own->reveal(), FALSE];
|
||||
$data['entity_first_own_bundle user_view_bundle_any'] = [$entity_first_own_bundle->reveal(), 'view', $user_view_bundle_own->reveal(), TRUE];
|
||||
$data['entity_second_other user_view_bundle_any'] = [$entity_second_other->reveal(), 'view', $user_view_bundle_own->reveal(), FALSE];
|
||||
$data['entity_second_own user_view_bundle_any'] = [$entity_second_own->reveal(), 'view', $user_view_bundle_own->reveal(), FALSE];
|
||||
$data['entity_second_own_bundle user_view_bundle_any'] = [$entity_second_own_bundle->reveal(), 'view', $user_view_bundle_own->reveal(), FALSE];
|
||||
|
||||
// Test the unpublished permissions.
|
||||
$entity_first_other_up = $this->buildMockEntity($entity_type->reveal(), 9999, 'first', FALSE);
|
||||
$entity_first_own_up = $this->buildMockEntity($entity_type->reveal(), 14, 'first', FALSE);
|
||||
$entity_first_own_bundle_up = $this->buildMockEntity($entity_type->reveal(), 15, 'first', FALSE);
|
||||
|
||||
$entity_second_other_up = $this->buildMockEntity($entity_type->reveal(), 9999, 'second', FALSE);
|
||||
$entity_second_own_up = $this->buildMockEntity($entity_type->reveal(), 14, 'second', FALSE);
|
||||
$entity_second_own_bundle_up = $this->buildMockEntity($entity_type->reveal(), 15, 'second', FALSE);
|
||||
|
||||
$user_view_own_up = $this->buildMockUser(14, 'view own unpublished green_entity');
|
||||
$user_view_other = $this->buildMockUser(15, 'view green_entity');
|
||||
|
||||
$data['entity_first_other_up user_view_own_up'] = [$entity_first_other_up->reveal(), 'view', $user_view_own_up->reveal(), FALSE];
|
||||
$data['entity_first_own_up user_view_own_up'] = [$entity_first_own_up->reveal(), 'view', $user_view_own_up->reveal(), TRUE];
|
||||
$data['entity_first_own_bundle_up user_view_own_up'] = [$entity_first_own_bundle_up->reveal(), 'view', $user_view_own_up->reveal(), FALSE];
|
||||
$data['entity_second_other_up user_view_own_up'] = [$entity_second_other_up->reveal(), 'view', $user_view_own_up->reveal(), FALSE];
|
||||
$data['entity_second_own_up user_view_own_up'] = [$entity_second_own_up->reveal(), 'view', $user_view_own_up->reveal(), TRUE];
|
||||
$data['entity_second_own_bundle_up user_view_own_up'] = [$entity_second_own_bundle_up->reveal(), 'view', $user_view_own_up->reveal(), FALSE];
|
||||
|
||||
$data['entity_first_other_up user_view_other'] = [$entity_first_other_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_first_own_up user_view_other'] = [$entity_first_own_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_first_own_bundle_up user_view_other'] = [$entity_first_own_bundle_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_second_other_up user_view_other'] = [$entity_second_other_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_second_own_up user_view_other'] = [$entity_second_own_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
$data['entity_second_own_bundle_up user_view_other'] = [$entity_second_own_bundle_up->reveal(), 'view', $user_view_other->reveal(), FALSE];
|
||||
|
||||
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');
|
||||
$entity_type->hasHandlerClass('permission_provider')->willReturn(TRUE);
|
||||
$entity_type->getHandlerClass('permission_provider')->willReturn(UncacheableEntityPermissionProvider::class);
|
||||
|
||||
// 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, $bundle = NULL, $published = NULL) {
|
||||
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
|
||||
$entity = $this->prophesize(ContentEntityInterface::class);
|
||||
if (isset($published)) {
|
||||
$entity->willImplement(EntityPublishedInterface::class);
|
||||
}
|
||||
if ($owner_id) {
|
||||
$entity->willImplement(EntityOwnerInterface::class);
|
||||
}
|
||||
if (isset($published)) {
|
||||
$entity->isPublished()->willReturn($published);
|
||||
}
|
||||
if ($owner_id) {
|
||||
$entity->getOwnerId()->willReturn($owner_id);
|
||||
}
|
||||
|
||||
$entity->bundle()->willReturn($bundle ?: $entity_type->id());
|
||||
$entity->isNew()->willReturn(FALSE);
|
||||
$entity->uuid()->willReturn('fake uuid');
|
||||
$entity->id()->willReturn('fake id');
|
||||
$entity->getRevisionId()->willReturn(NULL);
|
||||
$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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\entity\Unit;
|
||||
|
||||
use Drupal\Core\Entity\ContentEntityTypeInterface;
|
||||
use Drupal\Core\Entity\EntityPublishedInterface;
|
||||
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\entity\UncacheableEntityPermissionProvider;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\user\EntityOwnerInterface;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\entity\UncacheableEntityPermissionProvider
|
||||
* @group entity
|
||||
*/
|
||||
class UncacheableEntityPermissionProviderTest 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'],
|
||||
]);
|
||||
$entity_type_bundle_info->getBundleInfo('pink_entity')->willReturn([
|
||||
'third' => ['label' => 'Third'],
|
||||
]);
|
||||
$this->permissionProvider = new UncacheableEntityPermissionProvider($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->entityClassImplements(EntityOwnerInterface::class)->willReturn(FALSE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::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',
|
||||
'create green_entity' => 'Create green entities',
|
||||
'update green_entity' => 'Update green entities',
|
||||
'delete green_entity' => 'Delete green entities',
|
||||
'view any green_entity' => 'View any 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->entityClassImplements(EntityOwnerInterface::class)->willReturn(TRUE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::class)->willReturn(FALSE);
|
||||
$entity_type->getPermissionGranularity()->willReturn('entity_type');
|
||||
$expected_permissions = [
|
||||
'administer blue_entity' => 'Administer blue entities',
|
||||
'access blue_entity overview' => 'Access the blue entities overview page',
|
||||
'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',
|
||||
'view any blue_entity' => 'View any blue entities',
|
||||
'view own blue_entity' => 'View 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->entityClassImplements(EntityOwnerInterface::class)->willReturn(FALSE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::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',
|
||||
'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',
|
||||
'view any white_entity' => 'View any white entities',
|
||||
'view any first white_entity' => 'First: View any white entities',
|
||||
'view any second white_entity' => 'Second: View any 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->entityClassImplements(EntityOwnerInterface::class)->willReturn(TRUE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::class)->willReturn(FALSE);
|
||||
$entity_type->getPermissionGranularity()->willReturn('bundle');
|
||||
$expected_permissions = [
|
||||
'administer black_entity' => 'Administer black entities',
|
||||
'access black_entity overview' => 'Access the black entities overview page',
|
||||
'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',
|
||||
'view any black_entity' => 'View any black entities',
|
||||
'view own black_entity' => 'View own black entities',
|
||||
'view any third black_entity' => 'Third: View any black entities',
|
||||
'view own third black_entity' => 'Third: View own black entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
// Content entity type with bundles and owner and entity published.
|
||||
$entity_type = $this->prophesize(ContentEntityTypeInterface::class);
|
||||
$entity_type->getProvider()->willReturn('entity_module_test');
|
||||
$entity_type->id()->willReturn('pink_entity');
|
||||
$entity_type->getSingularLabel()->willReturn('pink entity');
|
||||
$entity_type->getPluralLabel()->willReturn('pink entities');
|
||||
$entity_type->entityClassImplements(EntityOwnerInterface::class)->willReturn(TRUE);
|
||||
$entity_type->entityClassImplements(EntityPublishedInterface::class)->willReturn(TRUE);
|
||||
$entity_type->getPermissionGranularity()->willReturn('bundle');
|
||||
$expected_permissions = [
|
||||
'administer pink_entity' => 'Administer pink entities',
|
||||
'access pink_entity overview' => 'Access the pink entities overview page',
|
||||
'view own unpublished pink_entity' => 'View own unpublished pink entities',
|
||||
'create third pink_entity' => 'Third: Create pink entities',
|
||||
'update any third pink_entity' => 'Third: Update any pink entity',
|
||||
'update own third pink_entity' => 'Third: Update own pink entities',
|
||||
'delete any third pink_entity' => 'Third: Delete any pink entity',
|
||||
'delete own third pink_entity' => 'Third: Delete own pink entities',
|
||||
'view any pink_entity' => 'View any pink entities',
|
||||
'view own pink_entity' => 'View own pink entities',
|
||||
'view any third pink_entity' => 'Third: View any pink entities',
|
||||
'view own third pink_entity' => 'Third: View own pink entities',
|
||||
];
|
||||
$data[] = [$entity_type->reveal(), $expected_permissions];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user