Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/model/db_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 * DbAclTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs.controller.components.dbacl.models | |
17 * @since CakePHP(tm) v 1.2.0.4206 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { | |
21 define('CAKEPHP_UNIT_TEST_EXECUTION', 1); | |
22 } | |
23 App::import('Component', 'Acl'); | |
24 App::import('Core', 'db_acl'); | |
25 | |
26 /** | |
27 * DB ACL wrapper test class | |
28 * | |
29 * @package cake | |
30 * @subpackage cake.tests.cases.libs.controller.components | |
31 */ | |
32 class DbAclNodeTestBase extends AclNode { | |
33 | |
34 /** | |
35 * useDbConfig property | |
36 * | |
37 * @var string 'test_suite' | |
38 * @access public | |
39 */ | |
40 var $useDbConfig = 'test_suite'; | |
41 | |
42 /** | |
43 * cacheSources property | |
44 * | |
45 * @var bool false | |
46 * @access public | |
47 */ | |
48 var $cacheSources = false; | |
49 } | |
50 | |
51 /** | |
52 * Aro Test Wrapper | |
53 * | |
54 * @package cake | |
55 * @subpackage cake.tests.cases.libs.controller.components | |
56 */ | |
57 class DbAroTest extends DbAclNodeTestBase { | |
58 | |
59 /** | |
60 * name property | |
61 * | |
62 * @var string 'DbAroTest' | |
63 * @access public | |
64 */ | |
65 var $name = 'DbAroTest'; | |
66 | |
67 /** | |
68 * useTable property | |
69 * | |
70 * @var string 'aros' | |
71 * @access public | |
72 */ | |
73 var $useTable = 'aros'; | |
74 | |
75 /** | |
76 * hasAndBelongsToMany property | |
77 * | |
78 * @var array | |
79 * @access public | |
80 */ | |
81 var $hasAndBelongsToMany = array('DbAcoTest' => array('with' => 'DbPermissionTest')); | |
82 } | |
83 | |
84 /** | |
85 * Aco Test Wrapper | |
86 * | |
87 * @package cake | |
88 * @subpackage cake.tests.cases.libs.controller.components | |
89 */ | |
90 class DbAcoTest extends DbAclNodeTestBase { | |
91 | |
92 /** | |
93 * name property | |
94 * | |
95 * @var string 'DbAcoTest' | |
96 * @access public | |
97 */ | |
98 var $name = 'DbAcoTest'; | |
99 | |
100 /** | |
101 * useTable property | |
102 * | |
103 * @var string 'acos' | |
104 * @access public | |
105 */ | |
106 var $useTable = 'acos'; | |
107 | |
108 /** | |
109 * hasAndBelongsToMany property | |
110 * | |
111 * @var array | |
112 * @access public | |
113 */ | |
114 var $hasAndBelongsToMany = array('DbAroTest' => array('with' => 'DbPermissionTest')); | |
115 } | |
116 | |
117 /** | |
118 * Permission Test Wrapper | |
119 * | |
120 * @package cake | |
121 * @subpackage cake.tests.cases.libs.controller.components | |
122 */ | |
123 class DbPermissionTest extends CakeTestModel { | |
124 | |
125 /** | |
126 * name property | |
127 * | |
128 * @var string 'DbPermissionTest' | |
129 * @access public | |
130 */ | |
131 var $name = 'DbPermissionTest'; | |
132 | |
133 /** | |
134 * useTable property | |
135 * | |
136 * @var string 'aros_acos' | |
137 * @access public | |
138 */ | |
139 var $useTable = 'aros_acos'; | |
140 | |
141 /** | |
142 * cacheQueries property | |
143 * | |
144 * @var bool false | |
145 * @access public | |
146 */ | |
147 var $cacheQueries = false; | |
148 | |
149 /** | |
150 * belongsTo property | |
151 * | |
152 * @var array | |
153 * @access public | |
154 */ | |
155 var $belongsTo = array('DbAroTest' => array('foreignKey' => 'aro_id'), 'DbAcoTest' => array('foreignKey' => 'aco_id')); | |
156 } | |
157 | |
158 /** | |
159 * DboActionTest class | |
160 * | |
161 * @package cake | |
162 * @subpackage cake.tests.cases.libs.controller.components | |
163 */ | |
164 class DbAcoActionTest extends CakeTestModel { | |
165 | |
166 /** | |
167 * name property | |
168 * | |
169 * @var string 'DbAcoActionTest' | |
170 * @access public | |
171 */ | |
172 var $name = 'DbAcoActionTest'; | |
173 | |
174 /** | |
175 * useTable property | |
176 * | |
177 * @var string 'aco_actions' | |
178 * @access public | |
179 */ | |
180 var $useTable = 'aco_actions'; | |
181 | |
182 /** | |
183 * belongsTo property | |
184 * | |
185 * @var array | |
186 * @access public | |
187 */ | |
188 var $belongsTo = array('DbAcoTest' => array('foreignKey' => 'aco_id')); | |
189 } | |
190 | |
191 /** | |
192 * DbAroUserTest class | |
193 * | |
194 * @package cake | |
195 * @subpackage cake.tests.cases.libs.controller.components | |
196 */ | |
197 class DbAroUserTest extends CakeTestModel { | |
198 | |
199 /** | |
200 * name property | |
201 * | |
202 * @var string 'AuthUser' | |
203 * @access public | |
204 */ | |
205 var $name = 'AuthUser'; | |
206 | |
207 /** | |
208 * useTable property | |
209 * | |
210 * @var string 'auth_users' | |
211 * @access public | |
212 */ | |
213 var $useTable = 'auth_users'; | |
214 | |
215 /** | |
216 * bindNode method | |
217 * | |
218 * @param mixed $ref | |
219 * @access public | |
220 * @return void | |
221 */ | |
222 function bindNode($ref = null) { | |
223 if (Configure::read('DbAclbindMode') == 'string') { | |
224 return 'ROOT/admins/Gandalf'; | |
225 } elseif (Configure::read('DbAclbindMode') == 'array') { | |
226 return array('DbAroTest' => array('DbAroTest.model' => 'AuthUser', 'DbAroTest.foreign_key' => 2)); | |
227 } | |
228 } | |
229 } | |
230 | |
231 /** | |
232 * DbAclTest class | |
233 * | |
234 * @package cake | |
235 * @subpackage cake.tests.cases.libs.controller.components | |
236 */ | |
237 class DbAclTest extends DbAcl { | |
238 | |
239 /** | |
240 * construct method | |
241 * | |
242 * @access private | |
243 * @return void | |
244 */ | |
245 function __construct() { | |
246 $this->Aro =& new DbAroTest(); | |
247 $this->Aro->Permission =& new DbPermissionTest(); | |
248 $this->Aco =& new DbAcoTest(); | |
249 $this->Aro->Permission =& new DbPermissionTest(); | |
250 } | |
251 } | |
252 | |
253 /** | |
254 * AclNodeTest class | |
255 * | |
256 * @package cake | |
257 * @subpackage cake.tests.cases.libs.controller.components.dbacl.models | |
258 */ | |
259 class AclNodeTest extends CakeTestCase { | |
260 | |
261 /** | |
262 * fixtures property | |
263 * | |
264 * @var array | |
265 * @access public | |
266 */ | |
267 var $fixtures = array('core.aro', 'core.aco', 'core.aros_aco', 'core.aco_action', 'core.auth_user'); | |
268 | |
269 /** | |
270 * setUp method | |
271 * | |
272 * @access public | |
273 * @return void | |
274 */ | |
275 function setUp() { | |
276 Configure::write('Acl.classname', 'DbAclTest'); | |
277 Configure::write('Acl.database', 'test_suite'); | |
278 } | |
279 | |
280 /** | |
281 * testNode method | |
282 * | |
283 * @access public | |
284 * @return void | |
285 */ | |
286 function testNode() { | |
287 $Aco =& new DbAcoTest(); | |
288 $result = Set::extract($Aco->node('Controller1'), '{n}.DbAcoTest.id'); | |
289 $expected = array(2, 1); | |
290 $this->assertEqual($result, $expected); | |
291 | |
292 $result = Set::extract($Aco->node('Controller1/action1'), '{n}.DbAcoTest.id'); | |
293 $expected = array(3, 2, 1); | |
294 $this->assertEqual($result, $expected); | |
295 | |
296 $result = Set::extract($Aco->node('Controller2/action1'), '{n}.DbAcoTest.id'); | |
297 $expected = array(7, 6, 1); | |
298 $this->assertEqual($result, $expected); | |
299 | |
300 $result = Set::extract($Aco->node('Controller1/action2'), '{n}.DbAcoTest.id'); | |
301 $expected = array(5, 2, 1); | |
302 $this->assertEqual($result, $expected); | |
303 | |
304 $result = Set::extract($Aco->node('Controller1/action1/record1'), '{n}.DbAcoTest.id'); | |
305 $expected = array(4, 3, 2, 1); | |
306 $this->assertEqual($result, $expected); | |
307 | |
308 $result = Set::extract($Aco->node('Controller2/action1/record1'), '{n}.DbAcoTest.id'); | |
309 $expected = array(8, 7, 6, 1); | |
310 $this->assertEqual($result, $expected); | |
311 | |
312 $result = Set::extract($Aco->node('Controller2/action3'), '{n}.DbAcoTest.id'); | |
313 $this->assertFalse($result); | |
314 | |
315 $result = Set::extract($Aco->node('Controller2/action3/record5'), '{n}.DbAcoTest.id'); | |
316 $this->assertFalse($result); | |
317 | |
318 $result = $Aco->node(''); | |
319 $this->assertEqual($result, null); | |
320 } | |
321 | |
322 /** | |
323 * test that node() doesn't dig deeper than it should. | |
324 * | |
325 * @return void | |
326 */ | |
327 function testNodeWithDuplicatePathSegments() { | |
328 $Aco =& new DbAcoTest(); | |
329 $nodes = $Aco->node('ROOT/Users'); | |
330 $this->assertEqual($nodes[0]['DbAcoTest']['parent_id'], 1, 'Parent id does not point at ROOT. %s'); | |
331 } | |
332 | |
333 /** | |
334 * testNodeArrayFind method | |
335 * | |
336 * @access public | |
337 * @return void | |
338 */ | |
339 function testNodeArrayFind() { | |
340 $Aro = new DbAroTest(); | |
341 Configure::write('DbAclbindMode', 'string'); | |
342 $result = Set::extract($Aro->node(array('DbAroUserTest' => array('id' => '1', 'foreign_key' => '1'))), '{n}.DbAroTest.id'); | |
343 $expected = array(3, 2, 1); | |
344 $this->assertEqual($result, $expected); | |
345 | |
346 Configure::write('DbAclbindMode', 'array'); | |
347 $result = Set::extract($Aro->node(array('DbAroUserTest' => array('id' => 4, 'foreign_key' => 2))), '{n}.DbAroTest.id'); | |
348 $expected = array(4); | |
349 $this->assertEqual($result, $expected); | |
350 } | |
351 /** | |
352 * testNodeObjectFind method | |
353 * | |
354 * @access public | |
355 * @return void | |
356 */ | |
357 function testNodeObjectFind() { | |
358 $Aro = new DbAroTest(); | |
359 $Model = new DbAroUserTest(); | |
360 $Model->id = 1; | |
361 $result = Set::extract($Aro->node($Model), '{n}.DbAroTest.id'); | |
362 $expected = array(3, 2, 1); | |
363 $this->assertEqual($result, $expected); | |
364 | |
365 $Model->id = 2; | |
366 $result = Set::extract($Aro->node($Model), '{n}.DbAroTest.id'); | |
367 $expected = array(4, 2, 1); | |
368 $this->assertEqual($result, $expected); | |
369 | |
370 } | |
371 | |
372 /** | |
373 * testNodeAliasParenting method | |
374 * | |
375 * @access public | |
376 * @return void | |
377 */ | |
378 function testNodeAliasParenting() { | |
379 $Aco = new DbAcoTest(); | |
380 $db =& ConnectionManager::getDataSource('test_suite'); | |
381 $db->truncate($Aco); | |
382 $db->_queriesLog = array(); | |
383 | |
384 $Aco->create(array('model' => null, 'foreign_key' => null, 'parent_id' => null, 'alias' => 'Application')); | |
385 $Aco->save(); | |
386 | |
387 $Aco->create(array('model' => null, 'foreign_key' => null, 'parent_id' => $Aco->id, 'alias' => 'Pages')); | |
388 $Aco->save(); | |
389 | |
390 $result = $Aco->find('all'); | |
391 $expected = array( | |
392 array('DbAcoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'Application', 'lft' => '1', 'rght' => '4'), 'DbAroTest' => array()), | |
393 array('DbAcoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'Pages', 'lft' => '2', 'rght' => '3', ), 'DbAroTest' => array()) | |
394 ); | |
395 $this->assertEqual($result, $expected); | |
396 } | |
397 } |