From 80fcec69f505185fb3d8226a4df15fb138707bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sat, 14 Dec 2013 11:49:10 +0000 Subject: [PATCH] Ported ACL model tests --- Tests/Model/AclClassTest.php | 2 - Tests/Model/EntryQueryTest.php | 135 +++++++++++ Tests/Model/EntryTest.php | 88 +++++++ Tests/Model/ObjectIdentityQueryTest.php | 134 +++++++++++ Tests/Model/ObjectIdentityTest.php | 302 ++++++++++++++++++++++++ Tests/Model/SecurityIdentityTest.php | 122 ++++++++++ 6 files changed, 781 insertions(+), 2 deletions(-) create mode 100644 Tests/Model/EntryQueryTest.php create mode 100644 Tests/Model/EntryTest.php create mode 100644 Tests/Model/ObjectIdentityQueryTest.php create mode 100644 Tests/Model/ObjectIdentityTest.php create mode 100644 Tests/Model/SecurityIdentityTest.php diff --git a/Tests/Model/AclClassTest.php b/Tests/Model/AclClassTest.php index a3e9c86..f6df52e 100644 --- a/Tests/Model/AclClassTest.php +++ b/Tests/Model/AclClassTest.php @@ -10,8 +10,6 @@ namespace Propel\PropelBundle\Tests\Model\Acl; -use Criteria; - use Propel\PropelBundle\Model\Acl\AclClass; use Propel\PropelBundle\Model\Acl\AclClassQuery; diff --git a/Tests/Model/EntryQueryTest.php b/Tests/Model/EntryQueryTest.php new file mode 100644 index 0000000..d875ee6 --- /dev/null +++ b/Tests/Model/EntryQueryTest.php @@ -0,0 +1,135 @@ + + */ +class EntryQueryTest extends AclTestCase +{ + public function setUp() + { + parent::setUp(); + + $obj = $this->createModelObjectIdentity(1); + $entry = $this->createEntry(); + $entry + ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity('ROLE_USER'))) + ->setAclClass($obj->getAclClass()) + ->setMask(64) + ; + $obj->addEntry($entry)->save($this->con); + } + + public function testFindByAclIdentityInvalidSecurityIdentity() + { + $this->setExpectedException('InvalidArgumentException'); + EntryQuery::create()->findByAclIdentity($this->getAclObjectIdentity(), array('foo'), $this->con); + } + + public function testFindByAclIdentityInvalidSecurityIdentityObject() + { + $this->setExpectedException('InvalidArgumentException'); + EntryQuery::create()->findByAclIdentity($this->getAclObjectIdentity(), array(new \stdClass()), $this->con); + } + + public function testFindByAclIdentityNotExists() + { + $this->assertCount(0, EntryQuery::create()->findByAclIdentity($this->getAclObjectIdentity(2), array(), $this->con)); + } + + public function testFindByAclIdentitySecurityIdentityNotFound() + { + $this->assertCount(0, EntryQuery::create()->findByAclIdentity($this->getAclObjectIdentity(1), array($this->getRoleSecurityIdentity('ROLE_ADMIN')), $this->con)); + } + + public function testFindByAclIdentity() + { + // Another Entry, should not be found (different ObjectIdentity). + $obj = $this->createModelObjectIdentity(2); + $entry = $this->createEntry(); + $entry + ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity())) + ->setAclClass($obj->getAclClass()) + ->setMask(64) + ; + $obj->addEntry($entry)->save($this->con); + + $entries = EntryQuery::create()->findByAclIdentity($this->getAclObjectIdentity(1), array(), $this->con); + $this->assertCount(1, $entries); + $this->assertEquals(1, $entries[0]->getObjectIdentityId()); + + // A class based entry for the wrong ObjectIdentity. + $classEntry = $this->createEntry(); + $classEntry + ->setObjectIdentityId(2) + ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity())) + ->setAclClass($obj->getAclClass()) + ->setMask(64) + ->save($this->con) + ; + + // A class based entry for the correct ObjectIdentity. + $classEntry = $this->createEntry(); + $classEntry + ->setObjectIdentityId(null) + ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity())) + ->setAclClass($this->getAclClass()) + ->setMask(64) + ->save($this->con) + ; + + $this->assertEquals(4, EntryQuery::create()->count($this->con)); + + $entries = EntryQuery::create()->findByAclIdentity($this->getAclObjectIdentity(1), array(), $this->con); + $this->assertCount(2, $entries); + $this->assertEquals($obj->getClassId(), $entries[0]->getClassId()); + $this->assertEquals($obj->getClassId(), $entries[1]->getClassId()); + } + + public function testFindByAclIdentityFilterSecurityIdentity() + { + // Another Entry, should not be found (different SecurityIdentity). + $entry = $this->createEntry(); + $entry + ->setObjectIdentityId(1) + ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity('ROLE_ADMIN'))) + ->setAclClass($this->getAclClass()) + ->setMask(64) + ->save($this->con) + ; + + $this->assertEquals(2, EntryQuery::create()->count($this->con)); + + $entries = EntryQuery::create()->findByAclIdentity($this->getAclObjectIdentity(1), array($this->getRoleSecurityIdentity('ROLE_USER')), $this->con); + $this->assertCount(1, $entries); + $this->assertEquals(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity('ROLE_USER'))->getId(), $entries[0]->getSecurityIdentityId()); + } + + public function testFindByAclIdentityOnlyClassEntries() + { + $this->assertEquals(1, EntryQuery::create()->count($this->con)); + EntryQuery::create()->findOne($this->con) + ->setObjectIdentity(null) + ->save($this->con); + + $entries = EntryQuery::create()->findByAclIdentity($this->getAclObjectIdentity(1), array(), $this->con); + $this->assertCount(1, $entries); + } +} diff --git a/Tests/Model/EntryTest.php b/Tests/Model/EntryTest.php new file mode 100644 index 0000000..a2271f8 --- /dev/null +++ b/Tests/Model/EntryTest.php @@ -0,0 +1,88 @@ + + */ +class EntryTest extends AclTestCase +{ + public function testToAclEntry() + { + $acl = $this->getMock('Propel\PropelBundle\Security\Acl\Domain\AuditableAcl', array(), array(), '', false, false); + $entry = $this->createModelEntry(); + + $aclEntry = ModelEntry::toAclEntry($entry, $acl); + $this->assertInstanceOf('Propel\PropelBundle\Security\Acl\Domain\Entry', $aclEntry); + $this->assertSame($acl, $aclEntry->getAcl()); + $this->assertEquals(42, $aclEntry->getId()); + $this->assertTrue($aclEntry->isAuditFailure()); + $this->assertFalse($aclEntry->isAuditSuccess()); + $this->assertEquals('all', $aclEntry->getStrategy()); + $this->assertTrue($aclEntry->isGranting()); + $this->assertEquals(64, $aclEntry->getMask()); + + return $aclEntry; + } + + /** + * @depends testToAclEntry + */ + public function testToAclEntryFieldEntry() + { + $acl = $this->getMock('Propel\PropelBundle\Security\Acl\Domain\AuditableAcl', array(), array(), '', false, false); + $entry = $this->createModelEntry(); + $entry->setFieldName('name'); + + $aclEntry = ModelEntry::toAclEntry($entry, $acl); + $this->assertInstanceOf('Propel\PropelBundle\Security\Acl\Domain\FieldEntry', $aclEntry); + } + + /** + * @depends testToAclEntry + */ + public function testFromAclEntry($aclEntry) + { + $modelEntry = ModelEntry::fromAclEntry($aclEntry); + + $this->assertInstanceOf('Propel\PropelBundle\Model\Acl\Entry', $modelEntry); + $this->assertEquals(42, $modelEntry->getId()); + $this->assertTrue($modelEntry->getAuditFailure()); + $this->assertFalse($modelEntry->getAuditSuccess()); + $this->assertEquals('all', $modelEntry->getGrantingStrategy()); + $this->assertTrue($modelEntry->getGranting()); + $this->assertEquals(64, $modelEntry->getMask()); + } + + protected function createModelEntry() + { + $entry = new ModelEntry(); + $entry + ->setId(42) + ->setAclClass($this->getAclClass()) + ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity())) + ->setAuditFailure(true) + ->setAuditSuccess(false) + ->setGrantingStrategy('all') + ->setGranting(true) + ->setMask(64) + ; + + return $entry; + } +} diff --git a/Tests/Model/ObjectIdentityQueryTest.php b/Tests/Model/ObjectIdentityQueryTest.php new file mode 100644 index 0000000..d6a7a73 --- /dev/null +++ b/Tests/Model/ObjectIdentityQueryTest.php @@ -0,0 +1,134 @@ + + */ +class ObjectIdentityQueryTest extends AclTestCase +{ + 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->assertCount(0, $result); + + $this->createModelObjectIdentity(1); + + $result = ObjectIdentityQuery::create()->filterByAclObjectIdentity($aclObj, $this->con)->find($this->con); + $this->assertCount(1, $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->assertCount(0, $result); + } + + /** + * @depends testFilterByAclObjectIdentity + */ + public function testFindOneByAclObjectIdentity() + { + $aclObj = new ObjectIdentity(1, 'Propel\PropelBundle\Tests\Fixtures\Model\Book'); + + $result = ObjectIdentityQuery::create()->findOneByAclObjectIdentity($aclObj, $this->con); + $this->assertEmpty($result); + + $objIdentity = $this->createModelObjectIdentity(1); + + $result = ObjectIdentityQuery::create()->findOneByAclObjectIdentity($aclObj, $this->con); + $this->assertInstanceOf('Propel\PropelBundle\Model\Acl\ObjectIdentity', $result); + $this->assertSame($objIdentity, $result); + } + + /** + * @depends testFindOneByAclObjectIdentity + */ + public function testFindChildren() + { + list($objIdentity, $childObjIdentity) = $this->createObjectIdentities(); + + // Parent not set, yet. + $result = ObjectIdentityQuery::create()->findChildren($objIdentity, $this->con); + $this->assertCount(0, $result); + + $childObjIdentity->setObjectIdentityRelatedByParentObjectIdentityId($objIdentity)->save($this->con); + + $result = ObjectIdentityQuery::create()->findChildren($objIdentity, $this->con); + $this->assertCount(1, $result); + $this->assertInstanceOf('Propel\PropelBundle\Model\Acl\ObjectIdentity', $result->getFirst()); + $this->assertSame($childObjIdentity, $result->getFirst()); + $this->assertSame($objIdentity, $result->getFirst()->getObjectIdentityRelatedByParentObjectIdentityId()); + } + + /** + * @depends testFindOneByAclObjectIdentity + */ + public function testFindGrandChildren() + { + list($objIdentity, $childObjIdentity, $grandChildObjIdentity) = $this->createObjectIdentities(); + + // Parents not set, yet. + $result = ObjectIdentityQuery::create()->findGrandChildren($objIdentity, $this->con); + $this->assertCount(0, $result); + + $childObjIdentity->setObjectIdentityRelatedByParentObjectIdentityId($objIdentity)->save($this->con); + + $result = ObjectIdentityQuery::create()->findGrandChildren($objIdentity, $this->con); + $this->assertCount(1, $result); + + $grandChildObjIdentity->setObjectIdentityRelatedByParentObjectIdentityId($childObjIdentity)->save($this->con); + + $result = ObjectIdentityQuery::create()->findGrandChildren($objIdentity, $this->con); + $this->assertCount(2, $result); + } + + /** + * @depends testFindOneByAclObjectIdentity + */ + public function testFindAncestors() + { + list($objIdentity, $childObjIdentity) = $this->createObjectIdentities(); + + // Parents not set, yet. + $result = ObjectIdentityQuery::create()->findAncestors($childObjIdentity, $this->con); + $this->assertCount(0, $result); + + $childObjIdentity->setObjectIdentityRelatedByParentObjectIdentityId($objIdentity)->save($this->con); + + $result = ObjectIdentityQuery::create()->findAncestors($childObjIdentity, $this->con); + $this->assertCount(1, $result); + } + + protected function createObjectIdentities() + { + return array( + $this->createModelObjectIdentity(1), + $this->createModelObjectIdentity(2), + $this->createModelObjectIdentity(3), + ); + } +} diff --git a/Tests/Model/ObjectIdentityTest.php b/Tests/Model/ObjectIdentityTest.php new file mode 100644 index 0000000..3f8379c --- /dev/null +++ b/Tests/Model/ObjectIdentityTest.php @@ -0,0 +1,302 @@ + + */ +class ObjectIdentityTest extends AclTestCase +{ + public function testCompatibleDefaultImplementation() + { + $objIdenity = $this->createModelObjectIdentity(1); + + $ancestorEntries = ObjectIdentityAncestorQuery::create()->find($this->con); + $this->assertCount(1, $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->assertCount(2, $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->assertCount(2, $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->assertCount(1, $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->assertCount(6, $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->assertCount(9, $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->assertCount(15, $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->assertCount(9, $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, ObjectIdentityQuery::create()->count($this->con)); + $this->assertEquals(0, ObjectIdentityAncestorQuery::create()->count($this->con)); + } + + public function testInsertWithAssignedParent() + { + $parent = $this->createModelObjectIdentity(1); + + $obj = new ObjectIdentity(); + $obj + ->setAclClass($this->getAclClass()) + ->setIdentifier(2) + ->setObjectIdentityRelatedByParentObjectIdentityId($parent) + ->save($this->con) + ; + + $entries = ObjectIdentityQuery::create()->orderByParentObjectIdentityId(Criteria::ASC)->find($this->con); + + $this->assertCount(2, $entries); + $this->assertNull($entries[0]->getParentObjectIdentityId()); + $this->assertEquals($entries[0]->getId(), $entries[1]->getParentObjectIdentityId()); + } +} diff --git a/Tests/Model/SecurityIdentityTest.php b/Tests/Model/SecurityIdentityTest.php new file mode 100644 index 0000000..3f27ba0 --- /dev/null +++ b/Tests/Model/SecurityIdentityTest.php @@ -0,0 +1,122 @@ + + */ +class SecurityIdentityTest extends AclTestCase +{ + 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 testToAclIdentityMultipleDashes() + { + $identity = new SecurityIdentity(); + $identity->setIdentifier('Propel\PropelBundle\Tests\Fixtures\UserProxy-some-username@domain.com'); + $identity->setUsername(true); + + $secIdentity = SecurityIdentity::toAclIdentity($identity); + $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\UserSecurityIdentity', $secIdentity); + $this->assertEquals('some-username@domain.com', $secIdentity->getUsername()); + } + + 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); + } +}