AclTest.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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. /**
  1082. * Returns an array of registered roles
  1083. * @issue ZF-5638
  1084. */
  1085. public function testGetRegisteredRoles()
  1086. {
  1087. $acl = $this->_acl;
  1088. $acl->addRole('developer');
  1089. $roles = $acl->getRegisteredRoles();
  1090. $this->assertTrue(is_array($roles));
  1091. $this->assertFalse(empty($roles));
  1092. }
  1093. /**
  1094. * Confirm that deleting a role after allowing access to all roles
  1095. * raise undefined index error
  1096. *
  1097. * @group ZF-5700
  1098. */
  1099. public function testRemovingRoleAfterItWasAllowedAccessToAllResourcesGivesError()
  1100. {
  1101. $acl = new Zend_Acl();
  1102. $acl->addRole(new Zend_Acl_Role('test0'));
  1103. $acl->addRole(new Zend_Acl_Role('test1'));
  1104. $acl->addRole(new Zend_Acl_Role('test2'));
  1105. $acl->addResource(new Zend_Acl_Resource('Test'));
  1106. $acl->allow(null,'Test','xxx');
  1107. // error test
  1108. $acl->removeRole('test0');
  1109. // Check after fix
  1110. $this->assertFalse($acl->hasRole('test0'));
  1111. }
  1112. /**
  1113. * @group ZF-8039
  1114. *
  1115. * Meant to test for the (in)existance of this notice:
  1116. * "Notice: Undefined index: allPrivileges in lib/Zend/Acl.php on line 682"
  1117. */
  1118. public function testMethodRemoveAllowDoesNotThrowNotice() {
  1119. $acl = new Zend_Acl();
  1120. $acl->addRole('admin');
  1121. $acl->addResource('blog');
  1122. $acl->allow('admin', 'blog', 'read');
  1123. $acl->removeAllow(array('admin'), array('blog'), null);
  1124. }
  1125. public function testRoleObjectImplementsToString() {
  1126. $role = new Zend_Acl_Role('_fooBar_');
  1127. $this->assertEquals('_fooBar_',(string)$role);
  1128. }
  1129. public function testResourceObjectImplementsToString() {
  1130. $resource = new Zend_Acl_Resource('_fooBar_');
  1131. $this->assertEquals('_fooBar_',(string)$resource);
  1132. }
  1133. /**
  1134. * @group ZF-7973
  1135. */
  1136. public function testAclPassesPrivilegeToAssertClass() {
  1137. require_once dirname(__FILE__) . '/_files/AssertionZF7973.php';
  1138. $assertion = new Zend_Acl_AclTest_AssertionZF7973();
  1139. $acl = new Zend_Acl();
  1140. $acl->addRole('role');
  1141. $acl->addResource('resource');
  1142. $acl->allow('role',null,null,$assertion);
  1143. $allowed = $acl->isAllowed('role','resource','privilege',$assertion);
  1144. $this->assertTrue($allowed);
  1145. }
  1146. }