From 4c7eb6b8f55d40d0a6dd3fd9f78a5edb79093782 Mon Sep 17 00:00:00 2001 From: Bartek Date: Fri, 12 Feb 2021 08:17:03 +0100 Subject: [PATCH] EZEE-3465: Made SiteaccessPreviewVoter repository aware (#1706) --- .../AbstractSiteaccessPreviewVoter.php | 33 ++- .../AdminSiteaccessPreviewVoterTest.php | 188 ++++++++++++++++++ 2 files changed, 216 insertions(+), 5 deletions(-) create mode 100644 src/lib/Tests/Siteaccess/AdminSiteaccessPreviewVoterTest.php diff --git a/src/lib/Siteaccess/AbstractSiteaccessPreviewVoter.php b/src/lib/Siteaccess/AbstractSiteaccessPreviewVoter.php index 5fda135591..bf19e870e7 100644 --- a/src/lib/Siteaccess/AbstractSiteaccessPreviewVoter.php +++ b/src/lib/Siteaccess/AbstractSiteaccessPreviewVoter.php @@ -8,6 +8,7 @@ namespace EzSystems\EzPlatformAdminUi\Siteaccess; +use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider; use eZ\Publish\Core\MVC\ConfigResolverInterface; abstract class AbstractSiteaccessPreviewVoter implements SiteaccessPreviewVoterInterface @@ -15,13 +16,15 @@ abstract class AbstractSiteaccessPreviewVoter implements SiteaccessPreviewVoterI /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ protected $configResolver; - /** - * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver - */ + /** @var \eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider */ + protected $repositoryConfigurationProvider; + public function __construct( - ConfigResolverInterface $configResolver + ConfigResolverInterface $configResolver, + RepositoryConfigurationProvider $repositoryConfigurationProvider ) { $this->configResolver = $configResolver; + $this->repositoryConfigurationProvider = $repositoryConfigurationProvider; } /** @@ -38,12 +41,17 @@ public function vote(SiteaccessPreviewVoterContext $context): bool return false; } + if (!$this->validateRepositoryMatch($siteaccess)) { + return false; + } + $siteaccessLanguages = $this->configResolver->getParameter( 'languages', null, $siteaccess ); - if (!in_array($languageCode, $siteaccessLanguages)) { + + if (!in_array($languageCode, $siteaccessLanguages, true)) { return false; } @@ -58,6 +66,21 @@ public function vote(SiteaccessPreviewVoterContext $context): bool return true; } + protected function validateRepositoryMatch(string $siteaccess): bool + { + $siteaccessRepository = $this->configResolver->getParameter( + 'repository', + null, + $siteaccess + ); + + // If SA does not have a repository configured we should obtain the default one, which is used as a fallback. + $siteaccessRepository = $siteaccessRepository ?: $this->repositoryConfigurationProvider->getDefaultRepositoryAlias(); + $currentRepository = $this->repositoryConfigurationProvider->getCurrentRepositoryAlias(); + + return $siteaccessRepository === $currentRepository; + } + /** * @param string $siteaccess * diff --git a/src/lib/Tests/Siteaccess/AdminSiteaccessPreviewVoterTest.php b/src/lib/Tests/Siteaccess/AdminSiteaccessPreviewVoterTest.php new file mode 100644 index 0000000000..c7313d1ea6 --- /dev/null +++ b/src/lib/Tests/Siteaccess/AdminSiteaccessPreviewVoterTest.php @@ -0,0 +1,188 @@ +configResolver = $this->createMock(ConfigResolverInterface::class); + $this->repositoryConfigurationProvider = $this->createMock(RepositoryConfigurationProvider::class); + + $this->adminSiteaccessPreviewVoter = new AdminSiteaccessPreviewVoter( + $this->configResolver, + $this->repositoryConfigurationProvider + ); + } + + public function testVoteWithInvalidPath(): void + { + $languageCode = self::LANGUAGE_CODE; + $location = new Location(['id' => 1234, 'path' => [1]]); + $versionInfo = new VersionInfo([ + 'contentInfo' => new ContentInfo(['mainLanguageCode' => $languageCode]), + ]); + $siteaccess = 'site'; + + $context = new SiteaccessPreviewVoterContext($location, $versionInfo, $siteaccess, $languageCode); + + $this->mockConfigMethods($context); + + $this->assertFalse($this->adminSiteaccessPreviewVoter->vote($context)); + } + + /** + * @dataProvider dataProviderForSiteaccessPreviewVoterContext + */ + public function testVoteWithInvalidLanguageMatch(SiteaccessPreviewVoterContext $context): void + { + $this->mockConfigMethods($context); + + $this->repositoryConfigurationProvider + ->expects($this->at(0)) + ->method('getDefaultRepositoryAlias') + ->willReturn('default'); + + $this->repositoryConfigurationProvider + ->expects($this->at(1)) + ->method('getCurrentRepositoryAlias') + ->willReturn('default'); + + $this->configResolver + ->expects($this->at(3)) + ->method('getParameter') + ->with('repository', null, $context->getSiteaccess()) + ->willReturn(null); + + $this->configResolver + ->expects($this->at(4)) + ->method('getParameter') + ->with('languages', null, $context->getSiteaccess()) + ->willReturn(['ger-DE']); + + $this->assertFalse($this->adminSiteaccessPreviewVoter->vote($context)); + } + + /** + * @dataProvider dataProviderForSiteaccessPreviewVoterContext + */ + public function testVoteWithInvalidRepositoryMatch(SiteaccessPreviewVoterContext $context): void + { + $this->mockConfigMethods($context); + + $this->configResolver + ->expects($this->at(3)) + ->method('getParameter') + ->with('repository', null, $context->getSiteaccess()) + ->willReturn(null); + + $this->repositoryConfigurationProvider + ->expects($this->at(0)) + ->method('getDefaultRepositoryAlias') + ->willReturn('default'); + + $this->repositoryConfigurationProvider + ->expects($this->at(1)) + ->method('getCurrentRepositoryAlias') + ->willReturn('main'); + + $this->assertFalse($this->adminSiteaccessPreviewVoter->vote($context)); + } + + /** + * @dataProvider dataProviderForSiteaccessPreviewVoterContext + */ + public function testVoteWithValidRepositoryAndLanguageMatch(SiteaccessPreviewVoterContext $context): void + { + $this->mockConfigMethods($context); + + $this->configResolver + ->expects($this->at(3)) + ->method('getParameter') + ->with('repository', null, $context->getSiteaccess()) + ->willReturn(null); + + $this->repositoryConfigurationProvider + ->expects($this->at(0)) + ->method('getDefaultRepositoryAlias') + ->willReturn('default'); + + $this->repositoryConfigurationProvider + ->expects($this->at(1)) + ->method('getCurrentRepositoryAlias') + ->willReturn('default'); + + $this->configResolver + ->expects($this->at(4)) + ->method('getParameter') + ->with('languages', null, $context->getSiteaccess()) + ->willReturn(['eng-GB', 'fre-FR']); + + $this->assertTrue($this->adminSiteaccessPreviewVoter->vote($context)); + } + + private function mockConfigMethods(SiteaccessPreviewVoterContext $context): void + { + $this->configResolver + ->expects($this->at(0)) + ->method('getParameter') + ->with('content.tree_root.location_id', null, $context->getSiteaccess()) + ->willReturn(2); + + $this->configResolver + ->expects($this->at(1)) + ->method('getParameter') + ->with('location_ids.media', null, $context->getSiteaccess()) + ->willReturn(43); + + $this->configResolver + ->expects($this->at(2)) + ->method('getParameter') + ->with('location_ids.users', null, $context->getSiteaccess()) + ->willReturn(5); + } + + public function dataProviderForSiteaccessPreviewVoterContext(): array + { + $languageCode = self::LANGUAGE_CODE; + $location = new Location(['id' => 123456, 'path' => [1, 2]]); + $versionInfo = new VersionInfo([ + 'contentInfo' => new ContentInfo(['mainLanguageCode' => $languageCode]), + ]); + $siteaccess = 'site'; + + $context = new SiteaccessPreviewVoterContext($location, $versionInfo, $siteaccess, $languageCode); + + return [ + [ + $context, + ], + ]; + } +}