2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* AclComponentTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.controller.components
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5435
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-09 05:13:11 +00:00
|
|
|
App::uses('AclComponent', 'Component');
|
|
|
|
App::uses('DbAcl', 'Model');
|
|
|
|
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* AclNodeTwoTestBase class
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.controller.components
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class AclNodeTwoTestBase extends AclNode {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* useDbConfig property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2010-09-20 02:58:30 +00:00
|
|
|
* @var string 'test'
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-09-20 02:58:30 +00:00
|
|
|
public $useDbConfig = 'test';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* cacheSources property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var bool false
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cacheSources = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* AroTwoTest class
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.controller.components
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class AroTwoTest extends AclNodeTwoTestBase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* name property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'AroTwoTest'
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'AroTwoTest';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* useTable property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'aro_twos'
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $useTable = 'aro_twos';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* hasAndBelongsToMany property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $hasAndBelongsToMany = array('AcoTwoTest' => array('with' => 'PermissionTwoTest'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* AcoTwoTest class
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.controller.components
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class AcoTwoTest extends AclNodeTwoTestBase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* name property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'AcoTwoTest'
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'AcoTwoTest';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* useTable property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'aco_twos'
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $useTable = 'aco_twos';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* hasAndBelongsToMany property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $hasAndBelongsToMany = array('AroTwoTest' => array('with' => 'PermissionTwoTest'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* PermissionTwoTest class
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.controller.components
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class PermissionTwoTest extends CakeTestModel {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* name property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'PermissionTwoTest'
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'PermissionTwoTest';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* useTable property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'aros_aco_twos'
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $useTable = 'aros_aco_twos';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* cacheQueries property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var bool false
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cacheQueries = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* belongsTo property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $belongsTo = array('AroTwoTest' => array('foreignKey' => 'aro_id'), 'AcoTwoTest' => array('foreignKey' => 'aco_id'));
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* actsAs property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var mixed null
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $actsAs = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* DbAclTwoTest class
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.controller.components
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class DbAclTwoTest extends DbAcl {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* construct method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access private
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function __construct() {
|
2010-04-24 03:59:57 +00:00
|
|
|
$this->Aro = new AroTwoTest();
|
|
|
|
$this->Aro->Permission = new PermissionTwoTest();
|
|
|
|
$this->Aco = new AcoTwoTest();
|
|
|
|
$this->Aro->Permission = new PermissionTwoTest();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.controller.components
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class AclComponentTest extends CakeTestCase {
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-09-26 01:36:49 +00:00
|
|
|
* setUp method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-06-11 00:47:35 +00:00
|
|
|
function setUp() {
|
2010-09-26 01:36:49 +00:00
|
|
|
parent::setUp();
|
2010-06-11 00:47:35 +00:00
|
|
|
if (!class_exists('MockAclImplementation', false)) {
|
|
|
|
$this->getMock('AclInterface', array(), array(), 'MockAclImplementation');
|
|
|
|
}
|
2010-04-24 03:59:57 +00:00
|
|
|
Configure::write('Acl.classname', 'MockAclImplementation');
|
2010-07-04 22:41:08 +00:00
|
|
|
$Collection = new ComponentCollection();
|
|
|
|
$this->Acl = new AclComponent($Collection);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function tearDown() {
|
2010-09-26 01:36:49 +00:00
|
|
|
parent::tearDown();
|
2009-03-18 17:55:58 +00:00
|
|
|
unset($this->Acl);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-04-24 03:03:51 +00:00
|
|
|
/**
|
|
|
|
* test that construtor throws an exception when Acl.classname is a
|
|
|
|
* non-existant class
|
|
|
|
*
|
2010-12-12 00:01:07 +00:00
|
|
|
* @expectedException CakeException
|
2010-04-24 03:03:51 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testConstrutorException() {
|
|
|
|
Configure::write('Acl.classname', 'AclClassNameThatDoesNotExist');
|
2010-07-04 22:41:08 +00:00
|
|
|
$Collection = new ComponentCollection();
|
|
|
|
$acl = new AclComponent($Collection);
|
2010-04-24 03:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that adapter() allows control of the interal implementation AclComponent uses.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testAdapter() {
|
2010-04-24 03:59:57 +00:00
|
|
|
$implementation = new MockAclImplementation();
|
2010-06-11 00:47:35 +00:00
|
|
|
$implementation->expects($this->once())->method('initialize')->with($this->Acl);
|
2010-04-24 03:03:51 +00:00
|
|
|
$this->assertNull($this->Acl->adapter($implementation));
|
|
|
|
|
|
|
|
$this->assertEqual($this->Acl->adapter(), $implementation, 'Returned object is different %s');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that adapter() whines when the class is not an AclBase
|
|
|
|
*
|
2010-12-12 00:01:07 +00:00
|
|
|
* @expectedException CakeException
|
2010-04-24 03:03:51 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testAdapterException() {
|
|
|
|
$thing = new StdClass();
|
|
|
|
$this->Acl->adapter($thing);
|
|
|
|
}
|
|
|
|
|
2010-04-24 03:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test case for the IniAcl implementation
|
|
|
|
*
|
|
|
|
* @package cake.tests.cases.libs.controller.components
|
|
|
|
*/
|
2010-06-13 14:06:00 +00:00
|
|
|
class IniAclTest extends CakeTestCase {
|
2010-04-24 03:52:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* testIniCheck method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testCheck() {
|
2010-12-20 04:03:37 +00:00
|
|
|
$iniFile = LIBS . 'tests' . DS . 'test_app' . DS . 'config'. DS . 'acl.ini.php';
|
2010-04-24 03:52:36 +00:00
|
|
|
|
|
|
|
$Ini = new IniAcl();
|
|
|
|
$Ini->config = $Ini->readConfigFile($iniFile);
|
|
|
|
|
|
|
|
$this->assertFalse($Ini->check('admin', 'ads'));
|
|
|
|
$this->assertTrue($Ini->check('admin', 'posts'));
|
|
|
|
|
|
|
|
$this->assertTrue($Ini->check('jenny', 'posts'));
|
|
|
|
$this->assertTrue($Ini->check('jenny', 'ads'));
|
|
|
|
|
|
|
|
$this->assertTrue($Ini->check('paul', 'posts'));
|
|
|
|
$this->assertFalse($Ini->check('paul', 'ads'));
|
|
|
|
|
|
|
|
$this->assertFalse($Ini->check('nobody', 'comments'));
|
|
|
|
}
|
2010-11-29 02:22:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check should accept a user array.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testCheckArray() {
|
2010-12-20 04:03:37 +00:00
|
|
|
$iniFile = LIBS . 'tests' . DS . 'test_app' . DS . 'config'. DS . 'acl.ini.php';
|
2010-11-29 02:22:10 +00:00
|
|
|
|
|
|
|
$Ini = new IniAcl();
|
|
|
|
$Ini->config = $Ini->readConfigFile($iniFile);
|
|
|
|
$Ini->userPath = 'User.username';
|
|
|
|
|
|
|
|
$user = array(
|
|
|
|
'User' => array('username' => 'admin')
|
|
|
|
);
|
|
|
|
$this->assertTrue($Ini->check($user, 'posts'));
|
|
|
|
}
|
2010-04-24 03:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test case for AclComponent using the DbAcl implementation.
|
|
|
|
*
|
|
|
|
* @package cake.tests.cases.libs.controller.components
|
|
|
|
*/
|
2010-06-13 14:06:00 +00:00
|
|
|
class DbAclTest extends CakeTestCase {
|
2010-04-24 03:52:36 +00:00
|
|
|
/**
|
|
|
|
* fixtures property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $fixtures = array('core.aro_two', 'core.aco_two', 'core.aros_aco_two');
|
|
|
|
|
|
|
|
/**
|
2010-09-26 01:36:49 +00:00
|
|
|
* setUp method
|
2010-04-24 03:52:36 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-26 01:36:49 +00:00
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
2010-04-24 03:52:36 +00:00
|
|
|
Configure::write('Acl.classname', 'DbAclTwoTest');
|
2010-09-20 02:58:30 +00:00
|
|
|
Configure::write('Acl.database', 'test');
|
2010-07-04 22:41:08 +00:00
|
|
|
$Collection = new ComponentCollection();
|
|
|
|
$this->Acl = new AclComponent($Collection);
|
2010-04-24 03:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-26 01:36:49 +00:00
|
|
|
function tearDown() {
|
|
|
|
parent::tearDown();
|
2010-04-24 03:52:36 +00:00
|
|
|
unset($this->Acl);
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testAclCreate method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testCreate() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Acl->Aro->create(array('alias' => 'Chotchkey'));
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->assertTrue((bool)$this->Acl->Aro->save());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$parent = $this->Acl->Aro->id;
|
|
|
|
|
|
|
|
$this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Joanna'));
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->assertTrue((bool)$this->Acl->Aro->save());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Stapler'));
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->assertTrue((bool)$this->Acl->Aro->save());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$root = $this->Acl->Aco->node('ROOT');
|
|
|
|
$parent = $root[0]['AcoTwoTest']['id'];
|
|
|
|
|
|
|
|
$this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'Drinks'));
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->assertTrue((bool)$this->Acl->Aco->save());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'PiecesOfFlair'));
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->assertTrue((bool)$this->Acl->Aco->save());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testAclCreateWithParent method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testCreateWithParent() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$parent = $this->Acl->Aro->findByAlias('Peter', null, null, -1);
|
|
|
|
$this->Acl->Aro->create();
|
|
|
|
$this->Acl->Aro->save(array(
|
2008-05-31 04:31:30 +00:00
|
|
|
'alias' => 'Subordinate',
|
|
|
|
'model' => 'User',
|
2008-05-30 11:40:08 +00:00
|
|
|
'foreign_key' => 7,
|
|
|
|
'parent_id' => $parent['AroTwoTest']['id']
|
|
|
|
));
|
|
|
|
$result = $this->Acl->Aro->findByAlias('Subordinate', null, null, -1);
|
|
|
|
$this->assertEqual($result['AroTwoTest']['lft'], 16);
|
|
|
|
$this->assertEqual($result['AroTwoTest']['rght'], 17);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDbAclAllow method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testAllow() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'read'));
|
|
|
|
$this->assertTrue($this->Acl->allow('Micheal', 'tpsReports', array('read', 'delete', 'update')));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'update'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'read'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
|
|
|
|
|
2008-05-31 04:31:30 +00:00
|
|
|
$this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'create'));
|
|
|
|
$this->assertTrue($this->Acl->allow('Micheal', 'ROOT/tpsReports', 'create'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'create'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
|
2008-06-10 22:38:05 +00:00
|
|
|
$this->assertTrue($this->Acl->allow('Micheal', 'printers', 'create'));
|
2008-05-31 04:31:30 +00:00
|
|
|
// Michael no longer has his delete permission for tpsReports!
|
2008-06-10 22:38:05 +00:00
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'printers', 'create'));
|
2008-05-31 04:31:30 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view'));
|
|
|
|
$this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/view', '*'));
|
|
|
|
$this->assertTrue($this->Acl->check('Samir', 'view', 'read'));
|
|
|
|
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
|
|
|
|
|
2008-06-10 22:38:05 +00:00
|
|
|
$this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update','*'));
|
2008-05-31 04:31:30 +00:00
|
|
|
$this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/update', '*'));
|
|
|
|
$this->assertTrue($this->Acl->check('Samir', 'update', 'read'));
|
|
|
|
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update', 'update'));
|
2008-06-10 22:38:05 +00:00
|
|
|
// Samir should still have his tpsReports/view permissions, but does not
|
2008-05-31 04:31:30 +00:00
|
|
|
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
|
|
|
|
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->expectError();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Acl->allow('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
|
2010-06-11 00:47:35 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-06-11 00:47:35 +00:00
|
|
|
/**
|
|
|
|
* testAllowInvalidNode method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAllowInvalidNode() {
|
|
|
|
$this->expectError();
|
|
|
|
$this->Acl->allow('Homer', 'tpsReports', 'create');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDbAclCheck method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testCheck() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($this->Acl->check('Samir', 'print', 'read'));
|
|
|
|
$this->assertTrue($this->Acl->check('Lumbergh', 'current', 'read'));
|
|
|
|
$this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
|
|
|
|
$this->assertFalse($this->Acl->check('Milton', 'current', 'update'));
|
|
|
|
|
|
|
|
$this->assertFalse($this->Acl->check(null, 'printers', 'create'));
|
|
|
|
$this->assertFalse($this->Acl->check('managers', null, 'read'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->Acl->check('Bobs', 'ROOT/tpsReports/view/current', 'read'));
|
|
|
|
$this->assertFalse($this->Acl->check('Samir', 'ROOT/tpsReports/update', 'read'));
|
|
|
|
|
|
|
|
$this->assertFalse($this->Acl->check('root/users/Milton', 'smash', 'delete'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-06-11 00:47:35 +00:00
|
|
|
/**
|
|
|
|
* testCheckInvalidNode method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCheckInvalidNode() {
|
|
|
|
$this->expectError();
|
|
|
|
$this->assertFalse($this->Acl->check('WRONG', 'tpsReports', 'read'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testCheckInvalidPermission method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCheckInvalidPermission() {
|
|
|
|
$this->expectError();
|
|
|
|
$this->assertFalse($this->Acl->check('Lumbergh', 'smash', 'foobar'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testCheckMissingPermission method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCheckMissingPermission() {
|
|
|
|
$this->expectError();
|
|
|
|
$this->assertFalse($this->Acl->check('users', 'NonExistant', 'read'));
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* testDbAclCascadingDeny function
|
|
|
|
*
|
2008-05-31 04:31:30 +00:00
|
|
|
* Setup the acl permissions such that Bobs inherits from admin.
|
2008-05-30 11:40:08 +00:00
|
|
|
* deny Admin delete access to a specific resource, check the permisssions are inherited.
|
2008-05-31 04:31:30 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testAclCascadingDeny() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Acl->inherit('Bobs', 'ROOT', '*');
|
|
|
|
$this->assertTrue($this->Acl->check('admin', 'tpsReports', 'delete'));
|
|
|
|
$this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'delete'));
|
|
|
|
$this->Acl->deny('admin', 'tpsReports', 'delete');
|
|
|
|
$this->assertFalse($this->Acl->check('admin', 'tpsReports', 'delete'));
|
|
|
|
$this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'delete'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDbAclDeny method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testDeny() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'delete'));
|
|
|
|
$this->Acl->deny('Micheal', 'smash', 'delete');
|
|
|
|
$this->assertFalse($this->Acl->check('Micheal', 'smash', 'delete'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'read'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'create'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'update'));
|
|
|
|
$this->assertFalse($this->Acl->check('Micheal', 'smash', '*'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->Acl->check('Samir', 'refill', '*'));
|
|
|
|
$this->Acl->deny('Samir', 'refill', '*');
|
|
|
|
$this->assertFalse($this->Acl->check('Samir', 'refill', 'create'));
|
|
|
|
$this->assertFalse($this->Acl->check('Samir', 'refill', 'update'));
|
|
|
|
$this->assertFalse($this->Acl->check('Samir', 'refill', 'read'));
|
|
|
|
$this->assertFalse($this->Acl->check('Samir', 'refill', 'delete'));
|
|
|
|
|
|
|
|
$result = $this->Acl->Aro->Permission->find('all', array('conditions' => array('AroTwoTest.alias' => 'Samir')));
|
|
|
|
$expected = '-1';
|
|
|
|
$this->assertEqual($result[0]['PermissionTwoTest']['_delete'], $expected);
|
|
|
|
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->expectError();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Acl->deny('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testAclNodeLookup method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testAclNodeLookup() {
|
|
|
|
$result = $this->Acl->Aro->node('root/users/Samir');
|
|
|
|
$expected = array(
|
|
|
|
array('AroTwoTest' => array('id' => '7', 'parent_id' => '4', 'model' => 'User', 'foreign_key' => 3, 'alias' => 'Samir')),
|
|
|
|
array('AroTwoTest' => array('id' => '4', 'parent_id' => '1', 'model' => 'Group', 'foreign_key' => 3, 'alias' => 'users')),
|
|
|
|
array('AroTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'root'))
|
|
|
|
);
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Acl->Aco->node('ROOT/tpsReports/view/current');
|
|
|
|
$expected = array(
|
|
|
|
array('AcoTwoTest' => array('id' => '4', 'parent_id' => '3', 'model' => null, 'foreign_key' => null, 'alias' => 'current')),
|
|
|
|
array('AcoTwoTest' => array('id' => '3', 'parent_id' => '2', 'model' => null, 'foreign_key' => null, 'alias' => 'view')),
|
|
|
|
array('AcoTwoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'tpsReports')),
|
|
|
|
array('AcoTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'ROOT')),
|
|
|
|
);
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDbInherit method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testInherit() {
|
2008-05-30 11:40:08 +00:00
|
|
|
//parent doesn't have access inherit should still deny
|
|
|
|
$this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
|
|
|
|
$this->Acl->inherit('Milton', 'smash', 'delete');
|
|
|
|
$this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
|
|
|
|
|
|
|
|
//inherit parent
|
|
|
|
$this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
|
|
|
|
$this->Acl->inherit('Milton', 'smash', 'read');
|
|
|
|
$this->assertTrue($this->Acl->check('Milton', 'smash', 'read'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDbGrant method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testGrant() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Acl->check('Samir', 'tpsReports', 'create'));
|
2010-04-24 03:14:55 +00:00
|
|
|
$this->Acl->allow('Samir', 'tpsReports', 'create');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($this->Acl->check('Samir', 'tpsReports', 'create'));
|
|
|
|
|
|
|
|
$this->assertFalse($this->Acl->check('Micheal', 'view', 'read'));
|
2010-04-24 03:14:55 +00:00
|
|
|
$this->Acl->allow('Micheal', 'view', array('read', 'create', 'update'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'view', 'read'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'view', 'create'));
|
|
|
|
$this->assertTrue($this->Acl->check('Micheal', 'view', 'update'));
|
|
|
|
$this->assertFalse($this->Acl->check('Micheal', 'view', 'delete'));
|
|
|
|
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->expectError();
|
2010-04-24 03:14:55 +00:00
|
|
|
$this->assertFalse($this->Acl->allow('Peter', 'ROOT/tpsReports/DoesNotExist', 'create'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDbRevoke method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 03:59:57 +00:00
|
|
|
function testRevoke() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'read'));
|
2010-04-24 03:14:55 +00:00
|
|
|
$this->Acl->deny('Bobs', 'tpsReports', 'read');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'read'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->Acl->check('users', 'printers', 'read'));
|
2010-04-24 03:14:55 +00:00
|
|
|
$this->Acl->deny('users', 'printers', 'read');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Acl->check('users', 'printers', 'read'));
|
|
|
|
$this->assertFalse($this->Acl->check('Samir', 'printers', 'read'));
|
|
|
|
$this->assertFalse($this->Acl->check('Peter', 'printers', 'read'));
|
|
|
|
|
2010-06-11 00:47:35 +00:00
|
|
|
$this->expectError();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Acl->deny('Bobs', 'ROOT/printers/DoesNotExist', 'create'));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* debug function - to help editing/creating test cases for the ACL component
|
2008-05-31 04:31:30 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* To check the overal ACL status at any time call $this->__debug();
|
|
|
|
* Generates a list of the current aro and aco structures and a grid dump of the permissions that are defined
|
|
|
|
* Only designed to work with the db based ACL
|
|
|
|
*
|
2008-05-31 04:31:30 +00:00
|
|
|
* @param bool $treesToo
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access private
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function __debug ($printTreesToo = false) {
|
|
|
|
$this->Acl->Aro->displayField = 'alias';
|
|
|
|
$this->Acl->Aco->displayField = 'alias';
|
|
|
|
$aros = $this->Acl->Aro->find('list', array('order' => 'lft'));
|
|
|
|
$acos = $this->Acl->Aco->find('list', array('order' => 'lft'));
|
|
|
|
$rights = array('*', 'create', 'read', 'update', 'delete');
|
|
|
|
$permissions['Aros v Acos >'] = $acos;
|
|
|
|
foreach ($aros as $aro) {
|
|
|
|
$row = array();
|
|
|
|
foreach ($acos as $aco) {
|
|
|
|
$perms = '';
|
|
|
|
foreach ($rights as $right) {
|
|
|
|
if ($this->Acl->check($aro, $aco, $right)) {
|
|
|
|
if ($right == '*') {
|
|
|
|
$perms .= '****';
|
2008-05-31 04:31:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$perms .= $right[0];
|
|
|
|
} elseif ($right != '*') {
|
2008-05-31 04:31:30 +00:00
|
|
|
$perms .= ' ';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-31 04:31:30 +00:00
|
|
|
$row[] = $perms;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$permissions[$aro] = $row;
|
|
|
|
}
|
|
|
|
foreach ($permissions as $key => $values) {
|
|
|
|
array_unshift($values, $key);
|
|
|
|
$values = array_map(array(&$this, '__pad'), $values);
|
|
|
|
$permissions[$key] = implode (' ', $values);
|
|
|
|
}
|
|
|
|
$permisssions = array_map(array(&$this, '__pad'), $permissions);
|
|
|
|
array_unshift($permissions, 'Current Permissions :');
|
|
|
|
if ($printTreesToo) {
|
2010-04-24 03:52:36 +00:00
|
|
|
debug(array('aros' => $this->Acl->Aro->generateTreeList(), 'acos' => $this->Acl->Aco->generateTreeList()));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-24 03:52:36 +00:00
|
|
|
debug(implode("\r\n", $permissions));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* pad function
|
|
|
|
* Used by debug to format strings used in the data dump
|
2008-05-31 04:31:30 +00:00
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @param int $len
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access private
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function __pad($string = '', $len = 14) {
|
2008-05-31 04:31:30 +00:00
|
|
|
return str_pad($string, $len);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|