AclShellTest.php 9.3 KB

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