AclTest.php 52 KB

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