class PageAccessTest in Page Manager 8
Same name and namespace in other branches
- 8.4 tests/src/Unit/PageAccessTest.php \Drupal\Tests\page_manager\Unit\PageAccessTest
Tests access for Page entities.
@coversDefaultClass \Drupal\page_manager\Entity\PageAccess
@group PageManager
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\page_manager\Unit\PageAccessTest
Expanded class hierarchy of PageAccessTest
File
- tests/
src/ Unit/ PageAccessTest.php, line 29 - Contains \Drupal\Tests\page_manager\Unit\PageAccessTest.
Namespace
Drupal\Tests\page_manager\UnitView source
class PageAccessTest extends UnitTestCase {
/**
* The context handler.
*
* @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $contextHandler;
/**
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityType;
/**
* @var \Drupal\Core\Cache\Context\CacheContextsManager|\Prophecy\Prophecy\ProphecyInterface
*/
protected $cacheContextsManager;
/**
* @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
*/
protected $pageAccess;
/**
* @covers ::__construct
*/
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);
}
/**
* @covers ::checkAccess
*/
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()
->shouldBeCalled();
$page
->getEntityTypeId()
->shouldBeCalled();
$account = $this
->prophesize(AccountInterface::class);
$this
->assertTrue($this->pageAccess
->access($page
->reveal(), 'view', $account
->reveal()));
}
/**
* @covers ::checkAccess
*/
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()
->shouldBeCalled();
$page
->getEntityTypeId()
->shouldBeCalled();
$account = $this
->prophesize(AccountInterface::class);
$this
->assertFalse($this->pageAccess
->access($page
->reveal(), 'view', $account
->reveal()));
}
/**
* @covers ::checkAccess
*
* @dataProvider providerTestAccessDelete
*/
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()
->shouldBeCalled();
$page
->getEntityTypeId()
->shouldBeCalled();
// Ensure that the cache tag is added for the temporary conditions.
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()));
}
/**
* Provides data for testAccessDelete().
*/
public function providerTestAccessDelete() {
$data = [];
$data[] = [
TRUE,
FALSE,
];
$data[] = [
FALSE,
TRUE,
];
return $data;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PageAccessTest:: |
protected | property | ||
PageAccessTest:: |
protected | property | The context handler. | |
PageAccessTest:: |
protected | property | ||
PageAccessTest:: |
protected | property | ||
PageAccessTest:: |
public | function | Provides data for testAccessDelete(). | |
PageAccessTest:: |
public | function |
@covers ::__construct Overrides UnitTestCase:: |
|
PageAccessTest:: |
public | function | @covers ::checkAccess | |
PageAccessTest:: |
public | function | @covers ::checkAccess | |
PageAccessTest:: |
public | function | @covers ::checkAccess | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |