Ported ACL model tests
This commit is contained in:
parent
6f92e55d20
commit
80fcec69f5
|
@ -10,8 +10,6 @@
|
|||
|
||||
namespace Propel\PropelBundle\Tests\Model\Acl;
|
||||
|
||||
use Criteria;
|
||||
|
||||
use Propel\PropelBundle\Model\Acl\AclClass;
|
||||
use Propel\PropelBundle\Model\Acl\AclClassQuery;
|
||||
|
||||
|
|
135
Tests/Model/EntryQueryTest.php
Normal file
135
Tests/Model/EntryQueryTest.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?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\Entry;
|
||||
use Propel\PropelBundle\Model\Acl\EntryQuery;
|
||||
use Propel\PropelBundle\Model\Acl\SecurityIdentity;
|
||||
|
||||
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
|
||||
|
||||
use Propel\PropelBundle\Tests\AclTestCase;
|
||||
|
||||
/**
|
||||
* @author Toni Uebernickel <tuebernickel@gmail.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
88
Tests/Model/EntryTest.php
Normal file
88
Tests/Model/EntryTest.php
Normal file
|
@ -0,0 +1,88 @@
|
|||
<?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\Entry as ModelEntry;
|
||||
use Propel\PropelBundle\Model\Acl\SecurityIdentity;
|
||||
|
||||
use Propel\PropelBundle\Security\Acl\Domain\Entry as AclEntry;
|
||||
|
||||
use Propel\PropelBundle\Tests\AclTestCase;
|
||||
|
||||
/**
|
||||
* @author Toni Uebernickel <tuebernickel@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
134
Tests/Model/ObjectIdentityQueryTest.php
Normal file
134
Tests/Model/ObjectIdentityQueryTest.php
Normal file
|
@ -0,0 +1,134 @@
|
|||
<?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\ObjectIdentityQuery;
|
||||
|
||||
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
|
||||
|
||||
use Propel\PropelBundle\Tests\AclTestCase;
|
||||
|
||||
/**
|
||||
* @author Toni Uebernickel <tuebernickel@gmail.com>
|
||||
*/
|
||||
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),
|
||||
);
|
||||
}
|
||||
}
|
302
Tests/Model/ObjectIdentityTest.php
Normal file
302
Tests/Model/ObjectIdentityTest.php
Normal file
|
@ -0,0 +1,302 @@
|
|||
<?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\Runtime\ActiveQuery\Criteria;
|
||||
|
||||
use Propel\PropelBundle\Model\Acl\ObjectIdentity;
|
||||
use Propel\PropelBundle\Model\Acl\ObjectIdentityQuery;
|
||||
use Propel\PropelBundle\Model\Acl\ObjectIdentityAncestorQuery;
|
||||
|
||||
use Propel\PropelBundle\Tests\AclTestCase;
|
||||
|
||||
/**
|
||||
* @author Toni Uebernickel <tuebernickel@gmail.com>
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
122
Tests/Model/SecurityIdentityTest.php
Normal file
122
Tests/Model/SecurityIdentityTest.php
Normal file
|
@ -0,0 +1,122 @@
|
|||
<?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 Propel\PropelBundle\Tests\AclTestCase;
|
||||
|
||||
/**
|
||||
* @author Toni Uebernickel <tuebernickel@gmail.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue