comparison cake/tests/cases/console/libs/acl.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * AclShell Test file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2006-2010, 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 2006-2010, Cake Software Foundation, Inc.
14 * @link http://cakephp.org CakePHP Project
15 * @package cake
16 * @subpackage cake.tests.cases.console.libs.tasks
17 * @since CakePHP v 1.2.0.7726
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19 */
20 App::import('Shell', 'Shell', false);
21
22 if (!defined('DISABLE_AUTO_DISPATCH')) {
23 define('DISABLE_AUTO_DISPATCH', true);
24 }
25
26 if (!class_exists('ShellDispatcher')) {
27 ob_start();
28 $argv = false;
29 require CAKE . 'console' . DS . 'cake.php';
30 ob_end_clean();
31 }
32
33 if (!class_exists('AclShell')) {
34 require CAKE . 'console' . DS . 'libs' . DS . 'acl.php';
35 }
36
37 Mock::generatePartial(
38 'ShellDispatcher', 'TestAclShellMockShellDispatcher',
39 array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment', 'dispatch')
40 );
41 Mock::generatePartial(
42 'AclShell', 'MockAclShell',
43 array('in', 'out', 'hr', 'createFile', 'error', 'err')
44 );
45
46 Mock::generate('AclComponent', 'MockAclShellAclComponent');
47
48 /**
49 * AclShellTest class
50 *
51 * @package cake
52 * @subpackage cake.tests.cases.console.libs.tasks
53 */
54 class AclShellTest extends CakeTestCase {
55
56 /**
57 * Fixtures
58 *
59 * @var array
60 * @access public
61 */
62 var $fixtures = array('core.aco', 'core.aro', 'core.aros_aco');
63
64 /**
65 * configure Configure for testcase
66 *
67 * @return void
68 * @access public
69 */
70 function startCase() {
71 $this->_aclDb = Configure::read('Acl.database');
72 $this->_aclClass = Configure::read('Acl.classname');
73
74 Configure::write('Acl.database', 'test_suite');
75 Configure::write('Acl.classname', 'DbAcl');
76 }
77
78 /**
79 * restore Environment settings
80 *
81 * @return void
82 * @access public
83 */
84 function endCase() {
85 Configure::write('Acl.database', $this->_aclDb);
86 Configure::write('Acl.classname', $this->_aclClass);
87 }
88
89 /**
90 * startTest method
91 *
92 * @return void
93 * @access public
94 */
95 function startTest() {
96 $this->Dispatcher =& new TestAclShellMockShellDispatcher();
97 $this->Task =& new MockAclShell($this->Dispatcher);
98 $this->Task->Dispatch =& $this->Dispatcher;
99 $this->Task->params['datasource'] = 'test_suite';
100 $this->Task->Acl =& new AclComponent();
101 $controller = null;
102 $this->Task->Acl->startup($controller);
103 }
104
105 /**
106 * endTest method
107 *
108 * @return void
109 * @access public
110 */
111 function endTest() {
112 ClassRegistry::flush();
113 }
114
115 /**
116 * test that model.foreign_key output works when looking at acl rows
117 *
118 * @return void
119 * @access public
120 */
121 function testViewWithModelForeignKeyOutput() {
122 $this->Task->command = 'view';
123 $this->Task->startup();
124 $data = array(
125 'parent_id' => null,
126 'model' => 'MyModel',
127 'foreign_key' => 2,
128 );
129 $this->Task->Acl->Aro->create($data);
130 $this->Task->Acl->Aro->save();
131 $this->Task->args[0] = 'aro';
132
133 $this->Task->expectAt(0, 'out', array('Aro tree:'));
134 $this->Task->expectAt(1, 'out', array(new PatternExpectation('/\[1\] ROOT/')));
135 $this->Task->expectAt(3, 'out', array(new PatternExpectation('/\[3\] Gandalf/')));
136 $this->Task->expectAt(5, 'out', array(new PatternExpectation('/\[5\] MyModel.2/')));
137
138 $this->Task->view();
139 }
140
141 /**
142 * test view with an argument
143 *
144 * @return void
145 * @access public
146 */
147 function testViewWithArgument() {
148 $this->Task->args = array('aro', 'admins');
149 $this->Task->expectAt(0, 'out', array('Aro tree:'));
150 $this->Task->expectAt(1, 'out', array(' [2] admins'));
151 $this->Task->expectAt(2, 'out', array(' [3] Gandalf'));
152 $this->Task->expectAt(3, 'out', array(' [4] Elrond'));
153 $this->Task->view();
154 }
155
156 /**
157 * test the method that splits model.foreign key. and that it returns an array.
158 *
159 * @return void
160 * @access public
161 */
162 function testParsingModelAndForeignKey() {
163 $result = $this->Task->parseIdentifier('Model.foreignKey');
164 $expected = array('model' => 'Model', 'foreign_key' => 'foreignKey');
165
166 $result = $this->Task->parseIdentifier('mySuperUser');
167 $this->assertEqual($result, 'mySuperUser');
168
169 $result = $this->Task->parseIdentifier('111234');
170 $this->assertEqual($result, '111234');
171 }
172
173 /**
174 * test creating aro/aco nodes
175 *
176 * @return void
177 * @access public
178 */
179 function testCreate() {
180 $this->Task->args = array('aro', 'root', 'User.1');
181 $this->Task->expectAt(0, 'out', array(new PatternExpectation('/created/'), '*'));
182 $this->Task->create();
183
184 $Aro =& ClassRegistry::init('Aro');
185 $Aro->cacheQueries = false;
186 $result = $Aro->read();
187 $this->assertEqual($result['Aro']['model'], 'User');
188 $this->assertEqual($result['Aro']['foreign_key'], 1);
189 $this->assertEqual($result['Aro']['parent_id'], null);
190 $id = $result['Aro']['id'];
191
192 $this->Task->args = array('aro', 'User.1', 'User.3');
193 $this->Task->expectAt(1, 'out', array(new PatternExpectation('/created/'), '*'));
194 $this->Task->create();
195
196 $Aro =& ClassRegistry::init('Aro');
197 $result = $Aro->read();
198 $this->assertEqual($result['Aro']['model'], 'User');
199 $this->assertEqual($result['Aro']['foreign_key'], 3);
200 $this->assertEqual($result['Aro']['parent_id'], $id);
201
202 $this->Task->args = array('aro', 'root', 'somealias');
203 $this->Task->expectAt(2, 'out', array(new PatternExpectation('/created/'), '*'));
204 $this->Task->create();
205
206 $Aro =& ClassRegistry::init('Aro');
207 $result = $Aro->read();
208 $this->assertEqual($result['Aro']['alias'], 'somealias');
209 $this->assertEqual($result['Aro']['model'], null);
210 $this->assertEqual($result['Aro']['foreign_key'], null);
211 $this->assertEqual($result['Aro']['parent_id'], null);
212 }
213
214 /**
215 * test the delete method with different node types.
216 *
217 * @return void
218 * @access public
219 */
220 function testDelete() {
221 $this->Task->args = array('aro', 'AuthUser.1');
222 $this->Task->expectAt(0, 'out', array(new NoPatternExpectation('/not/'), true));
223 $this->Task->delete();
224
225 $Aro =& ClassRegistry::init('Aro');
226 $result = $Aro->read(null, 3);
227 $this->assertFalse($result);
228 }
229
230 /**
231 * test setParent method.
232 *
233 * @return void
234 * @access public
235 */
236 function testSetParent() {
237 $this->Task->args = array('aro', 'AuthUser.2', 'root');
238 $this->Task->setParent();
239
240 $Aro =& ClassRegistry::init('Aro');
241 $result = $Aro->read(null, 4);
242 $this->assertEqual($result['Aro']['parent_id'], null);
243 }
244
245 /**
246 * test grant
247 *
248 * @return void
249 * @access public
250 */
251 function testGrant() {
252 $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
253 $this->Task->expectAt(0, 'out', array(new PatternExpectation('/Permission granted/'), true));
254 $this->Task->grant();
255
256 $node = $this->Task->Acl->Aro->read(null, 4);
257 $this->assertFalse(empty($node['Aco'][0]));
258 $this->assertEqual($node['Aco'][0]['Permission']['_create'], 1);
259 }
260
261 /**
262 * test deny
263 *
264 * @return void
265 * @access public
266 */
267 function testDeny() {
268 $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
269 $this->Task->expectAt(0, 'out', array(new PatternExpectation('/Permission denied/'), true));
270 $this->Task->deny();
271
272 $node = $this->Task->Acl->Aro->read(null, 4);
273 $this->assertFalse(empty($node['Aco'][0]));
274 $this->assertEqual($node['Aco'][0]['Permission']['_create'], -1);
275 }
276
277 /**
278 * test checking allowed and denied perms
279 *
280 * @return void
281 * @access public
282 */
283 function testCheck() {
284 $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
285 $this->Task->expectAt(0, 'out', array(new PatternExpectation('/not allowed/'), true));
286 $this->Task->check();
287
288 $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
289 $this->Task->expectAt(1, 'out', array(new PatternExpectation('/Permission granted/'), true));
290 $this->Task->grant();
291
292 $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
293 $this->Task->expectAt(2, 'out', array(new PatternExpectation('/is allowed/'), true));
294 $this->Task->check();
295
296 $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
297 $this->Task->expectAt(3, 'out', array(new PatternExpectation('/not allowed/'), true));
298 $this->Task->check();
299 }
300
301 /**
302 * test inherit and that it 0's the permission fields.
303 *
304 * @return void
305 * @access public
306 */
307 function testInherit() {
308 $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
309 $this->Task->expectAt(0, 'out', array(new PatternExpectation('/Permission granted/'), true));
310 $this->Task->grant();
311
312 $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'all');
313 $this->Task->expectAt(1, 'out', array(new PatternExpectation('/permission inherited/i'), true));
314 $this->Task->inherit();
315
316 $node = $this->Task->Acl->Aro->read(null, 4);
317 $this->assertFalse(empty($node['Aco'][0]));
318 $this->assertEqual($node['Aco'][0]['Permission']['_create'], 0);
319 }
320
321 /**
322 * test getting the path for an aro/aco
323 *
324 * @return void
325 * @access public
326 */
327 function testGetPath() {
328 $this->Task->args = array('aro', 'AuthUser.2');
329 $this->Task->expectAt(1, 'out', array('[1] ROOT'));
330 $this->Task->expectAt(2, 'out', array(' [2] admins'));
331 $this->Task->expectAt(3, 'out', array(' [4] Elrond'));
332 $this->Task->getPath();
333 }
334
335 /**
336 * test that initdb makes the correct call.
337 *
338 * @return void
339 */
340 function testInitDb() {
341 $this->Task->Dispatch->expectOnce('dispatch');
342 $this->Task->initdb();
343
344 $this->assertEqual($this->Task->Dispatch->args, array('schema', 'create', 'DbAcl'));
345 }
346 }