View source
<?php
namespace Drupal\Tests\entity\Unit;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity\BundleEntityAccessControlHandler;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class BundleEntityAccessControlHandlerTest extends UnitTestCase {
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);
}
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);
}
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 = $this
->buildMockUser(1, 'access content');
$data[] = [
$entity
->reveal(),
'view label',
$user
->reveal(),
FALSE,
];
$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;
}
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;
}
}