Add ModStrings API

- Add ModStrings entity
- Add ModStringsItemDataProvider with sample data
- Add ModStrings Legacy handler
- Add unit tests for legacy handler
This commit is contained in:
Clemente Raposo 2020-03-20 11:40:59 +00:00 committed by Dillon-Brown
parent bc24427738
commit 968580e0fe
4 changed files with 284 additions and 0 deletions

View file

@ -0,0 +1,80 @@
<?php
namespace SuiteCRM\Core\Legacy;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use App\Entity\ModStrings;
use App\Service\ModuleNameMapper;
class ModStringsHandler extends LegacyHandler
{
protected const MSG_LANGUAGE_NOT_FOUND = 'Not able to get language: ';
/**
* @var ModuleNameMapper
*/
private $moduleNameMapper;
/**
* SystemConfigHandler constructor.
* @param string $projectDir
* @param string $legacyDir
* @param string $legacySessionName
* @param string $defaultSessionName
* @param ModuleNameMapper $moduleNameMapper
*/
public function __construct(
string $projectDir,
string $legacyDir,
string $legacySessionName,
string $defaultSessionName,
ModuleNameMapper $moduleNameMapper
) {
parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName);
$this->moduleNameMapper = $moduleNameMapper;
}
/**
* Get mod strings for given $language
* @param $language
* @return ModStrings|null
*/
public function getModStrings(string $language): ?ModStrings
{
$this->init();
if (empty($language)) {
return null;
}
$enabledLanguages = get_languages();
if (empty($enabledLanguages) || !array_key_exists($language, $enabledLanguages)) {
throw new ItemNotFoundException(self::MSG_LANGUAGE_NOT_FOUND . "'$language'");
}
global $moduleList;
$allModStringsArray = [];
foreach ($moduleList as $module) {
$frontendName = $this->moduleNameMapper->toFrontEnd($module);
$allModStringsArray[$frontendName] = return_module_language($language, $module);
}
if (empty($allModStringsArray)) {
throw new ItemNotFoundException(self::MSG_LANGUAGE_NOT_FOUND . "'$language'");
}
$modStrings = new ModStrings();
$modStrings->setId($language);
$modStrings->setItems($allModStringsArray);
$this->close();
return $modStrings;
}
}

View file

@ -0,0 +1,59 @@
<?php
namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\ModStrings;
use SuiteCRM\Core\Legacy\ModStringsHandler;
/**
* Class ModStringsItemDataProvider
*/
final class ModStringsItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
/**
* @var ModStringsHandler
*/
private $modStringsHandler;
/**
* ModStringsItemDataProvider constructor.
* @param ModStringsHandler $modStringsHandler
*/
public function __construct(ModStringsHandler $modStringsHandler)
{
$this->modStringsHandler = $modStringsHandler;
}
/**
* Defined supported resources
* @param string $resourceClass
* @param string|null $operationName
* @param array $context
* @return bool
*/
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return ModStrings::class === $resourceClass;
}
/**
* Get mod strings for given language id
* @param string $resourceClass
* @param array|int|string $id
* @param string|null $operationName
* @param array $context
* @return ModStrings|null
*/
public function getItem(
string $resourceClass,
$id,
string $operationName = null,
array $context = []
): ?ModStrings {
return $this->modStringsHandler->getModStrings($id);
}
}

View file

@ -0,0 +1,76 @@
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiProperty;
/**
* @ApiResource(
* itemOperations={
* "get"
* },
* collectionOperations={
* },
* graphql={
* "item_query",
* },
* )
*/
class ModStrings
{
/**
* @ApiProperty(identifier=true)
* @var string|null
*/
protected $id;
/**
* @ApiProperty
* @var array|null
*/
protected $items;
/**
* Get Id
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}
/**
* Set Id
* @param string|null $id
* @return ModStrings
*/
public function setId(?string $id): ModStrings
{
$this->id = $id;
return $this;
}
/**
* Get items
* @return array|null
*/
public function getItems(): ?array
{
return $this->items;
}
/**
* Set Items
* @param array|null $items
* @return ModStrings
*/
public function setItems(?array $items): ModStrings
{
$this->items = $items;
return $this;
}
}

View file

@ -0,0 +1,69 @@
<?php namespace App\Tests;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use App\Entity\ModStrings;
use Codeception\Test\Unit;
use SuiteCRM\Core\Legacy\ModStringsHandler;
class ModStringsHandlerTest extends Unit
{
/**
* @var UnitTester
*/
protected $tester;
/**
* @var ModStringsHandler
*/
protected $handler;
protected function _before()
{
$projectDir = codecept_root_dir();
$legacyDir = $projectDir . '/legacy';
$legacySessionName = 'LEGACYSESSID';
$defaultSessionName = 'PHPSESSID';
$this->handler = new ModStringsHandler($projectDir, $legacyDir, $legacySessionName, $defaultSessionName);
}
// tests
/**
* Test Invalid language handling in ModStringsHandler
*/
public function testInvalidLanguageCheck()
{
$this->expectException(ItemNotFoundException::class);
$this->handler->getModStrings('invalid_lang');
}
/**
* Test default language retrieval in ModStringsHandler
*/
public function testDefaultLanguageKey()
{
$modStrings = $this->handler->getModStrings('en_us');
static::assertNotNull($modStrings);
static::assertEquals('en_us', $modStrings->getId());
static::assertIsArray($modStrings->getItems());
$this->assertLanguageKey('Home', 'LBL_MODULE_NAME', $modStrings);
$this->assertLanguageKey('Accounts', 'LNK_ACCOUNT_LIST', $modStrings);
$this->assertLanguageKey('Accounts', 'LNK_NEW_ACCOUNT', $modStrings);
}
/**
* Asserts that the given label $key exists in Mod
* @param $module
* @param string $key
* @param ModStrings $modStrings
*/
protected function assertLanguageKey($module, $key, ModStrings $modStrings)
{
static::assertArrayHasKey($module, $modStrings->getItems());
static::assertNotEmpty($modStrings->getItems()[$module]);
static::assertIsArray($modStrings->getItems()[$module]);
static::assertArrayHasKey($key, $modStrings->getItems()[$module]);
static::assertNotEmpty($modStrings->getItems()[$module][$key]);
}
}