Add SystemConfig Legacy Handler

- Add SystemConfi Legacy handler
- Add service configuration to specify which configs are exposed to the frontend
- Update Systemconfig Item and Collection data provider to use legacy handler
- Add unit tests for legacy handler
This commit is contained in:
Clemente Raposo 2020-03-10 13:06:53 +00:00 committed by Dillon-Brown
parent 32d40be3e7
commit c02ab30a05
7 changed files with 313 additions and 27 deletions

View file

@ -20,6 +20,7 @@ services:
$legacyDir: '%legacy.dir%'
$legacySessionName: '%legacy.session_name%'
$defaultSessionName: '%default_session_name%'
$exposedSystemConfigs: '%legacy.exposed_system_configs%'
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name

View file

@ -0,0 +1,6 @@
parameters:
legacy.exposed_system_configs:
default_language: true
passwordsetting:
forgotpasswordON: true
languages: true

View file

@ -0,0 +1,158 @@
<?php
namespace SuiteCRM\Core\Legacy;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use App\Entity\SystemConfig;
class SystemConfigHandler extends LegacyHandler
{
protected const MSG_CONFIG_NOT_FOUND = 'Not able to find config key: ';
/**
* @var array
*/
protected $exposedSystemConfigs = [];
/**
* SystemConfigHandler constructor.
* @param string $projectDir
* @param string $legacyDir
* @param string $legacySessionName
* @param string $defaultSessionName
* @param array $exposedSystemConfigs
*/
public function __construct(
string $projectDir,
string $legacyDir,
string $legacySessionName,
string $defaultSessionName,
array $exposedSystemConfigs
) {
parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName);
$this->exposedSystemConfigs = $exposedSystemConfigs;
}
/**
* Get all exposed system configs
* @return array
*/
public function getAllSystemConfigs(): array
{
$this->init();
$configs = [];
foreach ($this->exposedSystemConfigs as $configKey => $value) {
$config = $this->loadSystemConfig($configKey);
if (!empty($config)) {
$configs[] = $config;
}
}
$this->close();
return $configs;
}
/**
* Get system config
* @param string $configKey
* @return SystemConfig|null
*/
public function getSystemConfig(string $configKey): ?SystemConfig
{
$this->init();
$config = $this->loadSystemConfig($configKey);
$this->close();
return $config;
}
/**
* Load system config with given $key
* @param $configKey
* @return SystemConfig|null
*/
protected function loadSystemConfig(string $configKey): ?SystemConfig
{
global $sugar_config;
if (empty($configKey)) {
return null;
}
if (!isset($this->exposedSystemConfigs[$configKey])) {
throw new ItemNotFoundException(self::MSG_CONFIG_NOT_FOUND . "'$configKey'");
}
$config = new SystemConfig();
$config->setId($configKey);
if (!isset($sugar_config[$configKey])) {
return $config;
}
if (is_array($sugar_config[$configKey])) {
$items = $sugar_config[$configKey];
if (is_array($this->exposedSystemConfigs[$configKey])) {
$items = $this->filterItems($sugar_config[$configKey], $this->exposedSystemConfigs[$configKey]);
}
$config->setItems($items);
return $config;
}
$config->setValue($sugar_config[$configKey]);
return $config;
}
/**
* Filter to retrieve only exposed items
* @param array $allItems
* @param array $exposed
* @return array
*/
protected function filterItems(array $allItems, array $exposed): array
{
$items = [];
if (empty($exposed)) {
return $items;
}
foreach ($allItems as $configKey => $configValue) {
if (!isset($exposed[$configKey])) {
continue;
}
if (is_array($allItems[$configKey])) {
$subItems = $allItems[$configKey];
if (is_array($exposed[$configKey])) {
$subItems = $this->filterItems($allItems[$configKey], $exposed[$configKey]);
}
$items[$configKey] = $subItems;
continue;
}
$items[$configKey] = $configValue;
}
return $items;
}
}

View file

@ -8,15 +8,29 @@ use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\PaginatorInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\SystemConfig;
use SuiteCRM\Core\Legacy\SystemConfigHandler;
/**
* Class SystemConfigCollectionDataProvider
* @package App\DataProvider
*/
class SystemConfigCollectionDataProvider implements ContextAwareCollectionDataProviderInterface,
RestrictedDataProviderInterface
{
/**
* @var SystemConfigHandler
*/
private $systemConfigHandler;
/**
* SystemConfigCollectionDataProvider constructor.
* @param SystemConfigHandler $systemConfigHandler
*/
public function __construct(SystemConfigHandler $systemConfigHandler)
{
$this->systemConfigHandler = $systemConfigHandler;
}
/**
* Define supported Resource Classes
* @param string $resourceClass
@ -38,22 +52,7 @@ class SystemConfigCollectionDataProvider implements ContextAwareCollectionDataPr
string $operationName = null,
array $context = []
): PaginatorInterface {
$systemConfigs = [];
$defaultLanguage = new SystemConfig();
$defaultLanguage->setId('default_language');
$defaultLanguage->setValue('en_us');
$systemConfigs[] = $defaultLanguage;
$passwordSettings = new SystemConfig();
$passwordSettings->setId('password_settings');
$passwordSettings->setItems(
[
'password_recovery_enabled' => 'true'
]
);
$systemConfigs[] = $passwordSettings;
$systemConfigs = $this->systemConfigHandler->getAllSystemConfigs();
return new ArrayPaginator($systemConfigs, 0, count($systemConfigs));
}

View file

@ -5,12 +5,28 @@ namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\SystemConfig;
use SuiteCRM\Core\Legacy\SystemConfigHandler;
/**
* Class SystemConfigItemDataProvider
*/
final class SystemConfigItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
/**
* @var SystemConfigHandler
*/
private $systemConfigHandler;
/**
* SystemConfigItemDataProvider constructor.
* @param SystemConfigHandler $systemConfigHandler
*/
public function __construct(SystemConfigHandler $systemConfigHandler)
{
$this->systemConfigHandler = $systemConfigHandler;
}
/**
* Defined supported resources
* @param string $resourceClass
@ -37,10 +53,7 @@ final class SystemConfigItemDataProvider implements ItemDataProviderInterface, R
string $operationName = null,
array $context = []
): ?SystemConfig {
$config = new SystemConfig();
$config->setId('default_language');
$config->setValue('en_us');
return $config;
return $this->systemConfigHandler->getSystemConfig($id);
}
}

View file

@ -36,9 +36,9 @@ class SystemConfig
/**
* @ApiProperty
* @var array|null
* @var array
*/
protected $items;
protected $items = [];
/**
* Get Id
@ -84,19 +84,19 @@ class SystemConfig
/**
* Get items
* @return array|null
* @return array
*/
public function getItems(): ?array
public function getItems(): array
{
return $this->items;
}
/**
* Set Items
* @param array|null $items
* @param array $items
* @return SystemConfig
*/
public function setItems(?array $items): SystemConfig
public function setItems(array $items): SystemConfig
{
$this->items = $items;

View file

@ -0,0 +1,109 @@
<?php namespace App\Tests;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use Codeception\Test\Unit;
use SuiteCRM\Core\Legacy\SystemConfigHandler;
class SystemConfigHandlerTest extends Unit
{
/**
* @var UnitTester
*/
protected $tester;
/**
* @var SystemConfigHandler
*/
protected $handler;
protected function _before()
{
$exposedSystemConfigs = [
'default_language' => true,
'passwordsetting' => [
'forgotpasswordON' => true
],
'search' => [
'controller' => true,
'pagination' => [
'min' => true,
]
],
'languages' => true,
];
$projectDir = codecept_root_dir();
$legacyDir = $projectDir . '/legacy';
$legacySessionName = 'LEGACYSESSID';
$defaultSessionName = 'PHPSESSID';
$this->handler = new SystemConfigHandler($projectDir, $legacyDir, $legacySessionName, $defaultSessionName,
$exposedSystemConfigs);
}
protected function _after()
{
}
// tests
public function testEmptySystemConfigKeyCheck()
{
$defaultLanguage = $this->handler->getSystemConfig('');
static::assertNull($defaultLanguage);
}
public function testInvalidExposedSystemConfigCheck()
{
$this->expectException(ItemNotFoundException::class);
$this->handler->getSystemConfig('dbconfig');
}
public function testGetValidOneLevelSystemConfig()
{
$defaultLanguage = $this->handler->getSystemConfig('default_language');
static::assertNotNull($defaultLanguage);
static::assertEquals('default_language', $defaultLanguage->getId());
static::assertEquals('en_us', $defaultLanguage->getValue());
static::assertEmpty($defaultLanguage->getItems());
}
public function testGetValidTwoLevelSystemConfig()
{
$defaultLanguage = $this->handler->getSystemConfig('passwordsetting');
static::assertNotNull($defaultLanguage);
static::assertEquals('passwordsetting', $defaultLanguage->getId());
static::assertNull($defaultLanguage->getValue());
static::assertIsArray($defaultLanguage->getItems());
$expected = [
'forgotpasswordON' => false
];
$this->assertSame(
$expected,
$defaultLanguage->getItems()
);
}
public function testGetValidThreeLevelSystemConfig()
{
$defaultLanguage = $this->handler->getSystemConfig('search');
static::assertNotNull($defaultLanguage);
static::assertEquals('search', $defaultLanguage->getId());
static::assertNull($defaultLanguage->getValue());
static::assertIsArray($defaultLanguage->getItems());
$expected = [
'controller' => 'UnifiedSearch',
'pagination' => [
'min' => 10,
]
];
static::assertSame(
$expected,
$defaultLanguage->getItems()
);
}
}