Mock AclProvider in pimple AND Application.

fix CamelCase in `stubbedACL` name.
This commit is contained in:
Benoît Burnichon
2015-07-01 11:15:53 +02:00
parent d645b92afa
commit 19b8d41dcd
5 changed files with 58 additions and 70 deletions

View File

@@ -135,13 +135,12 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testGetCGUHasNoRights() public function testGetCGUHasNoRights()
{ {
$this->StubbedACL->expects($this->once()) $this->setAdmin(true)
->expects($this->once())
->method('has_right_on_sbas') ->method('has_right_on_sbas')
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->setAdmin(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->assertForbiddenResponse(self::$DI['client']->getResponse()); $this->assertForbiddenResponse(self::$DI['client']->getResponse());
@@ -152,13 +151,12 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testGetCGU() public function testGetCGU()
{ {
$this->StubbedACL->expects($this->once()) $this->setAdmin(true)
->expects($this->once())
->method('has_right_on_sbas') ->method('has_right_on_sbas')
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$this->setAdmin(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());
} }
@@ -184,13 +182,12 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testUpdateDatabaseCGU() public function testUpdateDatabaseCGU()
{ {
$this->StubbedACL->expects($this->once()) $this->setAdmin(true)
->expects($this->once())
->method('has_right_on_sbas') ->method('has_right_on_sbas')
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$this->setAdmin(true);
$cgusUpdate = 'Test update CGUS'; $cgusUpdate = 'Test update CGUS';
$this->XMLHTTPRequest('POST', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/', [ $this->XMLHTTPRequest('POST', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/', [

View File

@@ -34,9 +34,7 @@ class ShareTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testRouteSlash() public function testRouteSlash()
{ {
$stubbedACL = $this->getMockBuilder('\ACL') $stubbedACL = $this->stubACL();
->disableOriginalConstructor()
->getMock();
//has_right_on_base return true //has_right_on_base return true
$stubbedACL->expects($this->once()) $stubbedACL->expects($this->once())
@@ -48,14 +46,6 @@ class ShareTest extends \PhraseanetAuthenticatedWebTestCase
->method('has_access_to_subdef') ->method('has_access_to_subdef')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$aclProvider = $this->getMockBuilder('Alchemy\Phrasea\Authentication\ACLProvider')
->disableOriginalConstructor()
->getMock();
$aclProvider->expects($this->any())
->method('get')
->will($this->returnValue($stubbedACL));
self::$DI['app']['acl'] = $aclProvider;
$url = sprintf('/prod/share/record/%d/%d/', self::$DI['record_1']->get_base_id(), self::$DI['record_1']->get_record_id()); $url = sprintf('/prod/share/record/%d/%d/', self::$DI['record_1']->get_base_id(), self::$DI['record_1']->get_record_id());
self::$DI['client']->request('GET', $url); self::$DI['client']->request('GET', $url);

View File

@@ -1,7 +1,11 @@
<?php <?php
use Alchemy\Phrasea\Authentication\ACLProvider;
abstract class PhraseanetAuthenticatedTestCase extends \PhraseanetTestCase abstract class PhraseanetAuthenticatedTestCase extends \PhraseanetTestCase
{ {
/** @var PHPUnit_Framework_MockObject_MockObject */
protected $stubbedACL;
public function setUp() public function setUp()
{ {
@@ -14,4 +18,29 @@ abstract class PhraseanetAuthenticatedTestCase extends \PhraseanetTestCase
$this->logout(self::$DI['app']); $this->logout(self::$DI['app']);
parent::tearDown(); parent::tearDown();
} }
/**
* @return PHPUnit_Framework_MockObject_MockObject
*/
protected function stubACL()
{
$stubbedACL = $this->getMockBuilder('\ACL')
->disableOriginalConstructor()
->getMock();
$aclProvider = $this->getMockBuilder(ACLProvider::class)
->disableOriginalConstructor()
->getMock();
$aclProvider->expects($this->any())
->method('get')
->will($this->returnValue($stubbedACL));
/** @var \Alchemy\Phrasea\Application $app */
$app = self::$DI['app'];
$app['acl'] = $aclProvider;
$app->setAclProvider($aclProvider);
return $stubbedACL;
}
} }

View File

@@ -6,82 +6,65 @@ use Symfony\Component\DomCrawler\Crawler;
abstract class PhraseanetAuthenticatedWebTestCase extends \PhraseanetAuthenticatedTestCase abstract class PhraseanetAuthenticatedWebTestCase extends \PhraseanetAuthenticatedTestCase
{ {
protected $StubbedACL;
private static $createdDataboxes = []; private static $createdDataboxes = [];
public function setUp() /**
{ * @param bool $bool
parent::setUp(); * @return PHPUnit_Framework_MockObject_MockObject The stubbedACL
*/
$this->StubbedACL = $this->getMockBuilder('\ACL')
->disableOriginalConstructor()
->getMock();
}
public function setAdmin($bool) public function setAdmin($bool)
{ {
$stubAuthenticatedUser = $this->getMockBuilder('Alchemy\Phrasea\Model\Entities\User') $stubbedACL = $this->stubACL();
->setMethods(['getId'])
->disableOriginalConstructor()
->getMock();
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('is_admin') ->method('is_admin')
->will($this->returnValue($bool)); ->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('give_access_to_sbas') ->method('give_access_to_sbas')
->will($this->returnValue($this->StubbedACL)); ->will($this->returnSelf());
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('update_rights_to_sbas') ->method('update_rights_to_sbas')
->will($this->returnValue($this->StubbedACL)); ->will($this->returnSelf());
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('update_rights_to_bas') ->method('update_rights_to_bas')
->will($this->returnValue($this->StubbedACL)); ->will($this->returnSelf());
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('has_right_on_base') ->method('has_right_on_base')
->will($this->returnValue($bool)); ->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('has_right_on_sbas') ->method('has_right_on_sbas')
->will($this->returnValue($bool)); ->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('has_access_to_sbas') ->method('has_access_to_sbas')
->will($this->returnValue($bool)); ->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('has_access_to_base') ->method('has_access_to_base')
->will($this->returnValue($bool)); ->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('has_right') ->method('has_right')
->will($this->returnValue($bool)); ->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('has_access_to_module') ->method('has_access_to_module')
->will($this->returnValue($bool)); ->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('get_granted_base') ->method('get_granted_base')
->will($this->returnValue([self::$DI['collection']])); ->will($this->returnValue([self::$DI['collection']]));
$this->StubbedACL->expects($this->any()) $stubbedACL->expects($this->any())
->method('get_granted_sbas') ->method('get_granted_sbas')
->will($this->returnValue([self::$DI['collection']->get_databox()])); ->will($this->returnValue([self::$DI['collection']->get_databox()]));
$aclProvider = $this->getMockBuilder('Alchemy\Phrasea\Authentication\ACLProvider') return $stubbedACL;
->disableOriginalConstructor()
->getMock();
$aclProvider->expects($this->any())
->method('get')
->will($this->returnValue($this->StubbedACL));
self::$DI['app']['acl'] = $aclProvider;
} }
public function createDatabox() public function createDatabox()

View File

@@ -44,9 +44,7 @@ class record_adapterTest extends \PhraseanetAuthenticatedTestCase
*/ */
public function testSetExport() public function testSetExport()
{ {
$acl = $this->getMockBuilder('ACL') $acl = $this->stubACL();
->disableOriginalConstructor()
->getMock();
$acl->expects($this->any()) $acl->expects($this->any())
->method('has_right') ->method('has_right')
->with($this->equalTo('order')) ->with($this->equalTo('order'))
@@ -64,15 +62,6 @@ class record_adapterTest extends \PhraseanetAuthenticatedTestCase
->with($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT), $this->equalTo('cancmd')) ->with($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT), $this->equalTo('cancmd'))
->will($this->returnValue(true)); ->will($this->returnValue(true));
$aclProvider = $this->getMockBuilder('Alchemy\Phrasea\Authentication\ACLProvider')
->disableOriginalConstructor()
->getMock();
$aclProvider->expects($this->any())
->method('get')
->will($this->returnValue($acl));
self::$DI['app']['acl'] = $aclProvider;
self::$DI['app']['phraseanet.user-query'] = $this->getMockBuilder('\User_Query')->disableOriginalConstructor()->getMock(); self::$DI['app']['phraseanet.user-query'] = $this->getMockBuilder('\User_Query')->disableOriginalConstructor()->getMock();
self::$DI['app']['phraseanet.user-query']->expects($this->any())->method('get_results')->will($this->returnValue(new ArrayCollection([self::$DI['user_alt2']]))); self::$DI['app']['phraseanet.user-query']->expects($this->any())->method('get_results')->will($this->returnValue(new ArrayCollection([self::$DI['user_alt2']])));
self::$DI['app']['phraseanet.user-query']->expects($this->any())->method('on_base_ids')->will($this->returnSelf()); self::$DI['app']['phraseanet.user-query']->expects($this->any())->method('on_base_ids')->will($this->returnSelf());