Skip to content

Commit

Permalink
[TASK] Cover FormEngine integration scope with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Nov 5, 2022
1 parent 34654c0 commit 039af27
Show file tree
Hide file tree
Showing 24 changed files with 781 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public function addData(array $result)
{
if ($result['tableName'] === 'flux_field') {
$fieldValue = &$result['processedTca']['columns']['field_value'];
$fieldValue['label'] = $result['databaseRow']['field_label'];
if (!empty($result['databaseRow']['field_options'])) {
$fieldName['config'] = json_decode(
$fieldValue['config'] = json_decode(
$result['databaseRow']['field_options'],
true
) ?? ['type' => 'passthrough'];
}
$fieldValue['label'] = $result['databaseRow']['field_label'];
}
return $result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function addData(array $result): array
* @param string $table
* @param array $record
* @return ImplementationInterface[]
* @codeCoverageIgnore
*/
protected function resolveImplementationsForTableField(string $table, string $field, array $record): iterable
{
Expand Down
10 changes: 8 additions & 2 deletions Classes/Integration/FormEngine/ProviderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public function addData(array $result)
/** @var ContentTypeManager $contentTypeManager */
$contentTypeManager = GeneralUtility::makeInstance(ContentTypeManager::class);
$fluidContentTypeNames = (array) $contentTypeManager->fetchContentTypeNames();
foreach ($result['processedTca']['columns']['CType']['config']['items'] as $index => $optionArray) {
$currentItems = $result['processedTca']['columns']['CType']['config']['items'];
foreach ($currentItems as $index => $optionArray) {
$contentTypeName = $optionArray[1];
if (in_array($contentTypeName, $fluidContentTypeNames, true)
&& !in_array($contentTypeName, $enabledContentTypes)
&& !in_array($contentTypeName, $enabledContentTypes, true)
) {
unset($result['processedTca']['columns']['CType']['config']['items'][$index]);
}
Expand All @@ -73,6 +74,9 @@ public function addData(array $result)
return $result;
}

/**
* @codeCoverageIgnore
*/
protected function loadRecord(string $table, int $uid): array
{
/** @var ConnectionPool $connectionPool */
Expand All @@ -90,6 +94,7 @@ protected function loadRecord(string $table, int $uid): array

/**
* @return ProviderResolver
* @codeCoverageIgnore
*/
protected function getProviderResolver()
{
Expand All @@ -100,6 +105,7 @@ protected function getProviderResolver()

/**
* @return ObjectManagerInterface
* @codeCoverageIgnore
*/
protected function getObjectManager()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ public function processPageTemplateItems(array $tca, TcaSelectItems $bar): array
}
$label = (string) $form->getLabel();
$identity = $extensionName . '->' . lcfirst(pathinfo($templateFilename, PATHINFO_FILENAME));
try {
$label = LocalizationUtility::translate($label) ?: $identity;
} catch (\InvalidArgumentException $error) {
$label = $identity;
}
$label = $this->translate((string) $label) ?? $identity;
$tca['items'][] = [$label, $identity];
}
}
return $tca;
}

/**
* @codeCoverageIgnore
*/
protected function translate(string $label): ?string
{
return LocalizationUtility::translate($label);
}
}
56 changes: 31 additions & 25 deletions Classes/Integration/FormEngine/UserFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function renderClearValueWizardField(&$parameters, &$pObj)
*/
public function fluxFormFieldDisplayCondition(array $parameters, &$pObj)
{
list ($table, $field) = $parameters['conditionParameters'];
[$table, $field] = $parameters['conditionParameters'];
/** @var ObjectManagerInterface $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var ProviderResolver $providerResolver */
Expand Down Expand Up @@ -105,34 +105,37 @@ public function renderColumnPositionField(array &$parameters, &$pObj = null)
$inputValue,
$inputValue
);
} else {
// The field does not yet have a value, which means this is used for a new panel
// and we have to fill the fields that will be used by the JavaScript module to
// determine the value
$rowUid = $parameters['row']['uid'];
// Unsaved records may begin with "NEW", make sure we don't have one of those
// as we cannot look up anything in the database in that case
if (!isset($rowUid) || !is_int($rowUid)) {
$rowUid = 0;
}

$minimumColumnPosition = 0;
$maximumColumnPosition = ColumnNumberUtility::MULTIPLIER - 1;
$takenColumnPositions = $this->determineTakenColumnPositionsWithinParent('tt_content', $rowUid);
}

return sprintf(
'<input type="hidden" name="%s" id="%s" class="flux-flex-colPos-input" data-min-value="%d" '
. 'data-max-value="%d" data-taken-values="%s" />Column position: '
. '<strong class="flux-flex-colPos-text"></strong>',
$parameters['itemFormElName'],
$id,
$minimumColumnPosition,
$maximumColumnPosition,
implode(',', $takenColumnPositions)
);
// The field does not yet have a value, which means this is used for a new panel
// and we have to fill the fields that will be used by the JavaScript module to
// determine the value
$rowUid = $parameters['row']['uid'];
// Unsaved records may begin with "NEW", make sure we don't have one of those
// as we cannot look up anything in the database in that case
if (!isset($rowUid) || !is_int($rowUid)) {
$rowUid = 0;
}

$minimumColumnPosition = 0;
$maximumColumnPosition = ColumnNumberUtility::MULTIPLIER - 1;
$takenColumnPositions = $this->determineTakenColumnPositionsWithinParent('tt_content', $rowUid);

return sprintf(
'<input type="hidden" name="%s" id="%s" class="flux-flex-colPos-input" data-min-value="%d" '
. 'data-max-value="%d" data-taken-values="%s" />Column position: '
. '<strong class="flux-flex-colPos-text"></strong>',
$parameters['itemFormElName'],
$id,
$minimumColumnPosition,
$maximumColumnPosition,
implode(',', $takenColumnPositions)
);
}

/**
* @codeCoverageIgnore
*/
protected function determineTakenColumnPositionsWithinParent(string $table, int $parentUid) : array
{
if ($parentUid === 0) {
Expand All @@ -155,6 +158,9 @@ protected function determineTakenColumnPositionsWithinParent(string $table, int
}, array_unique(array_column($rows, 'colPos')));
}

/**
* @codeCoverageIgnore
*/
protected function translate(string $key, string $extensionName): string
{
return LocalizationUtility::translate($key, $extensionName) ?? $key;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Integration\Event;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Integration\Event\AfterLocalizationControllerColumnsEventListener;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use TYPO3\CMS\Backend\Controller\Event\AfterPageColumnsSelectedForLocalizationEvent;
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayout;

class AfterLocalizationControllerColumnsEventListenerTest extends AbstractTestCase
{
protected function setUp(): void
{
parent::setUp();

if (!class_exists(AfterPageColumnsSelectedForLocalizationEvent::class)) {
self::markTestSkipped('Event implementation not available on current TYPO3 version');
}
}

/**
* @dataProvider getTestModifyColumnsManifestTestValues
*/
public function testModifyColumnsManifest(array $expectedColumns, array $expectedColumnList, array $records): void
{
$columns = [];
$columnList = [];
$backendLayout = $this->getMockBuilder(BackendLayout::class)->disableOriginalConstructor()->getMock();
$subject = new AfterLocalizationControllerColumnsEventListener();
$event = new AfterPageColumnsSelectedForLocalizationEvent(
$columns,
$columnList,
$backendLayout,
$records,
[]
);
$subject->modifyColumnsManifest($event);
self::assertSame($expectedColumns, $event->getColumns(), 'Columns do not match');
self::assertSame($expectedColumnList, $event->getColumnList(), 'Column list does not match');
}

public function getTestModifyColumnsManifestTestValues(): array
{
return [
'without records' => [[], [], []],
'records only in page columns' => [[], [], [['uid' => 123, 'colPos' => 1]]],
'records in nested content' => [[12301 => 'Nested'], ['12301'], [['uid' => 123, 'colPos' => 12301]]],
];
}
}
41 changes: 41 additions & 0 deletions Tests/Unit/Integration/FormEngine/ColumnPositionNodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Integration\FormEngine;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Integration\FormEngine\ColumnPositionNode;
use FluidTYPO3\Flux\Integration\FormEngine\UserFunctions;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ColumnPositionNodeTest extends AbstractTestCase
{
public function testRender(): void
{
$nodeFactory = $this->getMockBuilder(NodeFactory::class)->disableOriginalConstructor()->getMock();
$data = [
'parameterArray' => [],
];
$subject = $this->getMockBuilder(ColumnPositionNode::class)
->setMethods(['initializeResultArray'])
->setConstructorArgs([$nodeFactory, $data])
->getMock();
$subject->method('initializeResultArray')->willReturn([]);

$userFunction = $this->getMockBuilder(UserFunctions::class)
->setMethods(['renderColumnPositionField'])
->disableOriginalConstructor()
->getMock();
$userFunction->method('renderColumnPositionField')->willReturn('html');
GeneralUtility::addInstance(UserFunctions::class, $userFunction);

$output = $subject->render();
self::assertSame(['html' => 'html'], $output);
}
}
45 changes: 45 additions & 0 deletions Tests/Unit/Integration/FormEngine/ContentTypeValidatorNodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Integration\FormEngine;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Content\ContentTypeValidator;
use FluidTYPO3\Flux\Integration\FormEngine\ContentTypeValidatorNode;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ContentTypeValidatorNodeTest extends AbstractTestCase
{
public function testRender(): void
{
$nodeFactory = $this->getMockBuilder(NodeFactory::class)->disableOriginalConstructor()->getMock();
$data = [
'parameterArray' => ['foo' => 'bar'],
'databaseRow' => ['uid' => 123],
];
$subject = $this->getMockBuilder(ContentTypeValidatorNode::class)
->setMethods(['initializeResultArray'])
->setConstructorArgs([$nodeFactory, $data])
->getMock();
$subject->method('initializeResultArray')->willReturn([]);

$userFunction = $this->getMockBuilder(ContentTypeValidator::class)
->setMethods(['validateContentTypeRecord'])
->disableOriginalConstructor()
->getMock();
$userFunction->expects(self::once())
->method('validateContentTypeRecord')
->with($data['parameterArray'] + ['row' => $data['databaseRow']])
->willReturn('html');
GeneralUtility::addInstance(ContentTypeValidator::class, $userFunction);

$output = $subject->render();
self::assertSame(['html' => 'html'], $output);
}
}
42 changes: 42 additions & 0 deletions Tests/Unit/Integration/FormEngine/HtmlOutputNodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Integration\FormEngine;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Integration\FormEngine\HtmlOutputNode;
use FluidTYPO3\Flux\Integration\FormEngine\UserFunctions;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class HtmlOutputNodeTest extends AbstractTestCase
{
public function testRender(): void
{
$nodeFactory = $this->getMockBuilder(NodeFactory::class)->disableOriginalConstructor()->getMock();
$data = [
'parameterArray' => ['foo' => 'bar'],
'databaseRow' => ['uid' => 123],
];
$subject = $this->getMockBuilder(HtmlOutputNode::class)
->setMethods(['initializeResultArray'])
->setConstructorArgs([$nodeFactory, $data])
->getMock();
$subject->method('initializeResultArray')->willReturn([]);

$userFunction = $this->getMockBuilder(UserFunctions::class)
->setMethods(['renderHtmlOutputField'])
->disableOriginalConstructor()
->getMock();
$userFunction->method('renderHtmlOutputField')->willReturn('html');
GeneralUtility::addInstance(UserFunctions::class, $userFunction);

$output = $subject->render();
self::assertSame(['html' => 'html'], $output);
}
}
Loading

0 comments on commit 039af27

Please sign in to comment.