2009-05-24 16:37:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AclShell Test file
|
|
|
|
*
|
2010-01-26 17:15:15 -05:00
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 21:28:17 +09:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-05-24 16:37:54 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 21:22:51 +09:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2009-05-24 16:37:54 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 21:28:17 +09:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 17:00:11 +11:00
|
|
|
* @link http://cakephp.org CakePHP Project
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Console.Command
|
2009-05-24 16:37:54 +00:00
|
|
|
* @since CakePHP v 1.2.0.7726
|
2013-05-31 00:11:14 +02:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2009-05-24 16:37:54 +00:00
|
|
|
*/
|
|
|
|
|
2012-05-25 23:27:40 +07:00
|
|
|
App::uses('ConsoleOutput', 'Console');
|
|
|
|
App::uses('ConsoleInput', 'Console');
|
2010-12-08 23:15:18 -04:30
|
|
|
App::uses('ShellDispatcher', 'Console');
|
|
|
|
App::uses('Shell', 'Console');
|
|
|
|
App::uses('AclShell', 'Console/Command');
|
2011-03-08 15:13:49 -04:30
|
|
|
App::uses('ComponentCollection', 'Controller');
|
2009-05-24 16:37:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AclShellTest class
|
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Console.Command
|
2009-05-24 16:37:54 +00:00
|
|
|
*/
|
|
|
|
class AclShellTest extends CakeTestCase {
|
2009-11-14 23:18:31 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixtures
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $fixtures = array('core.aco', 'core.aro', 'core.aros_aco');
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-05-24 16:37:54 +00:00
|
|
|
/**
|
2011-12-04 13:27:51 -08:00
|
|
|
* setUp method
|
2009-05-24 16:37:54 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-09-25 21:36:49 -04:00
|
|
|
public function setUp() {
|
2011-12-04 13:27:51 -08:00
|
|
|
parent::setUp();
|
2010-09-19 22:28:30 -04:30
|
|
|
Configure::write('Acl.database', 'test');
|
2009-05-24 16:37:54 +00:00
|
|
|
Configure::write('Acl.classname', 'DbAcl');
|
|
|
|
|
2010-10-06 22:52:42 -04:00
|
|
|
$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
|
|
|
|
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
|
|
|
|
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task = $this->getMock(
|
|
|
|
'AclShell',
|
2010-10-17 15:43:20 -04:00
|
|
|
array('in', 'out', 'hr', 'createFile', 'error', 'err', 'clear', 'dispatchShell'),
|
2010-10-24 14:55:16 -04:00
|
|
|
array($out, $out, $in)
|
2010-05-22 20:21:27 -04:00
|
|
|
);
|
2010-09-07 23:40:07 -04:00
|
|
|
$collection = new ComponentCollection();
|
|
|
|
$this->Task->Acl = new AclComponent($collection);
|
2010-09-19 22:28:30 -04:30
|
|
|
$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 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10: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-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
|
|
|
|
$this->Task->expects($this->at(2))->method('out')
|
2011-09-29 00:05:15 -04:30
|
|
|
->with($this->stringContains('[1] ROOT'));
|
2010-05-22 20:21:27 -04:00
|
|
|
|
|
|
|
$this->Task->expects($this->at(4))->method('out')
|
2011-09-29 00:05:15 -04:30
|
|
|
->with($this->stringContains('[3] Gandalf'));
|
2011-10-28 01:01:17 -04:00
|
|
|
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(6))->method('out')
|
2011-09-29 00:05:15 -04:30
|
|
|
->with($this->stringContains('[5] MyModel.2'));
|
2009-05-24 16:37:54 +00:00
|
|
|
|
|
|
|
$this->Task->view();
|
|
|
|
}
|
2009-08-07 08:37:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test view with an argument
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testViewWithArgument() {
|
2009-08-07 08:37:31 -04:00
|
|
|
$this->Task->args = array('aro', 'admins');
|
2010-05-22 20:21:27 -04: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 08:37:31 -04:00
|
|
|
$this->Task->view();
|
|
|
|
}
|
|
|
|
|
2009-08-01 18:57:54 -04:00
|
|
|
/**
|
|
|
|
* test the method that splits model.foreign key. and that it returns an array.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testParsingModelAndForeignKey() {
|
2009-08-01 18:57:54 -04:00
|
|
|
$result = $this->Task->parseIdentifier('Model.foreignKey');
|
|
|
|
$expected = array('model' => 'Model', 'foreign_key' => 'foreignKey');
|
2013-01-23 13:45:50 +01:00
|
|
|
$this->assertEquals($expected, $result);
|
2009-08-01 18:57:54 -04:00
|
|
|
|
|
|
|
$result = $this->Task->parseIdentifier('mySuperUser');
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('mySuperUser', $result);
|
2009-08-01 18:57:54 -04:00
|
|
|
|
|
|
|
$result = $this->Task->parseIdentifier('111234');
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('111234', $result);
|
2009-08-01 18:57:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test creating aro/aco nodes
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testCreate() {
|
2009-08-01 18:57:54 -04:00
|
|
|
$this->Task->args = array('aro', 'root', 'User.1');
|
2010-10-11 13:30:18 -04: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-22 20:21:27 -04:00
|
|
|
|
2009-08-01 18:57:54 -04:00
|
|
|
$this->Task->create();
|
|
|
|
|
2010-05-22 20:21:27 -04:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
2009-08-01 18:57:54 -04:00
|
|
|
$Aro->cacheQueries = false;
|
|
|
|
$result = $Aro->read();
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('User', $result['Aro']['model']);
|
|
|
|
$this->assertEquals(1, $result['Aro']['foreign_key']);
|
|
|
|
$this->assertEquals(null, $result['Aro']['parent_id']);
|
2009-08-01 18:57:54 -04:00
|
|
|
$id = $result['Aro']['id'];
|
|
|
|
|
|
|
|
$this->Task->args = array('aro', 'User.1', 'User.3');
|
|
|
|
$this->Task->create();
|
|
|
|
|
2010-05-22 20:21:27 -04:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
2009-08-01 18:57:54 -04:00
|
|
|
$result = $Aro->read();
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('User', $result['Aro']['model']);
|
|
|
|
$this->assertEquals(3, $result['Aro']['foreign_key']);
|
|
|
|
$this->assertEquals($id, $result['Aro']['parent_id']);
|
2009-08-01 20:17:44 -04:00
|
|
|
|
|
|
|
$this->Task->args = array('aro', 'root', 'somealias');
|
|
|
|
$this->Task->create();
|
|
|
|
|
2010-05-22 20:21:27 -04:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
2009-08-01 20:17:44 -04:00
|
|
|
$result = $Aro->read();
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('somealias', $result['Aro']['alias']);
|
|
|
|
$this->assertEquals(null, $result['Aro']['model']);
|
|
|
|
$this->assertEquals(null, $result['Aro']['foreign_key']);
|
|
|
|
$this->assertEquals(null, $result['Aro']['parent_id']);
|
2009-08-01 20:17:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test the delete method with different node types.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testDelete() {
|
2009-08-01 20:17:44 -04:00
|
|
|
$this->Task->args = array('aro', 'AuthUser.1');
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2010-10-11 13:30:18 -04:00
|
|
|
->with("<success>Aro deleted.</success>", 2);
|
2009-08-01 20:17:44 -04:00
|
|
|
$this->Task->delete();
|
|
|
|
|
2010-05-22 20:21:27 -04:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
|
|
|
$result = $Aro->findById(3);
|
2012-10-26 20:49:04 +02:00
|
|
|
$this->assertSame(array(), $result);
|
2009-08-01 18:57:54 -04:00
|
|
|
}
|
2009-08-01 20:56:58 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test setParent method.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testSetParent() {
|
2009-08-01 20:56:58 -04:00
|
|
|
$this->Task->args = array('aro', 'AuthUser.2', 'root');
|
|
|
|
$this->Task->setParent();
|
|
|
|
|
2010-05-22 20:21:27 -04:00
|
|
|
$Aro = ClassRegistry::init('Aro');
|
2009-08-01 20:56:58 -04:00
|
|
|
$result = $Aro->read(null, 4);
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals(null, $result['Aro']['parent_id']);
|
2009-08-01 20:56:58 -04:00
|
|
|
}
|
2009-08-05 00:23:59 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test grant
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testGrant() {
|
2009-08-05 00:23:59 -04:00
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2010-10-11 13:30:18 -04:00
|
|
|
->with($this->matchesRegularExpression('/granted/'), true);
|
2009-08-05 00:23:59 -04:00
|
|
|
$this->Task->grant();
|
2010-11-16 01:22:29 -04:30
|
|
|
$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
|
|
|
|
$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
|
2009-08-05 00:23:59 -04:00
|
|
|
|
|
|
|
$this->assertFalse(empty($node['Aco'][0]));
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals(1, $node['Aco'][0]['Permission']['_create']);
|
2009-08-05 00:23:59 -04:00
|
|
|
}
|
2009-08-05 00:37:53 -04:00
|
|
|
|
|
|
|
/**
|
2009-08-05 12:20:12 -04:00
|
|
|
* test deny
|
2009-08-05 00:37:53 -04:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testDeny() {
|
2009-08-05 00:37:53 -04:00
|
|
|
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2011-09-29 00:05:15 -04:30
|
|
|
->with($this->stringContains('Permission denied'), true);
|
2011-10-28 01:01:17 -04:00
|
|
|
|
2009-08-05 00:37:53 -04:00
|
|
|
$this->Task->deny();
|
|
|
|
|
2010-11-16 01:22:29 -04:30
|
|
|
$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
|
|
|
|
$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
|
2009-08-05 00:37:53 -04:00
|
|
|
$this->assertFalse(empty($node['Aco'][0]));
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals(-1, $node['Aco'][0]['Permission']['_create']);
|
2009-08-05 00:37:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test checking allowed and denied perms
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testCheck() {
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2010-10-11 13:30:18 -04:00
|
|
|
->with($this->matchesRegularExpression('/not allowed/'), true);
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(1))->method('out')
|
2010-10-11 13:30:18 -04:00
|
|
|
->with($this->matchesRegularExpression('/granted/'), true);
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(2))->method('out')
|
2010-10-11 13:30:18 -04:00
|
|
|
->with($this->matchesRegularExpression('/is.*allowed/'), true);
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(3))->method('out')
|
2010-10-11 13:30:18 -04:00
|
|
|
->with($this->matchesRegularExpression('/not.*allowed/'), true);
|
2010-05-22 20:21:27 -04:00
|
|
|
|
2009-08-05 00:37:53 -04: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-05 23:13:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test inherit and that it 0's the permission fields.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testInherit() {
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(0))->method('out')
|
2010-10-11 13:30:18 -04:00
|
|
|
->with($this->matchesRegularExpression('/Permission .*granted/'), true);
|
2010-05-22 20:21:27 -04:00
|
|
|
$this->Task->expects($this->at(1))->method('out')
|
2010-10-11 13:30:18 -04:00
|
|
|
->with($this->matchesRegularExpression('/Permission .*inherited/'), true);
|
2011-10-28 01:01:17 -04:00
|
|
|
|
2009-08-05 23:13:48 -04: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();
|
|
|
|
|
2010-11-16 01:22:29 -04:30
|
|
|
$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
|
|
|
|
$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
|
2009-08-05 23:13:48 -04:00
|
|
|
$this->assertFalse(empty($node['Aco'][0]));
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals(0, $node['Aco'][0]['Permission']['_create']);
|
2009-08-05 23:13:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test getting the path for an aro/aco
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:18:31 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function testGetPath() {
|
2009-08-05 23:13:48 -04:00
|
|
|
$this->Task->args = array('aro', 'AuthUser.2');
|
2010-11-16 01:22:29 -04:30
|
|
|
$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
|
|
|
|
$first = $node[0]['Aro']['id'];
|
|
|
|
$second = $node[1]['Aro']['id'];
|
|
|
|
$last = $node[2]['Aro']['id'];
|
2011-12-15 22:52:07 -08:00
|
|
|
$this->Task->expects($this->at(2))->method('out')->with('[' . $last . '] ROOT');
|
|
|
|
$this->Task->expects($this->at(3))->method('out')->with(' [' . $second . '] admins');
|
|
|
|
$this->Task->expects($this->at(4))->method('out')->with(' [' . $first . '] Elrond');
|
2009-08-05 23:13:48 -04:00
|
|
|
$this->Task->getPath();
|
|
|
|
}
|
2010-04-21 23:28:40 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that initdb makes the correct call.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testInitDb() {
|
2010-10-17 15:43:20 -04:00
|
|
|
$this->Task->expects($this->once())->method('dispatchShell')
|
|
|
|
->with('schema create DbAcl');
|
2011-10-28 01:01:17 -04:00
|
|
|
|
2010-04-21 23:28:40 -04:00
|
|
|
$this->Task->initdb();
|
|
|
|
}
|
2009-05-24 16:37:54 +00:00
|
|
|
}
|