SecurityDataCollectorTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\SecurityBundle\Tests\DataCollector;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  14. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  15. use Symfony\Component\Security\Core\Role\Role;
  16. use Symfony\Component\Security\Core\Role\RoleHierarchy;
  17. class SecurityDataCollectorTest extends TestCase
  18. {
  19. public function testCollectWhenSecurityIsDisabled()
  20. {
  21. $collector = new SecurityDataCollector();
  22. $collector->collect($this->getRequest(), $this->getResponse());
  23. $this->assertSame('security', $collector->getName());
  24. $this->assertFalse($collector->isEnabled());
  25. $this->assertFalse($collector->isAuthenticated());
  26. $this->assertNull($collector->getTokenClass());
  27. $this->assertFalse($collector->supportsRoleHierarchy());
  28. $this->assertCount(0, $collector->getRoles());
  29. $this->assertCount(0, $collector->getInheritedRoles());
  30. $this->assertEmpty($collector->getUser());
  31. }
  32. public function testCollectWhenAuthenticationTokenIsNull()
  33. {
  34. $tokenStorage = new TokenStorage();
  35. $collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
  36. $collector->collect($this->getRequest(), $this->getResponse());
  37. $this->assertTrue($collector->isEnabled());
  38. $this->assertFalse($collector->isAuthenticated());
  39. $this->assertNull($collector->getTokenClass());
  40. $this->assertTrue($collector->supportsRoleHierarchy());
  41. $this->assertCount(0, $collector->getRoles());
  42. $this->assertCount(0, $collector->getInheritedRoles());
  43. $this->assertEmpty($collector->getUser());
  44. }
  45. /**
  46. * @group legacy
  47. */
  48. public function testLegacyCollectWhenAuthenticationTokenIsNull()
  49. {
  50. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContextInterface')->getMock();
  51. $collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
  52. $collector->collect($this->getRequest(), $this->getResponse());
  53. $this->assertTrue($collector->isEnabled());
  54. $this->assertFalse($collector->isAuthenticated());
  55. $this->assertNull($collector->getTokenClass());
  56. $this->assertTrue($collector->supportsRoleHierarchy());
  57. $this->assertCount(0, $collector->getRoles());
  58. $this->assertCount(0, $collector->getInheritedRoles());
  59. $this->assertEmpty($collector->getUser());
  60. }
  61. /** @dataProvider provideRoles */
  62. public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles, array $inheritedRoles)
  63. {
  64. $tokenStorage = new TokenStorage();
  65. $tokenStorage->setToken(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles));
  66. $collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
  67. $collector->collect($this->getRequest(), $this->getResponse());
  68. $this->assertTrue($collector->isEnabled());
  69. $this->assertTrue($collector->isAuthenticated());
  70. $this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass());
  71. $this->assertTrue($collector->supportsRoleHierarchy());
  72. $this->assertSame($normalizedRoles, $collector->getRoles());
  73. $this->assertSame($inheritedRoles, $collector->getInheritedRoles());
  74. $this->assertSame('hhamon', $collector->getUser());
  75. }
  76. public function provideRoles()
  77. {
  78. return array(
  79. // Basic roles
  80. array(
  81. array('ROLE_USER'),
  82. array('ROLE_USER'),
  83. array(),
  84. ),
  85. array(
  86. array(new Role('ROLE_USER')),
  87. array('ROLE_USER'),
  88. array(),
  89. ),
  90. // Inherited roles
  91. array(
  92. array('ROLE_ADMIN'),
  93. array('ROLE_ADMIN'),
  94. array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
  95. ),
  96. array(
  97. array(new Role('ROLE_ADMIN')),
  98. array('ROLE_ADMIN'),
  99. array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
  100. ),
  101. array(
  102. array('ROLE_ADMIN', 'ROLE_OPERATOR'),
  103. array('ROLE_ADMIN', 'ROLE_OPERATOR'),
  104. array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
  105. ),
  106. );
  107. }
  108. private function getRoleHierarchy()
  109. {
  110. return new RoleHierarchy(array(
  111. 'ROLE_ADMIN' => array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
  112. 'ROLE_OPERATOR' => array('ROLE_USER'),
  113. ));
  114. }
  115. private function getRequest()
  116. {
  117. return $this
  118. ->getMockBuilder('Symfony\Component\HttpFoundation\Request')
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. }
  122. private function getResponse()
  123. {
  124. return $this
  125. ->getMockBuilder('Symfony\Component\HttpFoundation\Response')
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. }
  129. }