SecurityIdentityRetrievalStrategyTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\Security\Acl\Domain;
  11. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  12. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  13. use Symfony\Component\Security\Acl\Domain\SecurityIdentityRetrievalStrategy;
  14. class SecurityIdentityRetrievalStrategyTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @dataProvider getSecurityIdentityRetrievalTests
  18. */
  19. public function testGetSecurityIdentities($user, array $roles, $authenticationStatus, array $sids)
  20. {
  21. $strategy = $this->getStrategy($roles, $authenticationStatus);
  22. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  23. $token
  24. ->expects($this->once())
  25. ->method('getRoles')
  26. ->will($this->returnValue(array('foo')))
  27. ;
  28. $token
  29. ->expects($this->once())
  30. ->method('getUser')
  31. ->will($this->returnValue($user))
  32. ;
  33. $extractedSids = $strategy->getSecurityIdentities($token);
  34. foreach ($extractedSids as $index => $extractedSid) {
  35. if (!isset($sids[$index])) {
  36. $this->fail(sprintf('Expected SID at index %d, but there was none.', true));
  37. }
  38. if (false === $sids[$index]->equals($extractedSid)) {
  39. $this->fail(sprintf('Index: %d, expected SID "%s", but got "%s".', $index, $sids[$index], $extractedSid));
  40. }
  41. }
  42. }
  43. public function getSecurityIdentityRetrievalTests()
  44. {
  45. return array(
  46. array($this->getAccount('johannes', 'FooUser'), array('ROLE_USER', 'ROLE_SUPERADMIN'), 'fullFledged', array(
  47. new UserSecurityIdentity('johannes', 'FooUser'),
  48. new RoleSecurityIdentity('ROLE_USER'),
  49. new RoleSecurityIdentity('ROLE_SUPERADMIN'),
  50. new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
  51. new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
  52. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  53. )),
  54. array($this->getAccount('foo', 'FooBarUser'), array('ROLE_FOO'), 'rememberMe', array(
  55. new UserSecurityIdentity('foo', 'FooBarUser'),
  56. new RoleSecurityIdentity('ROLE_FOO'),
  57. new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
  58. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  59. )),
  60. array('guest', array('ROLE_FOO'), 'anonymous', array(
  61. new RoleSecurityIdentity('ROLE_FOO'),
  62. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  63. ))
  64. );
  65. }
  66. protected function getAccount($username, $class)
  67. {
  68. $account = $this->getMock('Symfony\Component\Security\User\AccountInterface', array(), array(), $class);
  69. $account
  70. ->expects($this->once())
  71. ->method('__toString')
  72. ->will($this->returnValue($username))
  73. ;
  74. return $account;
  75. }
  76. protected function getStrategy(array $roles = array(), $authenticationStatus = 'fullFledged')
  77. {
  78. $roleHierarchy = $this->getMock('Symfony\Component\Security\Role\RoleHierarchyInterface');
  79. $roleHierarchy
  80. ->expects($this->once())
  81. ->method('getReachableRoles')
  82. ->with($this->equalTo(array('foo')))
  83. ->will($this->returnValue($roles))
  84. ;
  85. $trustResolver = $this->getMock('Symfony\Component\Security\Authentication\AuthenticationTrustResolver', array(), array('', ''));
  86. $trustResolver
  87. ->expects($this->at(0))
  88. ->method('isAnonymous')
  89. ->will($this->returnValue('anonymous' === $authenticationStatus))
  90. ;
  91. if ('fullFledged' === $authenticationStatus) {
  92. $trustResolver
  93. ->expects($this->once())
  94. ->method('isFullFledged')
  95. ->will($this->returnValue(true))
  96. ;
  97. $trustResolver
  98. ->expects($this->never())
  99. ->method('isRememberMe')
  100. ;
  101. } else if ('rememberMe' === $authenticationStatus) {
  102. $trustResolver
  103. ->expects($this->once())
  104. ->method('isFullFledged')
  105. ->will($this->returnValue(false))
  106. ;
  107. $trustResolver
  108. ->expects($this->once())
  109. ->method('isRememberMe')
  110. ->will($this->returnValue(true))
  111. ;
  112. } else {
  113. $trustResolver
  114. ->expects($this->at(1))
  115. ->method('isAnonymous')
  116. ->will($this->returnValue(true))
  117. ;
  118. $trustResolver
  119. ->expects($this->once())
  120. ->method('isFullFledged')
  121. ->will($this->returnValue(false))
  122. ;
  123. $trustResolver
  124. ->expects($this->once())
  125. ->method('isRememberMe')
  126. ->will($this->returnValue(false))
  127. ;
  128. }
  129. return new SecurityIdentityRetrievalStrategy($roleHierarchy, $trustResolver);
  130. }
  131. }