/ZendFramework-1.11.12/tests/Zend/Acl/AclTest.php

# · PHP · 1376 lines · 836 code · 146 blank · 394 comment · 1 complexity · b03f3b157368b377a78dc62311770555 MD5 · raw file

Large files are truncated click here to view the full file

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