Created
December 2, 2011 15:54
-
-
Save jmoz/1423722 to your computer and use it in GitHub Desktop.
UserTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace JMOZ\Bundle\SecurityBundle\Tests\Functional; | |
| use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
| use JMOZ\Bundle\SecurityBundle\Entity\User; | |
| use JMOZ\Bundle\SecurityBundle\DataFixtures\LoadSecurityData; | |
| /** | |
| * @author James Morris <james@jmoz.co.uk> | |
| */ | |
| class SecurityTest extends WebTestCase | |
| { | |
| private $_em; | |
| private $_container; | |
| /** | |
| * Remember when testing User's they have a default role ROLE_USER which will show +1 in counts | |
| */ | |
| protected function setUp() | |
| { | |
| $kernel = static::createKernel(); | |
| $kernel->boot(); | |
| $this->_container = $kernel->getContainer(); | |
| $this->_em = $this->_container->get( 'doctrine' )->getEntityManager( 'user' ); | |
| } | |
| private function loadData() | |
| { | |
| $data = new LoadSecurityData( $this->_container->get( 'security.encoder_factory' ) ); | |
| $data->truncate( $this->_em ); | |
| $data->load( $this->_em ); | |
| } | |
| private function getRole( $role ) | |
| { | |
| return $this->_em->getRepository( 'JMOZSecurityBundle:Role' )->findOneBy( array( 'role' => $role) ); | |
| } | |
| public function testNewUserHasRoleDefaultRole() | |
| { | |
| $user = new User(); | |
| $this->assertTrue( $user->hasRole( 'ROLE_USER' ) ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_USER' ) ); // call again in case of the addition of duplicate objects (was a bug) | |
| $this->assertEquals( 1, count( $user->getRoles() ) ); | |
| } | |
| public function testNewUserNotHasRole() | |
| { | |
| $user = new User(); | |
| $this->assertTrue( $user->hasRole( 'ROLE_USER' ) ); | |
| $this->assertFalse( $user->hasRole( 'ROLE_FOO' ) ); | |
| } | |
| public function testNewUserDefaultRoleDoesNotSaveToDb() | |
| { | |
| $user = new User(); | |
| $user->setUsername( 'testusertemp' ); | |
| $user->setAlgorithm( 'sha512' ); | |
| $user->setPassword( 'testpass' ); | |
| $user->setEmail( 'testuser1@test.com' ); | |
| $this->_em->persist( $user ); | |
| $this->_em->flush(); | |
| $userId = $user->getId(); | |
| $this->_em->clear(); | |
| $user = $this->_em->getRepository( 'JMOZSecurityBundle:User' )->find( $userId ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_USER' ) ); | |
| $this->assertEquals( 1, count( $user->getRoles() ) ); | |
| $this->assertEquals( 0, $user->getRolesCollection()->count() ); // the real collection from the db, not the roles array with added default | |
| } | |
| public function testNewUserAddRoleSaves() | |
| { | |
| $this->loadData(); | |
| $user = new User(); | |
| $user->setUsername( 'testusertemp' ); | |
| $user->setAlgorithm( 'sha512' ); | |
| $user->setPassword( 'testpass' ); | |
| $user->setEmail( 'testuser1@test.com' ); | |
| $user->addRole( $this->getRole( 'ROLE_TEST1' ) ); | |
| $this->assertEquals( 2, count( $user->getRoles() ) ); | |
| $this->_em->persist( $user ); | |
| $this->_em->flush(); | |
| $user = $this->_em->getRepository( 'JMOZSecurityBundle:User' )->find( 3 ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_TEST1' ) ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_USER' ) ); | |
| $this->assertEquals( 2, count( $user->getRoles() ) ); | |
| } | |
| public function testNewUserRoleUniqueness() | |
| { | |
| $user = new User(); | |
| $user->addRole( $this->getRole( 'ROLE_TEST1' ) ); | |
| $user->addRole( $this->getRole( 'ROLE_TEST2' ) ); | |
| $user->addRole( $this->getRole( 'ROLE_TEST2' ) ); | |
| $user->addRole( $this->getRole( 'ROLE_TEST2' ) ); | |
| $this->assertEquals( 3, count( $user->getRoles() ) ); | |
| } | |
| public function testNewUserRemoveRole() | |
| { | |
| $user = new User(); | |
| $user->addRole( $this->getRole( 'ROLE_TEST1' ) ); | |
| $user->addRole( $this->getRole( 'ROLE_TEST2' ) ); | |
| $this->assertEquals( 3, count( $user->getRoles() ) ); | |
| $user->removeRole( 'ROLE_TEST1' ); | |
| $user->removeRole( 'ROLE_TEST2' ); | |
| $this->assertEquals( 1, count( $user->getRoles() ) ); | |
| } | |
| public function testExistingUserRemoveRoleSaves() | |
| { | |
| $this->loadData(); | |
| $user = $this->_em->getRepository( 'JMOZSecurityBundle:User' )->find( 1 ); | |
| $this->assertEquals( 3, count( $user->getRoles() ) ); | |
| $user->removeRole( 'ROLE_TEST2' ); | |
| $this->_em->flush(); | |
| $this->_em->clear(); | |
| $user = $this->_em->getRepository( 'JMOZSecurityBundle:User' )->find( 1 ); | |
| $this->assertEquals( 2, count( $user->getRoles() ) ); | |
| } | |
| public function testNewUserSetRoles() | |
| { | |
| $user = new User(); | |
| $user->addRole( $this->getRole( 'ROLE_TEST1' ) ); | |
| $user->addRole( $this->getRole( 'ROLE_TEST2' ) ); | |
| $this->assertEquals( 3, count( $user->getRoles() ) ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_TEST1' ) ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_TEST2' ) ); | |
| $user->setRoles( array( $this->getRole( 'ROLE_TAXONOMY_VIEW' ), $this->getRole( 'ROLE_TEST2' ) ) ); | |
| $this->assertEquals( 3, count( $user->getRoles() ) ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_TAXONOMY_VIEW' ) ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_TEST2' ) ); | |
| $this->assertFalse( $user->hasRole( 'ROLE_TEST1' ) ); | |
| } | |
| public function testExistingUserWithRoles() | |
| { | |
| $this->loadData(); | |
| $user = $this->_em->getRepository( 'JMOZSecurityBundle:User' )->find( 1 ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_TEST1' ) ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_TEST2' ) ); | |
| $this->assertTrue( $user->hasRole( 'ROLE_USER' ) ); | |
| $this->assertFalse( $user->hasRole( 'ROLE_FOO' ) ); | |
| $this->assertEquals( 3, count( $user->getRoles() ) ); | |
| } | |
| /** | |
| * Login the testuser (from fixture data) into the system. | |
| * The login proxess will trigger a lot of the User and Role code so is a useful test. | |
| * @return $client | |
| */ | |
| private function login( $username = 'testuser', $password = 'testpass' ) | |
| { | |
| $client = self::createClient(); | |
| $crawler = $client->request( 'GET', '/login' ); | |
| $form = $crawler->selectButton( 'Login' )->form( array( '_username' => $username, '_password' => $password ) ); | |
| $client->submit( $form ); | |
| $this->assertEquals( 302, $client->getResponse()->getStatusCode() ); | |
| $client->followRedirect(); | |
| $this->assertEquals( 200, $client->getResponse()->getStatusCode() ); | |
| return $client; | |
| } | |
| /** | |
| * testuser does not have ROLE_TAXONOMY_VIEW which /taxonomy is locked down with | |
| */ | |
| public function testTaxonomyRoleTaxonomyView() | |
| { | |
| $client = $this->login(); | |
| $client->request( 'GET', '/taxonomy/' ); | |
| $this->assertEquals( 403, $client->getResponse()->getStatusCode() ); | |
| } | |
| /** | |
| * testuser2 has ROLE_TAXONOMY_VIEW | |
| */ | |
| public function testTaxonomyRoleTaxonomyViewOk() | |
| { | |
| $client = $this->login( 'testuser2', 'testpass' ); | |
| $client->request( 'GET', '/taxonomy/' ); | |
| $this->assertEquals( 200, $client->getResponse()->getStatusCode() ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment