Refactor setAdmin method to properly handle special cases.

This commit is contained in:
Benoît Burnichon
2015-07-01 15:01:34 +02:00
parent 1eb7271810
commit d62a4eb4e7
2 changed files with 80 additions and 64 deletions

View File

@@ -135,11 +135,14 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testGetCGUHasNoRights() public function testGetCGUHasNoRights()
{ {
$this->setAdmin(true) $this->setAdmin(true, [
->expects($this->once()) 'has_right_on_sbas'=> function (\PHPUnit_Framework_MockObject_MockObject $acl) {
->method('has_right_on_sbas') $acl->expects($this->once())
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') ->method('has_right_on_sbas')
->will($this->returnValue(false)); ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(false));
}
]);
self::$DI['client']->request('GET', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/'); self::$DI['client']->request('GET', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/');
@@ -151,11 +154,14 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testGetCGU() public function testGetCGU()
{ {
$this->setAdmin(true) $this->setAdmin(true, [
->expects($this->once()) 'has_right_on_sbas'=> function (\PHPUnit_Framework_MockObject_MockObject $acl) {
->method('has_right_on_sbas') $acl->expects($this->once())
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') ->method('has_right_on_sbas')
->will($this->returnValue(true)); ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(true));
}
]);
self::$DI['client']->request('GET', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/'); self::$DI['client']->request('GET', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/');
$this->assertTrue(self::$DI['client']->getResponse()->isOk()); $this->assertTrue(self::$DI['client']->getResponse()->isOk());
@@ -182,11 +188,14 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testUpdateDatabaseCGU() public function testUpdateDatabaseCGU()
{ {
$this->setAdmin(true) $this->setAdmin(true, [
->expects($this->once()) 'has_right_on_sbas'=> function (\PHPUnit_Framework_MockObject_MockObject $acl) {
->method('has_right_on_sbas') $acl->expects($this->once())
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') ->method('has_right_on_sbas')
->will($this->returnValue(true)); ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(true));
}
]);
$cgusUpdate = 'Test update CGUS'; $cgusUpdate = 'Test update CGUS';

View File

@@ -9,64 +9,71 @@ abstract class PhraseanetAuthenticatedWebTestCase extends \PhraseanetAuthenticat
private static $createdDataboxes = []; private static $createdDataboxes = [];
/** /**
* @param bool $bool * @param bool $bool
* @param array $stubs List of Closure to call indexed by method names, null to avoid calls
* @return PHPUnit_Framework_MockObject_MockObject The stubbedACL * @return PHPUnit_Framework_MockObject_MockObject The stubbedACL
*/ */
public function setAdmin($bool) public function setAdmin($bool, array $stubs = [])
{ {
$stubbedACL = $this->stubACL(); $stubbedACL = $this->stubACL();
$stubs = array_filter(array_replace($this->getDefaultStubs(), $stubs));
$stubbedACL->expects($this->any()) foreach ($stubs as $method => $stub) {
->method('is_admin') call_user_func($stub, $stubbedACL, $method, $bool);
->will($this->returnValue($bool)); }
$stubbedACL->expects($this->any())
->method('give_access_to_sbas')
->will($this->returnSelf());
$stubbedACL->expects($this->any())
->method('update_rights_to_sbas')
->will($this->returnSelf());
$stubbedACL->expects($this->any())
->method('update_rights_to_bas')
->will($this->returnSelf());
$stubbedACL->expects($this->any())
->method('has_right_on_base')
->will($this->returnValue($bool));
$stubbedACL->expects($this->any())
->method('has_right_on_sbas')
->will($this->returnValue($bool));
$stubbedACL->expects($this->any())
->method('has_access_to_sbas')
->will($this->returnValue($bool));
$stubbedACL->expects($this->any())
->method('has_access_to_base')
->will($this->returnValue($bool));
$stubbedACL->expects($this->any())
->method('has_right')
->will($this->returnValue($bool));
$stubbedACL->expects($this->any())
->method('has_access_to_module')
->will($this->returnValue($bool));
$stubbedACL->expects($this->any())
->method('get_granted_base')
->will($this->returnValue([self::$DI['collection']]));
$stubbedACL->expects($this->any())
->method('get_granted_sbas')
->will($this->returnValue([self::$DI['collection']->get_databox()]));
return $stubbedACL; return $stubbedACL;
} }
protected function getDefaultStubs()
{
static $stubs;
if (is_array($stubs)) {
return $stubs;
}
$returnBool = function (PHPUnit_Framework_MockObject_MockObject $acl, $method, $is_admin) {
$acl->expects($this->any())
->method($method)
->will($this->returnValue($is_admin));
};
$returnSelf = function (PHPUnit_Framework_MockObject_MockObject $acl, $method) {
$acl->expects($this->any())
->method($method)
->will($this->returnSelf());
};
$stubGrantedBase = function (PHPUnit_Framework_MockObject_MockObject $acl, $method) {
$acl->expects($this->any())
->method($method)
->will($this->returnValue([self::$DI['collection']]));
};
$stubGrantedSBase = function (PHPUnit_Framework_MockObject_MockObject $acl, $method) {
$acl->expects($this->any())
->method($method)
->will($this->returnValue([self::$DI['collection']->get_databox()]));
};
$stubs = [
'is_admin' => $returnBool,
'give_access_to_sbas' => $returnSelf,
'update_rights_to_sbas' => $returnSelf,
'update_rights_to_bas' => $returnSelf,
'has_right_on_base' => $returnBool,
'has_right_on_sbas' => $returnBool,
'has_access_to_sbas' => $returnBool,
'has_access_to_base' => $returnBool,
'has_right' => $returnBool,
'has_access_to_module' => $returnBool,
'get_granted_base' => $stubGrantedBase,
'get_granted_sbas' => $stubGrantedSBase,
];
return $stubs;
}
public function createDatabox() public function createDatabox()
{ {
$this->createDatabase(); $this->createDatabase();