2018-12-08 20:25:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Unit\Domain\LawManagement;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use App\Domain\LawManagement\LawPermissionCheckerService;
|
|
|
|
use App\Domain\LawManagement\LawPermissionCheckerServiceInterface;
|
|
|
|
use App\Entity\Source\SourceInterface;
|
|
|
|
use App\Entity\Source\AbstractSource;
|
|
|
|
use App\Entity\Meta\Right;
|
|
|
|
use App\DBAL\Types\LayerType;
|
|
|
|
use App\DBAL\Types\RightType;
|
|
|
|
use App\Entity\Meta\Law;
|
|
|
|
use App\Entity\Meta\LawInterface;
|
|
|
|
use App\Entity\Meta\RightInterface;
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2018-12-29 23:12:28 +01:00
|
|
|
use App\Domain\SourceManagement\SourceMemberManager;
|
2018-12-08 20:25:29 +01:00
|
|
|
|
2018-12-14 10:10:28 +01:00
|
|
|
/**
|
|
|
|
* @author kevinfrantz
|
|
|
|
*/
|
2018-12-08 20:25:29 +01:00
|
|
|
class LawPermissionCheckerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2018-12-29 23:12:28 +01:00
|
|
|
* @var LawPermissionCheckerServiceInterface The service which checks the law
|
2018-12-08 20:25:29 +01:00
|
|
|
*/
|
2018-12-29 23:12:28 +01:00
|
|
|
private $lawPermissionChecker;
|
2018-12-08 20:25:29 +01:00
|
|
|
|
|
|
|
/**
|
2018-12-29 23:12:28 +01:00
|
|
|
* @var LawInterface The law which applies to the source
|
2018-12-08 20:25:29 +01:00
|
|
|
*/
|
2018-12-29 23:12:28 +01:00
|
|
|
private $law;
|
2018-12-08 20:25:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RightInterface
|
|
|
|
*/
|
2018-12-29 23:12:28 +01:00
|
|
|
private $clientRight;
|
2018-12-14 10:10:28 +01:00
|
|
|
|
|
|
|
/**
|
2018-12-29 23:12:28 +01:00
|
|
|
* @var SourceInterface The client which requests a law
|
2018-12-14 10:10:28 +01:00
|
|
|
*/
|
|
|
|
private $clientSource;
|
2018-12-08 20:25:29 +01:00
|
|
|
|
2018-12-29 23:12:28 +01:00
|
|
|
/**
|
|
|
|
* @var SourceInterface The source to which the law applies
|
|
|
|
*/
|
|
|
|
private $source;
|
|
|
|
|
2018-12-08 20:25:29 +01:00
|
|
|
private function getSourceMock(): SourceInterface
|
|
|
|
{
|
|
|
|
return new class() extends AbstractSource {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-08 21:20:07 +01:00
|
|
|
private function checkClientPermission(): bool
|
|
|
|
{
|
2018-12-14 10:10:28 +01:00
|
|
|
return $this->lawPermissionChecker->hasPermission($this->clientRight);
|
2018-12-08 21:20:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 20:25:29 +01:00
|
|
|
public function setUp(): void
|
|
|
|
{
|
2018-12-29 23:12:28 +01:00
|
|
|
$this->setSourceDummy();
|
|
|
|
$this->setLawDummy();
|
|
|
|
$this->setLawPermissionChecker();
|
|
|
|
$this->setClientSourceDummy();
|
|
|
|
$this->setClientRightDummy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setLawPermissionChecker(): void
|
|
|
|
{
|
2018-12-08 20:25:29 +01:00
|
|
|
$this->lawPermissionChecker = new LawPermissionCheckerService($this->law);
|
2018-12-29 23:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function setLawDummy(): void
|
|
|
|
{
|
|
|
|
$this->law = new Law();
|
|
|
|
$this->law->setSource($this->source);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setSourceDummy(): void
|
|
|
|
{
|
|
|
|
$this->source = $this->getSourceMock();
|
|
|
|
$this->source->setSlug('Requested Source');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setClientSourceDummy(): void
|
|
|
|
{
|
2018-12-14 10:10:28 +01:00
|
|
|
$this->clientSource = $this->getSourceMock();
|
2018-12-29 23:12:28 +01:00
|
|
|
$this->clientSource->setSlug('Client Source');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setClientRightDummy(): void
|
|
|
|
{
|
|
|
|
$this->clientRight = new Right();
|
2018-12-14 10:10:28 +01:00
|
|
|
$this->clientRight->setLayer(LayerType::SOURCE);
|
|
|
|
$this->clientRight->setType(RightType::READ);
|
2018-12-29 23:12:28 +01:00
|
|
|
$this->clientRight->setReciever($this->clientSource);
|
|
|
|
$this->clientRight->setSource($this->source);
|
2018-12-08 21:20:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 23:12:28 +01:00
|
|
|
private function getClonedClientRight(): RightInterface
|
|
|
|
{
|
|
|
|
return clone $this->clientRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInitialValues(): void
|
2018-12-08 21:20:07 +01:00
|
|
|
{
|
|
|
|
$this->assertFalse($this->checkClientPermission());
|
2018-12-29 23:12:28 +01:00
|
|
|
$this->assertTrue($this->clientRight->getGrant());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGeneralPermission(): void
|
|
|
|
{
|
|
|
|
$this->law->getRights()->add($this->getClonedClientRight());
|
2018-12-08 21:20:07 +01:00
|
|
|
$this->assertTrue($this->checkClientPermission());
|
2018-12-14 10:10:28 +01:00
|
|
|
$this->clientRight->setType(RightType::WRITE);
|
2018-12-08 21:20:07 +01:00
|
|
|
$this->assertFalse($this->checkClientPermission());
|
2018-12-08 20:25:29 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 00:31:03 +01:00
|
|
|
public function testChildMemberPermission(): void
|
|
|
|
{
|
|
|
|
$parentSource = $this->getSourceMock();
|
|
|
|
$parentSource->setSlug('Parent Source');
|
|
|
|
$parentSourceMemberManager = new SourceMemberManager($parentSource);
|
|
|
|
$parentSourceMemberManager->addMember($this->clientSource);
|
|
|
|
$this->assertEquals($parentSource, $this->clientSource->getMemberRelation()->getMemberships()->get(0)->getSource());
|
|
|
|
$this->assertEquals($this->clientSource, $parentSource->getMemberRelation()->getMembers()->get(0)->getSource());
|
|
|
|
$parentSourceRight = $this->getClonedClientRight();
|
|
|
|
$parentSourceRight->setReciever($parentSource);
|
|
|
|
$this->law->getRights()->add($parentSourceRight);
|
|
|
|
$this->assertEquals($parentSourceRight, $this->law->getRights()->get(0));
|
|
|
|
$this->assertEquals($parentSource, $parentSourceRight->getReciever());
|
|
|
|
$this->assertEquals($this->source, $parentSourceRight->getSource());
|
|
|
|
$this->assertTrue($this->checkClientPermission());
|
|
|
|
$this->law->setRights(new ArrayCollection());
|
|
|
|
$this->assertFalse($this->checkClientPermission());
|
|
|
|
}
|
2018-12-29 23:12:28 +01:00
|
|
|
|
|
|
|
public function testGetRightsByType(): void
|
2018-12-08 20:25:29 +01:00
|
|
|
{
|
2018-12-29 23:12:28 +01:00
|
|
|
$right = $this->getClonedClientRight();
|
|
|
|
$right->setType(RightType::WRITE);
|
|
|
|
$this->law->getRights()->add($right);
|
|
|
|
$this->assertFalse($this->checkClientPermission());
|
|
|
|
$right->setType(RightType::READ);
|
|
|
|
$this->assertTrue($this->checkClientPermission());
|
2018-12-08 20:25:29 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 23:12:28 +01:00
|
|
|
public function testGetRightsByLayer(): void
|
2018-12-08 20:25:29 +01:00
|
|
|
{
|
2018-12-29 23:12:28 +01:00
|
|
|
$right = $this->getClonedClientRight();
|
|
|
|
$right->setLayer(LayerType::LAW);
|
|
|
|
$this->law->getRights()->add($right);
|
|
|
|
$this->assertFalse($this->checkClientPermission());
|
|
|
|
$right->setLayer(LayerType::SOURCE);
|
|
|
|
$this->assertTrue($this->checkClientPermission());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortByPriority(): void
|
|
|
|
{
|
|
|
|
$right1 = $this->getClonedClientRight();
|
2018-12-08 20:25:29 +01:00
|
|
|
$right1->setPriority(123);
|
|
|
|
$right1->setGrant(false);
|
2018-12-14 10:10:28 +01:00
|
|
|
$right1->setReciever($this->clientSource);
|
2018-12-29 23:12:28 +01:00
|
|
|
$right2 = $this->getClonedClientRight();
|
2018-12-08 20:25:29 +01:00
|
|
|
$right2->setPriority(456);
|
|
|
|
$right2->setGrant(true);
|
2018-12-14 10:10:28 +01:00
|
|
|
$right2->setReciever($this->clientSource);
|
2018-12-29 23:12:28 +01:00
|
|
|
$this->law->setRights(new ArrayCollection([
|
|
|
|
$right1,
|
|
|
|
$right2,
|
|
|
|
]));
|
2018-12-08 21:20:07 +01:00
|
|
|
$this->assertFalse($this->checkClientPermission());
|
2018-12-08 20:25:29 +01:00
|
|
|
$right2->setPriority(789);
|
|
|
|
$right1->setPriority(101112);
|
2018-12-08 21:20:07 +01:00
|
|
|
$this->assertTrue($this->checkClientPermission());
|
2018-12-08 20:25:29 +01:00
|
|
|
}
|
2018-12-29 23:12:28 +01:00
|
|
|
|
|
|
|
public function testMemberFilter(): void
|
|
|
|
{
|
|
|
|
$right1 = $this->getClonedClientRight();
|
|
|
|
$right1->setPriority(123);
|
|
|
|
$right1->setGrant(false);
|
|
|
|
$right1->setReciever($this->getSourceMock());
|
|
|
|
$right1->getReciever()->setSlug('Rigth1 Reciever');
|
|
|
|
$right2 = $this->getClonedClientRight();
|
|
|
|
$right2->setPriority(456);
|
|
|
|
$right2->setGrant(true);
|
|
|
|
$right2->setReciever($this->clientSource);
|
|
|
|
$this->law->setRights(new ArrayCollection([
|
|
|
|
$right1,
|
|
|
|
$right2,
|
|
|
|
]));
|
|
|
|
$this->assertTrue($this->checkClientPermission());
|
|
|
|
}
|
2018-12-08 20:25:29 +01:00
|
|
|
}
|