Add date and time format config and preference mappers

- Update SystemConfigHander
-- Add ability to map config name
-- Add ability to map config value
-- Add SystemConfigMappers.php to store config mappers

- Update UserPreferenceHandler
-- Add ability to map preference name
-- Add ability to map preference value
-- Add UserPreferencesMappers to store preferences mappers

- Configure config mappers for date format
- Configure preference mappers for time format

- Update unit tests
This commit is contained in:
Clemente Raposo 2020-05-29 17:14:12 +01:00 committed by Dillon-Brown
parent fceea32f83
commit 21a831e9f3
33 changed files with 816 additions and 57 deletions

View file

@ -23,16 +23,23 @@ services:
$legacySessionName: '%legacy.session_name%'
$defaultSessionName: '%default_session_name%'
$exposedSystemConfigs: '%legacy.exposed_system_configs%'
$systemConfigKeyMap: '%legacy.system_config_key_map%'
$menuItemMap: '%legacy.menu_item_map%'
$legacyAssetPaths: '%legacy.asset_paths%'
$legacyApiPaths: '%legacy.api_paths%'
$exposedUserPreferences: '%legacy.exposed_user_preferences%'
$userPreferencesKeyMap: '%legacy.user_preferences_key_map%'
$themeImagePaths: '%themes.image_paths%'
$themeImageSupportedTypes: '%themes.image_supported_types%'
$frontendExcludedModules: '%legacy.frontend_excluded_modules%'
$datetimeFormatMap: '%legacy.datetime_format_map%'
_instanceof:
App\Service\ProcessHandlerInterface:
tags: ['app.process.handler']
SuiteCRM\Core\Legacy\UserPreferences\UserPreferencesMapperInterface:
tags: ['user.preferences.mapper']
SuiteCRM\Core\Legacy\SystemConfig\SystemConfigMapperInterface:
tags: ['system.config.mapper']
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
@ -72,3 +79,15 @@ services:
App\Security\LegacySessionLogoutHandler:
decorates: 'security.logout.handler.session'
SuiteCRM\Core\Legacy\UserPreferences\UserPreferencesMappers:
# inject all services tagged with user.preferences.mapper as first argument
# and use the value of the 'getKey' method to index the services
arguments:
- !tagged { tag: 'user.preferences.mapper', default_index_method: 'getKey' }
SuiteCRM\Core\Legacy\SystemConfig\SystemConfigMappers:
# inject all services tagged with system.config.mapper as first argument
# and use the value of the 'getKey' method to index the services
arguments:
- !tagged { tag: 'system.config.mapper', default_index_method: 'getKey' }

View file

@ -0,0 +1,20 @@
parameters:
legacy.datetime_format_map:
'A' : 'a'
'a' : 'aaaaaa' # extra format to map to lower case am pm
'D' : 'E'
'd' : 'dd'
'F' : 'MMMM'
'G' : 'H'
'g' : 'h'
'H' : 'HH'
'h' : 'hh'
'i' : 'mm'
'M' : 'MMM'
'm' : 'MM'
'n' : 'M'
'P' : 'ZZZZZ'
's' : 'ss'
'w' : 'e'
'Y' : 'yyyy'
'y' : 'yy'

View file

@ -12,3 +12,5 @@ parameters:
default_decimal_seperator: true
default_number_grouping_seperator: true
default_currency_significant_digits: true
datef: true
timef: true

View file

@ -0,0 +1,4 @@
parameters:
legacy.system_config_key_map:
datef: 'date_format'
timef: 'time_format'

View file

@ -0,0 +1,4 @@
parameters:
legacy.user_preferences_key_map:
datef: 'date_format'
timef: 'time_format'

View file

@ -0,0 +1,52 @@
<?php
namespace SuiteCRM\Core\Legacy;
class DateTimeHandler extends LegacyHandler
{
public const HANDLER_KEY = 'date-time';
/**
* @var array
*/
private $datetimeFormatMap;
/**
* SystemConfigHandler constructor.
* @param string $projectDir
* @param string $legacyDir
* @param string $legacySessionName
* @param string $defaultSessionName
* @param LegacyScopeState $legacyScopeState
* @param array $datetimeFormatMap
*/
public function __construct(
string $projectDir,
string $legacyDir,
string $legacySessionName,
string $defaultSessionName,
LegacyScopeState $legacyScopeState,
array $datetimeFormatMap
) {
parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName, $legacyScopeState);
$this->datetimeFormatMap = $datetimeFormatMap;
}
/**
* @inheritDoc
*/
public function getHandlerKey(): string
{
return self::HANDLER_KEY;
}
/**
* Map Datetime format
* @param string $format
* @return string
*/
public function mapFormat(string $format): string
{
return strtr($format, $this->datetimeFormatMap);
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace SuiteCRM\Core\Legacy\SystemConfig;
use App\Entity\SystemConfig;
use SuiteCRM\Core\Legacy\DateTimeHandler;
class DateFormatConfigMapper implements SystemConfigMapperInterface
{
/**
* @var DateTimeHandler
*/
private $dateTimeHandler;
/**
* DateFormatConfigMapper constructor.
* @param DateTimeHandler $dateTimeHandler
*/
public function __construct(DateTimeHandler $dateTimeHandler)
{
$this->dateTimeHandler = $dateTimeHandler;
}
/**
* @inheritDoc
*/
public function getKey(): string
{
return 'datef';
}
/**
* @inheritDoc
*/
public function map(SystemConfig $config): void
{
if (empty($config->getValue())) {
return;
}
$format = $this->dateTimeHandler->mapFormat($config->getValue());
$config->setValue($format);
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace SuiteCRM\Core\Legacy\SystemConfig;
use App\Entity\SystemConfig;
use App\Service\ModuleNameMapperInterface;
class DefaultModuleConfigMapper implements SystemConfigMapperInterface
{
/**
* @var ModuleNameMapperInterface
*/
private $moduleNameMapper;
/**
* DefaultModuleConfigMapper constructor.
* @param ModuleNameMapperInterface $moduleNameMapper
*/
public function __construct(ModuleNameMapperInterface $moduleNameMapper)
{
$this->moduleNameMapper = $moduleNameMapper;
}
/**
* @inheritDoc
*/
public function getKey(): string
{
return 'default_module';
}
/**
* @inheritDoc
*/
public function map(SystemConfig $config): void
{
if (empty($config->getValue())) {
return;
}
$frontendName = $this->moduleNameMapper->toFrontEnd($config->getValue());
$config->setValue($frontendName);
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace SuiteCRM\Core\Legacy\SystemConfig;
use App\Entity\SystemConfig;
interface SystemConfigMapperInterface
{
/**
* Get the System Config Key
* @return string
*/
public function getKey(): string;
/**
* Map value
* @param SystemConfig $systemConfig
* @return mixed
*/
public function map(SystemConfig $systemConfig): void;
}

View file

@ -0,0 +1,62 @@
<?php
namespace SuiteCRM\Core\Legacy\SystemConfig;
use ApiPlatform\Core\Exception\ItemNotFoundException;
class SystemConfigMappers
{
protected const MSG_HANDLER_NOT_FOUND = 'SystemConfig mapper is not defined';
/**
* @var SystemConfigMapperInterface[]
*/
protected $registry = [];
/**
* SystemConfigMappers constructor.
* @param iterable $handlers
*/
public function __construct(iterable $handlers)
{
/**
* @var SystemConfigMapperInterface[]
*/
$handlers = iterator_to_array($handlers);
foreach ($handlers as $handler) {
$type = $handler->getKey();
$this->registry[$type] = $handler;
}
}
/**
* Get the mapper for the given key
* @param string $systemConfigKey
* @return SystemConfigMapperInterface
*/
public function get(string $systemConfigKey): SystemConfigMapperInterface
{
if (empty($this->registry[$systemConfigKey])) {
throw new ItemNotFoundException(self::MSG_HANDLER_NOT_FOUND);
}
return $this->registry[$systemConfigKey];
}
/**
* Has mapper for the given key
* @param string $systemConfigKey
* @return bool
*/
public function hasMapper(string $systemConfigKey): bool
{
if (empty($this->registry[$systemConfigKey])) {
return false;
}
return true;
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace SuiteCRM\Core\Legacy\SystemConfig;
use App\Entity\SystemConfig;
use SuiteCRM\Core\Legacy\DateTimeHandler;
class TimeFormatConfigMapper implements SystemConfigMapperInterface
{
/**
* @var DateTimeHandler
*/
private $dateTimeHandler;
/**
* TimeFormatConfigMapper constructor.
* @param DateTimeHandler $dateTimeHandler
*/
public function __construct(DateTimeHandler $dateTimeHandler)
{
$this->dateTimeHandler = $dateTimeHandler;
}
/**
* @inheritDoc
*/
public function getKey(): string
{
return 'timef';
}
/**
* @inheritDoc
*/
public function map(SystemConfig $config): void
{
if (empty($config->getValue())) {
return;
}
$format = $this->dateTimeHandler->mapFormat($config->getValue());
$config->setValue($format);
}
}

View file

@ -7,6 +7,7 @@ use App\Entity\SystemConfig;
use App\Service\ActionNameMapperInterface;
use App\Service\ModuleNameMapperInterface;
use App\Service\SystemConfigProviderInterface;
use SuiteCRM\Core\Legacy\SystemConfig\SystemConfigMappers;
class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderInterface
{
@ -24,9 +25,14 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
protected $injectedSystemConfigs = [];
/**
* @var ModuleNameMapperInterface
* @var SystemConfigMappers
*/
private $moduleNameMapper;
private $mappers;
/**
* @var array
*/
private $systemConfigKeyMap;
/**
* SystemConfigHandler constructor.
@ -39,6 +45,8 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
* @param ActionNameMapperInterface $actionNameMapper
* @param ModuleNameMapperInterface $moduleNameMapper
* @param ClassicViewRoutingExclusionsHandler $exclusionsManager
* @param SystemConfigMappers $mappers
* @param array $systemConfigKeyMap
*/
public function __construct(
string $projectDir,
@ -49,15 +57,18 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
array $exposedSystemConfigs,
ActionNameMapperInterface $actionNameMapper,
ModuleNameMapperInterface $moduleNameMapper,
ClassicViewRoutingExclusionsHandler $exclusionsManager
ClassicViewRoutingExclusionsHandler $exclusionsManager,
SystemConfigMappers $mappers,
array $systemConfigKeyMap
) {
parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName, $legacyScopeState);
$this->exposedSystemConfigs = $exposedSystemConfigs;
$this->moduleNameMapper = $moduleNameMapper;
$this->injectedSystemConfigs['module_name_map'] = $moduleNameMapper->getLegacyToFrontendMap();
$this->injectedSystemConfigs['action_name_map'] = $actionNameMapper->getMap();
$this->injectedSystemConfigs['classicview_routing_exclusions'] = $exclusionsManager->get();
$this->mappers = $mappers;
$this->systemConfigKeyMap = $systemConfigKeyMap;
}
/**
@ -81,6 +92,7 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
foreach ($this->exposedSystemConfigs as $configKey => $value) {
$config = $this->loadSystemConfig($configKey);
$this->mapConfigValues($config);
$this->mapKey($config);
if ($config !== null) {
$configs[] = $config;
}
@ -103,6 +115,7 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
$config = $this->loadSystemConfig($configKey);
$this->mapConfigValues($config);
$this->mapKey($config);
$this->close();
@ -210,22 +223,24 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
return;
}
if ($config->getId() === 'default_module') {
$this->mapDefaultModule($config);
if ($this->mappers->hasMapper($config->getId())) {
$mapper = $this->mappers->get($config->getId());
$mapper->map($config);
}
}
/**
* Map default module config
* @param SystemConfig $config
* Map config key
* @param SystemConfig|null $config
*/
protected function mapDefaultModule(SystemConfig $config): void
protected function mapKey(?SystemConfig $config): void
{
if (empty($config->getValue())) {
if ($config === null || empty($config->getId())) {
return;
}
$frontendName = $this->moduleNameMapper->toFrontEnd($config->getValue());
$config->setValue($frontendName);
if (isset($this->systemConfigKeyMap[$config->getId()])) {
$config->setId($this->systemConfigKeyMap[$config->getId()]);
}
}
}

View file

@ -6,6 +6,7 @@ use ApiPlatform\Core\Exception\ItemNotFoundException;
use App\Entity\UserPreference;
use App\Service\UserPreferencesProviderInterface;
use RuntimeException;
use SuiteCRM\Core\Legacy\UserPreferences\UserPreferencesMappers;
use UnexpectedValueException;
use User;
@ -19,6 +20,16 @@ class UserPreferenceHandler extends LegacyHandler implements UserPreferencesProv
*/
protected $exposedUserPreferences = [];
/**
* @var UserPreferencesMappers
*/
private $mappers;
/**
* @var array
*/
private $userPreferencesKeyMap;
/**
* UserPreferenceHandler constructor.
* @param string $projectDir
@ -27,6 +38,8 @@ class UserPreferenceHandler extends LegacyHandler implements UserPreferencesProv
* @param string $defaultSessionName
* @param LegacyScopeState $legacyScopeState
* @param array $exposedUserPreferences
* @param UserPreferencesMappers $mappers
* @param array $userPreferencesKeyMap
*/
public function __construct(
string $projectDir,
@ -34,11 +47,15 @@ class UserPreferenceHandler extends LegacyHandler implements UserPreferencesProv
string $legacySessionName,
string $defaultSessionName,
LegacyScopeState $legacyScopeState,
array $exposedUserPreferences
array $exposedUserPreferences,
UserPreferencesMappers $mappers,
array $userPreferencesKeyMap
) {
parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName, $legacyScopeState);
$this->exposedUserPreferences = $exposedUserPreferences;
$this->mappers = $mappers;
$this->userPreferencesKeyMap = $userPreferencesKeyMap;
}
/**
@ -122,7 +139,11 @@ class UserPreferenceHandler extends LegacyHandler implements UserPreferencesProv
$items = [];
foreach ($this->exposedUserPreferences[$category] as $key => $value) {
$items[$key] = $this->loadUserPreference($key, $category);
$value = $this->loadUserPreference($key, $category);
$value = $this->mapValue($key, $value);
$key = $this->mapKey($key);
$items[$key] = $value;
}
$userPreference->setItems($items);
@ -216,10 +237,40 @@ class UserPreferenceHandler extends LegacyHandler implements UserPreferencesProv
{
global $current_user;
if ($current_user === null){
if ($current_user === null) {
throw new UnexpectedValueException('Current user is not loaded');
}
return $current_user;
}
/**
* Map user preference value if mapper defined
* @param string $key
* @param $preference
* @return mixed
*/
protected function mapValue(string $key, $preference)
{
if ($this->mappers->hasMapper($key)) {
$mapper = $this->mappers->get($key);
$preference = $mapper->map($preference);
}
return $preference;
}
/**
* Map user preference key if mapper defined
* @param string $key
* @return mixed
*/
protected function mapKey(string $key)
{
if ($key === null) {
return $key;
}
return $this->userPreferencesKeyMap[$key] ?? $key;
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace SuiteCRM\Core\Legacy\UserPreferences;
use SuiteCRM\Core\Legacy\DateTimeHandler;
class DateFormatPreferenceMapper implements UserPreferencesMapperInterface
{
/**
* @var DateTimeHandler
*/
private $dateTimeHandler;
/**
* DateFormatPreferenceMapper constructor.
* @param DateTimeHandler $dateTimeHandler
*/
public function __construct(DateTimeHandler $dateTimeHandler)
{
$this->dateTimeHandler = $dateTimeHandler;
}
/**
* @inheritDoc
*/
public function getKey(): string
{
return 'datef';
}
/**
* @inheritDoc
*/
public function map($value)
{
if (empty($value)) {
return $value;
}
return $this->dateTimeHandler->mapFormat($value);
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace SuiteCRM\Core\Legacy\UserPreferences;
use SuiteCRM\Core\Legacy\DateTimeHandler;
class TimeFormatPreferenceMapper implements UserPreferencesMapperInterface
{
/**
* @var DateTimeHandler
*/
private $dateTimeHandler;
/**
* TimeFormatPreferenceMapper constructor.
* @param DateTimeHandler $dateTimeHandler
*/
public function __construct(DateTimeHandler $dateTimeHandler)
{
$this->dateTimeHandler = $dateTimeHandler;
}
/**
* @inheritDoc
*/
public function getKey(): string
{
return 'timef';
}
/**
* @inheritDoc
*/
public function map($value)
{
if (empty($value)) {
return $value;
}
return $this->dateTimeHandler->mapFormat($value);
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace SuiteCRM\Core\Legacy\UserPreferences;
interface UserPreferencesMapperInterface
{
/**
* Get the User Preference Key
* @return string
*/
public function getKey(): string;
/**
* Map value
* @param mixed $value
* @return mixed
*/
public function map($value);
}

View file

@ -0,0 +1,62 @@
<?php
namespace SuiteCRM\Core\Legacy\UserPreferences;
use ApiPlatform\Core\Exception\ItemNotFoundException;
class UserPreferencesMappers
{
protected const MSG_HANDLER_NOT_FOUND = 'UserPreference mapper is not defined';
/**
* @var UserPreferencesMapperInterface[]
*/
protected $registry = [];
/**
* UserPreferencesMappers constructor.
* @param iterable $handlers
*/
public function __construct(iterable $handlers)
{
/**
* @var UserPreferencesMapperInterface[]
*/
$handlers = iterator_to_array($handlers);
foreach ($handlers as $handler) {
$type = $handler->getKey();
$this->registry[$type] = $handler;
}
}
/**
* Get the mapper for the given key
* @param string $userPreferenceKey
* @return UserPreferencesMapperInterface
*/
public function get(string $userPreferenceKey): UserPreferencesMapperInterface
{
if (empty($this->registry[$userPreferenceKey])) {
throw new ItemNotFoundException(self::MSG_HANDLER_NOT_FOUND);
}
return $this->registry[$userPreferenceKey];
}
/**
* Has mapper for the given key
* @param string $userPreferenceKey
* @return bool
*/
public function hasMapper(string $userPreferenceKey): bool
{
if (empty($this->registry[$userPreferenceKey])) {
return false;
}
return true;
}
}

View file

@ -11,34 +11,53 @@ class Unit extends Module
/**
* @var LegacyScopeState
*/
static public $legacyScope;
public static $legacyScope;
/**
* @var string
*/
static public $projectDir;
public static $projectDir;
/**
* @var string
*/
static public $legacyDir;
public static $legacyDir;
/**
* @var string
*/
static public $legacySessionName;
public static $legacySessionName;
/**
* @var string
*/
static public $defaultSessionName;
public static $defaultSessionName;
/**
* @var array
*/
public static $datetimeFormatMap;
/**
* @param array $settings
*/
public function _beforeSuite($settings = []): void
{
self::$projectDir = codecept_root_dir();
self::$legacyDir = self::$projectDir . '/legacy';
self::$legacySessionName = 'LEGACYSESSID';
self::$defaultSessionName = 'PHPSESSID';
self::$datetimeFormatMap = [
'A' => 'a',
'a' => 'aaaaaa',
'd' => 'dd',
'H' => 'HH',
'h' => 'hh',
'i' => 'mm',
'M' => 'MMM',
'm' => 'MM',
'Y' => 'yyyy'
];
}
/**
@ -46,6 +65,6 @@ class Unit extends Module
*/
public function _before(TestInterface $test): void
{
self::$legacyScope = new legacyScopeState;
self::$legacyScope = new LegacyScopeState();
}
}

View file

@ -63,4 +63,12 @@ class UnitTester extends Actor
{
return Tester::$defaultSessionName;
}
/**
* @return array
*/
public function getDatetimeFormatMap(): array
{
return Tester::$datetimeFormatMap;
}
}

View file

@ -24,8 +24,8 @@ class AppListStringsHandlerTest extends Unit
$this->handler = new AppListStringsHandler(
$this->tester->getProjectDir(),
$this->tester->getLegacyDir(),
$this->tester->getlegacySessionName(),
$this->tester->getdefaultSessionName(),
$this->tester->getLegacySessionName(),
$this->tester->getDefaultSessionName(),
$this->tester->getLegacyScope()
);
}

View file

@ -24,8 +24,8 @@ class AppStringsHandlerTest extends Unit
$this->handler = new AppStringsHandler(
$this->tester->getProjectDir(),
$this->tester->getLegacyDir(),
$this->tester->getlegacySessionName(),
$this->tester->getdefaultSessionName(),
$this->tester->getLegacySessionName(),
$this->tester->getDefaultSessionName(),
$this->tester->getLegacyScope()
);
}

View file

@ -19,8 +19,8 @@ class ClassicViewRoutingExclusionsHandlerTest extends Unit
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getlegacySessionName();
$defaultSessionName = $this->tester->getdefaultSessionName();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$legacyScope = $this->tester->getLegacyScope();
$this->handler = new ClassicViewRoutingExclusionsHandler(

View file

@ -4,6 +4,7 @@ namespace App\Tests;
use App\Entity\FieldDefinition;
use Codeception\Test\Unit;
use Exception;
use SuiteCRM\Core\Legacy\FieldDefinitionsHandler;
final class FieldDefinitionHandlerTest extends Unit
@ -28,12 +29,15 @@ final class FieldDefinitionHandlerTest extends Unit
$this->fieldDefinitionsHandler = new FieldDefinitionsHandler(
$this->tester->getProjectDir(),
$this->tester->getLegacyDir(),
$this->tester->getlegacySessionName(),
$this->tester->getdefaultSessionName(),
$this->tester->getLegacySessionName(),
$this->tester->getDefaultSessionName(),
$this->tester->getLegacyScope()
);
}
/**
* @throws Exception
*/
public function testGetUserVardef(): void
{
$this->fieldDefinition = $this->fieldDefinitionsHandler->getVardef('Accounts');

View file

@ -25,8 +25,8 @@ class ModStringsHandlerTest extends Unit
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getlegacySessionName();
$defaultSessionName = $this->tester->getdefaultSessionName();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$legacyScope = $this->tester->getLegacyScope();
$moduleNameMapper = new ModuleNameMapperHandler(

View file

@ -19,8 +19,8 @@ class ModuleRegistryHandlerTest extends Unit
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getlegacySessionName();
$defaultSessionName = $this->tester->getdefaultSessionName();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$legacyScope = $this->tester->getLegacyScope();
$excludedModules = [

View file

@ -5,6 +5,7 @@ namespace App\Tests;
use App\Entity\Navbar;
use AspectMock\Test;
use Codeception\Test\Unit;
use Exception;
use SuiteCRM\Core\Legacy\ActionNameMapperHandler;
use SuiteCRM\Core\Legacy\ModuleNameMapperHandler;
use SuiteCRM\Core\Legacy\ModuleRegistryHandler;
@ -28,13 +29,16 @@ final class NavbarTest extends Unit
*/
private $navbarHandler;
/** @noinspection StaticClosureCanBeUsedInspection */
/**
* @throws Exception
* @noinspection StaticClosureCanBeUsedInspection
*/
protected function _before(): void
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getlegacySessionName();
$defaultSessionName = $this->tester->getdefaultSessionName();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$menuItemMap = [
'default' => [

View file

@ -4,9 +4,14 @@ namespace App\Tests;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use Codeception\Test\Unit;
use Exception;
use SuiteCRM\Core\Legacy\ActionNameMapperHandler;
use SuiteCRM\Core\Legacy\ClassicViewRoutingExclusionsHandler;
use SuiteCRM\Core\Legacy\DateTimeHandler;
use SuiteCRM\Core\Legacy\ModuleNameMapperHandler;
use SuiteCRM\Core\Legacy\SystemConfig\DateFormatConfigMapper;
use SuiteCRM\Core\Legacy\SystemConfig\SystemConfigMappers;
use SuiteCRM\Core\Legacy\SystemConfig\TimeFormatConfigMapper;
use SuiteCRM\Core\Legacy\SystemConfigHandler;
class SystemConfigHandlerTest extends Unit
@ -21,12 +26,15 @@ class SystemConfigHandlerTest extends Unit
*/
protected $handler;
/**
* @throws Exception
*/
protected function _before(): void
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getlegacySessionName();
$defaultSessionName = $this->tester->getdefaultSessionName();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$legacyScope = $this->tester->getLegacyScope();
$exposedSystemConfigs = [
@ -42,7 +50,9 @@ class SystemConfigHandlerTest extends Unit
],
'languages' => true,
'module_name_map' => true,
'action_name_map' => true
'action_name_map' => true,
'datef' => true,
'timef' => true
];
$moduleMapper = new ModuleNameMapperHandler(
@ -69,6 +79,45 @@ class SystemConfigHandlerTest extends Unit
$legacyScope
);
$dateTimeHandler = new DateTimeHandler(
$projectDir,
$legacyDir,
$legacySessionName,
$defaultSessionName,
$legacyScope,
$this->tester->getDatetimeFormatMap()
);
$mappersArray = [
'datef' => new DateFormatConfigMapper($dateTimeHandler),
'timef' => new TimeFormatConfigMapper($dateTimeHandler)
];
/** @var SystemConfigMappers $mappers */
$mappers = $this->make(
SystemConfigMappers::class,
[
'get' => static function (string $key) use ($mappersArray) {
return $mappersArray[$key] ?? null;
},
'hasMapper' => static function (string $key) use ($mappersArray) {
if (isset($mappersArray[$key])) {
return true;
}
return false;
},
]
);
$systemConfigKeyMap = [
'datef' => 'date_format',
'timef' => 'time_format'
];
global $sugar_config;
$sugar_config['datef'] = 'm/d/Y';
$sugar_config['timef'] = 'H:i';
$this->handler = new SystemConfigHandler(
$projectDir,
$legacyDir,
@ -78,7 +127,9 @@ class SystemConfigHandlerTest extends Unit
$exposedSystemConfigs,
$actionMapper,
$moduleMapper,
$classicViewExclusionHandler
$classicViewExclusionHandler,
$mappers,
$systemConfigKeyMap
);
}
@ -174,4 +225,34 @@ class SystemConfigHandlerTest extends Unit
static::assertArrayHasKey('DetailView', $actionNameMap->getItems());
}
/**
* Test date format config transformation
*/
public function testDateFormatMapping(): void
{
$format = $this->handler->getSystemConfig('datef');
static::assertNotNull($format);
static::assertEquals('date_format', $format->getId());
static::assertNotNull($format->getValue());
static::assertIsArray($format->getItems());
static::assertEmpty($format->getItems());
static::assertEquals('MM/dd/yyyy', $format->getValue());
}
/**
* Test time format config transformation
*/
public function testTimeFormatMapping(): void
{
$format = $this->handler->getSystemConfig('timef');
static::assertNotNull($format);
static::assertEquals('time_format', $format->getId());
static::assertNotNull($format->getValue());
static::assertIsArray($format->getItems());
static::assertEmpty($format->getItems());
static::assertEquals('HH:mm', $format->getValue());
}
}

View file

@ -5,7 +5,12 @@ namespace App\Tests;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use AspectMock\Test;
use Codeception\Test\Unit;
use Exception;
use SuiteCRM\Core\Legacy\DateTimeHandler;
use SuiteCRM\Core\Legacy\UserPreferenceHandler;
use SuiteCRM\Core\Legacy\UserPreferences\DateFormatPreferenceMapper;
use SuiteCRM\Core\Legacy\UserPreferences\TimeFormatPreferenceMapper;
use SuiteCRM\Core\Legacy\UserPreferences\UserPreferencesMappers;
use User;
class UserPreferencesHandlerTest extends Unit
@ -20,24 +25,31 @@ class UserPreferencesHandlerTest extends Unit
*/
protected $handler;
/** @noinspection StaticClosureCanBeUsedInspection */
/**
* @throws Exception
* @noinspection StaticClosureCanBeUsedInspection
*/
protected function _before(): void
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getlegacySessionName();
$defaultSessionName = $this->tester->getdefaultSessionName();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$legacyScope = $this->tester->getLegacyScope();
$exposedUserPreferences = [
'global' => [
'timezone' => true
'timezone' => true,
'datef' => true,
'timef' => true
]
];
$mockPreferences = [
'global' => [
'timezone' => 'UTC'
'timezone' => 'UTC',
'datef' => 'm/d/Y',
'timef' => 'H:i'
]
];
@ -64,13 +76,51 @@ class UserPreferencesHandlerTest extends Unit
}
]);
$dateTimeHandler = new DateTimeHandler(
$projectDir,
$legacyDir,
$legacySessionName,
$defaultSessionName,
$legacyScope,
$this->tester->getDatetimeFormatMap()
);
$mappersArray = [
'datef' => new DateFormatPreferenceMapper($dateTimeHandler),
'timef' => new TimeFormatPreferenceMapper($dateTimeHandler)
];
/** @var UserPreferencesMappers $mappers */
$mappers = $this->make(
UserPreferencesMappers::class,
[
'get' => static function (string $key) use ($mappersArray) {
return $mappersArray[$key] ?? null;
},
'hasMapper' => static function (string $key) use ($mappersArray) {
if (isset($mappersArray[$key])) {
return true;
}
return false;
},
]
);
$systemConfigKeyMap = [
'datef' => 'date_format',
'timef' => 'time_format'
];
$this->handler = new UserPreferenceHandler(
$projectDir,
$legacyDir,
$legacySessionName,
$defaultSessionName,
$legacyScope,
$exposedUserPreferences
$exposedUserPreferences,
$mappers,
$systemConfigKeyMap
);
}
@ -108,4 +158,41 @@ class UserPreferencesHandlerTest extends Unit
static::assertEquals('UTC', $userPreferences['timezone']);
}
/**
* Test date format preference transformation
*/
public function testDateFormatMapping(): void
{
$userPref = $this->handler->getUserPreference('global');
static::assertNotNull($userPref);
static::assertEquals('global', $userPref->getId());
$userPreferences = $userPref->getItems();
static::assertArrayHasKey('date_format', $userPreferences);
$format = $userPreferences['date_format'];
static::assertNotNull($format);
static::assertEquals('MM/dd/yyyy', $format);
}
/**
* Test time format preference transformation
*/
public function testTimeFormatMapping(): void
{
$userPref = $this->handler->getUserPreference('global');
static::assertNotNull($userPref);
static::assertEquals('global', $userPref->getId());
$userPreferences = $userPref->getItems();
static::assertArrayHasKey('time_format', $userPreferences);
$format = $userPreferences['time_format'];
static::assertNotNull($format);
static::assertEquals('HH:mm', $format);
}
}

View file

@ -46,7 +46,10 @@ class LegacySessionLogoutHandlerTest extends Unit
public $closeCalled = false;
/** @noinspection StaticClosureCanBeUsedInspection */
/**
* @throws Exception
* @noinspection StaticClosureCanBeUsedInspection
*/
protected function _before(): void
{
$self = $this;
@ -86,8 +89,8 @@ class LegacySessionLogoutHandlerTest extends Unit
$originalHandler = new Authentication(
$this->tester->getProjectDir(),
$this->tester->getLegacyDir(),
$this->tester->getlegacySessionName(),
$this->tester->getdefaultSessionName(),
$this->tester->getLegacySessionName(),
$this->tester->getDefaultSessionName(),
$this->tester->getLegacyScope()
);

View file

@ -23,8 +23,8 @@ class ActionNameMapperTest extends Unit
$this->actionMapper = new ActionNameMapperHandler(
$this->tester->getProjectDir(),
$this->tester->getLegacyDir(),
$this->tester->getlegacySessionName(),
$this->tester->getdefaultSessionName(),
$this->tester->getLegacySessionName(),
$this->tester->getDefaultSessionName(),
$this->tester->getLegacyScope()
);
}

View file

@ -31,8 +31,8 @@ class LegacyNonViewActionRedirectHandlerTest extends Unit
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getlegacySessionName();
$defaultSessionName = $this->tester->getdefaultSessionName();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$legacyScope = $this->tester->getLegacyScope();
$moduleMapper = new ModuleNameMapperHandler(

View file

@ -24,8 +24,8 @@ class ModuleNameMapperTest extends Unit
$this->moduleMapper = new ModuleNameMapperHandler(
$this->tester->getProjectDir(),
$this->tester->getLegacyDir(),
$this->tester->getlegacySessionName(),
$this->tester->getdefaultSessionName(),
$this->tester->getLegacySessionName(),
$this->tester->getDefaultSessionName(),
$this->tester->getLegacyScope()
);
}

View file

@ -27,8 +27,8 @@ class RouteConverterTest extends Unit
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getlegacySessionName();
$defaultSessionName = $this->tester->getdefaultSessionName();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$legacyScope = $this->tester->getLegacyScope();
$moduleMapper = new ModuleNameMapperHandler(