View source
<?php
namespace Drupal\Tests\entity_print\Kernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_print\Controller\EntityPrintController;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
class EntityPrintAccessTest extends KernelTestBase {
use UserCreationTrait;
use NodeCreationTrait;
use ContentTypeCreationTrait;
public static $modules = [
'system',
'user',
'node',
'field',
'text',
'filter',
'entity_print',
];
protected $node;
public function setUp() : void {
parent::setUp();
$this
->installSchema('system', [
'sequences',
'key_value_expire',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installConfig([
'system',
'node',
'filter',
'field',
]);
$this
->createUser();
$this
->createContentType([
'type' => 'page',
'name' => 'Page',
]);
$this
->createContentType([
'type' => 'article',
'name' => 'Article',
]);
}
public function testAccessPermissions($permissions, $expected_access) {
$id = $this
->createNode()
->id();
$account = $this
->createUser($permissions);
$this->container
->get('current_user')
->setAccount($account);
$controller = EntityPrintController::create($this->container);
$this
->assertSame($expected_access, $controller
->checkAccess('pdf', 'node', $id)
->isAllowed());
}
public function accessPermissionsDataProvider() {
return [
'Permission "bypass entity print access" only cannot view PDF.' => [
[
'bypass entity print access',
],
FALSE,
],
'Permission "access content" only cannot view PDF.' => [
[
'access content',
],
FALSE,
],
'Permission "access content" and "bypass entity print access" can view PDF.' => [
[
'bypass entity print access',
'access content',
],
TRUE,
],
'Per entity type permissions allow access.' => [
[
'entity print access type node',
'access content',
],
TRUE,
],
'Per bundle permissions allow access.' => [
[
'entity print access bundle page',
'access content',
],
TRUE,
],
'Incorrect entity type permission cannot access' => [
[
'entity print access type user',
'access content',
],
FALSE,
],
'Incorrect bundle permissions cannot access' => [
[
'entity print access bundle article',
'access content',
],
FALSE,
],
'No permissions cannot access' => [
[],
FALSE,
],
];
}
public function testInvalidRouteParameters($entity_type, $entity_id, $export_type) {
$entity_id = $entity_id ?: $this
->createNode()
->id();
$account = $this
->createUser([
'bypass entity print access',
'access content',
]);
$this
->assertSame(FALSE, $this
->checkAccess($account, $entity_type, $entity_id, $export_type));
}
public function invalidRouteParametersDataProvider() {
return [
'Invalid entity type triggers access denied.' => [
'invalid',
FALSE,
'pdf',
],
'Invalid entity id triggers access denied.' => [
'node',
'invalid-entity-id',
'pdf',
],
'Invalid export type triggers access denied.' => [
'node',
FALSE,
'invalid-export-type',
],
];
}
public function testSecondaryEntityTypeAccess() {
$account = $this
->createUser([
'entity print access type user',
'access content',
]);
$this
->assertTrue($this
->checkAccess($account, 'user', $account
->id()), 'User with "type user" permission and access content permission is allowed to see the content.');
}
protected function checkAccess(AccountInterface $account, $entity_type, $entity_id, $export_type = 'pdf') {
$this->container
->get('current_user')
->setAccount($account);
$controller = EntityPrintController::create($this->container);
return $controller
->checkAccess($export_type, $entity_type, $entity_id)
->isAllowed();
}
}