infinito/application/tests/Unit/Domain/RightManagement/RightCheckerTest.php

105 lines
3.5 KiB
PHP
Raw Normal View History

2018-12-08 12:51:29 +01:00
<?php
2018-12-08 12:51:29 +01:00
namespace Tests\Unit\Domain\RightManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Meta\RightInterface;
use App\Entity\Meta\Right;
use App\Entity\Source\SourceInterface;
use App\Entity\Source\AbstractSource;
use App\DBAL\Types\LayerType;
use App\Domain\RightManagement\RightCheckerInterface;
use App\Domain\RightManagement\RightChecker;
use App\DBAL\Types\RightType;
class RightCheckerTest extends TestCase
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $layer;
2018-12-08 12:51:29 +01:00
/**
* @var SourceInterface
*/
private $source;
2018-12-08 12:51:29 +01:00
/**
* @var RightInterface
*/
private $right;
2018-12-08 12:51:29 +01:00
/**
* @var RightCheckerInterface
*/
private $rightManager;
private function getSourceMock(): SourceInterface
{
return new class() extends AbstractSource {
};
2018-12-08 12:51:29 +01:00
}
public function setUp(): void
{
$this->layer = LayerType::RELATION;
$this->type = RightType::READ;
2018-12-08 12:51:29 +01:00
$this->source = $this->getSourceMock();
$this->right = new Right();
2018-12-08 12:51:29 +01:00
$this->right->setSource($this->source);
$this->right->setType($this->type);
$this->right->setLayer($this->layer);
2018-12-08 12:51:29 +01:00
$this->rightManager = new RightChecker($this->right);
}
public function testFirstDimension(): void
{
$granted = $this->rightManager->isGranted($this->layer, $this->type, $this->source);
$this->assertTrue($granted);
$notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $this->source);
$this->assertFalse($notGranted);
$notGranted2 = $this->rightManager->isGranted($this->layer, RightType::WRITE, $this->source);
$this->assertFalse($notGranted2);
$this->right->setGrant(false);
$notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $this->source);
$this->assertFalse($notGranted3);
}
public function testSecondDimension(): void
{
$secondSource = $this->getSourceMock();
$this->source->getMemberRelation()->getMembers()->add($secondSource);
$granted = $this->rightManager->isGranted($this->layer, $this->type, $secondSource);
$this->assertTrue($granted);
$notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $secondSource);
$this->assertFalse($notGranted);
$notGranted2 = $this->rightManager->isGranted($this->layer, RightType::WRITE, $secondSource);
$this->assertFalse($notGranted2);
$this->right->setGrant(false);
$notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $secondSource);
$this->assertFalse($notGranted3);
}
public function testThirdDimension(): void
{
$thirdSource = $this->getSourceMock();
$secondSource = $this->getSourceMock();
$secondSource->getMemberRelation()->getMembers()->add($thirdSource);
$this->source->getMemberRelation()->getMembers()->add($secondSource);
$granted = $this->rightManager->isGranted($this->layer, $this->type, $thirdSource);
2018-12-08 12:51:29 +01:00
$this->assertTrue($granted);
$notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $thirdSource);
2018-12-08 12:51:29 +01:00
$this->assertFalse($notGranted);
$notGranted2 = $this->rightManager->isGranted($this->layer, RightType::WRITE, $thirdSource);
2018-12-08 12:51:29 +01:00
$this->assertFalse($notGranted2);
$this->right->setGrant(false);
$notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $thirdSource);
2018-12-08 12:51:29 +01:00
$this->assertFalse($notGranted3);
}
}