diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Admin/DataboxTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Admin/DataboxTest.php index 7d982a212b..1dfa9907c9 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Admin/DataboxTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Admin/DataboxTest.php @@ -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/', [ diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/ShareTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/ShareTest.php index 836b656a68..f4991949e9 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/ShareTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/ShareTest.php @@ -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); diff --git a/tests/classes/PhraseanetAuthenticatedTestCase.php b/tests/classes/PhraseanetAuthenticatedTestCase.php index 200e3bf17e..6882b10668 100644 --- a/tests/classes/PhraseanetAuthenticatedTestCase.php +++ b/tests/classes/PhraseanetAuthenticatedTestCase.php @@ -1,7 +1,11 @@ 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; + } } diff --git a/tests/classes/PhraseanetAuthenticatedWebTestCase.php b/tests/classes/PhraseanetAuthenticatedWebTestCase.php index eb75291aaf..5288eceae4 100644 --- a/tests/classes/PhraseanetAuthenticatedWebTestCase.php +++ b/tests/classes/PhraseanetAuthenticatedWebTestCase.php @@ -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() diff --git a/tests/classes/record/adapterTest.php b/tests/classes/record/adapterTest.php index f0a2e32fa3..ac6d271ee8 100644 --- a/tests/classes/record/adapterTest.php +++ b/tests/classes/record/adapterTest.php @@ -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());