PageRenderTime 85ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Symfony/Tests/Component/Security/Core/Authorization/AccessDecisionManagerTest.php

http://github.com/symfony/symfony
PHP | 151 lines | 108 code | 26 blank | 17 comment | 3 complexity | 7f8fdac54ff973569883a036aa1d2731 MD5 | raw file
  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\Tests\Component\Security\Core\Authorization;
  11. use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
  12. use Symfony\Component\Security\Core\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 InvalidArgumentException
  43. */
  44. public function testSetVotersEmpty()
  45. {
  46. $manager = new AccessDecisionManager(array());
  47. }
  48. /**
  49. * @dataProvider getStrategyTests
  50. */
  51. public function testStrategies($strategy, $voters, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions, $expected)
  52. {
  53. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  54. $manager = new AccessDecisionManager($voters, $strategy, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions);
  55. $this->assertSame($expected, $manager->decide($token, array('ROLE_FOO')));
  56. }
  57. public function getStrategyTests()
  58. {
  59. return array(
  60. // affirmative
  61. array('affirmative', $this->getVoters(1, 0, 0), false, true, true),
  62. array('affirmative', $this->getVoters(1, 2, 0), false, true, true),
  63. array('affirmative', $this->getVoters(0, 1, 0), false, true, false),
  64. array('affirmative', $this->getVoters(0, 0, 1), false, true, false),
  65. array('affirmative', $this->getVoters(0, 0, 1), true, true, true),
  66. // consensus
  67. array('consensus', $this->getVoters(1, 0, 0), false, true, true),
  68. array('consensus', $this->getVoters(1, 2, 0), false, true, false),
  69. array('consensus', $this->getVoters(2, 1, 0), false, true, true),
  70. array('consensus', $this->getVoters(0, 0, 1), false, true, false),
  71. array('consensus', $this->getVoters(0, 0, 1), true, true, true),
  72. array('consensus', $this->getVoters(2, 2, 0), false, true, true),
  73. array('consensus', $this->getVoters(2, 2, 1), false, true, true),
  74. array('consensus', $this->getVoters(2, 2, 0), false, false, false),
  75. array('consensus', $this->getVoters(2, 2, 1), false, false, false),
  76. // unanimous
  77. array('unanimous', $this->getVoters(1, 0, 0), false, true, true),
  78. array('unanimous', $this->getVoters(1, 0, 1), false, true, true),
  79. array('unanimous', $this->getVoters(1, 1, 0), false, true, false),
  80. array('unanimous', $this->getVoters(0, 0, 2), false, true, false),
  81. array('unanimous', $this->getVoters(0, 0, 2), true, true, true),
  82. );
  83. }
  84. protected function getVoters($grants, $denies, $abstains)
  85. {
  86. $voters = array();
  87. for ($i = 0; $i < $grants; $i++) {
  88. $voters[] = $this->getVoter(VoterInterface::ACCESS_GRANTED);
  89. }
  90. for ($i = 0; $i < $denies; $i++) {
  91. $voters[] = $this->getVoter(VoterInterface::ACCESS_DENIED);
  92. }
  93. for ($i = 0; $i < $abstains; $i++) {
  94. $voters[] = $this->getVoter(VoterInterface::ACCESS_ABSTAIN);
  95. }
  96. return $voters;
  97. }
  98. protected function getVoter($vote)
  99. {
  100. $voter = $this->getMock('Symfony\Component\Security\Core\Authorization\Voter\VoterInterface');
  101. $voter->expects($this->any())
  102. ->method('vote')
  103. ->will($this->returnValue($vote));
  104. ;
  105. return $voter;
  106. }
  107. protected function getVoterSupportsClass($ret)
  108. {
  109. $voter = $this->getMock('Symfony\Component\Security\Core\Authorization\Voter\VoterInterface');
  110. $voter->expects($this->any())
  111. ->method('supportsClass')
  112. ->will($this->returnValue($ret));
  113. ;
  114. return $voter;
  115. }
  116. protected function getVoterSupportsAttribute($ret)
  117. {
  118. $voter = $this->getMock('Symfony\Component\Security\Core\Authorization\Voter\VoterInterface');
  119. $voter->expects($this->any())
  120. ->method('supportsAttribute')
  121. ->will($this->returnValue($ret));
  122. ;
  123. return $voter;
  124. }
  125. }