PermissionsGeneratorTest.php in Entity Construction Kit (ECK) 8
File
tests/src/Unit/PermissionsGeneratorTest.php
View source
<?php
namespace Drupal\Tests\eck\Unit;
use Drupal\eck\PermissionsGenerator;
class PermissionsGeneratorTest extends UnitTestBase {
private $sut;
protected function setUp() {
parent::setUp();
$this->sut = $this
->createNewSubjectUnderTest();
}
private function createNewSubjectUnderTest() {
$permissionsGenerator = new PermissionsGenerator();
$permissionsGenerator
->setStringTranslation($this
->getStringTranslationStub());
return $permissionsGenerator;
}
public function generatesNoPermissionsIfNoEntityTypesAreDefined() {
$this
->assertSame([], $this->sut
->entityPermissions());
}
public function givenSingleEntityTypeGeneratesCorrectPermissions() {
$this
->addEntityToStorage($this
->createEckEntityType('entity_type'));
$permissions = $this->sut
->entityPermissions();
$this
->assertCreatePermission($permissions);
$this
->assertGlobalPermissions($permissions);
$this
->assertOwnerPermissions($permissions);
}
public function givenSingleEntityTypeWithAuthorFieldGeneratesCorrectPermissions() {
$this
->addEntityToStorage($this
->createEckEntityType('entity_type', [
'uid' => TRUE,
]));
$permissions = $this->sut
->entityPermissions();
$this
->assertCreatePermission($permissions);
$this
->assertGlobalPermissions($permissions);
$this
->assertOwnerPermissions($permissions);
}
public function givenMultipleEntityTypesWithMixedSettingsGeneratesCorrectPermissions() {
$this
->addEntityToStorage($this
->createEckEntityType('entity_type'));
$this
->addEntityToStorage($this
->createEckEntityType('another_type', [
'uid' => TRUE,
]));
$permissions = $this->sut
->entityPermissions();
$this
->assertCreatePermission($permissions);
$this
->assertGlobalPermissions($permissions);
$this
->assertOwnerPermissions($permissions);
}
protected function assertCreatePermission($permissions) {
foreach ($this->entities as $id => $entity) {
$this
->assertArrayHasKey("create {$id} entities", $permissions);
}
}
protected function assertGlobalPermissions($permissions) {
foreach ($this->entities as $id => $entity) {
$this
->assertArrayHasKey("edit any {$id} entities", $permissions);
$this
->assertArrayHasKey("delete any {$id} entities", $permissions);
$this
->assertArrayHasKey("view any {$id} entities", $permissions);
}
}
protected function assertOwnerPermissions($permissions) {
foreach ($this->entities as $id => $entity) {
if ($entity
->hasAuthorField()) {
$this
->assertArrayHasKey("edit own {$id} entities", $permissions);
$this
->assertArrayHasKey("delete own {$id} entities", $permissions);
$this
->assertArrayHasKey("view own {$id} entities", $permissions);
}
else {
$this
->assertArrayNotHasKey("edit own {$id} entities", $permissions);
$this
->assertArrayNotHasKey("delete own {$id} entities", $permissions);
$this
->assertArrayNotHasKey("view own {$id} entities", $permissions);
}
}
}
}