AclShellTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * AclShell Test file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
  14. * @link http://cakephp.org CakePHP Project
  15. * @package Cake.Test.Case.Console.Command
  16. * @since CakePHP v 1.2.0.7726
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ShellDispatcher', 'Console');
  20. App::uses('Shell', 'Console');
  21. App::uses('AclShell', 'Console/Command');
  22. App::uses('ComponentCollection', 'Controller');
  23. /**
  24. * AclShellTest class
  25. *
  26. * @package Cake.Test.Case.Console.Command
  27. */
  28. class AclShellTest extends CakeTestCase {
  29. /**
  30. * Fixtures
  31. *
  32. * @var array
  33. */
  34. public $fixtures = array('core.aco', 'core.aro', 'core.aros_aco');
  35. /**
  36. * setup method
  37. *
  38. * @return void
  39. */
  40. public function setUp() {
  41. Configure::write('Acl.database', 'test');
  42. Configure::write('Acl.classname', 'DbAcl');
  43. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  44. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  45. $this->Task = $this->getMock(
  46. 'AclShell',
  47. array('in', 'out', 'hr', 'createFile', 'error', 'err', 'clear', 'dispatchShell'),
  48. array($out, $out, $in)
  49. );
  50. $collection = new ComponentCollection();
  51. $this->Task->Acl = new AclComponent($collection);
  52. $this->Task->params['datasource'] = 'test';
  53. }
  54. /**
  55. * test that model.foreign_key output works when looking at acl rows
  56. *
  57. * @return void
  58. */
  59. public function testViewWithModelForeignKeyOutput() {
  60. $this->Task->command = 'view';
  61. $this->Task->startup();
  62. $data = array(
  63. 'parent_id' => null,
  64. 'model' => 'MyModel',
  65. 'foreign_key' => 2,
  66. );
  67. $this->Task->Acl->Aro->create($data);
  68. $this->Task->Acl->Aro->save();
  69. $this->Task->args[0] = 'aro';
  70. $this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
  71. $this->Task->expects($this->at(2))->method('out')
  72. ->with($this->stringContains('[1] ROOT'));
  73. $this->Task->expects($this->at(4))->method('out')
  74. ->with($this->stringContains('[3] Gandalf'));
  75. $this->Task->expects($this->at(6))->method('out')
  76. ->with($this->stringContains('[5] MyModel.2'));
  77. $this->Task->view();
  78. }
  79. /**
  80. * test view with an argument
  81. *
  82. * @return void
  83. */
  84. public function testViewWithArgument() {
  85. $this->Task->args = array('aro', 'admins');
  86. $this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
  87. $this->Task->expects($this->at(2))->method('out')->with(' [2] admins');
  88. $this->Task->expects($this->at(3))->method('out')->with(' [3] Gandalf');
  89. $this->Task->expects($this->at(4))->method('out')->with(' [4] Elrond');
  90. $this->Task->view();
  91. }
  92. /**
  93. * test the method that splits model.foreign key. and that it returns an array.
  94. *
  95. * @return void
  96. */
  97. public function testParsingModelAndForeignKey() {
  98. $result = $this->Task->parseIdentifier('Model.foreignKey');
  99. $expected = array('model' => 'Model', 'foreign_key' => 'foreignKey');
  100. $result = $this->Task->parseIdentifier('mySuperUser');
  101. $this->assertEqual($result, 'mySuperUser');
  102. $result = $this->Task->parseIdentifier('111234');
  103. $this->assertEqual($result, '111234');
  104. }
  105. /**
  106. * test creating aro/aco nodes
  107. *
  108. * @return void
  109. */
  110. public function testCreate() {
  111. $this->Task->args = array('aro', 'root', 'User.1');
  112. $this->Task->expects($this->at(0))->method('out')->with("<success>New Aro</success> 'User.1' created.", 2);
  113. $this->Task->expects($this->at(1))->method('out')->with("<success>New Aro</success> 'User.3' created.", 2);
  114. $this->Task->expects($this->at(2))->method('out')->with("<success>New Aro</success> 'somealias' created.", 2);
  115. $this->Task->create();
  116. $Aro = ClassRegistry::init('Aro');
  117. $Aro->cacheQueries = false;
  118. $result = $Aro->read();
  119. $this->assertEqual($result['Aro']['model'], 'User');
  120. $this->assertEqual($result['Aro']['foreign_key'], 1);
  121. $this->assertEqual($result['Aro']['parent_id'], null);
  122. $id = $result['Aro']['id'];
  123. $this->Task->args = array('aro', 'User.1', 'User.3');
  124. $this->Task->create();
  125. $Aro = ClassRegistry::init('Aro');
  126. $result = $Aro->read();
  127. $this->assertEqual($result['Aro']['model'], 'User');
  128. $this->assertEqual($result['Aro']['foreign_key'], 3);
  129. $this->assertEqual($result['Aro']['parent_id'], $id);
  130. $this->Task->args = array('aro', 'root', 'somealias');
  131. $this->Task->create();
  132. $Aro = ClassRegistry::init('Aro');
  133. $result = $Aro->read();
  134. $this->assertEqual($result['Aro']['alias'], 'somealias');
  135. $this->assertEqual($result['Aro']['model'], null);
  136. $this->assertEqual($result['Aro']['foreign_key'], null);
  137. $this->assertEqual($result['Aro']['parent_id'], null);
  138. }
  139. /**
  140. * test the delete method with different node types.
  141. *
  142. * @return void
  143. */
  144. public function testDelete() {
  145. $this->Task->args = array('aro', 'AuthUser.1');
  146. $this->Task->expects($this->at(0))->method('out')
  147. ->with("<success>Aro deleted.</success>", 2);
  148. $this->Task->delete();
  149. $Aro = ClassRegistry::init('Aro');
  150. $result = $Aro->findById(3);
  151. $this->assertFalse($result);
  152. }
  153. /**
  154. * test setParent method.
  155. *
  156. * @return void
  157. */
  158. public function testSetParent() {
  159. $this->Task->args = array('aro', 'AuthUser.2', 'root');
  160. $this->Task->setParent();
  161. $Aro = ClassRegistry::init('Aro');
  162. $result = $Aro->read(null, 4);
  163. $this->assertEqual($result['Aro']['parent_id'], null);
  164. }
  165. /**
  166. * test grant
  167. *
  168. * @return void
  169. */
  170. public function testGrant() {
  171. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  172. $this->Task->expects($this->at(0))->method('out')
  173. ->with($this->matchesRegularExpression('/granted/'), true);
  174. $this->Task->grant();
  175. $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
  176. $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
  177. $this->assertFalse(empty($node['Aco'][0]));
  178. $this->assertEqual($node['Aco'][0]['Permission']['_create'], 1);
  179. }
  180. /**
  181. * test deny
  182. *
  183. * @return void
  184. */
  185. public function testDeny() {
  186. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  187. $this->Task->expects($this->at(0))->method('out')
  188. ->with($this->stringContains('Permission denied'), true);
  189. $this->Task->deny();
  190. $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
  191. $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
  192. $this->assertFalse(empty($node['Aco'][0]));
  193. $this->assertEqual($node['Aco'][0]['Permission']['_create'], -1);
  194. }
  195. /**
  196. * test checking allowed and denied perms
  197. *
  198. * @return void
  199. */
  200. public function testCheck() {
  201. $this->Task->expects($this->at(0))->method('out')
  202. ->with($this->matchesRegularExpression('/not allowed/'), true);
  203. $this->Task->expects($this->at(1))->method('out')
  204. ->with($this->matchesRegularExpression('/granted/'), true);
  205. $this->Task->expects($this->at(2))->method('out')
  206. ->with($this->matchesRegularExpression('/is.*allowed/'), true);
  207. $this->Task->expects($this->at(3))->method('out')
  208. ->with($this->matchesRegularExpression('/not.*allowed/'), true);
  209. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
  210. $this->Task->check();
  211. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  212. $this->Task->grant();
  213. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  214. $this->Task->check();
  215. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
  216. $this->Task->check();
  217. }
  218. /**
  219. * test inherit and that it 0's the permission fields.
  220. *
  221. * @return void
  222. */
  223. public function testInherit() {
  224. $this->Task->expects($this->at(0))->method('out')
  225. ->with($this->matchesRegularExpression('/Permission .*granted/'), true);
  226. $this->Task->expects($this->at(1))->method('out')
  227. ->with($this->matchesRegularExpression('/Permission .*inherited/'), true);
  228. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  229. $this->Task->grant();
  230. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'all');
  231. $this->Task->inherit();
  232. $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
  233. $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
  234. $this->assertFalse(empty($node['Aco'][0]));
  235. $this->assertEqual($node['Aco'][0]['Permission']['_create'], 0);
  236. }
  237. /**
  238. * test getting the path for an aro/aco
  239. *
  240. * @return void
  241. */
  242. public function testGetPath() {
  243. $this->Task->args = array('aro', 'AuthUser.2');
  244. $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
  245. $first = $node[0]['Aro']['id'];
  246. $second = $node[1]['Aro']['id'];
  247. $last = $node[2]['Aro']['id'];
  248. $this->Task->expects($this->at(2))->method('out')->with('['.$last.'] ROOT');
  249. $this->Task->expects($this->at(3))->method('out')->with(' ['.$second.'] admins');
  250. $this->Task->expects($this->at(4))->method('out')->with(' ['.$first.'] Elrond');
  251. $this->Task->getPath();
  252. }
  253. /**
  254. * test that initdb makes the correct call.
  255. *
  256. * @return void
  257. */
  258. public function testInitDb() {
  259. $this->Task->expects($this->once())->method('dispatchShell')
  260. ->with('schema create DbAcl');
  261. $this->Task->initdb();
  262. }
  263. }