|
| 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\UsernamePasswordToken; |
| 7 | +use Symfony\Component\Security\Core\Role\Role; |
| 8 | + |
| 9 | +class SecurityDataCollectorTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + public function testCollectWhenSecurityIsDisabled() |
| 12 | + { |
| 13 | + $collector = new SecurityDataCollector(); |
| 14 | + $collector->collect($this->getRequest(), $this->getResponse()); |
| 15 | + |
| 16 | + $this->assertSame('security', $collector->getName()); |
| 17 | + $this->assertFalse($collector->isEnabled()); |
| 18 | + $this->assertFalse($collector->isAuthenticated()); |
| 19 | + $this->assertNull($collector->getTokenClass()); |
| 20 | + $this->assertCount(0, $collector->getRoles()); |
| 21 | + $this->assertEmpty($collector->getUser()); |
| 22 | + } |
| 23 | + |
| 24 | + public function testCollectWhenAuthenticationTokenIsNull() |
| 25 | + { |
| 26 | + $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'); |
| 27 | + $securityContext->expects($this->once())->method('getToken')->willReturn(null); |
| 28 | + |
| 29 | + $collector = new SecurityDataCollector($securityContext); |
| 30 | + $collector->collect($this->getRequest(), $this->getResponse()); |
| 31 | + |
| 32 | + $this->assertTrue($collector->isEnabled()); |
| 33 | + $this->assertFalse($collector->isAuthenticated()); |
| 34 | + $this->assertNull($collector->getTokenClass()); |
| 35 | + $this->assertCount(0, $collector->getRoles()); |
| 36 | + $this->assertEmpty($collector->getUser()); |
| 37 | + } |
| 38 | + |
| 39 | + /** @dataProvider provideRoles */ |
| 40 | + public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles) |
| 41 | + { |
| 42 | + $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'); |
| 43 | + $securityContext |
| 44 | + ->expects($this->once()) |
| 45 | + ->method('getToken') |
| 46 | + ->willReturn(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles)); |
| 47 | + |
| 48 | + $collector = new SecurityDataCollector($securityContext); |
| 49 | + $collector->collect($this->getRequest(), $this->getResponse()); |
| 50 | + |
| 51 | + $this->assertTrue($collector->isEnabled()); |
| 52 | + $this->assertTrue($collector->isAuthenticated()); |
| 53 | + $this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass()); |
| 54 | + $this->assertSame($normalizedRoles, $collector->getRoles()); |
| 55 | + $this->assertSame('hhamon', $collector->getUser()); |
| 56 | + } |
| 57 | + |
| 58 | + public function provideRoles() |
| 59 | + { |
| 60 | + return array( |
| 61 | + array( |
| 62 | + array('ROLE_USER'), |
| 63 | + array('ROLE_USER'), |
| 64 | + ), |
| 65 | + array( |
| 66 | + array(new Role('ROLE_USER')), |
| 67 | + array('ROLE_USER'), |
| 68 | + ), |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + private function getRequest() |
| 73 | + { |
| 74 | + return $this |
| 75 | + ->getMockBuilder('Symfony\Component\HttpFoundation\Request') |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + } |
| 79 | + |
| 80 | + private function getResponse() |
| 81 | + { |
| 82 | + return $this |
| 83 | + ->getMockBuilder('Symfony\Component\HttpFoundation\Response') |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->getMock(); |
| 86 | + } |
| 87 | +} |
0 commit comments