PageAccessTest.php in Page Manager 8.4
File
tests/src/Unit/PageAccessTest.php
View source
<?php
namespace Drupal\Tests\page_manager\Unit;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\page_manager\Entity\PageAccess;
use Drupal\page_manager\PageInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class PageAccessTest extends UnitTestCase {
protected $contextHandler;
protected $entityType;
protected $cacheContextsManager;
protected $pageAccess;
public function setUp() {
parent::setUp();
$this->contextHandler = $this
->prophesize(ContextHandlerInterface::class);
$this->entityType = $this
->prophesize(EntityTypeInterface::class);
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->invokeAll(Argument::cetera())
->willReturn([]);
$this->pageAccess = new PageAccess($this->entityType
->reveal(), $this->contextHandler
->reveal());
$this->pageAccess
->setModuleHandler($module_handler
->reveal());
$this->cacheContextsManager = $this
->prophesize(CacheContextsManager::class);
$container = new ContainerBuilder();
$container
->set('cache_contexts_manager', $this->cacheContextsManager
->reveal());
\Drupal::setContainer($container);
}
public function testAccessView() {
$page = $this
->prophesize(PageInterface::class);
$page
->getContexts()
->willReturn([]);
$page
->getAccessConditions()
->willReturn([]);
$page
->getAccessLogic()
->willReturn('and');
$page
->status()
->willReturn(TRUE);
$page
->language()
->willReturn($this
->prophesize(LanguageInterface::class)
->reveal());
$page
->uuid()
->willReturn('some-uuid');
$page
->getEntityTypeId()
->shouldBeCalled();
$account = $this
->prophesize(AccountInterface::class);
$this
->assertTrue($this->pageAccess
->access($page
->reveal(), 'view', $account
->reveal()));
}
public function testAccessViewDisabled() {
$page = $this
->prophesize(PageInterface::class);
$page
->status()
->willReturn(FALSE);
$page
->getCacheTags()
->willReturn([
'page:1',
]);
$page
->getCacheContexts()
->willReturn([]);
$page
->getCacheMaxAge()
->willReturn(0);
$page
->language()
->willReturn($this
->prophesize(LanguageInterface::class)
->reveal());
$page
->uuid()
->willReturn('some-uuid');
$page
->getEntityTypeId()
->shouldBeCalled();
$account = $this
->prophesize(AccountInterface::class);
$this
->assertFalse($this->pageAccess
->access($page
->reveal(), 'view', $account
->reveal()));
}
public function testAccessDelete($is_new, $expected) {
$this->entityType
->getAdminPermission()
->willReturn('test permission');
$page = $this
->prophesize(PageInterface::class);
$page
->isNew()
->willReturn($is_new);
$page
->language()
->willReturn($this
->prophesize(LanguageInterface::class)
->reveal());
$page
->uuid()
->willReturn('some-uuid');
$page
->getEntityTypeId()
->shouldBeCalled();
if ($is_new) {
$page
->getCacheTags()
->willReturn([
'page:1',
]);
$page
->getCacheContexts()
->willReturn([]);
$page
->getCacheMaxAge()
->willReturn(0);
}
else {
$this->cacheContextsManager
->assertValidTokens([
'user.permissions',
])
->willReturn(TRUE);
}
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission('test permission')
->willReturn(TRUE);
$account
->id()
->shouldBeCalled();
$this
->assertSame($expected, $this->pageAccess
->access($page
->reveal(), 'delete', $account
->reveal()));
}
public function providerTestAccessDelete() {
$data = [];
$data[] = [
TRUE,
FALSE,
];
$data[] = [
FALSE,
TRUE,
];
return $data;
}
}