Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?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 boolean
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 boolean
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
 * construct method
185
 *
186
 */
187
	public function __construct() {
188
		$this->Aro = new DbAroTest();
189
		$this->Aro->Permission = new DbPermissionTest();
190
		$this->Aco = new DbAcoTest();
191
		$this->Aro->Permission = new DbPermissionTest();
192
	}
193
 
194
}
195
 
196
/**
197
 * AclNodeTest class
198
 *
199
 * @package       Cake.Test.Case.Model
200
 */
201
class AclNodeTest extends CakeTestCase {
202
 
203
/**
204
 * fixtures property
205
 *
206
 * @var array
207
 */
208
	public $fixtures = array('core.aro', 'core.aco', 'core.aros_aco', 'core.aco_action', 'core.auth_user');
209
 
210
/**
211
 * setUp method
212
 *
213
 * @return void
214
 */
215
	public function setUp() {
216
		parent::setUp();
217
		Configure::write('Acl.classname', 'TestDbAcl');
218
		Configure::write('Acl.database', 'test');
219
	}
220
 
221
/**
222
 * testNode method
223
 *
224
 * @return void
225
 */
226
	public function testNode() {
227
		$Aco = new DbAcoTest();
228
		$result = Hash::extract($Aco->node('Controller1'), '{n}.DbAcoTest.id');
229
		$expected = array(2, 1);
230
		$this->assertEquals($expected, $result);
231
 
232
		$result = Hash::extract($Aco->node('Controller1/action1'), '{n}.DbAcoTest.id');
233
		$expected = array(3, 2, 1);
234
		$this->assertEquals($expected, $result);
235
 
236
		$result = Hash::extract($Aco->node('Controller2/action1'), '{n}.DbAcoTest.id');
237
		$expected = array(7, 6, 1);
238
		$this->assertEquals($expected, $result);
239
 
240
		$result = Hash::extract($Aco->node('Controller1/action2'), '{n}.DbAcoTest.id');
241
		$expected = array(5, 2, 1);
242
		$this->assertEquals($expected, $result);
243
 
244
		$result = Hash::extract($Aco->node('Controller1/action1/record1'), '{n}.DbAcoTest.id');
245
		$expected = array(4, 3, 2, 1);
246
		$this->assertEquals($expected, $result);
247
 
248
		$result = Hash::extract($Aco->node('Controller2/action1/record1'), '{n}.DbAcoTest.id');
249
		$expected = array(8, 7, 6, 1);
250
		$this->assertEquals($expected, $result);
251
 
252
		$this->assertFalse($Aco->node('Controller2/action3'));
253
 
254
		$this->assertFalse($Aco->node('Controller2/action3/record5'));
255
 
256
		$result = $Aco->node('');
257
		$this->assertEquals(null, $result);
258
	}
259
 
260
/**
261
 * test that node() doesn't dig deeper than it should.
262
 *
263
 * @return void
264
 */
265
	public function testNodeWithDuplicatePathSegments() {
266
		$Aco = new DbAcoTest();
267
		$nodes = $Aco->node('ROOT/Users');
268
		$this->assertEquals(1, $nodes[0]['DbAcoTest']['parent_id'], 'Parent id does not point at ROOT. %s');
269
	}
270
 
271
/**
272
 * testNodeArrayFind method
273
 *
274
 * @return void
275
 */
276
	public function testNodeArrayFind() {
277
		$Aro = new DbAroTest();
278
		Configure::write('DbAclbindMode', 'string');
279
		$result = Hash::extract($Aro->node(array('DbAroUserTest' => array('id' => '1', 'foreign_key' => '1'))), '{n}.DbAroTest.id');
280
		$expected = array(3, 2, 1);
281
		$this->assertEquals($expected, $result);
282
 
283
		Configure::write('DbAclbindMode', 'array');
284
		$result = Hash::extract($Aro->node(array('DbAroUserTest' => array('id' => 4, 'foreign_key' => 2))), '{n}.DbAroTest.id');
285
		$expected = array(4);
286
		$this->assertEquals($expected, $result);
287
	}
288
 
289
/**
290
 * testNodeObjectFind method
291
 *
292
 * @return void
293
 */
294
	public function testNodeObjectFind() {
295
		$Aro = new DbAroTest();
296
		$Model = new DbAroUserTest();
297
		$Model->id = 1;
298
		$result = Hash::extract($Aro->node($Model), '{n}.DbAroTest.id');
299
		$expected = array(3, 2, 1);
300
		$this->assertEquals($expected, $result);
301
 
302
		$Model->id = 2;
303
		$result = Hash::extract($Aro->node($Model), '{n}.DbAroTest.id');
304
		$expected = array(4, 2, 1);
305
		$this->assertEquals($expected, $result);
306
	}
307
 
308
/**
309
 * testNodeAliasParenting method
310
 *
311
 * @return void
312
 */
313
	public function testNodeAliasParenting() {
314
		$Aco = ClassRegistry::init('DbAcoTest');
315
		$db = $Aco->getDataSource();
316
		$db->truncate($Aco);
317
 
318
		$Aco->create(array('model' => null, 'foreign_key' => null, 'parent_id' => null, 'alias' => 'Application'));
319
		$Aco->save();
320
 
321
		$Aco->create(array('model' => null, 'foreign_key' => null, 'parent_id' => $Aco->id, 'alias' => 'Pages'));
322
		$Aco->save();
323
 
324
		$result = $Aco->find('all');
325
		$expected = array(
326
			array('DbAcoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'Application', 'lft' => '1', 'rght' => '4'), 'DbAroTest' => array()),
327
			array('DbAcoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'Pages', 'lft' => '2', 'rght' => '3'), 'DbAroTest' => array())
328
		);
329
		$this->assertEquals($expected, $result);
330
	}
331
 
332
/**
333
 * testNodeActionAuthorize method
334
 *
335
 * @return void
336
 */
337
	public function testNodeActionAuthorize() {
338
		App::build(array(
339
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
340
		), App::RESET);
341
		CakePlugin::load('TestPlugin');
342
 
343
		$Aro = new DbAroTest();
344
		$Aro->create();
345
		$Aro->save(array('model' => 'TestPluginAuthUser', 'foreign_key' => 1));
346
		$result = $Aro->id;
347
		$expected = 5;
348
		$this->assertEquals($expected, $result);
349
 
350
		$node = $Aro->node(array('TestPlugin.TestPluginAuthUser' => array('id' => 1, 'user' => 'mariano')));
351
		$result = Hash::get($node, '0.DbAroTest.id');
352
		$expected = $Aro->id;
353
		$this->assertEquals($expected, $result);
354
		CakePlugin::unload('TestPlugin');
355
	}
356
}