Skip to content
Permalink
Browse files

Merge branch 'b-6.2.x'

# Conflicts:
#	tests/Unit/Internal/Framework/Module/Setup/Validator/ControllersModuleSettingValidatorTest.php
  • Loading branch information
IgorIe committed Mar 25, 2020
2 parents ad442f5 + 95b8754 commit 30a768082f21f093b44f04ecdfa3e7139b16070b
@@ -317,10 +317,7 @@ protected function _sessionStart() // phpcs:ignore PSR2.Methods.MethodDeclaratio
session_cache_limiter('');
}

$config = \OxidEsales\Eshop\Core\Registry::getConfig();
$this->_blStarted = session_start([
'use_cookies' => $config->getConfigParam('blSessionUseCookies')
]);
$this->_blStarted = session_start();
if (!$this->getSessionChallengeToken()) {
$this->_initNewSessionChallenge();
}
@@ -16,8 +16,11 @@
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration\Controller;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Exception\ControllersDuplicationModuleConfigurationException;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use Psr\Log\LoggerInterface;

use function is_array;
use function in_array;
use function array_key_exists;

class ControllersValidator implements ModuleConfigurationValidatorInterface
{
@@ -32,16 +35,23 @@ class ControllersValidator implements ModuleConfigurationValidatorInterface
private $shopConfigurationSettingDao;

/**
* ControllersValidator constructor.
* @param ShopAdapterInterface $shopAdapter
* @var LoggerInterface
*/
private $logger;

/**
* @param ShopAdapterInterface $shopAdapter
* @param ShopConfigurationSettingDaoInterface $shopConfigurationSettingDao
* @param LoggerInterface $logger
*/
public function __construct(
ShopAdapterInterface $shopAdapter,
ShopConfigurationSettingDaoInterface $shopConfigurationSettingDao
ShopConfigurationSettingDaoInterface $shopConfigurationSettingDao,
LoggerInterface $logger
) {
$this->shopAdapter = $shopAdapter;
$this->shopConfigurationSettingDao = $shopConfigurationSettingDao;
$this->logger = $logger;
}

/**
@@ -53,18 +63,35 @@ public function __construct(
public function validate(ModuleConfiguration $configuration, int $shopId)
{
if ($configuration->hasControllers()) {
$shopControllerClassMap = $this->shopAdapter->getShopControllerClassMap();

$controllerClassMap = array_merge(
$shopControllerClassMap,
$this->getModulesControllerClassMap($shopId)
);

$this->validateForControllerKeyDuplication($configuration->getControllers(), $controllerClassMap);
$this->validateForControllerNamespaceDuplication($configuration->getControllers(), $controllerClassMap);
$controllerClassMap = $this->getControllersClassMap($shopId);

foreach ($configuration->getControllers() as $controller) {
if (!$this->controllerAlreadyExistsInMap($controller, $controllerClassMap)) {
$this->validateKeyDuplication($controller, $controllerClassMap);
$this->validateNamespaceDuplication($controller, $controllerClassMap);
} else {
/**
* @TODO this is a wrong place to check and log database discrepancy, not only controllers should be
* checked. It should be moved to separate module data discrepancy checker outside the module
* validation.
*/
$this->logger->error(
'Module data discrepancy error: module data (controller with id '
. $controller->getId() . ' and namespace: '
. $controller->getControllerClassNameSpace() . ' ) for module '
. $configuration->getId() . ' was present in the database before the module activation'
);
}
}
}
}

private function controllerAlreadyExistsInMap(Controller $controller, array $controllerClassMap): bool
{
return array_key_exists(strtolower($controller->getId()), $controllerClassMap)
&& $controllerClassMap[strtolower($controller->getId())] === $controller->getControllerClassNameSpace();
}

/**
* @param int $shopId
* @return array
@@ -90,76 +117,42 @@ private function getModulesControllerClassMap(int $shopId): array
}

/**
* @param Controller[] $controllers
* @param Controller $controller
* @param array $controllerClassMap
*
* @throws ControllersDuplicationModuleConfigurationException
*/
private function validateForControllerNamespaceDuplication(array $controllers, array $controllerClassMap)
private function validateKeyDuplication(Controller $controller, array $controllerClassMap): void
{
$duplications = $this->findDuplicateControllerClassNameSpaces(
$controllers,
$controllerClassMap
);

if (!empty($duplications)) {
if (array_key_exists(strtolower($controller->getId()), $controllerClassMap)) {
throw new ControllersDuplicationModuleConfigurationException(
'Controller namespaces duplication: ' . implode(', ', $duplications)
'Controller key duplication: ' . $controller->getId()
);
}
}

/**
* @param Controller[] $controllers
* @param Controller $controller
* @param array $controllerClassMap
*
* @throws ControllersDuplicationModuleConfigurationException
*/
private function validateForControllerKeyDuplication(array $controllers, array $controllerClassMap)
private function validateNamespaceDuplication(Controller $controller, array $controllerClassMap): void
{
$duplications = $this->findDuplicateControllerIds(
$controllers,
$controllerClassMap
);

if (!empty($duplications)) {
if (in_array($controller->getControllerClassNameSpace(), $controllerClassMap, true)) {
throw new ControllersDuplicationModuleConfigurationException(
'Controller keys duplication: ' . implode(', ', $duplications)
'Controller namespace duplication: ' . $controller->getControllerClassNameSpace()
);
}
}

/**
* @param Controller[] $controllers
* @param array $controllerClassMap
*
* @return array
*/
private function findDuplicateControllerClassNameSpaces(array $controllers, array $controllerClassMap): array
{
$controllerClassNameSpaces = [];

foreach ($controllers as $controller) {
$controllerClassNameSpaces[] = $controller->getControllerClassNameSpace();
}

return array_intersect($controllerClassNameSpaces, $controllerClassMap);
}

/**
* @param Controller[] $controllers
* @param array $controllerClassMap
*
* @param int $shopId
* @return array
*/
private function findDuplicateControllerIds(array $controllers, array $controllerClassMap): array
private function getControllersClassMap(int $shopId): array
{
$controllerIds = [];

foreach ($controllers as $controller) {
$controllerIds[] = strtolower($controller->getId());
}

return array_intersect($controllerIds, $controllerClassMap);
return array_merge(
$this->shopAdapter->getShopControllerClassMap(),
$this->getModulesControllerClassMap($shopId)
);
}
}
@@ -1,122 +0,0 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\EshopCommunity\Tests\Unit\Internal\Framework\Module\Setup\Validator;

use OxidEsales\EshopCommunity\Internal\Framework\Config\Dao\ShopConfigurationSettingDaoInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Config\DataObject\ShopConfigurationSetting;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Validator\ControllersValidator;
use PHPUnit\Framework\TestCase;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration\Controller;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Exception\ControllersDuplicationModuleConfigurationException;

/**
* @internal
*/
class ControllersModuleSettingValidatorTest extends TestCase
{
public function testValidationWithCorrectSetting()
{
$shopAdapter = $this->getMockBuilder(ShopAdapterInterface::class)->getMock();
$shopAdapter
->method('getShopControllerClassMap')
->willReturn([
'shopControllerName' => 'shopControllerNamespace',
]);

$shopConfigurationSetting = new ShopConfigurationSetting();
$shopConfigurationSetting->setValue(
[
'moduleId' => [
'alreadyActiveModuleControllerName' => 'alreadyActiveModuleControllerNamepace'
],
]
);

$shopConfigurationSettingDao = $this->getMockBuilder(ShopConfigurationSettingDaoInterface::class)->getMock();
$shopConfigurationSettingDao
->method('get')
->willReturn($shopConfigurationSetting);

$validator = new ControllersValidator($shopAdapter, $shopConfigurationSettingDao);

$moduleConfiguration = new ModuleConfiguration();
$moduleConfiguration->addController(
new Controller(
'newModuleControllerName',
'newModuleControllerNamepace'
)
);

$this->assertNull(
$validator->validate($moduleConfiguration, 1)
);
}

/**
* @dataProvider duplicatedSettingValueDataProvider
*
* @param Controller[] $duplicatedSettingValue
*
*/
public function testValidationWithDuplicatedControllerNamespace(array $duplicatedSettingValue)
{
$this->expectException(ControllersDuplicationModuleConfigurationException::class);
$shopAdapter = $this->getMockBuilder(ShopAdapterInterface::class)->getMock();
$shopAdapter
->method('getShopControllerClassMap')
->willReturn([
'duplicatedname' => 'duplicatedNamespace',
]);

$shopConfigurationSetting = new ShopConfigurationSetting();
$shopConfigurationSetting->setValue(
[
'moduleId' => [
'alreadyActiveModuleControllerName' => 'alreadyActiveModuleControllerNamepace'
],
]
);

$shopConfigurationSettingDao = $this->getMockBuilder(ShopConfigurationSettingDaoInterface::class)->getMock();
$shopConfigurationSettingDao
->method('get')
->willReturn($shopConfigurationSetting);

$validator = new ControllersValidator($shopAdapter, $shopConfigurationSettingDao);

$moduleConfiguration = new ModuleConfiguration();
foreach ($duplicatedSettingValue as $value) {
$moduleConfiguration->addController(
new Controller(
$value->getId(),
$value->getControllerClassNameSpace()
)
);
}

$validator->validate($moduleConfiguration, 1);
}

public function duplicatedSettingValueDataProvider(): array
{
return [
[
[
new Controller('moduleControllerName', 'duplicatedNamespace'),
],
[
new Controller('duplicatedName', 'moduleControllerNamepace'),
],
]
];
}
}

0 comments on commit 30a7680

Please sign in to comment.
You can’t perform that action at this time.