Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Tests\MyCLabs\ACL\Unit\Repository; | ||
|
||
use Doctrine\Common\Cache\ArrayCache; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Tools\SchemaTool; | ||
use Doctrine\ORM\Tools\Setup; | ||
use MyCLabs\ACL\ACLManager; | ||
use MyCLabs\ACL\Doctrine\ACLSetup; | ||
use MyCLabs\ACL\Model\Actions; | ||
use MyCLabs\ACL\Model\Authorization; | ||
use MyCLabs\ACL\Repository\AuthorizationRepository; | ||
use Tests\MyCLabs\ACL\Unit\Repository\Model\File; | ||
use Tests\MyCLabs\ACL\Unit\Repository\Model\FileOwnerRole; | ||
use Tests\MyCLabs\ACL\Unit\Repository\Model\User; | ||
|
||
/** | ||
* @covers \MyCLabs\ACL\Repository\AuthorizationRepository | ||
*/ | ||
class AuthorizationRepositoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var EntityManager | ||
*/ | ||
private $em; | ||
|
||
/** | ||
* @var ACLManager | ||
*/ | ||
private $aclManager; | ||
|
||
public function setUp() | ||
{ | ||
$paths = [ | ||
__DIR__ . '/../../../src/Model', | ||
__DIR__ . '/Model', | ||
]; | ||
$dbParams = [ | ||
'driver' => 'pdo_sqlite', | ||
'memory' => true, | ||
]; | ||
|
||
$setup = new ACLSetup(); | ||
$setup->setSecurityIdentityClass('Tests\MyCLabs\ACL\Unit\Repository\Model\User'); | ||
$setup->registerRoleClass('Tests\MyCLabs\ACL\Unit\Repository\Model\FileOwnerRole', 'fileOwner'); | ||
|
||
// Create the entity manager | ||
$config = Setup::createAnnotationMetadataConfiguration($paths, true, null, new ArrayCache(), false); | ||
$this->em = EntityManager::create($dbParams, $config); | ||
|
||
$this->aclManager = new ACLManager($this->em); | ||
|
||
$setup->setUpEntityManager($this->em, function () { | ||
return $this->aclManager; | ||
}); | ||
|
||
// Create the DB | ||
$tool = new SchemaTool($this->em); | ||
$tool->createSchema($this->em->getMetadataFactory()->getAllMetadata()); | ||
} | ||
|
||
public function testInsertBulk() | ||
{ | ||
$user = new User(); | ||
$this->em->persist($user); | ||
$resource = new File(); | ||
$this->em->persist($resource); | ||
$role = new FileOwnerRole($user, $resource); | ||
$this->em->persist($role); | ||
|
||
$this->em->flush(); | ||
|
||
$authorizations = [ | ||
Authorization::create($role, Actions::all(), $resource), | ||
]; | ||
|
||
/** @var AuthorizationRepository $repository */ | ||
$repository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization'); | ||
|
||
$repository->insertBulk($authorizations); | ||
|
||
// Check that the authorization was inserted and can be retrieved | ||
$inserted = $repository->findAll(); | ||
|
||
$this->assertCount(1, $inserted); | ||
|
||
/** @var Authorization $authorization */ | ||
$authorization = $inserted[0]; | ||
$this->assertSame($role, $authorization->getRole()); | ||
$this->assertSame($user, $authorization->getSecurityIdentity()); | ||
$this->assertEquals($resource->getId(), $authorization->getEntityId()); | ||
$this->assertEquals('Tests\MyCLabs\ACL\Unit\Repository\Model\File', $authorization->getEntityClass()); | ||
$this->assertEquals(Actions::all(), $authorization->getActions()); | ||
$this->assertNull($authorization->getParentAuthorization()); | ||
$this->assertEquals(0, count($authorization->getChildAuthorizations())); | ||
$this->assertTrue($authorization->isCascadable()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Tests\MyCLabs\ACL\Unit\Repository\Model; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use MyCLabs\ACL\Model\EntityResource; | ||
|
||
/** | ||
* @ORM\Entity | ||
*/ | ||
class File implements EntityResource | ||
{ | ||
/** | ||
* @ORM\Id @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
protected $id; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Tests\MyCLabs\ACL\Unit\Repository\Model; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use MyCLabs\ACL\ACLManager; | ||
use MyCLabs\ACL\Model\Actions; | ||
use MyCLabs\ACL\Model\Role; | ||
|
||
/** | ||
* @ORM\Entity(readOnly=true) | ||
*/ | ||
class FileOwnerRole extends Role | ||
{ | ||
/** | ||
* @var File | ||
* @ORM\ManyToOne(targetEntity="File", inversedBy="roles") | ||
*/ | ||
protected $file; | ||
|
||
public function __construct(User $identity, File $file) | ||
{ | ||
$this->file = $file; | ||
|
||
parent::__construct($identity); | ||
} | ||
|
||
public function createAuthorizations(ACLManager $aclManager) | ||
{ | ||
$aclManager->allow($this, new Actions([Actions::VIEW, Actions::EDIT]), $this->file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Tests\MyCLabs\ACL\Unit\Repository\Model; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use MyCLabs\ACL\Model\SecurityIdentityInterface; | ||
use MyCLabs\ACL\Model\SecurityIdentityTrait; | ||
|
||
/** | ||
* @ORM\Entity | ||
*/ | ||
class User implements SecurityIdentityInterface | ||
{ | ||
use SecurityIdentityTrait; | ||
|
||
/** | ||
* @ORM\Id @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @var FileOwnerRole[]|Collection | ||
* @ORM\OneToMany(targetEntity="FileOwnerRole", mappedBy="securityIdentity", | ||
* cascade={"persist", "remove"}, orphanRemoval=true) | ||
*/ | ||
protected $roles; | ||
|
||
public function __construct() | ||
{ | ||
$this->roles = new ArrayCollection(); | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
} |