|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Bundle\SecurityBundle\Tests\DataCollector; |
| 4 | + |
| 5 | +use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector; |
| 6 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
| 7 | +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
| 8 | +use Symfony\Component\Security\Core\Role\Role; |
| 9 | +use Symfony\Component\Security\Core\Role\RoleHierarchy; |
| 10 | + |
| 11 | +class SecurityDataCollectorTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + public function testCollectWhenSecurityIsDisabled() |
| 14 | + { |
| 15 | + $collector = new SecurityDataCollector(); |
| 16 | + $collector->collect($this->getRequest(), $this->getResponse()); |
| 17 | + |
| 18 | + $this->assertSame('security', $collector->getName()); |
| 19 | + $this->assertFalse($collector->isEnabled()); |
| 20 | + $this->assertFalse($collector->isAuthenticated()); |
| 21 | + $this->assertNull($collector->getTokenClass()); |
| 22 | + $this->assertFalse($collector->supportsRoleHierarchy()); |
| 23 | + $this->assertCount(0, $collector->getRoles()); |
| 24 | + $this->assertCount(0, $collector->getInheritedRoles()); |
| 25 | + $this->assertEmpty($collector->getUser()); |
| 26 | + } |
| 27 | + |
| 28 | + /** @dataProvider provideTokenStorage */ |
| 29 | + public function testCollectWhenAuthenticationTokenIsNull($tokenStorage) |
| 30 | + { |
| 31 | + $collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy()); |
| 32 | + $collector->collect($this->getRequest(), $this->getResponse()); |
| 33 | + |
| 34 | + $this->assertTrue($collector->isEnabled()); |
| 35 | + $this->assertFalse($collector->isAuthenticated()); |
| 36 | + $this->assertNull($collector->getTokenClass()); |
| 37 | + $this->assertTrue($collector->supportsRoleHierarchy()); |
| 38 | + $this->assertCount(0, $collector->getRoles()); |
| 39 | + $this->assertCount(0, $collector->getInheritedRoles()); |
| 40 | + $this->assertEmpty($collector->getUser()); |
| 41 | + } |
| 42 | + |
| 43 | + public function provideTokenStorage() |
| 44 | + { |
| 45 | + return array( |
| 46 | + array(new TokenStorage()), |
| 47 | + array($this->getMock('Symfony\Component\Security\Core\SecurityContextInterface')), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** @dataProvider provideRoles */ |
| 52 | + public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles, array $inheritedRoles) |
| 53 | + { |
| 54 | + $tokenStorage = new TokenStorage(); |
| 55 | + $tokenStorage->setToken(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles)); |
| 56 | + |
| 57 | + $collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy()); |
| 58 | + $collector->collect($this->getRequest(), $this->getResponse()); |
| 59 | + |
| 60 | + $this->assertTrue($collector->isEnabled()); |
| 61 | + $this->assertTrue($collector->isAuthenticated()); |
| 62 | + $this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass()); |
| 63 | + $this->assertTrue($collector->supportsRoleHierarchy()); |
| 64 | + $this->assertSame($normalizedRoles, $collector->getRoles()); |
| 65 | + $this->assertSame($inheritedRoles, $collector->getInheritedRoles()); |
| 66 | + $this->assertSame('hhamon', $collector->getUser()); |
| 67 | + } |
| 68 | + |
| 69 | + public function provideRoles() |
| 70 | + { |
| 71 | + return array( |
| 72 | + // Basic roles |
| 73 | + array( |
| 74 | + array('ROLE_USER'), |
| 75 | + array('ROLE_USER'), |
| 76 | + array(), |
| 77 | + ), |
| 78 | + array( |
| 79 | + array(new Role('ROLE_USER')), |
| 80 | + array('ROLE_USER'), |
| 81 | + array(), |
| 82 | + ), |
| 83 | + // Inherited roles |
| 84 | + array( |
| 85 | + array('ROLE_ADMIN'), |
| 86 | + array('ROLE_ADMIN'), |
| 87 | + array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'), |
| 88 | + ), |
| 89 | + array( |
| 90 | + array(new Role('ROLE_ADMIN')), |
| 91 | + array('ROLE_ADMIN'), |
| 92 | + array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'), |
| 93 | + ), |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + private function getRoleHierarchy() |
| 98 | + { |
| 99 | + return new RoleHierarchy(array( |
| 100 | + 'ROLE_ADMIN' => array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'), |
| 101 | + )); |
| 102 | + } |
| 103 | + |
| 104 | + private function getRequest() |
| 105 | + { |
| 106 | + return $this |
| 107 | + ->getMockBuilder('Symfony\Component\HttpFoundation\Request') |
| 108 | + ->disableOriginalConstructor() |
| 109 | + ->getMock(); |
| 110 | + } |
| 111 | + private function getResponse() |
| 112 | + { |
| 113 | + return $this |
| 114 | + ->getMockBuilder('Symfony\Component\HttpFoundation\Response') |
| 115 | + ->disableOriginalConstructor() |
| 116 | + ->getMock(); |
| 117 | + } |
| 118 | +} |
0 commit comments