SecurityIdentityRetrievalStrategyTest.php 5.1 KB

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