AclShellTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. $result = $this->Task->parseIdentifier('mySuperUser');
  104. $this->assertEquals('mySuperUser', $result);
  105. $result = $this->Task->parseIdentifier('111234');
  106. $this->assertEquals('111234', $result);
  107. }
  108. /**
  109. * test creating aro/aco nodes
  110. *
  111. * @return void
  112. */
  113. public function testCreate() {
  114. $this->Task->args = array('aro', 'root', 'User.1');
  115. $this->Task->expects($this->at(0))->method('out')->with("<success>New Aro</success> 'User.1' created.", 2);
  116. $this->Task->expects($this->at(1))->method('out')->with("<success>New Aro</success> 'User.3' created.", 2);
  117. $this->Task->expects($this->at(2))->method('out')->with("<success>New Aro</success> 'somealias' created.", 2);
  118. $this->Task->create();
  119. $Aro = ClassRegistry::init('Aro');
  120. $Aro->cacheQueries = false;
  121. $result = $Aro->read();
  122. $this->assertEquals('User', $result['Aro']['model']);
  123. $this->assertEquals(1, $result['Aro']['foreign_key']);
  124. $this->assertEquals(null, $result['Aro']['parent_id']);
  125. $id = $result['Aro']['id'];
  126. $this->Task->args = array('aro', 'User.1', 'User.3');
  127. $this->Task->create();
  128. $Aro = ClassRegistry::init('Aro');
  129. $result = $Aro->read();
  130. $this->assertEquals('User', $result['Aro']['model']);
  131. $this->assertEquals(3, $result['Aro']['foreign_key']);
  132. $this->assertEquals($id, $result['Aro']['parent_id']);
  133. $this->Task->args = array('aro', 'root', 'somealias');
  134. $this->Task->create();
  135. $Aro = ClassRegistry::init('Aro');
  136. $result = $Aro->read();
  137. $this->assertEquals('somealias', $result['Aro']['alias']);
  138. $this->assertEquals(null, $result['Aro']['model']);
  139. $this->assertEquals(null, $result['Aro']['foreign_key']);
  140. $this->assertEquals(null, $result['Aro']['parent_id']);
  141. }
  142. /**
  143. * test the delete method with different node types.
  144. *
  145. * @return void
  146. */
  147. public function testDelete() {
  148. $this->Task->args = array('aro', 'AuthUser.1');
  149. $this->Task->expects($this->at(0))->method('out')
  150. ->with("<success>Aro deleted.</success>", 2);
  151. $this->Task->delete();
  152. $Aro = ClassRegistry::init('Aro');
  153. $result = $Aro->findById(3);
  154. $this->assertSame(array(), $result);
  155. }
  156. /**
  157. * test setParent method.
  158. *
  159. * @return void
  160. */
  161. public function testSetParent() {
  162. $this->Task->args = array('aro', 'AuthUser.2', 'root');
  163. $this->Task->setParent();
  164. $Aro = ClassRegistry::init('Aro');
  165. $result = $Aro->read(null, 4);
  166. $this->assertEquals(null, $result['Aro']['parent_id']);
  167. }
  168. /**
  169. * test grant
  170. *
  171. * @return void
  172. */
  173. public function testGrant() {
  174. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  175. $this->Task->expects($this->at(0))->method('out')
  176. ->with($this->matchesRegularExpression('/granted/'), true);
  177. $this->Task->grant();
  178. $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
  179. $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
  180. $this->assertFalse(empty($node['Aco'][0]));
  181. $this->assertEquals(1, $node['Aco'][0]['Permission']['_create']);
  182. }
  183. /**
  184. * test deny
  185. *
  186. * @return void
  187. */
  188. public function testDeny() {
  189. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  190. $this->Task->expects($this->at(0))->method('out')
  191. ->with($this->stringContains('Permission denied'), true);
  192. $this->Task->deny();
  193. $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
  194. $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
  195. $this->assertFalse(empty($node['Aco'][0]));
  196. $this->assertEquals(-1, $node['Aco'][0]['Permission']['_create']);
  197. }
  198. /**
  199. * test checking allowed and denied perms
  200. *
  201. * @return void
  202. */
  203. public function testCheck() {
  204. $this->Task->expects($this->at(0))->method('out')
  205. ->with($this->matchesRegularExpression('/not allowed/'), true);
  206. $this->Task->expects($this->at(1))->method('out')
  207. ->with($this->matchesRegularExpression('/granted/'), true);
  208. $this->Task->expects($this->at(2))->method('out')
  209. ->with($this->matchesRegularExpression('/is.*allowed/'), true);
  210. $this->Task->expects($this->at(3))->method('out')
  211. ->with($this->matchesRegularExpression('/not.*allowed/'), true);
  212. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
  213. $this->Task->check();
  214. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  215. $this->Task->grant();
  216. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  217. $this->Task->check();
  218. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
  219. $this->Task->check();
  220. }
  221. /**
  222. * test inherit and that it 0's the permission fields.
  223. *
  224. * @return void
  225. */
  226. public function testInherit() {
  227. $this->Task->expects($this->at(0))->method('out')
  228. ->with($this->matchesRegularExpression('/Permission .*granted/'), true);
  229. $this->Task->expects($this->at(1))->method('out')
  230. ->with($this->matchesRegularExpression('/Permission .*inherited/'), true);
  231. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
  232. $this->Task->grant();
  233. $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'all');
  234. $this->Task->inherit();
  235. $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
  236. $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
  237. $this->assertFalse(empty($node['Aco'][0]));
  238. $this->assertEquals(0, $node['Aco'][0]['Permission']['_create']);
  239. }
  240. /**
  241. * test getting the path for an aro/aco
  242. *
  243. * @return void
  244. */
  245. public function testGetPath() {
  246. $this->Task->args = array('aro', 'AuthUser.2');
  247. $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
  248. $first = $node[0]['Aro']['id'];
  249. $second = $node[1]['Aro']['id'];
  250. $last = $node[2]['Aro']['id'];
  251. $this->Task->expects($this->at(2))->method('out')->with('[' . $last . '] ROOT');
  252. $this->Task->expects($this->at(3))->method('out')->with(' [' . $second . '] admins');
  253. $this->Task->expects($this->at(4))->method('out')->with(' [' . $first . '] Elrond');
  254. $this->Task->getPath();
  255. }
  256. /**
  257. * test that initdb makes the correct call.
  258. *
  259. * @return void
  260. */
  261. public function testInitDb() {
  262. $this->Task->expects($this->once())->method('dispatchShell')
  263. ->with('schema create DbAcl');
  264. $this->Task->initdb();
  265. }
  266. }