3
* AclComponentTest file
7
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The Open Group Test Suite License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
16
* @subpackage cake.tests.cases.libs.controller.components
17
* @since CakePHP(tm) v 1.2.0.5435
18
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
20
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
21
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
23
App::import(array('controller' .DS . 'components' . DS . 'acl', 'model' . DS . 'db_acl'));
26
* AclNodeTwoTestBase class
29
* @subpackage cake.tests.cases.libs.controller.components
31
class AclNodeTwoTestBase extends AclNode {
34
* useDbConfig property
36
* @var string 'test_suite'
39
var $useDbConfig = 'test_suite';
42
* cacheSources property
47
var $cacheSources = false;
54
* @subpackage cake.tests.cases.libs.controller.components
56
class AroTwoTest extends AclNodeTwoTestBase {
61
* @var string 'AroTwoTest'
64
var $name = 'AroTwoTest';
69
* @var string 'aro_twos'
72
var $useTable = 'aro_twos';
75
* hasAndBelongsToMany property
80
var $hasAndBelongsToMany = array('AcoTwoTest' => array('with' => 'PermissionTwoTest'));
87
* @subpackage cake.tests.cases.libs.controller.components
89
class AcoTwoTest extends AclNodeTwoTestBase {
94
* @var string 'AcoTwoTest'
97
var $name = 'AcoTwoTest';
102
* @var string 'aco_twos'
105
var $useTable = 'aco_twos';
108
* hasAndBelongsToMany property
113
var $hasAndBelongsToMany = array('AroTwoTest' => array('with' => 'PermissionTwoTest'));
117
* PermissionTwoTest class
120
* @subpackage cake.tests.cases.libs.controller.components
122
class PermissionTwoTest extends CakeTestModel {
127
* @var string 'PermissionTwoTest'
130
var $name = 'PermissionTwoTest';
135
* @var string 'aros_aco_twos'
138
var $useTable = 'aros_aco_twos';
141
* cacheQueries property
146
var $cacheQueries = false;
154
var $belongsTo = array('AroTwoTest' => array('foreignKey' => 'aro_id'), 'AcoTwoTest' => array('foreignKey' => 'aco_id'));
169
* @subpackage cake.tests.cases.libs.controller.components
171
class DbAclTwoTest extends DbAcl {
179
function __construct() {
180
$this->Aro =& new AroTwoTest();
181
$this->Aro->Permission =& new PermissionTwoTest();
182
$this->Aco =& new AcoTwoTest();
183
$this->Aro->Permission =& new PermissionTwoTest();
191
* @subpackage cake.tests.cases.libs.controller.components
193
class IniAclTest extends IniAcl {
197
* ACL Component Text case
200
* @subpackage cake.tests.cases.libs.controller.components
202
class AclComponentTest extends CakeTestCase {
210
var $fixtures = array('core.aro_two', 'core.aco_two', 'core.aros_aco_two');
218
function startTest() {
219
$this->Acl =& new AclComponent();
225
* @param mixed $method
229
function before($method) {
230
Configure::write('Acl.classname', 'DbAclTwoTest');
231
Configure::write('Acl.database', 'test_suite');
232
parent::before($method);
241
function tearDown() {
246
* testAclCreate method
251
function testAclCreate() {
252
$this->Acl->Aro->create(array('alias' => 'Chotchkey'));
253
$this->assertTrue($this->Acl->Aro->save());
255
$parent = $this->Acl->Aro->id;
257
$this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Joanna'));
258
$this->assertTrue($this->Acl->Aro->save());
260
$this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Stapler'));
261
$this->assertTrue($this->Acl->Aro->save());
263
$root = $this->Acl->Aco->node('ROOT');
264
$parent = $root[0]['AcoTwoTest']['id'];
266
$this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'Drinks'));
267
$this->assertTrue($this->Acl->Aco->save());
269
$this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'PiecesOfFlair'));
270
$this->assertTrue($this->Acl->Aco->save());
274
* testAclCreateWithParent method
279
function testAclCreateWithParent() {
280
$parent = $this->Acl->Aro->findByAlias('Peter', null, null, -1);
281
$this->Acl->Aro->create();
282
$this->Acl->Aro->save(array(
283
'alias' => 'Subordinate',
286
'parent_id' => $parent['AroTwoTest']['id']
288
$result = $this->Acl->Aro->findByAlias('Subordinate', null, null, -1);
289
$this->assertEqual($result['AroTwoTest']['lft'], 16);
290
$this->assertEqual($result['AroTwoTest']['rght'], 17);
294
* testDbAclAllow method
299
function testDbAclAllow() {
300
$this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'read'));
301
$this->assertTrue($this->Acl->allow('Micheal', 'tpsReports', array('read', 'delete', 'update')));
302
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'update'));
303
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'read'));
304
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
306
$this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'create'));
307
$this->assertTrue($this->Acl->allow('Micheal', 'ROOT/tpsReports', 'create'));
308
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'create'));
309
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
310
$this->assertTrue($this->Acl->allow('Micheal', 'printers', 'create'));
311
// Michael no longer has his delete permission for tpsReports!
312
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
313
$this->assertTrue($this->Acl->check('Micheal', 'printers', 'create'));
315
$this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view'));
316
$this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/view', '*'));
317
$this->assertTrue($this->Acl->check('Samir', 'view', 'read'));
318
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
320
$this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update','*'));
321
$this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/update', '*'));
322
$this->assertTrue($this->Acl->check('Samir', 'update', 'read'));
323
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update', 'update'));
324
// Samir should still have his tpsReports/view permissions, but does not
325
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
327
$this->expectError('DbAcl::allow() - Invalid node');
328
$this->assertFalse($this->Acl->allow('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
330
$this->expectError('DbAcl::allow() - Invalid node');
331
$this->assertFalse($this->Acl->allow('Homer', 'tpsReports', 'create'));
335
* testDbAclCheck method
340
function testDbAclCheck() {
341
$this->assertTrue($this->Acl->check('Samir', 'print', 'read'));
342
$this->assertTrue($this->Acl->check('Lumbergh', 'current', 'read'));
343
$this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
344
$this->assertFalse($this->Acl->check('Milton', 'current', 'update'));
346
$this->expectError("DbAcl::check() - Failed ARO/ACO node lookup in permissions check. Node references:\nAro: WRONG\nAco: tpsReports");
347
$this->assertFalse($this->Acl->check('WRONG', 'tpsReports', 'read'));
349
$this->expectError("ACO permissions key foobar does not exist in DbAcl::check()");
350
$this->assertFalse($this->Acl->check('Lumbergh', 'smash', 'foobar'));
352
$this->expectError("DbAcl::check() - Failed ARO/ACO node lookup in permissions check. Node references:\nAro: users\nAco: NonExistant");
353
$this->assertFalse($this->Acl->check('users', 'NonExistant', 'read'));
355
$this->assertFalse($this->Acl->check(null, 'printers', 'create'));
356
$this->assertFalse($this->Acl->check('managers', null, 'read'));
358
$this->assertTrue($this->Acl->check('Bobs', 'ROOT/tpsReports/view/current', 'read'));
359
$this->assertFalse($this->Acl->check('Samir', 'ROOT/tpsReports/update', 'read'));
361
$this->assertFalse($this->Acl->check('root/users/Milton', 'smash', 'delete'));
365
* testDbAclCascadingDeny function
367
* Setup the acl permissions such that Bobs inherits from admin.
368
* deny Admin delete access to a specific resource, check the permisssions are inherited.
373
function testDbAclCascadingDeny() {
374
$this->Acl->inherit('Bobs', 'ROOT', '*');
375
$this->assertTrue($this->Acl->check('admin', 'tpsReports', 'delete'));
376
$this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'delete'));
377
$this->Acl->deny('admin', 'tpsReports', 'delete');
378
$this->assertFalse($this->Acl->check('admin', 'tpsReports', 'delete'));
379
$this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'delete'));
383
* testDbAclDeny method
388
function testDbAclDeny() {
389
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'delete'));
390
$this->Acl->deny('Micheal', 'smash', 'delete');
391
$this->assertFalse($this->Acl->check('Micheal', 'smash', 'delete'));
392
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'read'));
393
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'create'));
394
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'update'));
395
$this->assertFalse($this->Acl->check('Micheal', 'smash', '*'));
397
$this->assertTrue($this->Acl->check('Samir', 'refill', '*'));
398
$this->Acl->deny('Samir', 'refill', '*');
399
$this->assertFalse($this->Acl->check('Samir', 'refill', 'create'));
400
$this->assertFalse($this->Acl->check('Samir', 'refill', 'update'));
401
$this->assertFalse($this->Acl->check('Samir', 'refill', 'read'));
402
$this->assertFalse($this->Acl->check('Samir', 'refill', 'delete'));
404
$result = $this->Acl->Aro->Permission->find('all', array('conditions' => array('AroTwoTest.alias' => 'Samir')));
406
$this->assertEqual($result[0]['PermissionTwoTest']['_delete'], $expected);
408
$this->expectError('DbAcl::allow() - Invalid node');
409
$this->assertFalse($this->Acl->deny('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
413
* testAclNodeLookup method
418
function testAclNodeLookup() {
419
$result = $this->Acl->Aro->node('root/users/Samir');
421
array('AroTwoTest' => array('id' => '7', 'parent_id' => '4', 'model' => 'User', 'foreign_key' => 3, 'alias' => 'Samir')),
422
array('AroTwoTest' => array('id' => '4', 'parent_id' => '1', 'model' => 'Group', 'foreign_key' => 3, 'alias' => 'users')),
423
array('AroTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'root'))
425
$this->assertEqual($result, $expected);
427
$result = $this->Acl->Aco->node('ROOT/tpsReports/view/current');
429
array('AcoTwoTest' => array('id' => '4', 'parent_id' => '3', 'model' => null, 'foreign_key' => null, 'alias' => 'current')),
430
array('AcoTwoTest' => array('id' => '3', 'parent_id' => '2', 'model' => null, 'foreign_key' => null, 'alias' => 'view')),
431
array('AcoTwoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'tpsReports')),
432
array('AcoTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'ROOT')),
434
$this->assertEqual($result, $expected);
438
* testDbInherit method
443
function testDbInherit() {
444
//parent doesn't have access inherit should still deny
445
$this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
446
$this->Acl->inherit('Milton', 'smash', 'delete');
447
$this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
450
$this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
451
$this->Acl->inherit('Milton', 'smash', 'read');
452
$this->assertTrue($this->Acl->check('Milton', 'smash', 'read'));
461
function testDbGrant() {
462
$this->assertFalse($this->Acl->check('Samir', 'tpsReports', 'create'));
463
$this->Acl->grant('Samir', 'tpsReports', 'create');
464
$this->assertTrue($this->Acl->check('Samir', 'tpsReports', 'create'));
466
$this->assertFalse($this->Acl->check('Micheal', 'view', 'read'));
467
$this->Acl->grant('Micheal', 'view', array('read', 'create', 'update'));
468
$this->assertTrue($this->Acl->check('Micheal', 'view', 'read'));
469
$this->assertTrue($this->Acl->check('Micheal', 'view', 'create'));
470
$this->assertTrue($this->Acl->check('Micheal', 'view', 'update'));
471
$this->assertFalse($this->Acl->check('Micheal', 'view', 'delete'));
473
$this->expectError('DbAcl::allow() - Invalid node');
474
$this->assertFalse($this->Acl->grant('Peter', 'ROOT/tpsReports/DoesNotExist', 'create'));
478
* testDbRevoke method
483
function testDbRevoke() {
484
$this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'read'));
485
$this->Acl->revoke('Bobs', 'tpsReports', 'read');
486
$this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'read'));
488
$this->assertTrue($this->Acl->check('users', 'printers', 'read'));
489
$this->Acl->revoke('users', 'printers', 'read');
490
$this->assertFalse($this->Acl->check('users', 'printers', 'read'));
491
$this->assertFalse($this->Acl->check('Samir', 'printers', 'read'));
492
$this->assertFalse($this->Acl->check('Peter', 'printers', 'read'));
494
$this->expectError('DbAcl::allow() - Invalid node');
495
$this->assertFalse($this->Acl->deny('Bobs', 'ROOT/printers/DoesNotExist', 'create'));
504
function testStartup() {
505
$controller = new Controller();
506
$this->assertTrue($this->Acl->startup($controller));
510
* testIniReadConfigFile
515
function testIniReadConfigFile() {
516
Configure::write('Acl.classname', 'IniAclTest');
518
$this->Acl = new AclComponent();
519
$iniFile = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'config'. DS . 'acl.ini.php';
520
$result = $this->Acl->_Instance->readConfigFile($iniFile);
523
'groups' => 'administrators',
535
'deny' => 'images, files',
538
'groups' => 'anonymous',
542
'administrators' => array(
544
'allow' => 'posts, comments, images, files, stats, ads',
547
'allow' => 'posts, comments, images, files',
548
'deny' => 'stats, ads',
550
'anonymous' => array(
552
'deny' => 'posts, comments, images, files, stats, ads',
555
$this->assertEqual($result, $expected);
559
* testIniCheck method
564
function testIniCheck() {
565
Configure::write('Acl.classname', 'IniAclTest');
567
$iniFile = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'config'. DS . 'acl.ini.php';
569
$this->Acl = new AclComponent();
570
$this->Acl->_Instance->config= $this->Acl->_Instance->readConfigFile($iniFile);
572
$this->assertFalse($this->Acl->check('admin', 'ads'));
573
$this->assertTrue($this->Acl->check('admin', 'posts'));
575
$this->assertTrue($this->Acl->check('jenny', 'posts'));
576
$this->assertTrue($this->Acl->check('jenny', 'ads'));
578
$this->assertTrue($this->Acl->check('paul', 'posts'));
579
$this->assertFalse($this->Acl->check('paul', 'ads'));
581
$this->assertFalse($this->Acl->check('nobody', 'comments'));
585
* debug function - to help editing/creating test cases for the ACL component
587
* To check the overal ACL status at any time call $this->__debug();
588
* Generates a list of the current aro and aco structures and a grid dump of the permissions that are defined
589
* Only designed to work with the db based ACL
591
* @param bool $treesToo
595
function __debug ($printTreesToo = false) {
596
$this->Acl->Aro->displayField = 'alias';
597
$this->Acl->Aco->displayField = 'alias';
598
$aros = $this->Acl->Aro->find('list', array('order' => 'lft'));
599
$acos = $this->Acl->Aco->find('list', array('order' => 'lft'));
600
$rights = array('*', 'create', 'read', 'update', 'delete');
601
$permissions['Aros v Acos >'] = $acos;
602
foreach ($aros as $aro) {
604
foreach ($acos as $aco) {
606
foreach ($rights as $right) {
607
if ($this->Acl->check($aro, $aco, $right)) {
613
} elseif ($right != '*') {
619
$permissions[$aro] = $row;
621
foreach ($permissions as $key => $values) {
622
array_unshift($values, $key);
623
$values = array_map(array(&$this, '__pad'), $values);
624
$permissions[$key] = implode (' ', $values);
626
$permisssions = array_map(array(&$this, '__pad'), $permissions);
627
array_unshift($permissions, 'Current Permissions :');
628
if ($printTreesToo) {
629
debug (array('aros' => $this->Acl->Aro->generateTreeList(), 'acos' => $this->Acl->Aco->generateTreeList()));
631
debug (implode("\r\n", $permissions));
636
* Used by debug to format strings used in the data dump
638
* @param string $string
643
function __pad($string = '', $len = 14) {
644
return str_pad($string, $len);