Skip to content

Commit

Permalink
Merge branch 1.5 into 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Feb 12, 2021
2 parents c33f5d8 + 4c7eb6b commit 729f44b
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/siteaccess.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ services:
$siteAccessGroups: '%ezpublish.siteaccess.groups%'

EzSystems\EzPlatformAdminUi\Siteaccess\AdminSiteaccessPreviewVoter:
arguments:
$repositoryConfigurationProvider: '@ezpublish.api.repository_configuration_provider'
tags: ['ezplatform.admin_ui.siteaccess_preview_voter']
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,
],
];
}
}

0 comments on commit 729f44b

Please # to comment.