Skip to content

Commit

Permalink
[TASK] Update SelectOptions service
Browse files Browse the repository at this point in the history
Use Extbase configuration manager to get the TypoScript and
update TCA
  • Loading branch information
garfieldius committed Jul 2, 2024
1 parent 56a9fb7 commit d20559a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 72 deletions.
32 changes: 9 additions & 23 deletions Classes/TCA/SelectOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

namespace Supseven\Supi\TCA;

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\TypoScript\TemplateService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\RootlineUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;

class SelectOptions
{
Expand All @@ -19,7 +16,8 @@ class SelectOptions
private readonly LanguageService $languageService;

public function __construct(
private readonly LanguageServiceFactory $languageServiceFactory
private readonly LanguageServiceFactory $languageServiceFactory,
private readonly ConfigurationManagerInterface $configurationManager,
) {
$this->languageService = $this->languageServiceFactory->createFromUserPreferences($GLOBALS['BE_USER']);
}
Expand All @@ -30,23 +28,8 @@ public function addServices(array &$configuration): void
$configuration['items'] = [];
}

$pid = (int)$configuration['row']['pid'];

if ($pid < 0 && strncasecmp((string)$configuration['row']['uid'], 'NEW', 3) === 0) {
$pid = -$pid;
$record = BackendUtility::getRecord('tt_content', $pid);

if (!empty($record['pid'])) {
$pid = (int)$record['pid'];
}
}

$rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pid)->get();
$templates = GeneralUtility::makeInstance(TemplateService::class);
$templates->runThroughTemplates($rootLine);
$templates->generateConfig();

$services = $templates->setup['plugin.']['tx_supi.']['settings.']['services.'] ?? [];
$setup = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
$services = $setup['plugin.']['tx_supi.']['settings.']['services.'] ?? [];

foreach ($services as $id => $service) {
if ((int)($service['internal'] ?? 0) !== 1) {
Expand All @@ -56,7 +39,10 @@ public function addServices(array &$configuration): void
$label = $this->languageService->sL($label);
}

$configuration['items'][] = [$label, trim($id, '.')];
$configuration['items'][] = [
'label' => $label,
'value' => trim($id, '.'),
];
}
}
}
Expand Down
68 changes: 19 additions & 49 deletions Tests/TCA/SelectOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
use PHPUnit\Framework\TestCase;
use Supseven\Supi\TCA\SelectOptions;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\TypoScript\TemplateService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\RootlineUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;

/**
* Test the select options helper
Expand All @@ -25,14 +20,7 @@ class SelectOptionsTest extends TestCase
public function testGetFromExistingRecords(): void
{
$pid = 2;
$rootLine = [['uid' => $pid], ['uid' => 1]];
$rootlineUtil = $this->createMock(RootlineUtility::class);
$rootlineUtil->expects(static::once())->method('get')->willReturn($rootLine);

$templatesUtil = $this->createMock(TemplateService::class);
$templatesUtil->expects(static::once())->method('runThroughTemplates')->with(static::equalTo($rootLine));
$templatesUtil->expects(static::once())->method('generateConfig');
$templatesUtil->setup = [
$setup = [
'plugin.' => [
'tx_supi.' => [
'settings.' => [
Expand All @@ -52,6 +40,11 @@ public function testGetFromExistingRecords(): void
],
],
];
$configMgr = $this->createMock(ConfigurationManager::class);
$configMgr->expects($this->once())
->method('getConfiguration')
->with(self::equalTo(ConfigurationManager::CONFIGURATION_TYPE_FULL_TYPOSCRIPT))
->willReturn($setup);

$languageService = $this->createMock(\TYPO3\CMS\Core\Localization\LanguageService::class);
$languageService->expects(static::any())->method('sL')->with(static::equalTo('LLL:sec'))->willReturn('Second');
Expand All @@ -64,18 +57,15 @@ public function testGetFromExistingRecords(): void
->with(self::equalTo($GLOBALS['BE_USER']))
->willReturn($languageService);

GeneralUtility::addInstance(RootlineUtility::class, $rootlineUtil);
GeneralUtility::addInstance(TemplateService::class, $templatesUtil);

$subject = new SelectOptions($languageServiceFactory);
$subject = new SelectOptions($languageServiceFactory, $configMgr);
$expected = [
'row' => [
'uid' => 3,
'pid' => $pid,
],
'items' => [
['First', 'first'],
['Second', 'second'],
['label' => 'First', 'value' => 'first'],
['label' => 'Second', 'value' => 'second'],
],
];
$actual = [
Expand All @@ -92,14 +82,7 @@ public function testGetFromExistingRecords(): void
public function testGetFromNewRecords(): void
{
$pid = 2;
$rootLine = [['uid' => $pid], ['uid' => 1]];
$rootlineUtil = $this->createMock(RootlineUtility::class);
$rootlineUtil->expects(static::once())->method('get')->willReturn($rootLine);

$templatesUtil = $this->createMock(TemplateService::class);
$templatesUtil->expects(static::once())->method('runThroughTemplates')->with(static::equalTo($rootLine));
$templatesUtil->expects(static::once())->method('generateConfig');
$templatesUtil->setup = [
$setup = [
'plugin.' => [
'tx_supi.' => [
'settings.' => [
Expand All @@ -119,6 +102,11 @@ public function testGetFromNewRecords(): void
],
],
];
$configMgr = $this->createMock(ConfigurationManager::class);
$configMgr->expects($this->once())
->method('getConfiguration')
->with(self::equalTo(ConfigurationManager::CONFIGURATION_TYPE_FULL_TYPOSCRIPT))
->willReturn($setup);

$languageService = $this->createMock(\TYPO3\CMS\Core\Localization\LanguageService::class);
$languageService->expects(static::any())->method('sL')->with(static::equalTo('LLL:sec'))->willReturn('Second');
Expand All @@ -131,35 +119,17 @@ public function testGetFromNewRecords(): void
->with(self::equalTo($GLOBALS['BE_USER']))
->willReturn($languageService);

GeneralUtility::addInstance(RootlineUtility::class, $rootlineUtil);
GeneralUtility::addInstance(TemplateService::class, $templatesUtil);

$GLOBALS['TCA']['tt_content']['ctrl']['delete'] = false;

$restrictions = new DefaultRestrictionContainer();

$res = $this->createMock(\Doctrine\DBAL\Result::class);
$res->expects(static::any())->method('fetchAssociative')->willReturn(['pid' => $pid]);

$qb = $this->createMock(QueryBuilder::class);
$qb->expects(static::any())->method('getRestrictions')->willReturn($restrictions);
$qb->expects(static::any())->method('from')->willReturn($qb);
$qb->expects(static::any())->method('where')->willReturn($qb);
$qb->expects(static::any())->method('andWhere')->willReturn($qb);
$qb->expects(static::any())->method('execute')->willReturn($res);
$pool = $this->createMock(ConnectionPool::class);
$pool->expects(static::any())->method('getQueryBuilderForTable')->willReturn($qb);
GeneralUtility::addInstance(ConnectionPool::class, $pool);

$subject = new SelectOptions($languageServiceFactory);
$subject = new SelectOptions($languageServiceFactory, $configMgr);
$expected = [
'row' => [
'uid' => 'NEW123',
'pid' => -$pid,
],
'items' => [
['First', 'first'],
['Second', 'second'],
['label' => 'First', 'value' => 'first'],
['label' => 'Second', 'value' => 'second'],
],
];
$actual = [
Expand Down

0 comments on commit d20559a

Please sign in to comment.