AclTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Acl
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * Zend_Acl
  28. */
  29. require_once 'Zend/Acl.php';
  30. /**
  31. * Zend_Acl_Resource
  32. */
  33. require_once 'Zend/Acl/Resource.php';
  34. /**
  35. * Zend_Acl_Role
  36. */
  37. require_once 'Zend/Acl/Role.php';
  38. /**
  39. * @see Zend_Acl_MockAssertion
  40. */
  41. require_once dirname(__FILE__) . '/_files/MockAssertion.php';
  42. /**
  43. * @category Zend
  44. * @package Zend_Acl
  45. * @subpackage UnitTests
  46. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. */
  49. class Zend_Acl_AclTest extends PHPUnit_Framework_TestCase
  50. {
  51. /**
  52. * ACL object for each test method
  53. *
  54. * @var Zend_Acl
  55. */
  56. protected $_acl;
  57. /**
  58. * Instantiates a new ACL object and creates internal reference to it for each test method
  59. *
  60. * @return void
  61. */
  62. public function setUp()
  63. {
  64. $this->_acl = new Zend_Acl();
  65. }
  66. /**
  67. * Ensures that basic addition and retrieval of a single Role works
  68. *
  69. * @return void
  70. */
  71. public function testRoleRegistryAddAndGetOne()
  72. {
  73. $roleGuest = new Zend_Acl_Role('guest');
  74. $role = $this->_acl->addRole($roleGuest)
  75. ->getRole($roleGuest->getRoleId());
  76. $this->assertTrue($roleGuest === $role);
  77. $role = $this->_acl->getRole($roleGuest);
  78. $this->assertTrue($roleGuest === $role);
  79. }
  80. /**
  81. * Ensures that basic removal of a single Role works
  82. *
  83. * @return void
  84. */
  85. public function testRoleRegistryRemoveOne()
  86. {
  87. $roleGuest = new Zend_Acl_Role('guest');
  88. $this->_acl->addRole($roleGuest)
  89. ->removeRole($roleGuest);
  90. $this->assertFalse($this->_acl->hasRole($roleGuest));
  91. }
  92. /**
  93. * Ensures that an exception is thrown when a non-existent Role is specified for removal
  94. *
  95. * @return void
  96. */
  97. public function testRoleRegistryRemoveOneNonExistent()
  98. {
  99. try {
  100. $this->_acl->removeRole('nonexistent');
  101. $this->fail('Expected Zend_Acl_Role_Registry_Exception not thrown upon removing a non-existent Role');
  102. } catch (Zend_Acl_Role_Registry_Exception $e) {
  103. $this->assertContains('not found', $e->getMessage());
  104. }
  105. }
  106. /**
  107. * Ensures that removal of all Roles works
  108. *
  109. * @return void
  110. */
  111. public function testRoleRegistryRemoveAll()
  112. {
  113. $roleGuest = new Zend_Acl_Role('guest');
  114. $this->_acl->addRole($roleGuest)
  115. ->removeRoleAll();
  116. $this->assertFalse($this->_acl->hasRole($roleGuest));
  117. }
  118. /**
  119. * Ensures that an exception is thrown when a non-existent Role is specified as a parent upon Role addition
  120. *
  121. * @return void
  122. */
  123. public function testRoleRegistryAddInheritsNonExistent()
  124. {
  125. try {
  126. $this->_acl->addRole(new Zend_Acl_Role('guest'), 'nonexistent');
  127. $this->fail('Expected Zend_Acl_Role_Registry_Exception not thrown upon specifying a non-existent parent');
  128. } catch (Zend_Acl_Role_Registry_Exception $e) {
  129. $this->assertContains('does not exist', $e->getMessage());
  130. }
  131. }
  132. /**
  133. * Ensures that an exception is thrown when a non-existent Role is specified to each parameter of inherits()
  134. *
  135. * @return void
  136. */
  137. public function testRoleRegistryInheritsNonExistent()
  138. {
  139. $roleGuest = new Zend_Acl_Role('guest');
  140. $this->_acl->addRole($roleGuest);
  141. try {
  142. $this->_acl->inheritsRole('nonexistent', $roleGuest);
  143. $this->fail('Expected Zend_Acl_Role_Registry_Exception not thrown upon specifying a non-existent child Role');
  144. } catch (Zend_Acl_Role_Registry_Exception $e) {
  145. $this->assertContains('not found', $e->getMessage());
  146. }
  147. try {
  148. $this->_acl->inheritsRole($roleGuest, 'nonexistent');
  149. $this->fail('Expected Zend_Acl_Role_Registry_Exception not thrown upon specifying a non-existent parent Role');
  150. } catch (Zend_Acl_Role_Registry_Exception $e) {
  151. $this->assertContains('not found', $e->getMessage());
  152. }
  153. }
  154. /**
  155. * Tests basic Role inheritance
  156. *
  157. * @return void
  158. */
  159. public function testRoleRegistryInherits()
  160. {
  161. $roleGuest = new Zend_Acl_Role('guest');
  162. $roleMember = new Zend_Acl_Role('member');
  163. $roleEditor = new Zend_Acl_Role('editor');
  164. $roleRegistry = new Zend_Acl_Role_Registry();
  165. $roleRegistry->add($roleGuest)
  166. ->add($roleMember, $roleGuest->getRoleId())
  167. ->add($roleEditor, $roleMember);
  168. $this->assertTrue(0 === count($roleRegistry->getParents($roleGuest)));
  169. $roleMemberParents = $roleRegistry->getParents($roleMember);
  170. $this->assertTrue(1 === count($roleMemberParents));
  171. $this->assertTrue(isset($roleMemberParents['guest']));
  172. $roleEditorParents = $roleRegistry->getParents($roleEditor);
  173. $this->assertTrue(1 === count($roleEditorParents));
  174. $this->assertTrue(isset($roleEditorParents['member']));
  175. $this->assertTrue($roleRegistry->inherits($roleMember, $roleGuest, true));
  176. $this->assertTrue($roleRegistry->inherits($roleEditor, $roleMember, true));
  177. $this->assertTrue($roleRegistry->inherits($roleEditor, $roleGuest));
  178. $this->assertFalse($roleRegistry->inherits($roleGuest, $roleMember));
  179. $this->assertFalse($roleRegistry->inherits($roleMember, $roleEditor));
  180. $this->assertFalse($roleRegistry->inherits($roleGuest, $roleEditor));
  181. $roleRegistry->remove($roleMember);
  182. $this->assertTrue(0 === count($roleRegistry->getParents($roleEditor)));
  183. $this->assertFalse($roleRegistry->inherits($roleEditor, $roleGuest));
  184. }
  185. /**
  186. * Tests basic Role multiple inheritance
  187. *
  188. * @return void
  189. */
  190. public function testRoleRegistryInheritsMultiple()
  191. {
  192. $roleParent1 = new Zend_Acl_Role('parent1');
  193. $roleParent2 = new Zend_Acl_Role('parent2');
  194. $roleChild = new Zend_Acl_Role('child');
  195. $roleRegistry = new Zend_Acl_Role_Registry();
  196. $roleRegistry->add($roleParent1)
  197. ->add($roleParent2)
  198. ->add($roleChild, array($roleParent1, $roleParent2));
  199. $roleChildParents = $roleRegistry->getParents($roleChild);
  200. $this->assertTrue(2 === count($roleChildParents));
  201. $i = 1;
  202. foreach ($roleChildParents as $roleParentId => $roleParent) {
  203. $this->assertTrue("parent$i" === $roleParentId);
  204. $i++;
  205. }
  206. $this->assertTrue($roleRegistry->inherits($roleChild, $roleParent1));
  207. $this->assertTrue($roleRegistry->inherits($roleChild, $roleParent2));
  208. $roleRegistry->remove($roleParent1);
  209. $roleChildParents = $roleRegistry->getParents($roleChild);
  210. $this->assertTrue(1 === count($roleChildParents));
  211. $this->assertTrue(isset($roleChildParents['parent2']));
  212. $this->assertTrue($roleRegistry->inherits($roleChild, $roleParent2));
  213. }
  214. /**
  215. * Ensures that the same Role cannot be registered more than once to the registry
  216. *
  217. * @return void
  218. */
  219. public function testRoleRegistryDuplicate()
  220. {
  221. $roleGuest = new Zend_Acl_Role('guest');
  222. $roleRegistry = new Zend_Acl_Role_Registry();
  223. try {
  224. $roleRegistry->add($roleGuest)
  225. ->add($roleGuest);
  226. $this->fail('Expected exception not thrown upon adding same Role twice');
  227. } catch (Zend_Acl_Role_Registry_Exception $e) {
  228. $this->assertContains('already exists', $e->getMessage());
  229. }
  230. }
  231. /**
  232. * Ensures that two Roles having the same ID cannot be registered
  233. *
  234. * @return void
  235. */
  236. public function testRoleRegistryDuplicateId()
  237. {
  238. $roleGuest1 = new Zend_Acl_Role('guest');
  239. $roleGuest2 = new Zend_Acl_Role('guest');
  240. $roleRegistry = new Zend_Acl_Role_Registry();
  241. try {
  242. $roleRegistry->add($roleGuest1)
  243. ->add($roleGuest2);
  244. $this->fail('Expected exception not thrown upon adding two Roles with same ID');
  245. } catch (Zend_Acl_Role_Registry_Exception $e) {
  246. $this->assertContains('already exists', $e->getMessage());
  247. }
  248. }
  249. /**
  250. * Ensures that basic addition and retrieval of a single Resource works
  251. *
  252. * @return void
  253. */
  254. public function testResourceAddAndGetOne()
  255. {
  256. $resourceArea = new Zend_Acl_Resource('area');
  257. $resource = $this->_acl->add($resourceArea)
  258. ->get($resourceArea->getResourceId());
  259. $this->assertTrue($resourceArea === $resource);
  260. $resource = $this->_acl->get($resourceArea);
  261. $this->assertTrue($resourceArea === $resource);
  262. }
  263. /**
  264. * Ensures that basic removal of a single Resource works
  265. *
  266. * @return void
  267. */
  268. public function testResourceRemoveOne()
  269. {
  270. $resourceArea = new Zend_Acl_Resource('area');
  271. $this->_acl->add($resourceArea)
  272. ->remove($resourceArea);
  273. $this->assertFalse($this->_acl->has($resourceArea));
  274. }
  275. /**
  276. * Ensures that an exception is thrown when a non-existent Resource is specified for removal
  277. *
  278. * @return void
  279. */
  280. public function testResourceRemoveOneNonExistent()
  281. {
  282. try {
  283. $this->_acl->remove('nonexistent');
  284. $this->fail('Expected Zend_Acl_Exception not thrown upon removing a non-existent Resource');
  285. } catch (Zend_Acl_Exception $e) {
  286. $this->assertContains('not found', $e->getMessage());
  287. }
  288. }
  289. /**
  290. * Ensures that removal of all Resources works
  291. *
  292. * @return void
  293. */
  294. public function testResourceRemoveAll()
  295. {
  296. $resourceArea = new Zend_Acl_Resource('area');
  297. $this->_acl->add($resourceArea)
  298. ->removeAll();
  299. $this->assertFalse($this->_acl->has($resourceArea));
  300. }
  301. /**
  302. * Ensures that an exception is thrown when a non-existent Resource is specified as a parent upon Resource addition
  303. *
  304. * @return void
  305. */
  306. public function testResourceAddInheritsNonExistent()
  307. {
  308. try {
  309. $this->_acl->add(new Zend_Acl_Resource('area'), 'nonexistent');
  310. $this->fail('Expected Zend_Acl_Exception not thrown upon specifying a non-existent parent');
  311. } catch (Zend_Acl_Exception $e) {
  312. $this->assertContains('does not exist', $e->getMessage());
  313. }
  314. }
  315. /**
  316. * Ensures that an exception is thrown when a non-existent Resource is specified to each parameter of inherits()
  317. *
  318. * @return void
  319. */
  320. public function testResourceInheritsNonExistent()
  321. {
  322. $resourceArea = new Zend_Acl_Resource('area');
  323. $this->_acl->add($resourceArea);
  324. try {
  325. $this->_acl->inherits('nonexistent', $resourceArea);
  326. $this->fail('Expected Zend_Acl_Exception not thrown upon specifying a non-existent child Resource');
  327. } catch (Zend_Acl_Exception $e) {
  328. $this->assertContains('not found', $e->getMessage());
  329. }
  330. try {
  331. $this->_acl->inherits($resourceArea, 'nonexistent');
  332. $this->fail('Expected Zend_Acl_Exception not thrown upon specifying a non-existent parent Resource');
  333. } catch (Zend_Acl_Exception $e) {
  334. $this->assertContains('not found', $e->getMessage());
  335. }
  336. }
  337. /**
  338. * Tests basic Resource inheritance
  339. *
  340. * @return void
  341. */
  342. public function testResourceInherits()
  343. {
  344. $resourceCity = new Zend_Acl_Resource('city');
  345. $resourceBuilding = new Zend_Acl_Resource('building');
  346. $resourceRoom = new Zend_Acl_Resource('room');
  347. $this->_acl->add($resourceCity)
  348. ->add($resourceBuilding, $resourceCity->getResourceId())
  349. ->add($resourceRoom, $resourceBuilding);
  350. $this->assertTrue($this->_acl->inherits($resourceBuilding, $resourceCity, true));
  351. $this->assertTrue($this->_acl->inherits($resourceRoom, $resourceBuilding, true));
  352. $this->assertTrue($this->_acl->inherits($resourceRoom, $resourceCity));
  353. $this->assertFalse($this->_acl->inherits($resourceCity, $resourceBuilding));
  354. $this->assertFalse($this->_acl->inherits($resourceBuilding, $resourceRoom));
  355. $this->assertFalse($this->_acl->inherits($resourceCity, $resourceRoom));
  356. $this->_acl->remove($resourceBuilding);
  357. $this->assertFalse($this->_acl->has($resourceRoom));
  358. }
  359. /**
  360. * Ensures that the same Resource cannot be added more than once
  361. *
  362. * @return void
  363. */
  364. public function testResourceDuplicate()
  365. {
  366. try {
  367. $resourceArea = new Zend_Acl_Resource('area');
  368. $this->_acl->add($resourceArea)
  369. ->add($resourceArea);
  370. $this->fail('Expected exception not thrown upon adding same Resource twice');
  371. } catch (Zend_Acl_Exception $e) {
  372. $this->assertContains('already exists', $e->getMessage());
  373. }
  374. }
  375. /**
  376. * Ensures that two Resources having the same ID cannot be added
  377. *
  378. * @return void
  379. */
  380. public function testResourceDuplicateId()
  381. {
  382. try {
  383. $resourceArea1 = new Zend_Acl_Resource('area');
  384. $resourceArea2 = new Zend_Acl_Resource('area');
  385. $this->_acl->add($resourceArea1)
  386. ->add($resourceArea2);
  387. $this->fail('Expected exception not thrown upon adding two Resources with same ID');
  388. } catch (Zend_Acl_Exception $e) {
  389. $this->assertContains('already exists', $e->getMessage());
  390. }
  391. }
  392. /**
  393. * Ensures that an exception is thrown when a non-existent Role and Resource parameters are specified to isAllowed()
  394. *
  395. * @return void
  396. */
  397. public function testIsAllowedNonExistent()
  398. {
  399. try {
  400. $this->_acl->isAllowed('nonexistent');
  401. $this->fail('Expected Zend_Acl_Role_Registry_Exception not thrown upon non-existent Role');
  402. } catch (Zend_Acl_Role_Registry_Exception $e) {
  403. $this->assertContains('not found', $e->getMessage());
  404. }
  405. try {
  406. $this->_acl->isAllowed(null, 'nonexistent');
  407. $this->fail('Expected Zend_Acl_Exception not thrown upon non-existent Resource');
  408. } catch (Zend_Acl_Exception $e) {
  409. $this->assertContains('not found', $e->getMessage());
  410. }
  411. }
  412. /**
  413. * Ensures that by default, Zend_Acl denies access to everything by all
  414. *
  415. * @return void
  416. */
  417. public function testDefaultDeny()
  418. {
  419. $this->assertFalse($this->_acl->isAllowed());
  420. }
  421. /**
  422. * Ensures that the default rule obeys its assertion
  423. *
  424. * @return void
  425. */
  426. public function testDefaultAssert()
  427. {
  428. $this->_acl->deny(null, null, null, new Zend_Acl_MockAssertion(false));
  429. $this->assertTrue($this->_acl->isAllowed());
  430. $this->assertTrue($this->_acl->isAllowed(null, null, 'somePrivilege'));
  431. }
  432. /**
  433. * Ensures that ACL-wide rules (all Roles, Resources, and privileges) work properly
  434. *
  435. * @return void
  436. */
  437. public function testDefaultRuleSet()
  438. {
  439. $this->_acl->allow();
  440. $this->assertTrue($this->_acl->isAllowed());
  441. $this->_acl->deny();
  442. $this->assertFalse($this->_acl->isAllowed());
  443. }
  444. /**
  445. * Ensures that by default, Zend_Acl denies access to a privilege on anything by all
  446. *
  447. * @return void
  448. */
  449. public function testDefaultPrivilegeDeny()
  450. {
  451. $this->assertFalse($this->_acl->isAllowed(null, null, 'somePrivilege'));
  452. }
  453. /**
  454. * Ensures that ACL-wide rules apply to privileges
  455. *
  456. * @return void
  457. */
  458. public function testDefaultRuleSetPrivilege()
  459. {
  460. $this->_acl->allow();
  461. $this->assertTrue($this->_acl->isAllowed(null, null, 'somePrivilege'));
  462. $this->_acl->deny();
  463. $this->assertFalse($this->_acl->isAllowed(null, null, 'somePrivilege'));
  464. }
  465. /**
  466. * Ensures that a privilege allowed for all Roles upon all Resources works properly
  467. *
  468. * @return void
  469. */
  470. public function testPrivilegeAllow()
  471. {
  472. $this->_acl->allow(null, null, 'somePrivilege');
  473. $this->assertTrue($this->_acl->isAllowed(null, null, 'somePrivilege'));
  474. }
  475. /**
  476. * Ensures that a privilege denied for all Roles upon all Resources works properly
  477. *
  478. * @return void
  479. */
  480. public function testPrivilegeDeny()
  481. {
  482. $this->_acl->allow();
  483. $this->_acl->deny(null, null, 'somePrivilege');
  484. $this->assertFalse($this->_acl->isAllowed(null, null, 'somePrivilege'));
  485. }
  486. /**
  487. * Ensures that multiple privileges work properly
  488. *
  489. * @return void
  490. */
  491. public function testPrivileges()
  492. {
  493. $this->_acl->allow(null, null, array('p1', 'p2', 'p3'));
  494. $this->assertTrue($this->_acl->isAllowed(null, null, 'p1'));
  495. $this->assertTrue($this->_acl->isAllowed(null, null, 'p2'));
  496. $this->assertTrue($this->_acl->isAllowed(null, null, 'p3'));
  497. $this->assertFalse($this->_acl->isAllowed(null, null, 'p4'));
  498. $this->_acl->deny(null, null, 'p1');
  499. $this->assertFalse($this->_acl->isAllowed(null, null, 'p1'));
  500. $this->_acl->deny(null, null, array('p2', 'p3'));
  501. $this->assertFalse($this->_acl->isAllowed(null, null, 'p2'));
  502. $this->assertFalse($this->_acl->isAllowed(null, null, 'p3'));
  503. }
  504. /**
  505. * Ensures that assertions on privileges work properly
  506. *
  507. * @return void
  508. */
  509. public function testPrivilegeAssert()
  510. {
  511. $this->_acl->allow(null, null, 'somePrivilege', new Zend_Acl_MockAssertion(true));
  512. $this->assertTrue($this->_acl->isAllowed(null, null, 'somePrivilege'));
  513. $this->_acl->allow(null, null, 'somePrivilege', new Zend_Acl_MockAssertion(false));
  514. $this->assertFalse($this->_acl->isAllowed(null, null, 'somePrivilege'));
  515. }
  516. /**
  517. * Ensures that by default, Zend_Acl denies access to everything for a particular Role
  518. *
  519. * @return void
  520. */
  521. public function testRoleDefaultDeny()
  522. {
  523. $roleGuest = new Zend_Acl_Role('guest');
  524. $this->_acl->addRole($roleGuest);
  525. $this->assertFalse($this->_acl->isAllowed($roleGuest));
  526. }
  527. /**
  528. * Ensures that ACL-wide rules (all Resources and privileges) work properly for a particular Role
  529. *
  530. * @return void
  531. */
  532. public function testRoleDefaultRuleSet()
  533. {
  534. $roleGuest = new Zend_Acl_Role('guest');
  535. $this->_acl->addRole($roleGuest)
  536. ->allow($roleGuest);
  537. $this->assertTrue($this->_acl->isAllowed($roleGuest));
  538. $this->_acl->deny($roleGuest);
  539. $this->assertFalse($this->_acl->isAllowed($roleGuest));
  540. }
  541. /**
  542. * Ensures that by default, Zend_Acl denies access to a privilege on anything for a particular Role
  543. *
  544. * @return void
  545. */
  546. public function testRoleDefaultPrivilegeDeny()
  547. {
  548. $roleGuest = new Zend_Acl_Role('guest');
  549. $this->_acl->addRole($roleGuest);
  550. $this->assertFalse($this->_acl->isAllowed($roleGuest, null, 'somePrivilege'));
  551. }
  552. /**
  553. * Ensures that ACL-wide rules apply to privileges for a particular Role
  554. *
  555. * @return void
  556. */
  557. public function testRoleDefaultRuleSetPrivilege()
  558. {
  559. $roleGuest = new Zend_Acl_Role('guest');
  560. $this->_acl->addRole($roleGuest)
  561. ->allow($roleGuest);
  562. $this->assertTrue($this->_acl->isAllowed($roleGuest, null, 'somePrivilege'));
  563. $this->_acl->deny($roleGuest);
  564. $this->assertFalse($this->_acl->isAllowed($roleGuest, null, 'somePrivilege'));
  565. }
  566. /**
  567. * Ensures that a privilege allowed for a particular Role upon all Resources works properly
  568. *
  569. * @return void
  570. */
  571. public function testRolePrivilegeAllow()
  572. {
  573. $roleGuest = new Zend_Acl_Role('guest');
  574. $this->_acl->addRole($roleGuest)
  575. ->allow($roleGuest, null, 'somePrivilege');
  576. $this->assertTrue($this->_acl->isAllowed($roleGuest, null, 'somePrivilege'));
  577. }
  578. /**
  579. * Ensures that a privilege denied for a particular Role upon all Resources works properly
  580. *
  581. * @return void
  582. */
  583. public function testRolePrivilegeDeny()
  584. {
  585. $roleGuest = new Zend_Acl_Role('guest');
  586. $this->_acl->addRole($roleGuest)
  587. ->allow($roleGuest)
  588. ->deny($roleGuest, null, 'somePrivilege');
  589. $this->assertFalse($this->_acl->isAllowed($roleGuest, null, 'somePrivilege'));
  590. }
  591. /**
  592. * Ensures that multiple privileges work properly for a particular Role
  593. *
  594. * @return void
  595. */
  596. public function testRolePrivileges()
  597. {
  598. $roleGuest = new Zend_Acl_Role('guest');
  599. $this->_acl->addRole($roleGuest)
  600. ->allow($roleGuest, null, array('p1', 'p2', 'p3'));
  601. $this->assertTrue($this->_acl->isAllowed($roleGuest, null, 'p1'));
  602. $this->assertTrue($this->_acl->isAllowed($roleGuest, null, 'p2'));
  603. $this->assertTrue($this->_acl->isAllowed($roleGuest, null, 'p3'));
  604. $this->assertFalse($this->_acl->isAllowed($roleGuest, null, 'p4'));
  605. $this->_acl->deny($roleGuest, null, 'p1');
  606. $this->assertFalse($this->_acl->isAllowed($roleGuest, null, 'p1'));
  607. $this->_acl->deny($roleGuest, null, array('p2', 'p3'));
  608. $this->assertFalse($this->_acl->isAllowed($roleGuest, null, 'p2'));
  609. $this->assertFalse($this->_acl->isAllowed($roleGuest, null, 'p3'));
  610. }
  611. /**
  612. * Ensures that assertions on privileges work properly for a particular Role
  613. *
  614. * @return void
  615. */
  616. public function testRolePrivilegeAssert()
  617. {
  618. $roleGuest = new Zend_Acl_Role('guest');
  619. $this->_acl->addRole($roleGuest)
  620. ->allow($roleGuest, null, 'somePrivilege', new Zend_Acl_MockAssertion(true));
  621. $this->assertTrue($this->_acl->isAllowed($roleGuest, null, 'somePrivilege'));
  622. $this->_acl->allow($roleGuest, null, 'somePrivilege', new Zend_Acl_MockAssertion(false));
  623. $this->assertFalse($this->_acl->isAllowed($roleGuest, null, 'somePrivilege'));
  624. }
  625. /**
  626. * Ensures that removing the default deny rule results in default deny rule
  627. *
  628. * @return void
  629. */
  630. public function testRemoveDefaultDeny()
  631. {
  632. $this->assertFalse($this->_acl->isAllowed());
  633. $this->_acl->removeDeny();
  634. $this->assertFalse($this->_acl->isAllowed());
  635. }
  636. /**
  637. * Ensures that removing the default deny rule results in assertion method being removed
  638. *
  639. * @return void
  640. */
  641. public function testRemoveDefaultDenyAssert()
  642. {
  643. $this->_acl->deny(null, null, null, new Zend_Acl_MockAssertion(false));
  644. $this->assertTrue($this->_acl->isAllowed());
  645. $this->_acl->removeDeny();
  646. $this->assertFalse($this->_acl->isAllowed());
  647. }
  648. /**
  649. * Ensures that removing the default allow rule results in default deny rule being assigned
  650. *
  651. * @return void
  652. */
  653. public function testRemoveDefaultAllow()
  654. {
  655. $this->_acl->allow();
  656. $this->assertTrue($this->_acl->isAllowed());
  657. $this->_acl->removeAllow();
  658. $this->assertFalse($this->_acl->isAllowed());
  659. }
  660. /**
  661. * Ensures that removing non-existent default allow rule does nothing
  662. *
  663. * @return void
  664. */
  665. public function testRemoveDefaultAllowNonExistent()
  666. {
  667. $this->_acl->removeAllow();
  668. $this->assertFalse($this->_acl->isAllowed());
  669. }
  670. /**
  671. * Ensures that removing non-existent default deny rule does nothing
  672. *
  673. * @return void
  674. */
  675. public function testRemoveDefaultDenyNonExistent()
  676. {
  677. $this->_acl->allow()
  678. ->removeDeny();
  679. $this->assertTrue($this->_acl->isAllowed());
  680. }
  681. /**
  682. * Ensures that for a particular Role, a deny rule on a specific Resource is honored before an allow rule
  683. * on the entire ACL
  684. *
  685. * @return void
  686. */
  687. public function testRoleDefaultAllowRuleWithResourceDenyRule()
  688. {
  689. $this->_acl->addRole(new Zend_Acl_Role('guest'))
  690. ->addRole(new Zend_Acl_Role('staff'), 'guest')
  691. ->add(new Zend_Acl_Resource('area1'))
  692. ->add(new Zend_Acl_Resource('area2'))
  693. ->deny()
  694. ->allow('staff')
  695. ->deny('staff', array('area1', 'area2'));
  696. $this->assertFalse($this->_acl->isAllowed('staff', 'area1'));
  697. }
  698. /**
  699. * Ensures that for a particular Role, a deny rule on a specific privilege is honored before an allow
  700. * rule on the entire ACL
  701. *
  702. * @return void
  703. */
  704. public function testRoleDefaultAllowRuleWithPrivilegeDenyRule()
  705. {
  706. $this->_acl->addRole(new Zend_Acl_Role('guest'))
  707. ->addRole(new Zend_Acl_Role('staff'), 'guest')
  708. ->deny()
  709. ->allow('staff')
  710. ->deny('staff', null, array('privilege1', 'privilege2'));
  711. $this->assertFalse($this->_acl->isAllowed('staff', null, 'privilege1'));
  712. }
  713. /**
  714. * Ensure that basic rule removal works
  715. *
  716. * @return void
  717. */
  718. public function testRulesRemove()
  719. {
  720. $this->_acl->allow(null, null, array('privilege1', 'privilege2'));
  721. $this->assertFalse($this->_acl->isAllowed());
  722. $this->assertTrue($this->_acl->isAllowed(null, null, 'privilege1'));
  723. $this->assertTrue($this->_acl->isAllowed(null, null, 'privilege2'));
  724. $this->_acl->removeAllow(null, null, 'privilege1');
  725. $this->assertFalse($this->_acl->isAllowed(null, null, 'privilege1'));
  726. $this->assertTrue($this->_acl->isAllowed(null, null, 'privilege2'));
  727. }
  728. /**
  729. * Ensures that removal of a Role results in its rules being removed
  730. *
  731. * @return void
  732. */
  733. public function testRuleRoleRemove()
  734. {
  735. $this->_acl->addRole(new Zend_Acl_Role('guest'))
  736. ->allow('guest');
  737. $this->assertTrue($this->_acl->isAllowed('guest'));
  738. $this->_acl->removeRole('guest');
  739. try {
  740. $this->_acl->isAllowed('guest');
  741. $this->fail('Expected Zend_Acl_Role_Registry_Exception not thrown upon isAllowed() on non-existent Role');
  742. } catch (Zend_Acl_Role_Registry_Exception $e) {
  743. $this->assertContains('not found', $e->getMessage());
  744. }
  745. $this->_acl->addRole(new Zend_Acl_Role('guest'));
  746. $this->assertFalse($this->_acl->isAllowed('guest'));
  747. }
  748. /**
  749. * Ensures that removal of all Roles results in Role-specific rules being removed
  750. *
  751. * @return void
  752. */
  753. public function testRuleRoleRemoveAll()
  754. {
  755. $this->_acl->addRole(new Zend_Acl_Role('guest'))
  756. ->allow('guest');
  757. $this->assertTrue($this->_acl->isAllowed('guest'));
  758. $this->_acl->removeRoleAll();
  759. try {
  760. $this->_acl->isAllowed('guest');
  761. $this->fail('Expected Zend_Acl_Role_Registry_Exception not thrown upon isAllowed() on non-existent Role');
  762. } catch (Zend_Acl_Role_Registry_Exception $e) {
  763. $this->assertContains('not found', $e->getMessage());
  764. }
  765. $this->_acl->addRole(new Zend_Acl_Role('guest'));
  766. $this->assertFalse($this->_acl->isAllowed('guest'));
  767. }
  768. /**
  769. * Ensures that removal of a Resource results in its rules being removed
  770. *
  771. * @return void
  772. */
  773. public function testRulesResourceRemove()
  774. {
  775. $this->_acl->add(new Zend_Acl_Resource('area'))
  776. ->allow(null, 'area');
  777. $this->assertTrue($this->_acl->isAllowed(null, 'area'));
  778. $this->_acl->remove('area');
  779. try {
  780. $this->_acl->isAllowed(null, 'area');
  781. $this->fail('Expected Zend_Acl_Exception not thrown upon isAllowed() on non-existent Resource');
  782. } catch (Zend_Acl_Exception $e) {
  783. $this->assertContains('not found', $e->getMessage());
  784. }
  785. $this->_acl->add(new Zend_Acl_Resource('area'));
  786. $this->assertFalse($this->_acl->isAllowed(null, 'area'));
  787. }
  788. /**
  789. * Ensures that removal of all Resources results in Resource-specific rules being removed
  790. *
  791. * @return void
  792. */
  793. public function testRulesResourceRemoveAll()
  794. {
  795. $this->_acl->add(new Zend_Acl_Resource('area'))
  796. ->allow(null, 'area');
  797. $this->assertTrue($this->_acl->isAllowed(null, 'area'));
  798. $this->_acl->removeAll();
  799. try {
  800. $this->_acl->isAllowed(null, 'area');
  801. $this->fail('Expected Zend_Acl_Exception not thrown upon isAllowed() on non-existent Resource');
  802. } catch (Zend_Acl_Exception $e) {
  803. $this->assertContains('not found', $e->getMessage());
  804. }
  805. $this->_acl->add(new Zend_Acl_Resource('area'));
  806. $this->assertFalse($this->_acl->isAllowed(null, 'area'));
  807. }
  808. /**
  809. * Ensures that an example for a content management system is operable
  810. *
  811. * @return void
  812. */
  813. public function testCMSExample()
  814. {
  815. // Add some roles to the Role registry
  816. $this->_acl->addRole(new Zend_Acl_Role('guest'))
  817. ->addRole(new Zend_Acl_Role('staff'), 'guest') // staff inherits permissions from guest
  818. ->addRole(new Zend_Acl_Role('editor'), 'staff') // editor inherits permissions from staff
  819. ->addRole(new Zend_Acl_Role('administrator'));
  820. // Guest may only view content
  821. $this->_acl->allow('guest', null, 'view');
  822. // Staff inherits view privilege from guest, but also needs additional privileges
  823. $this->_acl->allow('staff', null, array('edit', 'submit', 'revise'));
  824. // Editor inherits view, edit, submit, and revise privileges, but also needs additional privileges
  825. $this->_acl->allow('editor', null, array('publish', 'archive', 'delete'));
  826. // Administrator inherits nothing but is allowed all privileges
  827. $this->_acl->allow('administrator');
  828. // Access control checks based on above permission sets
  829. $this->assertTrue($this->_acl->isAllowed('guest', null, 'view'));
  830. $this->assertFalse($this->_acl->isAllowed('guest', null, 'edit'));
  831. $this->assertFalse($this->_acl->isAllowed('guest', null, 'submit'));
  832. $this->assertFalse($this->_acl->isAllowed('guest', null, 'revise'));
  833. $this->assertFalse($this->_acl->isAllowed('guest', null, 'publish'));
  834. $this->assertFalse($this->_acl->isAllowed('guest', null, 'archive'));
  835. $this->assertFalse($this->_acl->isAllowed('guest', null, 'delete'));
  836. $this->assertFalse($this->_acl->isAllowed('guest', null, 'unknown'));
  837. $this->assertFalse($this->_acl->isAllowed('guest'));
  838. $this->assertTrue($this->_acl->isAllowed('staff', null, 'view'));
  839. $this->assertTrue($this->_acl->isAllowed('staff', null, 'edit'));
  840. $this->assertTrue($this->_acl->isAllowed('staff', null, 'submit'));
  841. $this->assertTrue($this->_acl->isAllowed('staff', null, 'revise'));
  842. $this->assertFalse($this->_acl->isAllowed('staff', null, 'publish'));
  843. $this->assertFalse($this->_acl->isAllowed('staff', null, 'archive'));
  844. $this->assertFalse($this->_acl->isAllowed('staff', null, 'delete'));
  845. $this->assertFalse($this->_acl->isAllowed('staff', null, 'unknown'));
  846. $this->assertFalse($this->_acl->isAllowed('staff'));
  847. $this->assertTrue($this->_acl->isAllowed('editor', null, 'view'));
  848. $this->assertTrue($this->_acl->isAllowed('editor', null, 'edit'));
  849. $this->assertTrue($this->_acl->isAllowed('editor', null, 'submit'));
  850. $this->assertTrue($this->_acl->isAllowed('editor', null, 'revise'));
  851. $this->assertTrue($this->_acl->isAllowed('editor', null, 'publish'));
  852. $this->assertTrue($this->_acl->isAllowed('editor', null, 'archive'));
  853. $this->assertTrue($this->_acl->isAllowed('editor', null, 'delete'));
  854. $this->assertFalse($this->_acl->isAllowed('editor', null, 'unknown'));
  855. $this->assertFalse($this->_acl->isAllowed('editor'));
  856. $this->assertTrue($this->_acl->isAllowed('administrator', null, 'view'));
  857. $this->assertTrue($this->_acl->isAllowed('administrator', null, 'edit'));
  858. $this->assertTrue($this->_acl->isAllowed('administrator', null, 'submit'));
  859. $this->assertTrue($this->_acl->isAllowed('administrator', null, 'revise'));
  860. $this->assertTrue($this->_acl->isAllowed('administrator', null, 'publish'));
  861. $this->assertTrue($this->_acl->isAllowed('administrator', null, 'archive'));
  862. $this->assertTrue($this->_acl->isAllowed('administrator', null, 'delete'));
  863. $this->assertTrue($this->_acl->isAllowed('administrator', null, 'unknown'));
  864. $this->assertTrue($this->_acl->isAllowed('administrator'));
  865. // Some checks on specific areas, which inherit access controls from the root ACL node
  866. $this->_acl->add(new Zend_Acl_Resource('newsletter'))
  867. ->add(new Zend_Acl_Resource('pending'), 'newsletter')
  868. ->add(new Zend_Acl_Resource('gallery'))
  869. ->add(new Zend_Acl_Resource('profiles', 'gallery'))
  870. ->add(new Zend_Acl_Resource('config'))
  871. ->add(new Zend_Acl_Resource('hosts'), 'config');
  872. $this->assertTrue($this->_acl->isAllowed('guest', 'pending', 'view'));
  873. $this->assertTrue($this->_acl->isAllowed('staff', 'profiles', 'revise'));
  874. $this->assertTrue($this->_acl->isAllowed('staff', 'pending', 'view'));
  875. $this->assertTrue($this->_acl->isAllowed('staff', 'pending', 'edit'));
  876. $this->assertFalse($this->_acl->isAllowed('staff', 'pending', 'publish'));
  877. $this->assertFalse($this->_acl->isAllowed('staff', 'pending'));
  878. $this->assertFalse($this->_acl->isAllowed('editor', 'hosts', 'unknown'));
  879. $this->assertTrue($this->_acl->isAllowed('administrator', 'pending'));
  880. // Add a new group, marketing, which bases its permissions on staff
  881. $this->_acl->addRole(new Zend_Acl_Role('marketing'), 'staff');
  882. // Refine the privilege sets for more specific needs
  883. // Allow marketing to publish and archive newsletters
  884. $this->_acl->allow('marketing', 'newsletter', array('publish', 'archive'));
  885. // Allow marketing to publish and archive latest news
  886. $this->_acl->add(new Zend_Acl_Resource('news'))
  887. ->add(new Zend_Acl_Resource('latest'), 'news');
  888. $this->_acl->allow('marketing', 'latest', array('publish', 'archive'));
  889. // Deny staff (and marketing, by inheritance) rights to revise latest news
  890. $this->_acl->deny('staff', 'latest', 'revise');
  891. // Deny everyone access to archive news announcements
  892. $this->_acl->add(new Zend_Acl_Resource('announcement'), 'news');
  893. $this->_acl->deny(null, 'announcement', 'archive');
  894. // Access control checks for the above refined permission sets
  895. $this->assertTrue($this->_acl->isAllowed('marketing', null, 'view'));
  896. $this->assertTrue($this->_acl->isAllowed('marketing', null, 'edit'));
  897. $this->assertTrue($this->_acl->isAllowed('marketing', null, 'submit'));
  898. $this->assertTrue($this->_acl->isAllowed('marketing', null, 'revise'));
  899. $this->assertFalse($this->_acl->isAllowed('marketing', null, 'publish'));
  900. $this->assertFalse($this->_acl->isAllowed('marketing', null, 'archive'));
  901. $this->assertFalse($this->_acl->isAllowed('marketing', null, 'delete'));
  902. $this->assertFalse($this->_acl->isAllowed('marketing', null, 'unknown'));
  903. $this->assertFalse($this->_acl->isAllowed('marketing'));
  904. $this->assertTrue($this->_acl->isAllowed('marketing', 'newsletter', 'publish'));
  905. $this->assertFalse($this->_acl->isAllowed('staff', 'pending', 'publish'));
  906. $this->assertTrue($this->_acl->isAllowed('marketing', 'pending', 'publish'));
  907. $this->assertTrue($this->_acl->isAllowed('marketing', 'newsletter', 'archive'));
  908. $this->assertFalse($this->_acl->isAllowed('marketing', 'newsletter', 'delete'));
  909. $this->assertFalse($this->_acl->isAllowed('marketing', 'newsletter'));
  910. $this->assertTrue($this->_acl->isAllowed('marketing', 'latest', 'publish'));
  911. $this->assertTrue($this->_acl->isAllowed('marketing', 'latest', 'archive'));
  912. $this->assertFalse($this->_acl->isAllowed('marketing', 'latest', 'delete'));
  913. $this->assertFalse($this->_acl->isAllowed('marketing', 'latest', 'revise'));
  914. $this->assertFalse($this->_acl->isAllowed('marketing', 'latest'));
  915. $this->assertFalse($this->_acl->isAllowed('marketing', 'announcement', 'archive'));
  916. $this->assertFalse($this->_acl->isAllowed('staff', 'announcement', 'archive'));
  917. $this->assertFalse($this->_acl->isAllowed('administrator', 'announcement', 'archive'));
  918. $this->assertFalse($this->_acl->isAllowed('staff', 'latest', 'publish'));
  919. $this->assertFalse($this->_acl->isAllowed('editor', 'announcement', 'archive'));
  920. // Remove some previous permission specifications
  921. // Marketing can no longer publish and archive newsletters
  922. $this->_acl->removeAllow('marketing', 'newsletter', array('publish', 'archive'));
  923. // Marketing can no longer archive the latest news
  924. $this->_acl->removeAllow('marketing', 'latest', 'archive');
  925. // Now staff (and marketing, by inheritance) may revise latest news
  926. $this->_acl->removeDeny('staff', 'latest', 'revise');
  927. // Access control checks for the above refinements
  928. $this->assertFalse($this->_acl->isAllowed('marketing', 'newsletter', 'publish'));
  929. $this->assertFalse($this->_acl->isAllowed('marketing', 'newsletter', 'archive'));
  930. $this->assertFalse($this->_acl->isAllowed('marketing', 'latest', 'archive'));
  931. $this->assertTrue($this->_acl->isAllowed('staff', 'latest', 'revise'));
  932. $this->assertTrue($this->_acl->isAllowed('marketing', 'latest', 'revise'));
  933. // Grant marketing all permissions on the latest news
  934. $this->_acl->allow('marketing', 'latest');
  935. // Access control checks for the above refinement
  936. $this->assertTrue($this->_acl->isAllowed('marketing', 'latest', 'archive'));
  937. $this->assertTrue($this->_acl->isAllowed('marketing', 'latest', 'publish'));
  938. $this->assertTrue($this->_acl->isAllowed('marketing', 'latest', 'edit'));
  939. $this->assertTrue($this->_acl->isAllowed('marketing', 'latest'));
  940. }
  941. /**
  942. * Ensures that the $onlyParents argument to inheritsRole() works
  943. *
  944. * @return void
  945. * @see http://framework.zend.com/issues/browse/ZF-2502
  946. */
  947. public function testRoleInheritanceSupportsCheckingOnlyParents()
  948. {
  949. $this->_acl->addRole(new Zend_Acl_Role('grandparent'))
  950. ->addRole(new Zend_Acl_Role('parent'), 'grandparent')
  951. ->addRole(new Zend_Acl_Role('child'), 'parent');
  952. $this->assertFalse($this->_acl->inheritsRole('child', 'grandparent', true));
  953. }
  954. /**
  955. * Ensures that the solution for ZF-2234 works as expected
  956. *
  957. * @return void
  958. * @see http://framework.zend.com/issues/browse/ZF-2234
  959. */
  960. public function testAclInternalDFSMethodsBehaveProperly()
  961. {
  962. require_once dirname(__FILE__) . '/_files/ExtendedAclZF2234.php';
  963. $acl = new Zend_Acl_ExtendedAclZF2234();
  964. $someResource = new Zend_Acl_Resource('someResource');
  965. $someRole = new Zend_Acl_Role('someRole');
  966. $acl->add($someResource)
  967. ->addRole($someRole);
  968. $nullValue = null;
  969. $nullReference =& $nullValue;
  970. try {
  971. $acl->roleDFSVisitAllPrivileges($someRole, $someResource, $nullReference);
  972. $this->fail('Expected Zend_Acl_Exception not thrown');
  973. } catch (Zend_Acl_Exception $e) {
  974. $this->assertEquals('$dfs parameter may not be null', $e->getMessage());
  975. }
  976. try {
  977. $acl->roleDFSOnePrivilege($someRole, $someResource, null);
  978. $this->fail('Expected Zend_Acl_Exception not thrown');
  979. } catch (Zend_Acl_Exception $e) {
  980. $this->assertEquals('$privilege parameter may not be null', $e->getMessage());
  981. }
  982. try {
  983. $acl->roleDFSVisitOnePrivilege($someRole, $someResource, null);
  984. $this->fail('Expected Zend_Acl_Exception not thrown');
  985. } catch (Zend_Acl_Exception $e) {
  986. $this->assertEquals('$privilege parameter may not be null', $e->getMessage());
  987. }
  988. try {
  989. $acl->roleDFSVisitOnePrivilege($someRole, $someResource, 'somePrivilege', $nullReference);
  990. $this->fail('Expected Zend_Acl_Exception not thrown');
  991. } catch (Zend_Acl_Exception $e) {
  992. $this->assertEquals('$dfs parameter may not be null', $e->getMessage());
  993. }
  994. }
  995. /**
  996. * @group ZF-1721
  997. */
  998. public function testAclAssertionsGetProperRoleWhenInheritenceIsUsed()
  999. {
  1000. $acl = $this->_loadUseCase1();
  1001. $user = new Zend_Acl_Role('publisher');
  1002. $blogPost = new Zend_Acl_Resource('blogPost');
  1003. /**
  1004. * @var Zend_Acl_UseCase1_UserIsBlogPostOwnerAssertion
  1005. */
  1006. $assertion = $acl->customAssertion;
  1007. $this->assertTrue($acl->isAllowed($user, $blogPost, 'modify'));
  1008. $this->assertEquals('publisher', $assertion->lastAssertRole->getRoleId());
  1009. }
  1010. /**
  1011. *
  1012. * @group ZF-1722
  1013. */
  1014. public function testAclAssertionsGetOriginalIsAllowedObjects()
  1015. {
  1016. $acl = $this->_loadUseCase1();
  1017. $user = new Zend_Acl_UseCase1_User();
  1018. $blogPost = new Zend_Acl_UseCase1_BlogPost();
  1019. $this->assertTrue($acl->isAllowed($user, $blogPost, 'view'));
  1020. /**
  1021. * @var Zend_Acl_UseCase1_UserIsBlogPostOwnerAssertion
  1022. */
  1023. $assertion = $acl->customAssertion;
  1024. $assertion->assertReturnValue = true;
  1025. $user->role = 'contributor';
  1026. $this->assertTrue($acl->isAllowed($user, $blogPost, 'modify'), 'Assertion should return true');
  1027. $assertion->assertReturnValue = false;
  1028. $this->assertFalse($acl->isAllowed($user, $blogPost, 'modify'), 'Assertion should return false');
  1029. // check to see if the last assertion has the proper objets
  1030. $this->assertType('Zend_Acl_UseCase1_User', $assertion->lastAssertRole, 'Assertion did not recieve proper role object');
  1031. $this->assertType('Zend_Acl_UseCase1_BlogPost', $assertion->lastAssertResource, 'Assertion did not recieve proper resource object');
  1032. }
  1033. /**
  1034. *
  1035. * @return Zend_Acl_UseCase1_Acl
  1036. */
  1037. protected function _loadUseCase1()
  1038. {
  1039. if (!class_exists('Zend_Acl_UseCase1_Acl')) {
  1040. require_once dirname(__FILE__) . '/_files/UseCase1/User.php';
  1041. require_once dirname(__FILE__) . '/_files/UseCase1/BlogPost.php';
  1042. require_once dirname(__FILE__) . '/_files/UseCase1/UserIsBlogPostOwnerAssertion.php';
  1043. require_once dirname(__FILE__) . '/_files/UseCase1/Acl.php';
  1044. }
  1045. return new Zend_Acl_UseCase1_Acl();
  1046. }
  1047. }