PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Console/Command/AclShellTest.php

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