pictcode / lib / Cake / Test / Case / Model / AclNodeTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (8.069 KB)
1 | 635eef61 | spyder1211 | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * AclNodeTest file
|
||
4 | *
|
||
5 | * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
||
6 | * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
7 | *
|
||
8 | * Licensed under The MIT License
|
||
9 | * For full copyright and license information, please see the LICENSE.txt
|
||
10 | * Redistributions of files must retain the above copyright notice
|
||
11 | *
|
||
12 | * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
13 | * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
||
14 | * @package Cake.Test.Case.Model
|
||
15 | * @since CakePHP(tm) v 1.2.0.4206
|
||
16 | * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
17 | */
|
||
18 | |||
19 | App::uses('DbAcl', 'Controller/Component/Acl'); |
||
20 | App::uses('AclNode', 'Model'); |
||
21 | |||
22 | /**
|
||
23 | * DB ACL wrapper test class
|
||
24 | *
|
||
25 | * @package Cake.Test.Case.Model
|
||
26 | */
|
||
27 | class DbAclNodeTestBase extends AclNode { |
||
28 | |||
29 | /**
|
||
30 | * useDbConfig property
|
||
31 | *
|
||
32 | * @var string
|
||
33 | */
|
||
34 | public $useDbConfig = 'test'; |
||
35 | |||
36 | /**
|
||
37 | * cacheSources property
|
||
38 | *
|
||
39 | * @var bool
|
||
40 | */
|
||
41 | public $cacheSources = false; |
||
42 | } |
||
43 | |||
44 | /**
|
||
45 | * Aro Test Wrapper
|
||
46 | *
|
||
47 | * @package Cake.Test.Case.Model
|
||
48 | */
|
||
49 | class DbAroTest extends DbAclNodeTestBase { |
||
50 | |||
51 | /**
|
||
52 | * useTable property
|
||
53 | *
|
||
54 | * @var string
|
||
55 | */
|
||
56 | public $useTable = 'aros'; |
||
57 | |||
58 | /**
|
||
59 | * hasAndBelongsToMany property
|
||
60 | *
|
||
61 | * @var array
|
||
62 | */
|
||
63 | public $hasAndBelongsToMany = array('DbAcoTest' => array('with' => 'DbPermissionTest')); |
||
64 | } |
||
65 | |||
66 | /**
|
||
67 | * Aco Test Wrapper
|
||
68 | *
|
||
69 | * @package Cake.Test.Case.Model
|
||
70 | */
|
||
71 | class DbAcoTest extends DbAclNodeTestBase { |
||
72 | |||
73 | /**
|
||
74 | * useTable property
|
||
75 | *
|
||
76 | * @var string
|
||
77 | */
|
||
78 | public $useTable = 'acos'; |
||
79 | |||
80 | /**
|
||
81 | * hasAndBelongsToMany property
|
||
82 | *
|
||
83 | * @var array
|
||
84 | */
|
||
85 | public $hasAndBelongsToMany = array('DbAroTest' => array('with' => 'DbPermissionTest')); |
||
86 | } |
||
87 | |||
88 | /**
|
||
89 | * Permission Test Wrapper
|
||
90 | *
|
||
91 | * @package Cake.Test.Case.Model
|
||
92 | */
|
||
93 | class DbPermissionTest extends CakeTestModel { |
||
94 | |||
95 | /**
|
||
96 | * useTable property
|
||
97 | *
|
||
98 | * @var string
|
||
99 | */
|
||
100 | public $useTable = 'aros_acos'; |
||
101 | |||
102 | /**
|
||
103 | * cacheQueries property
|
||
104 | *
|
||
105 | * @var bool
|
||
106 | */
|
||
107 | public $cacheQueries = false; |
||
108 | |||
109 | /**
|
||
110 | * belongsTo property
|
||
111 | *
|
||
112 | * @var array
|
||
113 | */
|
||
114 | public $belongsTo = array('DbAroTest' => array('foreignKey' => 'aro_id'), 'DbAcoTest' => array('foreignKey' => 'aco_id')); |
||
115 | } |
||
116 | |||
117 | /**
|
||
118 | * DboActionTest class
|
||
119 | *
|
||
120 | * @package Cake.Test.Case.Model
|
||
121 | */
|
||
122 | class DbAcoActionTest extends CakeTestModel { |
||
123 | |||
124 | /**
|
||
125 | * useTable property
|
||
126 | *
|
||
127 | * @var string
|
||
128 | */
|
||
129 | public $useTable = 'aco_actions'; |
||
130 | |||
131 | /**
|
||
132 | * belongsTo property
|
||
133 | *
|
||
134 | * @var array
|
||
135 | */
|
||
136 | public $belongsTo = array('DbAcoTest' => array('foreignKey' => 'aco_id')); |
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * DbAroUserTest class
|
||
141 | *
|
||
142 | * @package Cake.Test.Case.Model
|
||
143 | */
|
||
144 | class DbAroUserTest extends CakeTestModel { |
||
145 | |||
146 | /**
|
||
147 | * name property
|
||
148 | *
|
||
149 | * @var string
|
||
150 | */
|
||
151 | public $name = 'AuthUser'; |
||
152 | |||
153 | /**
|
||
154 | * useTable property
|
||
155 | *
|
||
156 | * @var string
|
||
157 | */
|
||
158 | public $useTable = 'auth_users'; |
||
159 | |||
160 | /**
|
||
161 | * bindNode method
|
||
162 | *
|
||
163 | * @param string|array|Model $ref
|
||
164 | * @return void
|
||
165 | */
|
||
166 | public function bindNode($ref = null) { |
||
167 | if (Configure::read('DbAclbindMode') === 'string') { |
||
168 | return 'ROOT/admins/Gandalf'; |
||
169 | } elseif (Configure::read('DbAclbindMode') === 'array') { |
||
170 | return array('DbAroTest' => array('DbAroTest.model' => 'AuthUser', 'DbAroTest.foreign_key' => 2)); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | } |
||
175 | |||
176 | /**
|
||
177 | * TestDbAcl class
|
||
178 | *
|
||
179 | * @package Cake.Test.Case.Model
|
||
180 | */
|
||
181 | class TestDbAcl extends DbAcl { |
||
182 | |||
183 | /**
|
||
184 | * Constructor
|
||
185 | */
|
||
186 | public function __construct() { |
||
187 | $this->Aro = new DbAroTest(); |
||
188 | $this->Aro->Permission = new DbPermissionTest(); |
||
189 | $this->Aco = new DbAcoTest(); |
||
190 | $this->Aro->Permission = new DbPermissionTest(); |
||
191 | } |
||
192 | |||
193 | } |
||
194 | |||
195 | /**
|
||
196 | * AclNodeTest class
|
||
197 | *
|
||
198 | * @package Cake.Test.Case.Model
|
||
199 | */
|
||
200 | class AclNodeTest extends CakeTestCase { |
||
201 | |||
202 | /**
|
||
203 | * fixtures property
|
||
204 | *
|
||
205 | * @var array
|
||
206 | */
|
||
207 | public $fixtures = array('core.aro', 'core.aco', 'core.aros_aco', 'core.aco_action', 'core.auth_user'); |
||
208 | |||
209 | /**
|
||
210 | * setUp method
|
||
211 | *
|
||
212 | * @return void
|
||
213 | */
|
||
214 | public function setUp() { |
||
215 | parent::setUp();
|
||
216 | Configure::write('Acl.classname', 'TestDbAcl'); |
||
217 | Configure::write('Acl.database', 'test'); |
||
218 | } |
||
219 | |||
220 | /**
|
||
221 | * testNode method
|
||
222 | *
|
||
223 | * @return void
|
||
224 | */
|
||
225 | public function testNode() { |
||
226 | $Aco = new DbAcoTest(); |
||
227 | $result = Hash::extract($Aco->node('Controller1'), '{n}.DbAcoTest.id'); |
||
228 | $expected = array(2, 1); |
||
229 | $this->assertEquals($expected, $result); |
||
230 | |||
231 | $result = Hash::extract($Aco->node('Controller1/action1'), '{n}.DbAcoTest.id'); |
||
232 | $expected = array(3, 2, 1); |
||
233 | $this->assertEquals($expected, $result); |
||
234 | |||
235 | $result = Hash::extract($Aco->node('Controller2/action1'), '{n}.DbAcoTest.id'); |
||
236 | $expected = array(7, 6, 1); |
||
237 | $this->assertEquals($expected, $result); |
||
238 | |||
239 | $result = Hash::extract($Aco->node('Controller1/action2'), '{n}.DbAcoTest.id'); |
||
240 | $expected = array(5, 2, 1); |
||
241 | $this->assertEquals($expected, $result); |
||
242 | |||
243 | $result = Hash::extract($Aco->node('Controller1/action1/record1'), '{n}.DbAcoTest.id'); |
||
244 | $expected = array(4, 3, 2, 1); |
||
245 | $this->assertEquals($expected, $result); |
||
246 | |||
247 | $result = Hash::extract($Aco->node('Controller2/action1/record1'), '{n}.DbAcoTest.id'); |
||
248 | $expected = array(8, 7, 6, 1); |
||
249 | $this->assertEquals($expected, $result); |
||
250 | |||
251 | $this->assertFalse($Aco->node('Controller2/action3')); |
||
252 | |||
253 | $this->assertFalse($Aco->node('Controller2/action3/record5')); |
||
254 | |||
255 | $result = $Aco->node(''); |
||
256 | $this->assertEquals(null, $result); |
||
257 | } |
||
258 | |||
259 | /**
|
||
260 | * test that node() doesn't dig deeper than it should.
|
||
261 | *
|
||
262 | * @return void
|
||
263 | */
|
||
264 | public function testNodeWithDuplicatePathSegments() { |
||
265 | $Aco = new DbAcoTest(); |
||
266 | $nodes = $Aco->node('ROOT/Users'); |
||
267 | $this->assertEquals(1, $nodes[0]['DbAcoTest']['parent_id'], 'Parent id does not point at ROOT. %s'); |
||
268 | } |
||
269 | |||
270 | /**
|
||
271 | * testNodeArrayFind method
|
||
272 | *
|
||
273 | * @return void
|
||
274 | */
|
||
275 | public function testNodeArrayFind() { |
||
276 | $Aro = new DbAroTest(); |
||
277 | Configure::write('DbAclbindMode', 'string'); |
||
278 | $result = Hash::extract($Aro->node(array('DbAroUserTest' => array('id' => '1', 'foreign_key' => '1'))), '{n}.DbAroTest.id'); |
||
279 | $expected = array(3, 2, 1); |
||
280 | $this->assertEquals($expected, $result); |
||
281 | |||
282 | Configure::write('DbAclbindMode', 'array'); |
||
283 | $result = Hash::extract($Aro->node(array('DbAroUserTest' => array('id' => 4, 'foreign_key' => 2))), '{n}.DbAroTest.id'); |
||
284 | $expected = array(4); |
||
285 | $this->assertEquals($expected, $result); |
||
286 | } |
||
287 | |||
288 | /**
|
||
289 | * testNodeObjectFind method
|
||
290 | *
|
||
291 | * @return void
|
||
292 | */
|
||
293 | public function testNodeObjectFind() { |
||
294 | $Aro = new DbAroTest(); |
||
295 | $Model = new DbAroUserTest(); |
||
296 | $Model->id = 1; |
||
297 | $result = Hash::extract($Aro->node($Model), '{n}.DbAroTest.id'); |
||
298 | $expected = array(3, 2, 1); |
||
299 | $this->assertEquals($expected, $result); |
||
300 | |||
301 | $Model->id = 2; |
||
302 | $result = Hash::extract($Aro->node($Model), '{n}.DbAroTest.id'); |
||
303 | $expected = array(4, 2, 1); |
||
304 | $this->assertEquals($expected, $result); |
||
305 | } |
||
306 | |||
307 | /**
|
||
308 | * testNodeAliasParenting method
|
||
309 | *
|
||
310 | * @return void
|
||
311 | */
|
||
312 | public function testNodeAliasParenting() { |
||
313 | $Aco = ClassRegistry::init('DbAcoTest'); |
||
314 | $db = $Aco->getDataSource(); |
||
315 | $db->truncate($Aco); |
||
316 | |||
317 | $Aco->create(array('model' => null, 'foreign_key' => null, 'parent_id' => null, 'alias' => 'Application')); |
||
318 | $Aco->save();
|
||
319 | |||
320 | $Aco->create(array('model' => null, 'foreign_key' => null, 'parent_id' => $Aco->id, 'alias' => 'Pages')); |
||
321 | $Aco->save();
|
||
322 | |||
323 | $result = $Aco->find('all'); |
||
324 | $expected = array( |
||
325 | array('DbAcoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'Application', 'lft' => '1', 'rght' => '4'), 'DbAroTest' => array()), |
||
326 | array('DbAcoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'Pages', 'lft' => '2', 'rght' => '3'), 'DbAroTest' => array()) |
||
327 | ); |
||
328 | $this->assertEquals($expected, $result); |
||
329 | } |
||
330 | |||
331 | /**
|
||
332 | * testNodeActionAuthorize method
|
||
333 | *
|
||
334 | * @return void
|
||
335 | */
|
||
336 | public function testNodeActionAuthorize() { |
||
337 | App::build(array( |
||
338 | 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS) |
||
339 | ), App::RESET); |
||
340 | CakePlugin::load('TestPlugin'); |
||
341 | |||
342 | $Aro = new DbAroTest(); |
||
343 | $Aro->create();
|
||
344 | $Aro->save(array('model' => 'TestPluginAuthUser', 'foreign_key' => 1)); |
||
345 | $result = $Aro->id; |
||
346 | $expected = 5; |
||
347 | $this->assertEquals($expected, $result); |
||
348 | |||
349 | $node = $Aro->node(array('TestPlugin.TestPluginAuthUser' => array('id' => 1, 'user' => 'mariano'))); |
||
350 | $result = Hash::get($node, '0.DbAroTest.id'); |
||
351 | $expected = $Aro->id; |
||
352 | $this->assertEquals($expected, $result); |
||
353 | CakePlugin::unload('TestPlugin'); |
||
354 | } |
||
355 | } |