2009-05-24 16:37:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AclShell Test file
|
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2009-05-24 16:37:54 +00:00
|
|
|
*
|
2010-01-26 22:15:15 +00:00
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2010-01-14 04:47:14 +00:00
|
|
|
* Copyright 2006-2010, Cake Software Foundation, Inc.
|
2009-05-24 16:37:54 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-14 04:47:14 +00:00
|
|
|
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP Project
|
2009-05-24 16:37:54 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.console.libs.tasks
|
|
|
|
* @since CakePHP v 1.2.0.7726
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2009-05-24 16:37:54 +00:00
|
|
|
*/
|
2009-07-26 01:27:02 +00:00
|
|
|
App::import('Shell', 'Shell', false);
|
2010-10-17 19:43:20 +00:00
|
|
|
App::import('Shell', 'Acl');
|
2009-05-24 16:37:54 +00:00
|
|
|
|
2010-10-03 05:53:42 +00:00
|
|
|
require_once CAKE . 'console' . DS . 'shell_dispatcher.php';
|
2009-05-24 16:37:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AclShellTest class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.console.libs.tasks
|
|
|
|
*/
|
|
|
|
class AclShellTest extends CakeTestCase {
|
2009-11-14 12:18:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixtures
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $fixtures = array('core.aco', 'core.aro', 'core.aros_aco');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-24 16:37:54 +00:00
|
|
|
/**
|
2010-09-26 01:36:49 +00:00
|
|
|
* setup method
|
2009-05-24 16:37:54 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-09-26 01:36:49 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2009-05-24 16:37:54 +00:00
|
|
|
|
2010-09-20 02:58:30 +00:00
|
|
|
Configure::write('Acl.database', 'test');
|
2009-05-24 16:37:54 +00:00
|
|
|
Configure::write('Acl.classname', 'DbAcl');
|
|
|
|
|
2010-10-07 02:52:42 +00:00
|
|
|
$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
|
|
|
|
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
|
|
|
|
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task = $this->getMock(
|
|
|
|
'AclShell',
|
2010-10-17 19:43:20 +00:00
|
|
|
array('in', 'out', 'hr', 'createFile', 'error', 'err', 'clear', 'dispatchShell'),
|
2010-10-24 18:55:16 +00:00
|
|
|
array($out, $out, $in)
|
2010-05-23 00:21:27 +00:00
|
|
|
);
|
2010-09-08 03:40:07 +00:00
|
|
|
$collection = new ComponentCollection();
|
|
|
|
$this->Task->Acl = new AclComponent($collection);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-20 02:58:30 +00:00
|
|
|
$this->Task->params['datasource'] = 'test';
|
2009-05-24 16:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that model.foreign_key output works when looking at acl rows
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testViewWithModelForeignKeyOutput() {
|
2009-05-24 16:37:54 +00:00
|
|
|
$this->Task->command = 'view';
|
|
|
|
$this->Task->startup();
|
|
|
|
$data = array(
|
|
|
|
'parent_id' => null,
|
|
|
|
'model' => 'MyModel',
|
|
|
|
'foreign_key' => 2,
|
|
|
|
);
|
|
|
|
$this->Task->Acl->Aro->create($data);
|
|
|
|
$this->Task->Acl->Aro->save();
|
|
|
|
$this->Task->args[0] = 'aro';
|
|
|
|
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
|
|
|
|
$this->Task->expects($this->at(2))->method('out')
|
|
|
|
->with(new PHPUnit_Framework_Constraint_PCREMatch('/\[1\] ROOT/'));
|
|
|
|
|
|
|
|
$this->Task->expects($this->at(4))->method('out')
|
|
|
|
->with(new PHPUnit_Framework_Constraint_PCREMatch('/\[3\] Gandalf/'));
|
|
|
|
|
|
|
|
$this->Task->expects($this->at(6))->method('out')
|
|
|
|
->with(new PHPUnit_Framework_Constraint_PCREMatch('/\[5\] MyModel\.2/'));
|
2009-05-24 16:37:54 +00:00
|
|
|
|
|
|
|
$this->Task->view();
|
|
|
|
}
|
2009-08-07 12:37:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test view with an argument
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testViewWithArgument() {
|
2009-08-07 12:37:31 +00:00
|
|
|
$this->Task->args = array('aro', 'admins');
|
2010-05-23 00:21:27 +00:00
|
|
|
|
|
|
|
$this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
|
|
|
|
$this->Task->expects($this->at(2))->method('out')->with(' [2] admins');
|
|
|
|
$this->Task->expects($this->at(3))->method('out')->with(' [3] Gandalf');
|
|
|
|
$this->Task->expects($this->at(4))->method('out')->with(' [4] Elrond');
|
|
|
|
|
2009-08-07 12:37:31 +00:00
|
|
|
$this->Task->view();
|
|
|
|
}
|
|
|
|
|
2009-08-01 22:57:54 +00:00
|
|
|
/**
|
|
|
|
* test the method that splits model.foreign key. and that it returns an array.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testParsingModelAndForeignKey() {
|
2009-08-01 22:57:54 +00:00
|
|
|
$result = $this->Task->parseIdentifier('Model.foreignKey');
|
|
|
|
$expected = array('model' => 'Model', 'foreign_key' => 'foreignKey');
|
|
|
|
|
|
|
|
$result = $this->Task->parseIdentifier('mySuperUser');
|
|
|
|
$this->assertEqual($result, 'mySuperUser');
|
|
|
|
|
|
|
|
$result = $this->Task->parseIdentifier('111234');
|
|
|
|
$this->assertEqual($result, '111234');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test creating aro/aco nodes
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testCreate() {
|
2009-08-01 22:57:54 +00:00
|
|
|
$this->Task->args = array('aro', 'root', 'User.1');
|
2010-10-11 17:30:18 +00:00
|
|
|
$this->Task->expects($this->at(0))->method('out')->with("<success>New Aro</success> 'User.1' created.", 2);
|
|
|
|
$this->Task->expects($this->at(1))->method('out')->with("<success>New Aro</success> 'User.3' created.", 2);
|
|
|
|
$this->Task->expects($this->at(2))->method('out')->with("<success>New Aro</success> 'somealias' created.", 2);
|
2010-05-23 00:21:27 +00:00
|
|
|
|
2009-08-01 22:57:54 +00:00
|
|
|
$this->Task->create();
|
|
|
|
|
2010-05-23 00:21:27 +00:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
2009-08-01 22:57:54 +00:00
|
|
|
$Aro->cacheQueries = false;
|
|
|
|
$result = $Aro->read();
|
|
|
|
$this->assertEqual($result['Aro']['model'], 'User');
|
|
|
|
$this->assertEqual($result['Aro']['foreign_key'], 1);
|
|
|
|
$this->assertEqual($result['Aro']['parent_id'], null);
|
|
|
|
$id = $result['Aro']['id'];
|
|
|
|
|
|
|
|
$this->Task->args = array('aro', 'User.1', 'User.3');
|
|
|
|
$this->Task->create();
|
|
|
|
|
2010-05-23 00:21:27 +00:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
2009-08-01 22:57:54 +00:00
|
|
|
$result = $Aro->read();
|
|
|
|
$this->assertEqual($result['Aro']['model'], 'User');
|
|
|
|
$this->assertEqual($result['Aro']['foreign_key'], 3);
|
|
|
|
$this->assertEqual($result['Aro']['parent_id'], $id);
|
2009-08-02 00:17:44 +00:00
|
|
|
|
|
|
|
$this->Task->args = array('aro', 'root', 'somealias');
|
|
|
|
$this->Task->create();
|
|
|
|
|
2010-05-23 00:21:27 +00:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
2009-08-02 00:17:44 +00:00
|
|
|
$result = $Aro->read();
|
|
|
|
$this->assertEqual($result['Aro']['alias'], 'somealias');
|
|
|
|
$this->assertEqual($result['Aro']['model'], null);
|
|
|
|
$this->assertEqual($result['Aro']['foreign_key'], null);
|
|
|
|
$this->assertEqual($result['Aro']['parent_id'], null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test the delete method with different node types.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testDelete() {
|
2009-08-02 00:17:44 +00:00
|
|
|
$this->Task->args = array('aro', 'AuthUser.1');
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2010-10-11 17:30:18 +00:00
|
|
|
->with("<success>Aro deleted.</success>", 2);
|
2009-08-02 00:17:44 +00:00
|
|
|
$this->Task->delete();
|
|
|
|
|
2010-05-23 00:21:27 +00:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
|
|
|
$result = $Aro->findById(3);
|
2009-08-02 00:17:44 +00:00
|
|
|
$this->assertFalse($result);
|
2009-08-01 22:57:54 +00:00
|
|
|
}
|
2009-08-02 00:56:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test setParent method.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testSetParent() {
|
2009-08-02 00:56:58 +00:00
|
|
|
$this->Task->args = array('aro', 'AuthUser.2', 'root');
|
|
|
|
$this->Task->setParent();
|
|
|
|
|
2010-05-23 00:21:27 +00:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
2009-08-02 00:56:58 +00:00
|
|
|
$result = $Aro->read(null, 4);
|
|
|
|
$this->assertEqual($result['Aro']['parent_id'], null);
|
|
|
|
}
|
2009-08-05 04:23:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test grant
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testGrant() {
|
2009-08-05 04:23:59 +00:00
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2010-10-11 17:30:18 +00:00
|
|
|
->with($this->matchesRegularExpression('/granted/'), true);
|
2009-08-05 04:23:59 +00:00
|
|
|
$this->Task->grant();
|
|
|
|
|
|
|
|
$node = $this->Task->Acl->Aro->read(null, 4);
|
|
|
|
$this->assertFalse(empty($node['Aco'][0]));
|
|
|
|
$this->assertEqual($node['Aco'][0]['Permission']['_create'], 1);
|
|
|
|
}
|
2009-08-05 04:37:53 +00:00
|
|
|
|
|
|
|
/**
|
2009-08-05 16:20:12 +00:00
|
|
|
* test deny
|
2009-08-05 04:37:53 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testDeny() {
|
2009-08-05 04:37:53 +00:00
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
|
|
|
->with(new PHPUnit_Framework_Constraint_PCREMatch('/Permission denied/'), true);
|
|
|
|
|
2009-08-05 04:37:53 +00:00
|
|
|
$this->Task->deny();
|
|
|
|
|
|
|
|
$node = $this->Task->Acl->Aro->read(null, 4);
|
|
|
|
$this->assertFalse(empty($node['Aco'][0]));
|
|
|
|
$this->assertEqual($node['Aco'][0]['Permission']['_create'], -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test checking allowed and denied perms
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testCheck() {
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2010-10-11 17:30:18 +00:00
|
|
|
->with($this->matchesRegularExpression('/not allowed/'), true);
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(1))->method('out')
|
2010-10-11 17:30:18 +00:00
|
|
|
->with($this->matchesRegularExpression('/granted/'), true);
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(2))->method('out')
|
2010-10-11 17:30:18 +00:00
|
|
|
->with($this->matchesRegularExpression('/is.*allowed/'), true);
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(3))->method('out')
|
2010-10-11 17:30:18 +00:00
|
|
|
->with($this->matchesRegularExpression('/not.*allowed/'), true);
|
2010-05-23 00:21:27 +00:00
|
|
|
|
2009-08-05 04:37:53 +00:00
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
|
|
|
|
$this->Task->check();
|
|
|
|
|
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
|
|
|
|
$this->Task->grant();
|
|
|
|
|
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
|
|
|
|
$this->Task->check();
|
|
|
|
|
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
|
|
|
|
$this->Task->check();
|
|
|
|
}
|
2009-08-06 03:13:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test inherit and that it 0's the permission fields.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testInherit() {
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2010-10-11 17:30:18 +00:00
|
|
|
->with($this->matchesRegularExpression('/Permission .*granted/'), true);
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(1))->method('out')
|
2010-10-11 17:30:18 +00:00
|
|
|
->with($this->matchesRegularExpression('/Permission .*inherited/'), true);
|
2010-05-23 00:21:27 +00:00
|
|
|
|
2009-08-06 03:13:48 +00:00
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
|
|
|
|
$this->Task->grant();
|
|
|
|
|
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'all');
|
|
|
|
$this->Task->inherit();
|
|
|
|
|
|
|
|
$node = $this->Task->Acl->Aro->read(null, 4);
|
|
|
|
$this->assertFalse(empty($node['Aco'][0]));
|
|
|
|
$this->assertEqual($node['Aco'][0]['Permission']['_create'], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test getting the path for an aro/aco
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testGetPath() {
|
2009-08-06 03:13:48 +00:00
|
|
|
$this->Task->args = array('aro', 'AuthUser.2');
|
2010-05-23 00:21:27 +00:00
|
|
|
$this->Task->expects($this->at(2))->method('out')->with('[1] ROOT');
|
|
|
|
$this->Task->expects($this->at(3))->method('out')->with(' [2] admins');
|
|
|
|
$this->Task->expects($this->at(4))->method('out')->with(' [4] Elrond');
|
2009-08-06 03:13:48 +00:00
|
|
|
$this->Task->getPath();
|
|
|
|
}
|
2010-04-22 03:28:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that initdb makes the correct call.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testInitDb() {
|
2010-10-17 19:43:20 +00:00
|
|
|
$this->Task->expects($this->once())->method('dispatchShell')
|
|
|
|
->with('schema create DbAcl');
|
|
|
|
|
2010-04-22 03:28:40 +00:00
|
|
|
$this->Task->initdb();
|
|
|
|
}
|
2009-05-24 16:37:54 +00:00
|
|
|
}
|