View source
<?php
namespace Drupal\Tests\Core\Entity\Access;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Entity\Entity\Access\EntityFormDisplayAccessControlHandler;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
class EntityFormDisplayAccessControlHandlerTest extends UnitTestCase {
protected $accessControlHandler;
protected $moduleHandler;
protected $anon;
protected $member;
protected $parent_member;
protected $entity;
protected function getEntityTypeManager() {
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
return $entity_type_manager
->reveal();
}
protected function setUp() {
parent::setUp();
$this->anon = $this
->createMock(AccountInterface::class);
$this->anon
->expects($this
->any())
->method('hasPermission')
->will($this
->returnValue(FALSE));
$this->anon
->expects($this
->any())
->method('id')
->will($this
->returnValue(0));
$this->member = $this
->createMock(AccountInterface::class);
$this->member
->expects($this
->any())
->method('hasPermission')
->will($this
->returnValueMap([
[
'administer foobar form display',
TRUE,
],
]));
$this->member
->expects($this
->any())
->method('id')
->will($this
->returnValue(2));
$this->parent_member = $this
->createMock(AccountInterface::class);
$this->parent_member
->expects($this
->any())
->method('hasPermission')
->will($this
->returnValueMap([
[
'Llama',
TRUE,
],
]));
$this->parent_member
->expects($this
->any())
->method('id')
->will($this
->returnValue(3));
$entity_form_display_entity_type = $this
->createMock(ConfigEntityTypeInterface::class);
$entity_form_display_entity_type
->expects($this
->any())
->method('getAdminPermission')
->will($this
->returnValue('Llama'));
$entity_form_display_entity_type
->expects($this
->any())
->method('getKey')
->will($this
->returnValueMap([
[
'langcode',
'langcode',
],
]));
$entity_form_display_entity_type
->expects($this
->any())
->method('entityClassImplements')
->will($this
->returnValue(TRUE));
$entity_form_display_entity_type
->expects($this
->any())
->method('getConfigPrefix')
->willReturn('');
$this->moduleHandler = $this
->createMock(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 EntityFormDisplayAccessControlHandler($entity_form_display_entity_type);
$storage_access_control_handler
->setModuleHandler($this->moduleHandler);
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_manager
->expects($this
->any())
->method('getStorage')
->willReturnMap([
[
'entity_display',
$this
->createMock(EntityStorageInterface::class),
],
]);
$entity_type_manager
->expects($this
->any())
->method('getAccessControlHandler')
->willReturnMap([
[
'entity_display',
$storage_access_control_handler,
],
]);
$entity_type_manager
->expects($this
->any())
->method('getDefinition')
->will($this
->returnValue($entity_form_display_entity_type));
$entity_field_manager = $this
->createMock(EntityFieldManagerInterface::class);
$entity_field_manager
->expects($this
->any())
->method('getFieldDefinitions')
->will($this
->returnValue([]));
$container = new Container();
$container
->set('entity_type.manager', $entity_type_manager);
$container
->set('entity_field.manager', $entity_field_manager);
$container
->set('language_manager', $this
->createMock(LanguageManagerInterface::class));
$container
->set('plugin.manager.field.widget', $this
->prophesize(PluginManagerInterface::class));
$container
->set('plugin.manager.field.field_type', $this
->createMock(FieldTypePluginManagerInterface::class));
$container
->set('plugin.manager.field.formatter', $this
->prophesize(FormatterPluginManager::class));
$container
->set('uuid', $this
->createMock(UuidInterface::class));
$container
->set('renderer', $this
->createMock(RendererInterface::class));
$container
->set('cache_contexts_manager', $this
->prophesize(CacheContextsManager::class));
\Drupal::setContainer($container);
$this->entity = new EntityFormDisplay([
'targetEntityType' => 'foobar',
'bundle' => 'bazqux',
'mode' => 'default',
'id' => 'foobar.bazqux.default',
'uuid' => '6f2f259a-f3c7-42ea-bdd5-111ad1f85ed1',
], 'entity_display');
$this->accessControlHandler = $storage_access_control_handler;
}
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.");
}
}
public function testAccess() {
$this
->assertAllowOperations([], $this->anon);
$this
->assertAllowOperations([
'view',
'update',
'delete',
], $this->member);
$this
->assertAllowOperations([
'view',
'update',
'delete',
], $this->parent_member);
$this->entity
->enforceIsNew(TRUE)
->save();
$this->accessControlHandler
->resetCache();
$this
->assertAllowOperations([], $this->anon);
$this
->assertAllowOperations([
'view',
'update',
], $this->member);
$this
->assertAllowOperations([
'view',
'update',
], $this->parent_member);
}
}