Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZEE-3465: Made SiteaccessPreviewVoter repository aware #1706

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/lib/Siteaccess/AbstractSiteaccessPreviewVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@

namespace EzSystems\EzPlatformAdminUi\Siteaccess;

use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;
use eZ\Publish\Core\MVC\ConfigResolverInterface;

abstract class AbstractSiteaccessPreviewVoter implements SiteaccessPreviewVoterInterface
{
/** @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;
}

/**
Expand All @@ -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;
}

Expand All @@ -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
*
Expand Down
188 changes: 188 additions & 0 deletions src/lib/Tests/Siteaccess/AdminSiteaccessPreviewVoterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Tests\Siteaccess;

use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\Core\Repository\Values\Content\Location;
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
use EzSystems\EzPlatformAdminUi\Siteaccess\AdminSiteaccessPreviewVoter;
use EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessPreviewVoterContext;
use PHPUnit\Framework\TestCase;

class AdminSiteaccessPreviewVoterTest extends TestCase
{
private const LANGUAGE_CODE = 'eng-GB';

/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

/** @var \eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider */
private $repositoryConfigurationProvider;

/** @var \EzSystems\EzPlatformAdminUi\Siteaccess\AdminSiteaccessPreviewVoter */
private $adminSiteaccessPreviewVoter;

public function setUp(): void
{
$this->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,
],
];
}
}