AccessDecisionManagerTest.php 6.1 KB

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