AclVoterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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\Acl\Voter;
  11. use Symfony\Component\Security\Acl\Exception\NoAceFoundException;
  12. use Symfony\Component\Security\Acl\Voter\FieldVote;
  13. use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
  14. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  15. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  16. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  17. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  18. use Symfony\Component\Security\Acl\Voter\AclVoter;
  19. class AclVoterTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @dataProvider getSupportsAttributeTests
  23. */
  24. public function testSupportsAttribute($attribute, $supported)
  25. {
  26. list($voter,, $permissionMap,,) = $this->getVoter();
  27. $permissionMap
  28. ->expects($this->once())
  29. ->method('contains')
  30. ->with($this->identicalTo($attribute))
  31. ->will($this->returnValue($supported))
  32. ;
  33. $this->assertSame($supported, $voter->supportsAttribute($attribute));
  34. }
  35. public function getSupportsAttributeTests()
  36. {
  37. return array(
  38. array('foo', true),
  39. array('foo', false),
  40. );
  41. }
  42. /**
  43. * @dataProvider getSupportsClassTests
  44. */
  45. public function testSupportsClass($class)
  46. {
  47. list($voter,,,,) = $this->getVoter();
  48. $this->assertTrue($voter->supportsClass($class));
  49. }
  50. public function getSupportsClassTests()
  51. {
  52. return array(
  53. array('foo'),
  54. array('bar'),
  55. array('moo'),
  56. );
  57. }
  58. public function testVote()
  59. {
  60. list($voter,, $permissionMap,,) = $this->getVoter();
  61. $permissionMap
  62. ->expects($this->atLeastOnce())
  63. ->method('contains')
  64. ->will($this->returnValue(false))
  65. ;
  66. $this->assertSame(VoterInterface::ACCESS_ABSTAIN, $voter->vote($this->getToken(), null, array('VIEW', 'EDIT', 'DELETE')));
  67. }
  68. /**
  69. * @dataProvider getTrueFalseTests
  70. */
  71. public function testVoteWhenNoObjectIsPassed($allowIfObjectIdentityUnavailable)
  72. {
  73. list($voter,, $permissionMap,,) = $this->getVoter($allowIfObjectIdentityUnavailable);
  74. $permissionMap
  75. ->expects($this->once())
  76. ->method('contains')
  77. ->will($this->returnValue(true))
  78. ;
  79. if ($allowIfObjectIdentityUnavailable) {
  80. $vote = VoterInterface::ACCESS_GRANTED;
  81. } else {
  82. $vote = VoterInterface::ACCESS_ABSTAIN;
  83. }
  84. $this->assertSame($vote, $voter->vote($this->getToken(), null, array('VIEW')));
  85. }
  86. /**
  87. * @dataProvider getTrueFalseTests
  88. */
  89. public function testVoteWhenOidStrategyReturnsNull($allowIfUnavailable)
  90. {
  91. list($voter,, $permissionMap, $oidStrategy,) = $this->getVoter($allowIfUnavailable);
  92. $permissionMap
  93. ->expects($this->once())
  94. ->method('contains')
  95. ->will($this->returnValue(true))
  96. ;
  97. $oidStrategy
  98. ->expects($this->once())
  99. ->method('getObjectIdentity')
  100. ->will($this->returnValue(null))
  101. ;
  102. if ($allowIfUnavailable) {
  103. $vote = VoterInterface::ACCESS_GRANTED;
  104. } else {
  105. $vote = VoterInterface::ACCESS_ABSTAIN;
  106. }
  107. $this->assertSame($vote, $voter->vote($this->getToken(), new \stdClass(), array('VIEW')));
  108. }
  109. public function getTrueFalseTests()
  110. {
  111. return array(array(true), array(false));
  112. }
  113. public function testVoteNoAclFound()
  114. {
  115. list($voter, $provider, $permissionMap, $oidStrategy, $sidStrategy) = $this->getVoter();
  116. $permissionMap
  117. ->expects($this->once())
  118. ->method('contains')
  119. ->will($this->returnValue(true))
  120. ;
  121. $oidStrategy
  122. ->expects($this->once())
  123. ->method('getObjectIdentity')
  124. ->will($this->returnValue($oid = new ObjectIdentity('1', 'Foo')))
  125. ;
  126. $sidStrategy
  127. ->expects($this->once())
  128. ->method('getSecurityIdentities')
  129. ->will($this->returnValue($sids = array(new UserSecurityIdentity('johannes', 'Foo'), new RoleSecurityIdentity('ROLE_FOO'))))
  130. ;
  131. $provider
  132. ->expects($this->once())
  133. ->method('findAcl')
  134. ->with($this->equalTo($oid), $this->equalTo($sids))
  135. ->will($this->throwException(new AclNotFoundException('Not found.')))
  136. ;
  137. $this->assertSame(VoterInterface::ACCESS_DENIED, $voter->vote($this->getToken(), new \stdClass(), array('VIEW')));
  138. }
  139. /**
  140. * @dataProvider getTrueFalseTests
  141. */
  142. public function testVoteGrantsAccess($grant)
  143. {
  144. list($voter, $provider, $permissionMap, $oidStrategy, $sidStrategy) = $this->getVoter();
  145. $permissionMap
  146. ->expects($this->once())
  147. ->method('contains')
  148. ->will($this->returnValue(true))
  149. ;
  150. $permissionMap
  151. ->expects($this->once())
  152. ->method('getMasks')
  153. ->with($this->equalTo('VIEW'))
  154. ->will($this->returnValue($masks = array(1, 2, 3)))
  155. ;
  156. $oidStrategy
  157. ->expects($this->once())
  158. ->method('getObjectIdentity')
  159. ->will($this->returnValue($oid = new ObjectIdentity('1', 'Foo')))
  160. ;
  161. $sidStrategy
  162. ->expects($this->once())
  163. ->method('getSecurityIdentities')
  164. ->will($this->returnValue($sids = array(new UserSecurityIdentity('johannes', 'Foo'), new RoleSecurityIdentity('ROLE_FOO'))))
  165. ;
  166. $provider
  167. ->expects($this->once())
  168. ->method('findAcl')
  169. ->with($this->equalTo($oid), $this->equalTo($sids))
  170. ->will($this->returnValue($acl = $this->getMock('Symfony\Component\Security\Acl\Model\AclInterface')))
  171. ;
  172. $acl
  173. ->expects($this->once())
  174. ->method('isGranted')
  175. ->with($this->identicalTo($masks), $this->equalTo($sids), $this->isFalse())
  176. ->will($this->returnValue($grant))
  177. ;
  178. if ($grant) {
  179. $vote = VoterInterface::ACCESS_GRANTED;
  180. } else {
  181. $vote = VoterInterface::ACCESS_DENIED;
  182. }
  183. $this->assertSame($vote, $voter->vote($this->getToken(), new \stdClass(), array('VIEW')));
  184. }
  185. public function testVoteNoAceFound()
  186. {
  187. list($voter, $provider, $permissionMap, $oidStrategy, $sidStrategy) = $this->getVoter();
  188. $permissionMap
  189. ->expects($this->once())
  190. ->method('contains')
  191. ->will($this->returnValue(true))
  192. ;
  193. $permissionMap
  194. ->expects($this->once())
  195. ->method('getMasks')
  196. ->with($this->equalTo('VIEW'))
  197. ->will($this->returnValue($masks = array(1, 2, 3)))
  198. ;
  199. $oidStrategy
  200. ->expects($this->once())
  201. ->method('getObjectIdentity')
  202. ->will($this->returnValue($oid = new ObjectIdentity('1', 'Foo')))
  203. ;
  204. $sidStrategy
  205. ->expects($this->once())
  206. ->method('getSecurityIdentities')
  207. ->will($this->returnValue($sids = array(new UserSecurityIdentity('johannes', 'Foo'), new RoleSecurityIdentity('ROLE_FOO'))))
  208. ;
  209. $provider
  210. ->expects($this->once())
  211. ->method('findAcl')
  212. ->with($this->equalTo($oid), $this->equalTo($sids))
  213. ->will($this->returnValue($acl = $this->getMock('Symfony\Component\Security\Acl\Model\AclInterface')))
  214. ;
  215. $acl
  216. ->expects($this->once())
  217. ->method('isGranted')
  218. ->with($this->identicalTo($masks), $this->equalTo($sids), $this->isFalse())
  219. ->will($this->throwException(new NoAceFoundException('No ACE')))
  220. ;
  221. $this->assertSame(VoterInterface::ACCESS_DENIED, $voter->vote($this->getToken(), new \stdClass(), array('VIEW')));
  222. }
  223. /**
  224. * @dataProvider getTrueFalseTests
  225. */
  226. public function testVoteGrantsFieldAccess($grant)
  227. {
  228. list($voter, $provider, $permissionMap, $oidStrategy, $sidStrategy) = $this->getVoter();
  229. $permissionMap
  230. ->expects($this->once())
  231. ->method('contains')
  232. ->will($this->returnValue(true))
  233. ;
  234. $permissionMap
  235. ->expects($this->once())
  236. ->method('getMasks')
  237. ->with($this->equalTo('VIEW'))
  238. ->will($this->returnValue($masks = array(1, 2, 3)))
  239. ;
  240. $oidStrategy
  241. ->expects($this->once())
  242. ->method('getObjectIdentity')
  243. ->will($this->returnValue($oid = new ObjectIdentity('1', 'Foo')))
  244. ;
  245. $sidStrategy
  246. ->expects($this->once())
  247. ->method('getSecurityIdentities')
  248. ->will($this->returnValue($sids = array(new UserSecurityIdentity('johannes', 'Foo'), new RoleSecurityIdentity('ROLE_FOO'))))
  249. ;
  250. $provider
  251. ->expects($this->once())
  252. ->method('findAcl')
  253. ->with($this->equalTo($oid), $this->equalTo($sids))
  254. ->will($this->returnValue($acl = $this->getMock('Symfony\Component\Security\Acl\Model\AclInterface')))
  255. ;
  256. $acl
  257. ->expects($this->once())
  258. ->method('isFieldGranted')
  259. ->with($this->identicalTo('foo'), $this->identicalTo($masks), $this->equalTo($sids), $this->isFalse())
  260. ->will($this->returnValue($grant))
  261. ;
  262. if ($grant) {
  263. $vote = VoterInterface::ACCESS_GRANTED;
  264. } else {
  265. $vote = VoterInterface::ACCESS_DENIED;
  266. }
  267. $this->assertSame($vote, $voter->vote($this->getToken(), new FieldVote(new \stdClass(), 'foo'), array('VIEW')));
  268. }
  269. public function testVoteNoFieldAceFound()
  270. {
  271. list($voter, $provider, $permissionMap, $oidStrategy, $sidStrategy) = $this->getVoter();
  272. $permissionMap
  273. ->expects($this->once())
  274. ->method('contains')
  275. ->will($this->returnValue(true))
  276. ;
  277. $permissionMap
  278. ->expects($this->once())
  279. ->method('getMasks')
  280. ->with($this->equalTo('VIEW'))
  281. ->will($this->returnValue($masks = array(1, 2, 3)))
  282. ;
  283. $oidStrategy
  284. ->expects($this->once())
  285. ->method('getObjectIdentity')
  286. ->will($this->returnValue($oid = new ObjectIdentity('1', 'Foo')))
  287. ;
  288. $sidStrategy
  289. ->expects($this->once())
  290. ->method('getSecurityIdentities')
  291. ->will($this->returnValue($sids = array(new UserSecurityIdentity('johannes', 'Foo'), new RoleSecurityIdentity('ROLE_FOO'))))
  292. ;
  293. $provider
  294. ->expects($this->once())
  295. ->method('findAcl')
  296. ->with($this->equalTo($oid), $this->equalTo($sids))
  297. ->will($this->returnValue($acl = $this->getMock('Symfony\Component\Security\Acl\Model\AclInterface')))
  298. ;
  299. $acl
  300. ->expects($this->once())
  301. ->method('isFieldGranted')
  302. ->with($this->identicalTo('foo'), $this->identicalTo($masks), $this->equalTo($sids), $this->isFalse())
  303. ->will($this->throwException(new NoAceFoundException('No ACE')))
  304. ;
  305. $this->assertSame(VoterInterface::ACCESS_DENIED, $voter->vote($this->getToken(), new FieldVote(new \stdClass(), 'foo'), array('VIEW')));
  306. }
  307. protected function getToken()
  308. {
  309. return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  310. }
  311. protected function getVoter($allowIfObjectIdentityUnavailable = true)
  312. {
  313. $provider = $this->getMock('Symfony\Component\Security\Acl\Model\AclProviderInterface');
  314. $permissionMap = $this->getMock('Symfony\Component\Security\Acl\Permission\PermissionMapInterface');
  315. $oidStrategy = $this->getMock('Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface');
  316. $sidStrategy = $this->getMock('Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface');
  317. return array(
  318. new AclVoter($provider, $oidStrategy, $sidStrategy, $permissionMap, null, $allowIfObjectIdentityUnavailable),
  319. $provider,
  320. $permissionMap,
  321. $oidStrategy,
  322. $sidStrategy,
  323. );
  324. }
  325. }