View source
<?php
namespace Drupal\Tests\entity_print_views\Kernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\entity_print_views\Controller\ViewPrintController;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class ViewsAccessTest extends KernelTestBase {
use UserCreationTrait;
public static $modules = [
'system',
'node',
'user',
'entity_print',
'views',
'entity_print_views',
'entity_print_views_test_views',
];
public function setUp() : void {
parent::setUp();
$this
->installSchema('system', [
'sequences',
'key_value_expire',
]);
$this
->installEntitySchema('user');
$this
->installConfig('entity_print_views_test_views');
$this
->createUser();
}
public function testEntityPrintAccess() {
$this
->assertFalse($this
->checkAccess(new AnonymousUserSession(), 'my_test_view', 'page_1'), 'Access not allowed because we do not have permission');
$account = $this
->createUser([
'entity print views access',
]);
$this
->assertFalse($this
->checkAccess($account, 'my_test_view', 'page_1'), 'Access not allowed because user does not have permission set on view');
$account = $this
->createUser([
'administer nodes',
]);
$this
->assertFalse($this
->checkAccess($account, 'my_test_view', 'page_1'), 'Access not allowed because user does not have entity print permission.');
$account = $this
->createUser([
'entity print views access',
'administer nodes',
]);
$this
->assertTrue($this
->checkAccess($account, 'my_test_view', 'page_1'), 'Access allowed for user with "entity print views access" and "administer nodes"');
}
protected function checkAccess(AccountInterface $account, $view_name, $display_id) {
$this->container
->get('current_user')
->setAccount($account);
$controller = ViewPrintController::create($this->container);
return $controller
->checkAccess('pdf', $view_name, $display_id)
->isAllowed();
}
}