Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * DbAclTest file.
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
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://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Test.Case.Controller.Component.Acl
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ComponentCollection', 'Controller');
20
App::uses('AclComponent', 'Controller/Component');
21
App::uses('DbAcl', 'Controller/Component/Acl');
22
App::uses('AclNode', 'Model');
23
App::uses('Permission', 'Model');
24
require_once dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'Model' . DS . 'models.php';
25
 
26
/**
27
 * AclNodeTwoTestBase class
28
 *
29
 * @package       Cake.Test.Case.Controller.Component.Acl
30
 */
31
class AclNodeTwoTestBase extends AclNode {
32
 
33
/**
34
 * useDbConfig property
35
 *
36
 * @var string
37
 */
38
	public $useDbConfig = 'test';
39
 
40
/**
41
 * cacheSources property
42
 *
43
 * @var boolean
44
 */
45
	public $cacheSources = false;
46
}
47
 
48
/**
49
 * AroTwoTest class
50
 *
51
 * @package       Cake.Test.Case.Controller.Component.Acl
52
 */
53
class AroTwoTest extends AclNodeTwoTestBase {
54
 
55
/**
56
 * name property
57
 *
58
 * @var string
59
 */
60
	public $name = 'AroTwoTest';
61
 
62
/**
63
 * useTable property
64
 *
65
 * @var string
66
 */
67
	public $useTable = 'aro_twos';
68
 
69
/**
70
 * hasAndBelongsToMany property
71
 *
72
 * @var array
73
 */
74
	public $hasAndBelongsToMany = array('AcoTwoTest' => array('with' => 'PermissionTwoTest'));
75
}
76
 
77
/**
78
 * AcoTwoTest class
79
 *
80
 * @package       Cake.Test.Case.Controller.Component.Acl
81
 */
82
class AcoTwoTest extends AclNodeTwoTestBase {
83
 
84
/**
85
 * name property
86
 *
87
 * @var string
88
 */
89
	public $name = 'AcoTwoTest';
90
 
91
/**
92
 * useTable property
93
 *
94
 * @var string
95
 */
96
	public $useTable = 'aco_twos';
97
 
98
/**
99
 * hasAndBelongsToMany property
100
 *
101
 * @var array
102
 */
103
	public $hasAndBelongsToMany = array('AroTwoTest' => array('with' => 'PermissionTwoTest'));
104
}
105
 
106
/**
107
 * PermissionTwoTest class
108
 *
109
 * @package       Cake.Test.Case.Controller.Component.Acl
110
 */
111
class PermissionTwoTest extends Permission {
112
 
113
/**
114
 * name property
115
 *
116
 * @var string
117
 */
118
	public $name = 'PermissionTwoTest';
119
 
120
/**
121
 * useTable property
122
 *
123
 * @var string
124
 */
125
	public $useTable = 'aros_aco_twos';
126
 
127
/**
128
 * cacheQueries property
129
 *
130
 * @var boolean
131
 */
132
	public $cacheQueries = false;
133
 
134
/**
135
 * belongsTo property
136
 *
137
 * @var array
138
 */
139
	public $belongsTo = array('AroTwoTest' => array('foreignKey' => 'aro_id'), 'AcoTwoTest' => array('foreignKey' => 'aco_id'));
140
 
141
/**
142
 * actsAs property
143
 *
144
 * @var mixed null
145
 */
146
	public $actsAs = null;
147
}
148
 
149
/**
150
 * DbAclTwoTest class
151
 *
152
 * @package       Cake.Test.Case.Controller.Component.Acl
153
 */
154
class DbAclTwoTest extends DbAcl {
155
 
156
/**
157
 * construct method
158
 *
159
 */
160
	public function __construct() {
161
		$this->Aro = new AroTwoTest();
162
		$this->Aro->Permission = new PermissionTwoTest();
163
		$this->Aco = new AcoTwoTest();
164
		$this->Aro->Permission = new PermissionTwoTest();
165
 
166
		$this->Permission = $this->Aro->Permission;
167
		$this->Permission->Aro = $this->Aro;
168
		$this->Permission->Aco = $this->Aco;
169
	}
170
 
171
}
172
 
173
/**
174
 * Test case for AclComponent using the DbAcl implementation.
175
 *
176
 * @package       Cake.Test.Case.Controller.Component.Acl
177
 */
178
class DbAclTest extends CakeTestCase {
179
 
180
/**
181
 * fixtures property
182
 *
183
 * @var array
184
 */
185
	public $fixtures = array('core.aro_two', 'core.aco_two', 'core.aros_aco_two');
186
 
187
/**
188
 * setUp method
189
 *
190
 * @return void
191
 */
192
	public function setUp() {
193
		parent::setUp();
194
		Configure::write('Acl.classname', 'DbAclTwoTest');
195
		Configure::write('Acl.database', 'test');
196
		$Collection = new ComponentCollection();
197
		$this->Acl = new AclComponent($Collection);
198
	}
199
 
200
/**
201
 * tearDown method
202
 *
203
 * @return void
204
 */
205
	public function tearDown() {
206
		parent::tearDown();
207
		unset($this->Acl);
208
	}
209
 
210
/**
211
 * testAclCreate method
212
 *
213
 * @return void
214
 */
215
	public function testCreate() {
216
		$this->Acl->Aro->create(array('alias' => 'Chotchkey'));
217
		$this->assertTrue((bool)$this->Acl->Aro->save());
218
 
219
		$parent = $this->Acl->Aro->id;
220
 
221
		$this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Joanna'));
222
		$this->assertTrue((bool)$this->Acl->Aro->save());
223
 
224
		$this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Stapler'));
225
		$this->assertTrue((bool)$this->Acl->Aro->save());
226
 
227
		$root = $this->Acl->Aco->node('ROOT');
228
		$parent = $root[0]['AcoTwoTest']['id'];
229
 
230
		$this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'Drinks'));
231
		$this->assertTrue((bool)$this->Acl->Aco->save());
232
 
233
		$this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'PiecesOfFlair'));
234
		$this->assertTrue((bool)$this->Acl->Aco->save());
235
	}
236
 
237
/**
238
 * testAclCreateWithParent method
239
 *
240
 * @return void
241
 */
242
	public function testCreateWithParent() {
243
		$parent = $this->Acl->Aro->findByAlias('Peter', null, null, -1);
244
		$this->Acl->Aro->create();
245
		$this->Acl->Aro->save(array(
246
			'alias' => 'Subordinate',
247
			'model' => 'User',
248
			'foreign_key' => 7,
249
			'parent_id' => $parent['AroTwoTest']['id']
250
		));
251
		$result = $this->Acl->Aro->findByAlias('Subordinate', null, null, -1);
252
		$this->assertEquals(16, $result['AroTwoTest']['lft']);
253
		$this->assertEquals(17, $result['AroTwoTest']['rght']);
254
	}
255
 
256
/**
257
 * testDbAclAllow method
258
 *
259
 * @expectedException PHPUnit_Framework_Error_Warning
260
 * @return void
261
 */
262
	public function testAllow() {
263
		$this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'read'));
264
		$this->assertTrue($this->Acl->allow('Micheal', 'tpsReports', array('read', 'delete', 'update')));
265
		$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'update'));
266
		$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'read'));
267
		$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
268
 
269
		$this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'create'));
270
		$this->assertTrue($this->Acl->allow('Micheal', 'ROOT/tpsReports', 'create'));
271
		$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'create'));
272
		$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
273
		$this->assertTrue($this->Acl->allow('Micheal', 'printers', 'create'));
274
		// Michael no longer has his delete permission for tpsReports!
275
		$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
276
		$this->assertTrue($this->Acl->check('Micheal', 'printers', 'create'));
277
 
278
		$this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view'));
279
		$this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/view', '*'));
280
		$this->assertTrue($this->Acl->check('Samir', 'view', 'read'));
281
		$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
282
 
283
		$this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update', '*'));
284
		$this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/update', '*'));
285
		$this->assertTrue($this->Acl->check('Samir', 'update', 'read'));
286
		$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update', 'update'));
287
		// Samir should still have his tpsReports/view permissions, but does not
288
		$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
289
 
290
		$this->assertFalse($this->Acl->allow('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
291
	}
292
 
293
/**
294
 * Test that allow() with an invalid permission name triggers an error.
295
 *
296
 * @expectedException CakeException
297
 * @return void
298
 */
299
	public function testAllowInvalidPermission() {
300
		$this->Acl->allow('Micheal', 'tpsReports', 'derp');
301
	}
302
 
303
/**
304
 * testAllowInvalidNode method
305
 *
306
 * @expectedException PHPUnit_Framework_Error_Warning
307
 * @return void
308
 */
309
	public function testAllowInvalidNode() {
310
		$this->Acl->allow('Homer', 'tpsReports', 'create');
311
	}
312
 
313
/**
314
 * testDbAclCheck method
315
 *
316
 * @return void
317
 */
318
	public function testCheck() {
319
		$this->assertTrue($this->Acl->check('Samir', 'print', 'read'));
320
		$this->assertTrue($this->Acl->check('Lumbergh', 'current', 'read'));
321
		$this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
322
		$this->assertFalse($this->Acl->check('Milton', 'current', 'update'));
323
 
324
		$this->assertFalse($this->Acl->check(null, 'printers', 'create'));
325
		$this->assertFalse($this->Acl->check('managers', null, 'read'));
326
 
327
		$this->assertTrue($this->Acl->check('Bobs', 'ROOT/tpsReports/view/current', 'read'));
328
		$this->assertFalse($this->Acl->check('Samir', 'ROOT/tpsReports/update', 'read'));
329
 
330
		$this->assertFalse($this->Acl->check('root/users/Milton', 'smash', 'delete'));
331
	}
332
 
333
/**
334
 * testCheckInvalidNode method
335
 *
336
 * @expectedException PHPUnit_Framework_Error_Warning
337
 * @return void
338
 */
339
	public function testCheckInvalidNode() {
340
		$this->assertFalse($this->Acl->check('WRONG', 'tpsReports', 'read'));
341
	}
342
 
343
/**
344
 * testCheckInvalidPermission method
345
 *
346
 * @expectedException PHPUnit_Framework_Error_Notice
347
 * @return void
348
 */
349
	public function testCheckInvalidPermission() {
350
		$this->Acl->check('Lumbergh', 'smash', 'foobar');
351
	}
352
 
353
/**
354
 * testCheckMissingPermission method
355
 *
356
 * @expectedException PHPUnit_Framework_Error_Warning
357
 * @return void
358
 */
359
	public function testCheckMissingPermission() {
360
		$this->Acl->check('users', 'NonExistent', 'read');
361
	}
362
 
363
/**
364
 * testDbAclCascadingDeny function
365
 *
366
 * Setup the acl permissions such that Bobs inherits from admin.
367
 * deny Admin delete access to a specific resource, check the permissions are inherited.
368
 *
369
 * @return void
370
 */
371
	public function testAclCascadingDeny() {
372
		$this->Acl->inherit('Bobs', 'ROOT', '*');
373
		$this->assertTrue($this->Acl->check('admin', 'tpsReports', 'delete'));
374
		$this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'delete'));
375
		$this->Acl->deny('admin', 'tpsReports', 'delete');
376
		$this->assertFalse($this->Acl->check('admin', 'tpsReports', 'delete'));
377
		$this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'delete'));
378
	}
379
 
380
/**
381
 * testDbAclDeny method
382
 *
383
 * @expectedException PHPUnit_Framework_Error_Warning
384
 * @return void
385
 */
386
	public function testDeny() {
387
		$this->assertTrue($this->Acl->check('Micheal', 'smash', 'delete'));
388
		$this->Acl->deny('Micheal', 'smash', 'delete');
389
		$this->assertFalse($this->Acl->check('Micheal', 'smash', 'delete'));
390
		$this->assertTrue($this->Acl->check('Micheal', 'smash', 'read'));
391
		$this->assertTrue($this->Acl->check('Micheal', 'smash', 'create'));
392
		$this->assertTrue($this->Acl->check('Micheal', 'smash', 'update'));
393
		$this->assertFalse($this->Acl->check('Micheal', 'smash', '*'));
394
 
395
		$this->assertTrue($this->Acl->check('Samir', 'refill', '*'));
396
		$this->Acl->deny('Samir', 'refill', '*');
397
		$this->assertFalse($this->Acl->check('Samir', 'refill', 'create'));
398
		$this->assertFalse($this->Acl->check('Samir', 'refill', 'update'));
399
		$this->assertFalse($this->Acl->check('Samir', 'refill', 'read'));
400
		$this->assertFalse($this->Acl->check('Samir', 'refill', 'delete'));
401
 
402
		$result = $this->Acl->Aro->Permission->find('all', array('conditions' => array('AroTwoTest.alias' => 'Samir')));
403
		$expected = '-1';
404
		$this->assertEquals($expected, $result[0]['PermissionTwoTest']['_delete']);
405
 
406
		$this->assertFalse($this->Acl->deny('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
407
	}
408
 
409
/**
410
 * testAclNodeLookup method
411
 *
412
 * @return void
413
 */
414
	public function testAclNodeLookup() {
415
		$result = $this->Acl->Aro->node('root/users/Samir');
416
		$expected = array(
417
			array('AroTwoTest' => array('id' => '7', 'parent_id' => '4', 'model' => 'User', 'foreign_key' => 3, 'alias' => 'Samir')),
418
			array('AroTwoTest' => array('id' => '4', 'parent_id' => '1', 'model' => 'Group', 'foreign_key' => 3, 'alias' => 'users')),
419
			array('AroTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'root'))
420
		);
421
		$this->assertEquals($expected, $result);
422
 
423
		$result = $this->Acl->Aco->node('ROOT/tpsReports/view/current');
424
		$expected = array(
425
			array('AcoTwoTest' => array('id' => '4', 'parent_id' => '3', 'model' => null, 'foreign_key' => null, 'alias' => 'current')),
426
			array('AcoTwoTest' => array('id' => '3', 'parent_id' => '2', 'model' => null, 'foreign_key' => null, 'alias' => 'view')),
427
			array('AcoTwoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'tpsReports')),
428
			array('AcoTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'ROOT')),
429
		);
430
		$this->assertEquals($expected, $result);
431
	}
432
 
433
/**
434
 * testDbInherit method
435
 *
436
 * @return void
437
 */
438
	public function testInherit() {
439
		//parent doesn't have access inherit should still deny
440
		$this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
441
		$this->Acl->inherit('Milton', 'smash', 'delete');
442
		$this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
443
 
444
		//inherit parent
445
		$this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
446
		$this->Acl->inherit('Milton', 'smash', 'read');
447
		$this->assertTrue($this->Acl->check('Milton', 'smash', 'read'));
448
	}
449
 
450
/**
451
 * testDbGrant method
452
 *
453
 * @expectedException PHPUnit_Framework_Error_Warning
454
 * @return void
455
 */
456
	public function testGrant() {
457
		$this->assertFalse($this->Acl->check('Samir', 'tpsReports', 'create'));
458
		$this->Acl->allow('Samir', 'tpsReports', 'create');
459
		$this->assertTrue($this->Acl->check('Samir', 'tpsReports', 'create'));
460
 
461
		$this->assertFalse($this->Acl->check('Micheal', 'view', 'read'));
462
		$this->Acl->allow('Micheal', 'view', array('read', 'create', 'update'));
463
		$this->assertTrue($this->Acl->check('Micheal', 'view', 'read'));
464
		$this->assertTrue($this->Acl->check('Micheal', 'view', 'create'));
465
		$this->assertTrue($this->Acl->check('Micheal', 'view', 'update'));
466
		$this->assertFalse($this->Acl->check('Micheal', 'view', 'delete'));
467
 
468
		$this->assertFalse($this->Acl->allow('Peter', 'ROOT/tpsReports/DoesNotExist', 'create'));
469
	}
470
 
471
/**
472
 * testDbRevoke method
473
 *
474
 * @expectedException PHPUnit_Framework_Error_Warning
475
 * @return void
476
 */
477
	public function testRevoke() {
478
		$this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'read'));
479
		$this->Acl->deny('Bobs', 'tpsReports', 'read');
480
		$this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'read'));
481
 
482
		$this->assertTrue($this->Acl->check('users', 'printers', 'read'));
483
		$this->Acl->deny('users', 'printers', 'read');
484
		$this->assertFalse($this->Acl->check('users', 'printers', 'read'));
485
		$this->assertFalse($this->Acl->check('Samir', 'printers', 'read'));
486
		$this->assertFalse($this->Acl->check('Peter', 'printers', 'read'));
487
 
488
		$this->Acl->deny('Bobs', 'ROOT/printers/DoesNotExist', 'create');
489
	}
490
 
491
/**
492
 * debug function - to help editing/creating test cases for the ACL component
493
 *
494
 * To check the overall ACL status at any time call $this->_debug();
495
 * Generates a list of the current aro and aco structures and a grid dump of the permissions that are defined
496
 * Only designed to work with the db based ACL
497
 *
498
 * @param boolean $treesToo
499
 * @return void
500
 */
501
	protected function _debug($printTreesToo = false) {
502
		$this->Acl->Aro->displayField = 'alias';
503
		$this->Acl->Aco->displayField = 'alias';
504
		$aros = $this->Acl->Aro->find('list', array('order' => 'lft'));
505
		$acos = $this->Acl->Aco->find('list', array('order' => 'lft'));
506
		$rights = array('*', 'create', 'read', 'update', 'delete');
507
		$permissions['Aros v Acos >'] = $acos;
508
		foreach ($aros as $aro) {
509
			$row = array();
510
			foreach ($acos as $aco) {
511
				$perms = '';
512
				foreach ($rights as $right) {
513
					if ($this->Acl->check($aro, $aco, $right)) {
514
						if ($right === '*') {
515
							$perms .= '****';
516
							break;
517
						}
518
						$perms .= $right[0];
519
					} elseif ($right !== '*') {
520
						$perms .= ' ';
521
					}
522
				}
523
				$row[] = $perms;
524
			}
525
			$permissions[$aro] = $row;
526
		}
527
		foreach ($permissions as $key => $values) {
528
			array_unshift($values, $key);
529
			$values = array_map(array(&$this, '_pad'), $values);
530
			$permissions[$key] = implode(' ', $values);
531
		}
532
		$permissions = array_map(array(&$this, '_pad'), $permissions);
533
		array_unshift($permissions, 'Current Permissions :');
534
		if ($printTreesToo) {
535
			debug(array('aros' => $this->Acl->Aro->generateTreeList(), 'acos' => $this->Acl->Aco->generateTreeList()));
536
		}
537
		debug(implode("\r\n", $permissions));
538
	}
539
 
540
/**
541
 * pad function
542
 * Used by debug to format strings used in the data dump
543
 *
544
 * @param string $string
545
 * @param integer $len
546
 * @return void
547
 */
548
	protected function _pad($string = '', $len = 14) {
549
		return str_pad($string, $len);
550
	}
551
}