diff --git a/Export/ExportManager.php b/Export/ExportManager.php index ace1580fa..e18c643cd 100644 --- a/Export/ExportManager.php +++ b/Export/ExportManager.php @@ -28,7 +28,7 @@ use Psr\Log\LoggerInterface; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\QueryBuilder; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; -use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Chill\MainBundle\Form\Type\Export\PickCenterType; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; @@ -104,7 +104,7 @@ class ExportManager public function __construct( LoggerInterface $logger, EntityManagerInterface $em, - AuthorizationChecker $authorizationChecker, + AuthorizationCheckerInterface $authorizationChecker, AuthorizationHelper $authorizationHelper, TokenStorageInterface $tokenStorage) { diff --git a/Test/PrepareUserTrait.php b/Test/PrepareUserTrait.php index 697e28f5b..5eccf2770 100644 --- a/Test/PrepareUserTrait.php +++ b/Test/PrepareUserTrait.php @@ -45,10 +45,10 @@ trait PrepareUserTrait * ``` * array( * array( 'center' => $centerA, 'permissionsGroup' => array( - * [ 'role' => 'CHILL_REPORT_SEE', 'scope' => $scopeA] + * 'role' => 'CHILL_REPORT_SEE', 'scope' => $scopeA * ), * array( 'center' => $centerB, 'permissionsGroup' => array( - * [ 'role' => 'CHILL_ACTIVITY_UPDATE', 'scope' => $scopeB] + * 'role' => 'CHILL_ACTIVITY_UPDATE', 'scope' => $scopeB * ) * ) * ``` diff --git a/Tests/Export/ExportManagerTest.php b/Tests/Export/ExportManagerTest.php index c2d7110ae..0a8338112 100644 --- a/Tests/Export/ExportManagerTest.php +++ b/Tests/Export/ExportManagerTest.php @@ -21,6 +21,7 @@ namespace Chill\MainBundle\Tests\Export; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Chill\MainBundle\Export\ExportManager; +use Symfony\Component\Security\Core\Role\Role; /** * Test the export manager @@ -30,6 +31,11 @@ use Chill\MainBundle\Export\ExportManager; */ class ExportManagerTest extends KernelTestCase { + + use \Chill\MainBundle\Test\PrepareCenterTrait; + use \Chill\MainBundle\Test\PrepareUserTrait; + use \Chill\MainBundle\Test\PrepareScopeTrait; + /** * * @var \Symfony\Component\DependencyInjection\ContainerInterface @@ -69,10 +75,10 @@ class ExportManagerTest extends KernelTestCase * @param \Symfony\Component\Security\Core\User\UserInterface $user * @return ExportManager */ - public function createExportManager( + protected function createExportManager( \Psr\Log\LoggerInterface $logger = null, \Doctrine\ORM\EntityManagerInterface $em = null, - \Symfony\Component\Security\Core\Authorization\AuthorizationChecker $authorizationChecker = null, + \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $authorizationChecker = null, \Chill\MainBundle\Security\Authorization\AuthorizationHelper $authorizationHelper = null, \Symfony\Component\Security\Core\User\UserInterface $user = null ) @@ -287,9 +293,53 @@ class ExportManagerTest extends KernelTestCase $this->assertNotContains($formatterBar->reveal(), $obtained); } - public function testIsGrantedForElementUserIsGranted() + public function testIsGrantedForElementWithExportAndUserIsGranted() { - $this->markTestSkipped(); + $center = $this->prepareCenter(100, 'center A'); + $user = $this->prepareUser(array()); + + $authorizationChecker = $this->prophet->prophesize(); + $authorizationChecker->willImplement('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface'); + $authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center) + ->willReturn(True); + + $exportManager = $this->createExportManager(null, null, + $authorizationChecker->reveal(), null, $user); + + $export = $this->prophet->prophesize(); + $export->willImplement('Chill\MainBundle\Export\ExportInterface'); + $export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); + + $result = $exportManager->isGrantedForElement($export->reveal(), null, array($center)); + + $this->assertTrue($result); + + } + + public function testIsGrantedForElementWithExportAndUserIsGrantedNotForAllCenters() + { + $center = $this->prepareCenter(100, 'center A'); + $centerB = $this->prepareCenter(102, 'center B'); + $user = $this->prepareUser(array()); + + $authorizationChecker = $this->prophet->prophesize(); + $authorizationChecker->willImplement('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface'); + $authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center) + ->willReturn(true); + $authorizationChecker->isGranted('CHILL_STAT_DUMMY', $centerB) + ->willReturn(false); + + $exportManager = $this->createExportManager(null, null, + $authorizationChecker->reveal(), null, $user); + + $export = $this->prophet->prophesize(); + $export->willImplement('Chill\MainBundle\Export\ExportInterface'); + $export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); + + $result = $exportManager->isGrantedForElement($export->reveal(), null, array($center, $centerB)); + + $this->assertFalse($result); + }