AclTest.php 45 KB

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