AclVoterTest.php 12 KB

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