Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * AclBehaviorTest file
4
 *
5
 * Test the Acl Behavior
6
 *
7
 * CakePHP : Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://cakephp.org CakePHP Project
16
 * @package       Cake.Test.Case.Model.Behavior
17
 * @since         CakePHP v 1.2.0.4487
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('AclBehavior', 'Model/Behavior');
22
App::uses('Aco', 'Model');
23
App::uses('Aro', 'Model');
24
App::uses('AclNode', 'Model');
25
App::uses('DbAcl', 'Model');
26
 
27
/**
28
 * Test Person class - self joined model
29
 *
30
 * @package       Cake.Test.Case.Model.Behavior
31
 */
32
class AclPerson extends CakeTestModel {
33
 
34
/**
35
 * useTable property
36
 *
37
 * @var string
38
 */
39
	public $useTable = 'people';
40
 
41
/**
42
 * actsAs property
43
 *
44
 * @var array
45
 */
46
	public $actsAs = array('Acl' => 'both');
47
 
48
/**
49
 * belongsTo property
50
 *
51
 * @var array
52
 */
53
	public $belongsTo = array(
54
		'Mother' => array(
55
			'className' => 'AclPerson',
56
			'foreignKey' => 'mother_id',
57
		)
58
	);
59
 
60
/**
61
 * hasMany property
62
 *
63
 * @var array
64
 */
65
	public $hasMany = array(
66
		'Child' => array(
67
			'className' => 'AclPerson',
68
			'foreignKey' => 'mother_id'
69
		)
70
	);
71
 
72
/**
73
 * parentNode method
74
 *
75
 * @return void
76
 */
77
	public function parentNode() {
78
		if (!$this->id && empty($this->data)) {
79
			return null;
80
		}
81
		if (isset($this->data['AclPerson']['mother_id'])) {
82
			$motherId = $this->data['AclPerson']['mother_id'];
83
		} else {
84
			$motherId = $this->field('mother_id');
85
		}
86
		if (!$motherId) {
87
			return null;
88
		}
89
		return array('AclPerson' => array('id' => $motherId));
90
	}
91
 
92
}
93
 
94
/**
95
 * AclUser class
96
 *
97
 * @package       Cake.Test.Case.Model.Behavior
98
 */
99
class AclUser extends CakeTestModel {
100
 
101
/**
102
 * name property
103
 *
104
 * @var string
105
 */
106
	public $name = 'User';
107
 
108
/**
109
 * useTable property
110
 *
111
 * @var string
112
 */
113
	public $useTable = 'users';
114
 
115
/**
116
 * actsAs property
117
 *
118
 * @var array
119
 */
120
	public $actsAs = array('Acl' => array('type' => 'requester'));
121
 
122
/**
123
 * parentNode
124
 *
125
 * @return null
126
 */
127
	public function parentNode() {
128
		return null;
129
	}
130
 
131
}
132
 
133
/**
134
 * AclPost class
135
 *
136
 * @package       Cake.Test.Case.Model.Behavior
137
 */
138
class AclPost extends CakeTestModel {
139
 
140
/**
141
 * name property
142
 *
143
 * @var string
144
 */
145
	public $name = 'Post';
146
 
147
/**
148
 * useTable property
149
 *
150
 * @var string
151
 */
152
	public $useTable = 'posts';
153
 
154
/**
155
 * actsAs property
156
 *
157
 * @var array
158
 */
159
	public $actsAs = array('Acl' => array('type' => 'Controlled'));
160
 
161
/**
162
 * parentNode
163
 *
164
 * @return null
165
 */
166
	public function parentNode() {
167
		return null;
168
	}
169
 
170
}
171
 
172
/**
173
 * AclBehaviorTest class
174
 *
175
 * @package       Cake.Test.Case.Model.Behavior
176
 */
177
class AclBehaviorTest extends CakeTestCase {
178
 
179
/**
180
 * Aco property
181
 *
182
 * @var Aco
183
 */
184
	public $Aco;
185
 
186
/**
187
 * Aro property
188
 *
189
 * @var Aro
190
 */
191
	public $Aro;
192
 
193
/**
194
 * fixtures property
195
 *
196
 * @var array
197
 */
198
	public $fixtures = array('core.person', 'core.user', 'core.post', 'core.aco', 'core.aro', 'core.aros_aco');
199
 
200
/**
201
 * Set up the test
202
 *
203
 * @return void
204
 */
205
	public function setUp() {
206
		parent::setUp();
207
		Configure::write('Acl.database', 'test');
208
 
209
		$this->Aco = new Aco();
210
		$this->Aro = new Aro();
211
	}
212
 
213
/**
214
 * tearDown method
215
 *
216
 * @return void
217
 */
218
	public function tearDown() {
219
		parent::tearDown();
220
		unset($this->Aro, $this->Aco);
221
	}
222
 
223
/**
224
 * Test Setup of AclBehavior
225
 *
226
 * @return void
227
 */
228
	public function testSetup() {
229
		parent::setUp();
230
		$User = new AclUser();
231
		$this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
232
		$this->assertEquals('requester', $User->Behaviors->Acl->settings['User']['type']);
233
		$this->assertTrue(is_object($User->Aro));
234
 
235
		$Post = new AclPost();
236
		$this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
237
		$this->assertEquals('controlled', $Post->Behaviors->Acl->settings['Post']['type']);
238
		$this->assertTrue(is_object($Post->Aco));
239
	}
240
 
241
/**
242
 * Test Setup of AclBehavior as both requester and controlled
243
 *
244
 * @return void
245
 */
246
	public function testSetupMulti() {
247
		$User = new AclPerson();
248
		$this->assertTrue(isset($User->Behaviors->Acl->settings['AclPerson']));
249
		$this->assertEquals('both', $User->Behaviors->Acl->settings['AclPerson']['type']);
250
		$this->assertTrue(is_object($User->Aro));
251
		$this->assertTrue(is_object($User->Aco));
252
	}
253
 
254
/**
255
 * test After Save
256
 *
257
 * @return void
258
 */
259
	public function testAfterSave() {
260
		$Post = new AclPost();
261
		$data = array(
262
			'Post' => array(
263
				'author_id' => 1,
264
				'title' => 'Acl Post',
265
				'body' => 'post body',
266
				'published' => 1
267
			),
268
		);
269
		$Post->save($data);
270
		$result = $this->Aco->find('first', array(
271
			'conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)
272
		));
273
		$this->assertTrue(is_array($result));
274
		$this->assertEquals('Post', $result['Aco']['model']);
275
		$this->assertEquals($Post->id, $result['Aco']['foreign_key']);
276
 
277
		$aroData = array(
278
			'Aro' => array(
279
				'model' => 'AclPerson',
280
				'foreign_key' => 2,
281
				'parent_id' => null
282
			)
283
		);
284
		$this->Aro->save($aroData);
285
 
286
		$acoData = array(
287
			'Aco' => array(
288
				'model' => 'AclPerson',
289
				'foreign_key' => 2,
290
				'parent_id' => null
291
			)
292
		);
293
		$this->Aco->save($acoData);
294
 
295
		$Person = new AclPerson();
296
		$data = array(
297
			'AclPerson' => array(
298
				'name' => 'Trent',
299
				'mother_id' => 2,
300
				'father_id' => 3,
301
			),
302
		);
303
		$Person->save($data);
304
		$result = $this->Aro->find('first', array(
305
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
306
		));
307
		$this->assertTrue(is_array($result));
308
		$this->assertEquals(5, $result['Aro']['parent_id']);
309
 
310
		$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
311
		$this->assertEquals(2, count($node));
312
		$this->assertEquals(5, $node[0]['Aro']['parent_id']);
313
		$this->assertEquals(null, $node[1]['Aro']['parent_id']);
314
 
315
		$aroData = array(
316
			'Aro' => array(
317
			'model' => 'AclPerson',
318
				'foreign_key' => 1,
319
				'parent_id' => null
320
			)
321
		);
322
		$this->Aro->create();
323
		$this->Aro->save($aroData);
324
		$acoData = array(
325
			'Aco' => array(
326
				'model' => 'AclPerson',
327
				'foreign_key' => 1,
328
				'parent_id' => null
329
		));
330
		$this->Aco->create();
331
		$this->Aco->save($acoData);
332
		$Person->read(null, 8);
333
		$Person->set('mother_id', 1);
334
		$Person->save();
335
		$result = $this->Aro->find('first', array(
336
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
337
		));
338
		$this->assertTrue(is_array($result));
339
		$this->assertEquals(7, $result['Aro']['parent_id']);
340
 
341
		$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
342
		$this->assertEquals(2, count($node));
343
		$this->assertEquals(7, $node[0]['Aro']['parent_id']);
344
		$this->assertEquals(null, $node[1]['Aro']['parent_id']);
345
	}
346
 
347
/**
348
 * test that an afterSave on an update does not cause parent_id to become null.
349
 *
350
 * @return void
351
 */
352
	public function testAfterSaveUpdateParentIdNotNull() {
353
		$aroData = array(
354
			'Aro' => array(
355
				'model' => 'AclPerson',
356
				'foreign_key' => 2,
357
				'parent_id' => null
358
			)
359
		);
360
		$this->Aro->save($aroData);
361
 
362
		$acoData = array(
363
			'Aco' => array(
364
				'model' => 'AclPerson',
365
				'foreign_key' => 2,
366
				'parent_id' => null
367
			)
368
		);
369
		$this->Aco->save($acoData);
370
 
371
		$Person = new AclPerson();
372
		$data = array(
373
			'AclPerson' => array(
374
				'name' => 'Trent',
375
				'mother_id' => 2,
376
				'father_id' => 3,
377
			),
378
		);
379
		$Person->save($data);
380
		$result = $this->Aro->find('first', array(
381
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
382
		));
383
		$this->assertTrue(is_array($result));
384
		$this->assertEquals(5, $result['Aro']['parent_id']);
385
 
386
		$Person->save(array('id' => $Person->id, 'name' => 'Bruce'));
387
		$result = $this->Aro->find('first', array(
388
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
389
		));
390
		$this->assertEquals(5, $result['Aro']['parent_id']);
391
	}
392
 
393
/**
394
 * Test After Delete
395
 *
396
 * @return void
397
 */
398
	public function testAfterDelete() {
399
		$aroData = array(
400
			'Aro' => array(
401
				'model' => 'AclPerson',
402
				'foreign_key' => 2,
403
				'parent_id' => null
404
			)
405
		);
406
		$this->Aro->save($aroData);
407
 
408
		$acoData = array(
409
			'Aco' => array(
410
				'model' => 'AclPerson',
411
				'foreign_key' => 2,
412
				'parent_id' => null
413
			)
414
		);
415
		$this->Aco->save($acoData);
416
		$Person = new AclPerson();
417
 
418
		$data = array(
419
			'AclPerson' => array(
420
				'name' => 'Trent',
421
				'mother_id' => 2,
422
				'father_id' => 3,
423
			),
424
		);
425
		$Person->save($data);
426
		$id = $Person->id;
427
		$node = $Person->node(null, 'Aro');
428
		$this->assertEquals(2, count($node));
429
		$this->assertEquals(5, $node[0]['Aro']['parent_id']);
430
		$this->assertEquals(null, $node[1]['Aro']['parent_id']);
431
 
432
		$Person->delete($id);
433
		$result = $this->Aro->find('first', array(
434
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
435
		));
436
		$this->assertTrue(empty($result));
437
		$result = $this->Aro->find('first', array(
438
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
439
		));
440
		$this->assertFalse(empty($result));
441
 
442
		$data = array(
443
			'AclPerson' => array(
444
				'name' => 'Trent',
445
				'mother_id' => 2,
446
				'father_id' => 3,
447
			),
448
		);
449
		$Person->save($data);
450
		$id = $Person->id;
451
		$Person->delete(2);
452
		$result = $this->Aro->find('first', array(
453
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
454
		));
455
		$this->assertTrue(empty($result));
456
 
457
		$result = $this->Aro->find('first', array(
458
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
459
		));
460
		$this->assertTrue(empty($result));
461
	}
462
 
463
/**
464
 * Test Node()
465
 *
466
 * @return void
467
 */
468
	public function testNode() {
469
		$Person = new AclPerson();
470
		$aroData = array(
471
			'Aro' => array(
472
				'model' => 'AclPerson',
473
				'foreign_key' => 2,
474
				'parent_id' => null
475
			)
476
		);
477
		$this->Aro->save($aroData);
478
 
479
		$Person->id = 2;
480
		$result = $Person->node(null, 'Aro');
481
		$this->assertTrue(is_array($result));
482
		$this->assertEquals(1, count($result));
483
	}
484
}