Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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
 */
126
	public function parentNode() {
127
		return null;
128
	}
129
 
130
}
131
 
132
/**
133
 * AclPost class
134
 *
135
 * @package       Cake.Test.Case.Model.Behavior
136
 */
137
class AclPost extends CakeTestModel {
138
 
139
/**
140
 * name property
141
 *
142
 * @var string
143
 */
144
	public $name = 'Post';
145
 
146
/**
147
 * useTable property
148
 *
149
 * @var string
150
 */
151
	public $useTable = 'posts';
152
 
153
/**
154
 * actsAs property
155
 *
156
 * @var array
157
 */
158
	public $actsAs = array('Acl' => array('type' => 'Controlled'));
159
 
160
/**
161
 * parentNode
162
 *
163
 */
164
	public function parentNode() {
165
		return null;
166
	}
167
 
168
}
169
 
170
/**
171
 * AclBehaviorTest class
172
 *
173
 * @package       Cake.Test.Case.Model.Behavior
174
 */
175
class AclBehaviorTest extends CakeTestCase {
176
 
177
/**
178
 * Aco property
179
 *
180
 * @var Aco
181
 */
182
	public $Aco;
183
 
184
/**
185
 * Aro property
186
 *
187
 * @var Aro
188
 */
189
	public $Aro;
190
 
191
/**
192
 * fixtures property
193
 *
194
 * @var array
195
 */
196
	public $fixtures = array('core.person', 'core.user', 'core.post', 'core.aco', 'core.aro', 'core.aros_aco');
197
 
198
/**
199
 * Set up the test
200
 *
201
 * @return void
202
 */
203
	public function setUp() {
204
		parent::setUp();
205
		Configure::write('Acl.database', 'test');
206
 
207
		$this->Aco = new Aco();
208
		$this->Aro = new Aro();
209
	}
210
 
211
/**
212
 * tearDown method
213
 *
214
 * @return void
215
 */
216
	public function tearDown() {
217
		parent::tearDown();
218
		unset($this->Aro, $this->Aco);
219
	}
220
 
221
/**
222
 * Test Setup of AclBehavior
223
 *
224
 * @return void
225
 */
226
	public function testSetup() {
227
		parent::setUp();
228
		$User = new AclUser();
229
		$this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
230
		$this->assertEquals('requester', $User->Behaviors->Acl->settings['User']['type']);
231
		$this->assertTrue(is_object($User->Aro));
232
 
233
		$Post = new AclPost();
234
		$this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
235
		$this->assertEquals('controlled', $Post->Behaviors->Acl->settings['Post']['type']);
236
		$this->assertTrue(is_object($Post->Aco));
237
	}
238
 
239
/**
240
 * Test Setup of AclBehavior as both requester and controlled
241
 *
242
 * @return void
243
 */
244
	public function testSetupMulti() {
245
		$User = new AclPerson();
246
		$this->assertTrue(isset($User->Behaviors->Acl->settings['AclPerson']));
247
		$this->assertEquals('both', $User->Behaviors->Acl->settings['AclPerson']['type']);
248
		$this->assertTrue(is_object($User->Aro));
249
		$this->assertTrue(is_object($User->Aco));
250
	}
251
 
252
/**
253
 * test After Save
254
 *
255
 * @return void
256
 */
257
	public function testAfterSave() {
258
		$Post = new AclPost();
259
		$data = array(
260
			'Post' => array(
261
				'author_id' => 1,
262
				'title' => 'Acl Post',
263
				'body' => 'post body',
264
				'published' => 1
265
			),
266
		);
267
		$Post->save($data);
268
		$result = $this->Aco->find('first', array(
269
			'conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)
270
		));
271
		$this->assertTrue(is_array($result));
272
		$this->assertEquals('Post', $result['Aco']['model']);
273
		$this->assertEquals($Post->id, $result['Aco']['foreign_key']);
274
 
275
		$aroData = array(
276
			'Aro' => array(
277
				'model' => 'AclPerson',
278
				'foreign_key' => 2,
279
				'parent_id' => null
280
			)
281
		);
282
		$this->Aro->save($aroData);
283
 
284
		$acoData = array(
285
			'Aco' => array(
286
				'model' => 'AclPerson',
287
				'foreign_key' => 2,
288
				'parent_id' => null
289
			)
290
		);
291
		$this->Aco->save($acoData);
292
 
293
		$Person = new AclPerson();
294
		$data = array(
295
			'AclPerson' => array(
296
				'name' => 'Trent',
297
				'mother_id' => 2,
298
				'father_id' => 3,
299
			),
300
		);
301
		$Person->save($data);
302
		$result = $this->Aro->find('first', array(
303
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
304
		));
305
		$this->assertTrue(is_array($result));
306
		$this->assertEquals(5, $result['Aro']['parent_id']);
307
 
308
		$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
309
		$this->assertEquals(2, count($node));
310
		$this->assertEquals(5, $node[0]['Aro']['parent_id']);
311
		$this->assertEquals(null, $node[1]['Aro']['parent_id']);
312
 
313
		$aroData = array(
314
			'Aro' => array(
315
			'model' => 'AclPerson',
316
				'foreign_key' => 1,
317
				'parent_id' => null
318
			)
319
		);
320
		$this->Aro->create();
321
		$this->Aro->save($aroData);
322
		$acoData = array(
323
			'Aco' => array(
324
				'model' => 'AclPerson',
325
				'foreign_key' => 1,
326
				'parent_id' => null
327
		));
328
		$this->Aco->create();
329
		$this->Aco->save($acoData);
330
		$Person->read(null, 8);
331
		$Person->set('mother_id', 1);
332
		$Person->save();
333
		$result = $this->Aro->find('first', array(
334
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
335
		));
336
		$this->assertTrue(is_array($result));
337
		$this->assertEquals(7, $result['Aro']['parent_id']);
338
 
339
		$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
340
		$this->assertEquals(2, count($node));
341
		$this->assertEquals(7, $node[0]['Aro']['parent_id']);
342
		$this->assertEquals(null, $node[1]['Aro']['parent_id']);
343
	}
344
 
345
/**
346
 * test that an afterSave on an update does not cause parent_id to become null.
347
 *
348
 * @return void
349
 */
350
	public function testAfterSaveUpdateParentIdNotNull() {
351
		$aroData = array(
352
			'Aro' => array(
353
				'model' => 'AclPerson',
354
				'foreign_key' => 2,
355
				'parent_id' => null
356
			)
357
		);
358
		$this->Aro->save($aroData);
359
 
360
		$acoData = array(
361
			'Aco' => array(
362
				'model' => 'AclPerson',
363
				'foreign_key' => 2,
364
				'parent_id' => null
365
			)
366
		);
367
		$this->Aco->save($acoData);
368
 
369
		$Person = new AclPerson();
370
		$data = array(
371
			'AclPerson' => array(
372
				'name' => 'Trent',
373
				'mother_id' => 2,
374
				'father_id' => 3,
375
			),
376
		);
377
		$Person->save($data);
378
		$result = $this->Aro->find('first', array(
379
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
380
		));
381
		$this->assertTrue(is_array($result));
382
		$this->assertEquals(5, $result['Aro']['parent_id']);
383
 
384
		$Person->save(array('id' => $Person->id, 'name' => 'Bruce'));
385
		$result = $this->Aro->find('first', array(
386
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
387
		));
388
		$this->assertEquals(5, $result['Aro']['parent_id']);
389
	}
390
 
391
/**
392
 * Test After Delete
393
 *
394
 * @return void
395
 */
396
	public function testAfterDelete() {
397
		$aroData = array(
398
			'Aro' => array(
399
				'model' => 'AclPerson',
400
				'foreign_key' => 2,
401
				'parent_id' => null
402
			)
403
		);
404
		$this->Aro->save($aroData);
405
 
406
		$acoData = array(
407
			'Aco' => array(
408
				'model' => 'AclPerson',
409
				'foreign_key' => 2,
410
				'parent_id' => null
411
			)
412
		);
413
		$this->Aco->save($acoData);
414
		$Person = new AclPerson();
415
 
416
		$data = array(
417
			'AclPerson' => array(
418
				'name' => 'Trent',
419
				'mother_id' => 2,
420
				'father_id' => 3,
421
			),
422
		);
423
		$Person->save($data);
424
		$id = $Person->id;
425
		$node = $Person->node(null, 'Aro');
426
		$this->assertEquals(2, count($node));
427
		$this->assertEquals(5, $node[0]['Aro']['parent_id']);
428
		$this->assertEquals(null, $node[1]['Aro']['parent_id']);
429
 
430
		$Person->delete($id);
431
		$result = $this->Aro->find('first', array(
432
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
433
		));
434
		$this->assertTrue(empty($result));
435
		$result = $this->Aro->find('first', array(
436
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
437
		));
438
		$this->assertFalse(empty($result));
439
 
440
		$data = array(
441
			'AclPerson' => array(
442
				'name' => 'Trent',
443
				'mother_id' => 2,
444
				'father_id' => 3,
445
			),
446
		);
447
		$Person->save($data);
448
		$id = $Person->id;
449
		$Person->delete(2);
450
		$result = $this->Aro->find('first', array(
451
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
452
		));
453
		$this->assertTrue(empty($result));
454
 
455
		$result = $this->Aro->find('first', array(
456
			'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
457
		));
458
		$this->assertTrue(empty($result));
459
	}
460
 
461
/**
462
 * Test Node()
463
 *
464
 * @return void
465
 */
466
	public function testNode() {
467
		$Person = new AclPerson();
468
		$aroData = array(
469
			'Aro' => array(
470
				'model' => 'AclPerson',
471
				'foreign_key' => 2,
472
				'parent_id' => null
473
			)
474
		);
475
		$this->Aro->save($aroData);
476
 
477
		$Person->id = 2;
478
		$result = $Person->node(null, 'Aro');
479
		$this->assertTrue(is_array($result));
480
		$this->assertEquals(1, count($result));
481
	}
482
}