AccessDecisionManagerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\Security\Authorization;
  10. use Symfony\Component\Security\Authorization\AccessDecisionManager;
  11. use Symfony\Component\Security\Authorization\Voter\VoterInterface;
  12. class AccessDecisionManagerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testSupportsClass()
  15. {
  16. $manager = new AccessDecisionManager(array(
  17. $this->getVoterSupportsClass(true),
  18. $this->getVoterSupportsClass(false),
  19. ));
  20. $this->assertTrue($manager->supportsClass('FooClass'));
  21. $manager = new AccessDecisionManager(array(
  22. $this->getVoterSupportsClass(false),
  23. $this->getVoterSupportsClass(false),
  24. ));
  25. $this->assertFalse($manager->supportsClass('FooClass'));
  26. }
  27. public function testSupportsAttribute()
  28. {
  29. $manager = new AccessDecisionManager(array(
  30. $this->getVoterSupportsAttribute(true),
  31. $this->getVoterSupportsAttribute(false),
  32. ));
  33. $this->assertTrue($manager->supportsAttribute('foo'));
  34. $manager = new AccessDecisionManager(array(
  35. $this->getVoterSupportsAttribute(false),
  36. $this->getVoterSupportsAttribute(false),
  37. ));
  38. $this->assertFalse($manager->supportsAttribute('foo'));
  39. }
  40. /**
  41. * @expectedException LogicException
  42. */
  43. public function testSetVotersEmpty()
  44. {
  45. $manager = new AccessDecisionManager();
  46. $manager->setVoters(array());
  47. }
  48. public function testSetVoters()
  49. {
  50. $manager = new AccessDecisionManager();
  51. $manager->setVoters(array($voter = $this->getVoterSupportsAttribute(true)));
  52. $this->assertSame(array($voter), $manager->getVoters());
  53. }
  54. public function testGetVoters()
  55. {
  56. $manager = new AccessDecisionManager(array($voter = $this->getVoterSupportsAttribute(true)));
  57. $this->assertSame(array($voter), $manager->getVoters());
  58. }
  59. /**
  60. * @dataProvider getStrategyTests
  61. */
  62. public function testStrategies($strategy, $voters, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions, $expected)
  63. {
  64. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
  65. $manager = new AccessDecisionManager($voters, $strategy, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions);
  66. $this->assertSame($expected, $manager->decide($token, array('ROLE_FOO')));
  67. }
  68. public function getStrategyTests()
  69. {
  70. return array(
  71. // affirmative
  72. array('affirmative', $this->getVoters(1, 0, 0), false, true, true),
  73. array('affirmative', $this->getVoters(1, 2, 0), false, true, true),
  74. array('affirmative', $this->getVoters(0, 1, 0), false, true, false),
  75. array('affirmative', $this->getVoters(0, 0, 0), false, true, false),
  76. array('affirmative', $this->getVoters(0, 0, 1), false, true, false),
  77. array('affirmative', $this->getVoters(0, 0, 1), true, true, true),
  78. // consensus
  79. array('consensus', $this->getVoters(1, 0, 0), false, true, true),
  80. array('consensus', $this->getVoters(1, 2, 0), false, true, false),
  81. array('consensus', $this->getVoters(2, 1, 0), false, true, true),
  82. array('consensus', $this->getVoters(0, 0, 0), false, true, false),
  83. array('consensus', $this->getVoters(0, 0, 1), false, true, false),
  84. array('consensus', $this->getVoters(0, 0, 0), true, true, true),
  85. array('consensus', $this->getVoters(0, 0, 1), true, true, true),
  86. array('consensus', $this->getVoters(2, 2, 0), false, true, true),
  87. array('consensus', $this->getVoters(2, 2, 1), false, true, true),
  88. array('consensus', $this->getVoters(2, 2, 0), false, false, false),
  89. array('consensus', $this->getVoters(2, 2, 1), false, false, false),
  90. // unanimous
  91. array('unanimous', $this->getVoters(1, 0, 0), false, true, true),
  92. array('unanimous', $this->getVoters(1, 0, 1), false, true, true),
  93. array('unanimous', $this->getVoters(1, 1, 0), false, true, false),
  94. array('unanimous', $this->getVoters(0, 0, 0), false, true, false),
  95. array('unanimous', $this->getVoters(0, 0, 0), true, true, true),
  96. array('unanimous', $this->getVoters(0, 0, 2), false, true, false),
  97. array('unanimous', $this->getVoters(0, 0, 2), true, true, true),
  98. );
  99. }
  100. protected function getVoters($grants, $denies, $abstains)
  101. {
  102. $voters = array();
  103. for ($i = 0; $i < $grants; $i++) {
  104. $voters[] = $this->getVoter(VoterInterface::ACCESS_GRANTED);
  105. }
  106. for ($i = 0; $i < $denies; $i++) {
  107. $voters[] = $this->getVoter(VoterInterface::ACCESS_DENIED);
  108. }
  109. for ($i = 0; $i < $abstains; $i++) {
  110. $voters[] = $this->getVoter(VoterInterface::ACCESS_ABSTAIN);
  111. }
  112. return $voters;
  113. }
  114. protected function getVoter($vote)
  115. {
  116. $voter = $this->getMock('Symfony\Component\Security\Authorization\Voter\VoterInterface');
  117. $voter->expects($this->any())
  118. ->method('vote')
  119. ->will($this->returnValue($vote));
  120. ;
  121. return $voter;
  122. }
  123. protected function getVoterSupportsClass($ret)
  124. {
  125. $voter = $this->getMock('Symfony\Component\Security\Authorization\Voter\VoterInterface');
  126. $voter->expects($this->any())
  127. ->method('supportsClass')
  128. ->will($this->returnValue($ret));
  129. ;
  130. return $voter;
  131. }
  132. protected function getVoterSupportsAttribute($ret)
  133. {
  134. $voter = $this->getMock('Symfony\Component\Security\Authorization\Voter\VoterInterface');
  135. $voter->expects($this->any())
  136. ->method('supportsAttribute')
  137. ->will($this->returnValue($ret));
  138. ;
  139. return $voter;
  140. }
  141. }