add tests for ACL related models

This commit is contained in:
Toni Uebernickel 2012-02-01 19:17:59 +01:00
parent 15b387a950
commit 78424c5519
7 changed files with 702 additions and 20 deletions

View file

@ -44,11 +44,22 @@ class ObjectIdentity extends BaseObjectIdentity
public function preDelete(PropelPDO $con = null)
{
$children = ObjectIdentityQuery::create()->findGrandChildren($this, $con);
$objIds = array($this->getId());
// Only retrieve direct children, it's faster and grand children will be retrieved recursively.
$children = ObjectIdentityQuery::create()->findChildren($this, $con);
foreach ($children as $eachChild) {
$objIds[] = $eachChild->getId();
$eachChild->delete($con);
}
// Manually delete those for DBAdapter not capable of cascading the DELETE.
ObjectIdentityAncestorQuery::create()
->filterByObjectIdentityId($objIds, Criteria::IN)
->delete($con)
;
return true;
}
@ -61,28 +72,32 @@ class ObjectIdentity extends BaseObjectIdentity
*/
protected function updateAncestorsTree(PropelPDO $con = null)
{
$con->beginTransaction();
$oldAncestors = ObjectIdentityQuery::create()->findAncestors($this, $con);
$children = ObjectIdentityQuery::create()->findGrandChildren($this, $con);
$children->append($this);
foreach ($children as $eachChild) {
/*
* Delete only those entries, that are ancestors based on the parent relation.
* Ancestors of grand children up to the current node will be kept.
*/
$query = ObjectIdentityAncestorQuery::create()->filterByObjectIdentityId($eachChild->getId());
if (count($oldAncestors)) {
$query->filterByObjectIdentityRelatedByAncestorId($oldAncestors, Criteria::IN);
if (count($oldAncestors)) {
foreach ($children as $eachChild) {
/*
* Delete only those entries, that are ancestors based on the parent relation.
* Ancestors of grand children up to the current node will be kept.
*/
$query = ObjectIdentityAncestorQuery::create()
->filterByObjectIdentityId($eachChild->getId())
->filterByObjectIdentityRelatedByAncestorId($oldAncestors, Criteria::IN)
;
if ($eachChild->getId() !== $this->getId()) {
$query->filterByAncestorId(array($eachChild->getId(), $this->getId()), Criteria::NOT_IN);
} else {
$query->filterByAncestorId($this->getId(), Criteria::NOT_EQUAL);
}
$query->delete($con);
}
if ($eachChild->getId() !== $this->getId()) {
$query->filterByAncestorId(array($eachChild->getId(), $this->getId()), Criteria::NOT_IN);
} else {
$query->filterByAncestorId($this->getId(), Criteria::NOT_EQUAL);
}
$query->delete($con);
}
// This is the new parent object identity!
@ -104,6 +119,9 @@ class ObjectIdentity extends BaseObjectIdentity
continue;
}
// Save the new ancestor to avoid integrity constraint violation.
$ancestor->save($con);
if ($eachChild->getId() === $this->getId()) {
// Do not save() here, as it would result in an infinite recursion loop!
$this->addObjectIdentityAncestorRelatedByObjectIdentityId($ancestor);
@ -117,6 +135,8 @@ class ObjectIdentity extends BaseObjectIdentity
}
}
$con->commit();
return $this;
}
}

View file

@ -30,14 +30,20 @@ class SecurityIdentity extends BaseSecurityIdentity
*/
public static function toAclIdentity(SecurityIdentity $securityIdentity)
{
$identifier = $securityIdentity->getIdentifier();
if ($securityIdentity->getUsername()) {
list($class, $username) = explode('-', $securityIdentity->getIdentifier());
if (false === strpos($identifier, '-')) {
throw new InvalidArgumentException('The given identifier does not resolve to a UserSecurityIdentity.');
}
list($class, $username) = explode('-', $identifier);
return new UserSecurityIdentity($username, $class);
}
if (0 === strpos($securityIdentity->getIdentifier(), 'ROLE_') or 0 === strpos($securityIdentity->getIdentifier(), 'IS_AUTHENTICATED_')) {
return new RoleSecurityIdentity($securityIdentity->getIdentifier());
if (0 === strpos($identifier, 'ROLE_') or 0 === strpos($identifier, 'IS_AUTHENTICATED_')) {
return new RoleSecurityIdentity($identifier);
}
throw new InvalidArgumentException('The security identity does not resolve to either UserSecurityIdentity or RoleSecurityIdentity.');

View file

@ -0,0 +1,39 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Tests\Model\Acl;
use Criteria;
use Propel\PropelBundle\Model\Acl\AclClass;
use Propel\PropelBundle\Model\Acl\AclClassPeer;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
/**
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class AclClassTest extends TestCase
{
public function testFromAclObjectIdentity()
{
$type = 'Merchant';
$aclClass = AclClass::fromAclObjectIdentity(new ObjectIdentity(5, $type), $this->con);
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\AclClass', $aclClass);
$this->assertEquals($type, $aclClass->getType());
$dbEntry = AclClassPeer::doSelectOne(new Criteria(), $this->con);
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\AclClass', $dbEntry);
$this->assertEquals($type, $dbEntry->getType());
$this->assertEquals($dbEntry->getId(), $aclClass->getId());
}
}

View file

@ -0,0 +1,157 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Tests\Model\Acl;
use Propel\PropelBundle\Model\Acl\AclClass;
use Propel\PropelBundle\Model\Acl\ObjectIdentity as ModelObjectIdentity;
use Propel\PropelBundle\Model\Acl\ObjectIdentityQuery;
use Propel\PropelBundle\Tests\Fixtures\Model\Book;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
/**
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class ObjectIdentityQueryTest extends TestCase
{
public function testFilterByAclObjectIdentity()
{
$aclObj = new ObjectIdentity(1, 'Propel\PropelBundle\Tests\Fixtures\Model\Book');
$aclClass = AclClass::fromAclObjectIdentity($aclObj, $this->con);
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\AclClass', $aclClass);
// None given.
$result = ObjectIdentityQuery::create()->filterByAclObjectIdentity($aclObj, $this->con)->find($this->con);
$this->assertEquals(0, count($result));
$objIdentity = new ModelObjectIdentity();
$this->assertTrue((bool) $objIdentity
->setAclClass($aclClass)
->setIdentifier(1)
->save($this->con)
);
$result = ObjectIdentityQuery::create()->filterByAclObjectIdentity($aclObj, $this->con)->find($this->con);
$this->assertEquals(1, count($result));
$this->assertEquals($aclClass->getId(), $result->getFirst()->getClassId());
$this->assertEquals(1, $result->getFirst()->getIdentifier());
// Change the entity.
$aclObj = new ObjectIdentity(2, 'Propel\PropelBundle\Tests\Fixtures\Model\Book');
$result = ObjectIdentityQuery::create()->filterByAclObjectIdentity($aclObj, $this->con)->find($this->con);
$this->assertEquals(0, count($result));
}
/**
* @depends testFilterByAclObjectIdentity
*/
public function testFindOneByAclObjectIdentity()
{
$aclObj = new ObjectIdentity(1, 'Propel\PropelBundle\Tests\Fixtures\Model\Book');
$aclClass = AclClass::fromAclObjectIdentity($aclObj, $this->con);
$result = ObjectIdentityQuery::create()->findOneByAclObjectIdentity($aclObj, $this->con);
$this->assertEmpty($result);
$objIdentity = new ModelObjectIdentity();
$this->assertTrue((bool) $objIdentity
->setAclClass($aclClass)
->setIdentifier(1)
->save($this->con)
);
$result = ObjectIdentityQuery::create()->findOneByAclObjectIdentity($aclObj, $this->con);
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\ObjectIdentity', $result);
$this->assertSame($objIdentity, $result);
}
protected function createObjectIdentities()
{
$aclObj = new ObjectIdentity(1, 'Propel\PropelBundle\Tests\Fixtures\Model\Book');
$aclClass = AclClass::fromAclObjectIdentity($aclObj, $this->con);
$objIdentity = new ModelObjectIdentity();
$this->assertTrue((bool) $objIdentity
->setAclClass($aclClass)
->setIdentifier(1)
->save($this->con)
);
$childObjIdentity = new ModelObjectIdentity();
$this->assertTrue((bool) $childObjIdentity
->setAclClass($aclClass)
->setIdentifier(2)
->save($this->con)
);
$grandChildObjIdentity = new ModelObjectIdentity();
$this->assertTrue((bool) $grandChildObjIdentity
->setAclClass($aclClass)
->setIdentifier(3)
->save($this->con)
);
return array($objIdentity, $childObjIdentity, $grandChildObjIdentity);
}
public function testFindChildren()
{
list($objIdentity, $childObjIdentity) = $this->createObjectIdentities();
// Parent not set, yet.
$result = ObjectIdentityQuery::create()->findChildren($objIdentity, $this->con);
$this->assertEquals(0, count($result));
$childObjIdentity->setObjectIdentityRelatedByParentObjectIdentityId($objIdentity)->save($this->con);
$result = ObjectIdentityQuery::create()->findChildren($objIdentity, $this->con);
$this->assertEquals(1, count($result));
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\ObjectIdentity', $result->getFirst());
$this->assertSame($childObjIdentity, $result->getFirst());
$this->assertSame($objIdentity, $result->getFirst()->getObjectIdentityRelatedByParentObjectIdentityId());
}
public function testFindGrandChildren()
{
list($objIdentity, $childObjIdentity, $grandChildObjIdentity) = $this->createObjectIdentities();
// Parents not set, yet.
$result = ObjectIdentityQuery::create()->findGrandChildren($objIdentity, $this->con);
$this->assertEquals(0, count($result));
$childObjIdentity->setObjectIdentityRelatedByParentObjectIdentityId($objIdentity)->save($this->con);
$result = ObjectIdentityQuery::create()->findGrandChildren($objIdentity, $this->con);
$this->assertEquals(1, count($result));
$grandChildObjIdentity->setObjectIdentityRelatedByParentObjectIdentityId($childObjIdentity)->save($this->con);
$result = ObjectIdentityQuery::create()->findGrandChildren($objIdentity, $this->con);
$this->assertEquals(2, count($result));
}
public function testFindAncestors()
{
list($objIdentity, $childObjIdentity) = $this->createObjectIdentities();
// Parents not set, yet.
$result = ObjectIdentityQuery::create()->findAncestors($childObjIdentity, $this->con);
$this->assertEquals(0, count($result));
$childObjIdentity->setObjectIdentityRelatedByParentObjectIdentityId($objIdentity)->save($this->con);
$result = ObjectIdentityQuery::create()->findAncestors($childObjIdentity, $this->con);
$this->assertEquals(1, count($result));
}
}

View file

@ -0,0 +1,308 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Tests\Model\Acl;
use Criteria;
use Propel\PropelBundle\Model\Acl\AclClass;
use Propel\PropelBundle\Model\Acl\ObjectIdentity as ModelObjectIdentity;
use Propel\PropelBundle\Model\Acl\ObjectIdentityQuery;
use Propel\PropelBundle\Model\Acl\ObjectIdentityAncestorQuery;
use Propel\PropelBundle\Tests\Fixtures\Model\Book;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
/**
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class ObjectIdentityTest extends TestCase
{
public function testCompatibleDefaultImplementation()
{
$objIdenity = $this->createModelObjectIdentity(1);
$ancestorEntries = ObjectIdentityAncestorQuery::create()->find($this->con);
$this->assertEquals(1, count($ancestorEntries));
$this->assertEquals($objIdenity->getId(), $ancestorEntries->getFirst()->getAncestorId());
$this->assertEquals($objIdenity->getId(), $ancestorEntries->getFirst()->getObjectIdentityId());
$anotherIdenity = $this->createModelObjectIdentity(2);
$ancestorEntries = ObjectIdentityAncestorQuery::create()->orderByAncestorId(Criteria::ASC)->find($this->con);
$this->assertEquals(2, count($ancestorEntries));
$this->assertEquals($objIdenity->getId(), $ancestorEntries[0]->getAncestorId());
$this->assertEquals($objIdenity->getId(), $ancestorEntries[0]->getObjectIdentityId());
$this->assertEquals($anotherIdenity->getId(), $ancestorEntries[1]->getAncestorId());
$this->assertEquals($anotherIdenity->getId(), $ancestorEntries[1]->getObjectIdentityId());
}
public function testTreeSimpleParent()
{
$parent = $this->createModelObjectIdentity(1);
$obj = $this->createModelObjectIdentity(2);
$this->assertTrue((bool) $obj->setObjectIdentityRelatedByParentObjectIdentityId($parent)->save($this->con));
$entries = ObjectIdentityAncestorQuery::create()
->filterByObjectIdentityId($obj->getId())
->orderByAncestorId(Criteria::ASC)
->find($this->con)
;
$this->assertEquals(2, count($entries));
$this->assertEquals($obj->getId(), $entries[0]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[0]->getAncestorId());
$this->assertEquals($obj->getId(), $entries[1]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[1]->getAncestorId());
$this->assertTrue((bool) $obj->setObjectIdentityRelatedByParentObjectIdentityId(null)->save($this->con));
$entries = ObjectIdentityAncestorQuery::create()
->filterByObjectIdentityId($obj->getId())
->orderByAncestorId(Criteria::ASC)
->find($this->con)
;
$this->assertEquals(1, count($entries));
$this->assertEquals($obj->getId(), $entries[0]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[0]->getAncestorId());
}
/**
* @depends testTreeSimpleParent
*/
public function testTreeAddParentChildHavingChild()
{
$parent = $this->createModelObjectIdentity(1);
$obj = $this->createModelObjectIdentity(2);
$child = $this->createModelObjectIdentity(3);
$child->setObjectIdentityRelatedByParentObjectIdentityId($obj)->save($this->con);
$obj->setObjectIdentityRelatedByParentObjectIdentityId($parent)->save($this->con);
$entries = ObjectIdentityAncestorQuery::create()
->orderByObjectIdentityId(Criteria::ASC)
->orderByAncestorId(Criteria::ASC)
->find($this->con)
;
$this->assertEquals(6, count($entries));
$this->assertEquals($parent->getId(), $entries[0]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[0]->getAncestorId());
$this->assertEquals($obj->getId(), $entries[1]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[1]->getAncestorId());
$this->assertEquals($obj->getId(), $entries[2]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[2]->getAncestorId());
$this->assertEquals($child->getId(), $entries[3]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[3]->getAncestorId());
$this->assertEquals($child->getId(), $entries[4]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[4]->getAncestorId());
$this->assertEquals($child->getId(), $entries[5]->getObjectIdentityId());
$this->assertEquals($child->getId(), $entries[5]->getAncestorId());
}
/**
* Tree splitted:
* 1-2
* 3-4-5
*
* Tree merged:
* 1-2-3-4-5
*
* @depends testTreeAddParentChildHavingChild
*/
public function testTreeAddParentChildHavingGrandchildrenAndParentHavingParent()
{
// Part I, before.
$grandParent = $this->createModelObjectIdentity(1);
$parent = $this->createModelObjectIdentity(2);
$parent->setObjectIdentityRelatedByParentObjectIdentityId($grandParent)->save($this->con);
// Part II, before.
$obj = $this->createModelObjectIdentity(3);
$child = $this->createModelObjectIdentity(4);
$grandChild = $this->createModelObjectIdentity(5);
$grandChild->setObjectIdentityRelatedByParentObjectIdentityId($child)->save($this->con);
$child->setObjectIdentityRelatedByParentObjectIdentityId($obj)->save($this->con);
// Verify "before"
$entries = ObjectIdentityAncestorQuery::create()
->orderByObjectIdentityId(Criteria::ASC)
->orderByAncestorId(Criteria::ASC)
->find($this->con)
;
$this->assertEquals(9, count($entries));
$this->assertEquals($grandParent->getId(), $entries[0]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[0]->getAncestorId());
$this->assertEquals($parent->getId(), $entries[1]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[1]->getAncestorId());
$this->assertEquals($parent->getId(), $entries[2]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[2]->getAncestorId());
$this->assertEquals($obj->getId(), $entries[3]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[3]->getAncestorId());
$this->assertEquals($child->getId(), $entries[4]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[4]->getAncestorId());
$this->assertEquals($child->getId(), $entries[5]->getObjectIdentityId());
$this->assertEquals($child->getId(), $entries[5]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[6]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[6]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[7]->getObjectIdentityId());
$this->assertEquals($child->getId(), $entries[7]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[8]->getObjectIdentityId());
$this->assertEquals($grandChild->getId(), $entries[8]->getAncestorId());
// Merge Trees
$obj->setObjectIdentityRelatedByParentObjectIdentityId($parent)->save($this->con);
$entries = ObjectIdentityAncestorQuery::create()
->orderByObjectIdentityId(Criteria::ASC)
->orderByAncestorId(Criteria::ASC)
->find($this->con)
;
$this->assertEquals(15, count($entries));
$this->assertEquals($grandParent->getId(), $entries[0]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[0]->getAncestorId());
$this->assertEquals($parent->getId(), $entries[1]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[1]->getAncestorId());
$this->assertEquals($parent->getId(), $entries[2]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[2]->getAncestorId());
$this->assertEquals($obj->getId(), $entries[3]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[3]->getAncestorId());
$this->assertEquals($obj->getId(), $entries[4]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[4]->getAncestorId());
$this->assertEquals($obj->getId(), $entries[5]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[5]->getAncestorId());
$this->assertEquals($child->getId(), $entries[6]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[6]->getAncestorId());
$this->assertEquals($child->getId(), $entries[7]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[7]->getAncestorId());
$this->assertEquals($child->getId(), $entries[8]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[8]->getAncestorId());
$this->assertEquals($child->getId(), $entries[9]->getObjectIdentityId());
$this->assertEquals($child->getId(), $entries[9]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[10]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[10]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[11]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[11]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[12]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[12]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[13]->getObjectIdentityId());
$this->assertEquals($child->getId(), $entries[13]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[14]->getObjectIdentityId());
$this->assertEquals($grandChild->getId(), $entries[14]->getAncestorId());
// Split Tree
$obj->setObjectIdentityRelatedByParentObjectIdentityId(null)->save($this->con);
$entries = ObjectIdentityAncestorQuery::create()
->orderByObjectIdentityId(Criteria::ASC)
->orderByAncestorId(Criteria::ASC)
->find($this->con)
;
$this->assertEquals(9, count($entries));
$this->assertEquals($grandParent->getId(), $entries[0]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[0]->getAncestorId());
$this->assertEquals($parent->getId(), $entries[1]->getObjectIdentityId());
$this->assertEquals($grandParent->getId(), $entries[1]->getAncestorId());
$this->assertEquals($parent->getId(), $entries[2]->getObjectIdentityId());
$this->assertEquals($parent->getId(), $entries[2]->getAncestorId());
$this->assertEquals($obj->getId(), $entries[3]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[3]->getAncestorId());
$this->assertEquals($child->getId(), $entries[4]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[4]->getAncestorId());
$this->assertEquals($child->getId(), $entries[5]->getObjectIdentityId());
$this->assertEquals($child->getId(), $entries[5]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[6]->getObjectIdentityId());
$this->assertEquals($obj->getId(), $entries[6]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[7]->getObjectIdentityId());
$this->assertEquals($child->getId(), $entries[7]->getAncestorId());
$this->assertEquals($grandChild->getId(), $entries[8]->getObjectIdentityId());
$this->assertEquals($grandChild->getId(), $entries[8]->getAncestorId());
}
/**
* @depends testTreeAddParentChildHavingChild
*/
public function testDeleteRemovesGrandchildren()
{
$parent = $this->createModelObjectIdentity(1);
$obj = $this->createModelObjectIdentity(2);
$child = $this->createModelObjectIdentity(3);
$child->setObjectIdentityRelatedByParentObjectIdentityId($obj)->save($this->con);
$obj->setObjectIdentityRelatedByParentObjectIdentityId($parent)->save($this->con);
$parent->delete($this->con);
$this->assertEquals(0, count(ObjectIdentityQuery::create()->find($this->con)));
$this->assertEquals(0, count(ObjectIdentityAncestorQuery::create()->find($this->con)));
}
/**
* @return \Propel\PropelBundle\Model\Acl\ObjectIdentity
*/
protected function createModelObjectIdentity($identifier)
{
$aclClass = $this->getAclClass();
$objIdentity = new ModelObjectIdentity();
$this->assertTrue((bool) $objIdentity
->setAclClass($aclClass)
->setIdentifier($identifier)
->save($this->con)
);
return $objIdentity;
}
protected function getAclClass()
{
return AclClass::fromAclObjectIdentity(new ObjectIdentity(1, 'Propel\PropelBundle\Tests\Fixtures\Model\Book'), $this->con);
}
}

View file

@ -0,0 +1,110 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Tests\Model\Acl;
use Propel\PropelBundle\Model\Acl\SecurityIdentity;
use Propel\PropelBundle\Model\Acl\SecurityIdentityQuery;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
/**
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class SecurityIdentityTest extends TestCase
{
public function testToAclIdentityUserWithInvalidIdentifier()
{
$identity = new SecurityIdentity();
$identity->setIdentifier('invalidIdentifier');
$identity->setUsername(true);
$this->setExpectedException('InvalidArgumentException');
SecurityIdentity::toAclIdentity($identity);
}
public function testToAclIdentityUnknownSecurityIdentity()
{
$identity = new SecurityIdentity();
$identity->setIdentifier('invalidIdentifier');
$identity->setUsername(false);
$this->setExpectedException('InvalidArgumentException');
SecurityIdentity::toAclIdentity($identity);
}
public function testToAclIdentityValidUser()
{
$identity = new SecurityIdentity();
$identity->setIdentifier('Propel\PropelBundle\Tests\Fixtures\UserProxy-propel');
$identity->setUsername(true);
$secIdentity = SecurityIdentity::toAclIdentity($identity);
$this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\UserSecurityIdentity', $secIdentity);
}
public function testToAclIdentityValidRole()
{
$identity = new SecurityIdentity();
$identity->setIdentifier('ROLE_ADMIN');
$identity->setUsername(false);
$secIdentity = SecurityIdentity::toAclIdentity($identity);
$this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity', $secIdentity);
$identity = new SecurityIdentity();
$identity->setIdentifier('IS_AUTHENTICATED_ANONYMOUSLY');
$identity->setUsername(false);
$secIdentity = SecurityIdentity::toAclIdentity($identity);
$this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity', $secIdentity);
}
public function testFromAclIdentityWithInvalid()
{
$secIdentity = $this->getMock('Symfony\Component\Security\Acl\Model\SecurityIdentityInterface');
$this->setExpectedException('InvalidArgumentException');
SecurityIdentity::fromAclIdentity($secIdentity, $this->con);
}
public function testFromAclIdentityWithUser()
{
$secIdentity = new UserSecurityIdentity('propel', 'Propel\PropelBundle\Tests\Fixtures\UserProxy');
$identity = SecurityIdentity::fromAclIdentity($secIdentity, $this->con);
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\SecurityIdentity', $identity);
$this->assertEquals(true, $identity->getUsername());
$this->assertEquals('Propel\PropelBundle\Tests\Fixtures\UserProxy-propel', $identity->getIdentifier());
$this->assertGreaterThan(0, $identity->getId());
$dbEntry = SecurityIdentityQuery::create()->findPk($identity->getId());
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\SecurityIdentity', $dbEntry);
}
public function testFromAclIdentityWithRole()
{
$secIdentity = new RoleSecurityIdentity(new Role('ROLE_USER'));
$identity = SecurityIdentity::fromAclIdentity($secIdentity, $this->con);
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\SecurityIdentity', $identity);
$this->assertEquals(false, $identity->getUsername());
$this->assertEquals('ROLE_USER', $identity->getIdentifier());
$this->assertGreaterThan(0, $identity->getId());
$dbEntry = SecurityIdentityQuery::create()->findPk($identity->getId());
$this->assertInstanceOf('Propel\PropelBundle\Model\Acl\SecurityIdentity', $dbEntry);
}
}

View file

@ -0,0 +1,42 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Tests\Model\Acl;
use PropelQuickBuilder;
use Propel\PropelBundle\Tests\TestCase as BaseTestCase;
/**
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class TestCase extends BaseTestCase
{
protected $con = null;
public function setUp()
{
parent::setUp();
$this->loadPropelQuickBuilder();
$schema = file_get_contents(__DIR__.'/../../../Resources/config/acl_schema.xml');
$builder = new PropelQuickBuilder();
$builder->setSchema($schema);
if (!class_exists('Propel\PropelBundle\Model\Acl\map\AclClassTableMap')) {
$builder->setClassTargets(array('tablemap', 'peer', 'object', 'query'));
} else {
$builder->setClassTargets(array());
}
$this->con = $builder->build();
}
}