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()
{
$this->StubbedACL->expects($this->once())
$this->setAdmin(true)
->expects($this->once())
->method('has_right_on_sbas')
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(false));
$this->setAdmin(true);
self::$DI['client']->request('GET', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/');
$this->assertForbiddenResponse(self::$DI['client']->getResponse());
@@ -152,13 +151,12 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
*/
public function testGetCGU()
{
$this->StubbedACL->expects($this->once())
$this->setAdmin(true)
->expects($this->once())
->method('has_right_on_sbas')
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(true));
$this->setAdmin(true);
self::$DI['client']->request('GET', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/');
$this->assertTrue(self::$DI['client']->getResponse()->isOk());
}
@@ -184,13 +182,12 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
*/
public function testUpdateDatabaseCGU()
{
$this->StubbedACL->expects($this->once())
$this->setAdmin(true)
->expects($this->once())
->method('has_right_on_sbas')
->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct')
->will($this->returnValue(true));
$this->setAdmin(true);
$cgusUpdate = 'Test update 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()
{
$stubbedACL = $this->getMockBuilder('\ACL')
->disableOriginalConstructor()
->getMock();
$stubbedACL = $this->stubACL();
//has_right_on_base return true
$stubbedACL->expects($this->once())
@@ -48,14 +46,6 @@ class ShareTest extends \PhraseanetAuthenticatedWebTestCase
->method('has_access_to_subdef')
->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());
self::$DI['client']->request('GET', $url);

View File

@@ -1,7 +1,11 @@
<?php
use Alchemy\Phrasea\Authentication\ACLProvider;
abstract class PhraseanetAuthenticatedTestCase extends \PhraseanetTestCase
{
/** @var PHPUnit_Framework_MockObject_MockObject */
protected $stubbedACL;
public function setUp()
{
@@ -14,4 +18,29 @@ abstract class PhraseanetAuthenticatedTestCase extends \PhraseanetTestCase
$this->logout(self::$DI['app']);
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
{
protected $StubbedACL;
private static $createdDataboxes = [];
public function setUp()
{
parent::setUp();
$this->StubbedACL = $this->getMockBuilder('\ACL')
->disableOriginalConstructor()
->getMock();
}
/**
* @param bool $bool
* @return PHPUnit_Framework_MockObject_MockObject The stubbedACL
*/
public function setAdmin($bool)
{
$stubAuthenticatedUser = $this->getMockBuilder('Alchemy\Phrasea\Model\Entities\User')
->setMethods(['getId'])
->disableOriginalConstructor()
->getMock();
$stubbedACL = $this->stubACL();
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->method('is_admin')
->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->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')
->will($this->returnValue($this->StubbedACL));
->will($this->returnSelf());
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->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')
->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->method('has_right_on_sbas')
->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->method('has_access_to_sbas')
->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->method('has_access_to_base')
->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->method('has_right')
->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->method('has_access_to_module')
->will($this->returnValue($bool));
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->method('get_granted_base')
->will($this->returnValue([self::$DI['collection']]));
$this->StubbedACL->expects($this->any())
$stubbedACL->expects($this->any())
->method('get_granted_sbas')
->will($this->returnValue([self::$DI['collection']->get_databox()]));
$aclProvider = $this->getMockBuilder('Alchemy\Phrasea\Authentication\ACLProvider')
->disableOriginalConstructor()
->getMock();
$aclProvider->expects($this->any())
->method('get')
->will($this->returnValue($this->StubbedACL));
self::$DI['app']['acl'] = $aclProvider;
return $stubbedACL;
}
public function createDatabox()

View File

@@ -44,9 +44,7 @@ class record_adapterTest extends \PhraseanetAuthenticatedTestCase
*/
public function testSetExport()
{
$acl = $this->getMockBuilder('ACL')
->disableOriginalConstructor()
->getMock();
$acl = $this->stubACL();
$acl->expects($this->any())
->method('has_right')
->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'))
->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']->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());