propel-bundle/Tests/Model/SecurityIdentityTest.php

123 lines
4.7 KiB
PHP
Raw Normal View History

2013-12-14 12:49:10 +01:00
<?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
*/
2016-02-11 19:14:03 +01:00
namespace Propel\Bundle\PropelBundle\Tests\Model\Acl;
2013-12-14 12:49:10 +01:00
2016-02-11 19:14:03 +01:00
use Propel\Bundle\PropelBundle\Model\Acl\SecurityIdentity;
use Propel\Bundle\PropelBundle\Model\Acl\SecurityIdentityQuery;
2013-12-14 12:49:10 +01:00
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
2016-02-11 19:14:03 +01:00
use Propel\Bundle\PropelBundle\Tests\AclTestCase;
2013-12-14 12:49:10 +01:00
/**
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class SecurityIdentityTest extends AclTestCase
{
public function testToAclIdentityUserWithInvalidIdentifier()
{
$identity = new SecurityIdentity();
$identity->setIdentifier('invalidIdentifier');
$identity->setUsername(true);
$this->expectException('InvalidArgumentException');
2013-12-14 12:49:10 +01:00
SecurityIdentity::toAclIdentity($identity);
}
public function testToAclIdentityUnknownSecurityIdentity()
{
$identity = new SecurityIdentity();
$identity->setIdentifier('invalidIdentifier');
$identity->setUsername(false);
$this->expectException('InvalidArgumentException');
2013-12-14 12:49:10 +01:00
SecurityIdentity::toAclIdentity($identity);
}
public function testToAclIdentityValidUser()
{
$identity = new SecurityIdentity();
2016-02-11 19:14:03 +01:00
$identity->setIdentifier('Propel\Bundle\PropelBundle\Tests\Fixtures\UserProxy-propel');
2013-12-14 12:49:10 +01:00
$identity->setUsername(true);
$secIdentity = SecurityIdentity::toAclIdentity($identity);
$this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\UserSecurityIdentity', $secIdentity);
}
public function testToAclIdentityMultipleDashes()
{
$identity = new SecurityIdentity();
2016-02-11 19:14:03 +01:00
$identity->setIdentifier('Propel\Bundle\PropelBundle\Tests\Fixtures\UserProxy-some-username@domain.com');
2013-12-14 12:49:10 +01:00
$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->getMockBuilder('Symfony\Component\Security\Acl\Model\SecurityIdentityInterface')->getMock();
2013-12-14 12:49:10 +01:00
$this->expectException('InvalidArgumentException');
2013-12-14 12:49:10 +01:00
SecurityIdentity::fromAclIdentity($secIdentity, $this->con);
}
public function testFromAclIdentityWithUser()
{
2016-02-11 19:14:03 +01:00
$secIdentity = new UserSecurityIdentity('propel', 'Propel\Bundle\PropelBundle\Tests\Fixtures\UserProxy');
2013-12-14 12:49:10 +01:00
$identity = SecurityIdentity::fromAclIdentity($secIdentity, $this->con);
2016-02-11 19:14:03 +01:00
$this->assertInstanceOf('Propel\Bundle\PropelBundle\Model\Acl\SecurityIdentity', $identity);
2013-12-14 12:49:10 +01:00
$this->assertEquals(true, $identity->getUsername());
2016-02-11 19:14:03 +01:00
$this->assertEquals('Propel\Bundle\PropelBundle\Tests\Fixtures\UserProxy-propel', $identity->getIdentifier());
2013-12-14 12:49:10 +01:00
$this->assertGreaterThan(0, $identity->getId());
$dbEntry = SecurityIdentityQuery::create()->findPk($identity->getId());
2016-02-11 19:14:03 +01:00
$this->assertInstanceOf('Propel\Bundle\PropelBundle\Model\Acl\SecurityIdentity', $dbEntry);
2013-12-14 12:49:10 +01:00
}
public function testFromAclIdentityWithRole()
{
$secIdentity = new RoleSecurityIdentity(new Role('ROLE_USER'));
$identity = SecurityIdentity::fromAclIdentity($secIdentity, $this->con);
2016-02-11 19:14:03 +01:00
$this->assertInstanceOf('Propel\Bundle\PropelBundle\Model\Acl\SecurityIdentity', $identity);
2013-12-14 12:49:10 +01:00
$this->assertEquals(false, $identity->getUsername());
$this->assertEquals('ROLE_USER', $identity->getIdentifier());
$this->assertGreaterThan(0, $identity->getId());
$dbEntry = SecurityIdentityQuery::create()->findPk($identity->getId());
2016-02-11 19:14:03 +01:00
$this->assertInstanceOf('Propel\Bundle\PropelBundle\Model\Acl\SecurityIdentity', $dbEntry);
2013-12-14 12:49:10 +01:00
}
}