Skip to content

Commit

Permalink
[BUGFIX] Fix rendering of CEs
Browse files Browse the repository at this point in the history
Resolves: #655
  • Loading branch information
twoldanski committed Dec 15, 2023
1 parent 23f3057 commit 64bb9af
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
28 changes: 11 additions & 17 deletions Classes/DataProcessing/DatabaseQueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,8 @@
*/
class DatabaseQueryProcessor implements DataProcessorInterface
{
/**
* @var ContentDataProcessor
*/
protected $contentDataProcessor;

/**
* @var TypoScriptService
*/
private $typoScriptService;
private ContentDataProcessor $contentDataProcessor;
private TypoScriptService $typoScriptService;

public function __construct(ContentDataProcessor $contentDataProcessor = null, TypoScriptService $typoScriptService = null)
{
Expand Down Expand Up @@ -100,7 +93,7 @@ public function process(ContentObjectRenderer $cObj, array $contentObjectConfigu
$targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'records');

$records = $cObj->getRecords($tableName, $processorConfiguration);
$processedRecordVariables = $this->processRecordVariables($records, $tableName, $processorConfiguration);
$processedRecordVariables = $this->processRecordVariables($cObj, $records, $tableName, $processorConfiguration);

$processedData[$targetVariableName] = $processedRecordVariables;

Expand All @@ -114,20 +107,24 @@ public function process(ContentObjectRenderer $cObj, array $contentObjectConfigu
*
* @return array
*/
private function processRecordVariables(array $records, string $tableName, array $processorConfiguration): array
private function processRecordVariables(ContentObjectRenderer $cObj, array $records, string $tableName, array $processorConfiguration): array
{
$processedRecordVariables = [];
foreach ($records as $key => $record) {
$recordContentObjectRenderer = $this->createContentObjectRenderer();
$recordContentObjectRenderer->start($record, $tableName);
$recordContentObjectRenderer->setRequest($cObj->getRequest());

$objConf = [];
$objName = '< ' . $tableName;

if (isset($processorConfiguration['fields.'])) {
$objName = 'JSON';
$fields = $this->typoScriptService->convertTypoScriptArrayToPlainArray($processorConfiguration['fields.']);
$jsonCE = $this->typoScriptService->convertPlainArrayToTypoScriptArray(['fields' => $fields, '_typoScriptNodeValue' => 'JSON']);
$record = \json_decode($recordContentObjectRenderer->cObjGetSingle('JSON', $jsonCE), true);
$objConf = $this->typoScriptService->convertPlainArrayToTypoScriptArray(['fields' => $fields, '_typoScriptNodeValue' => 'JSON']);
}

$processedRecordVariables[$key] = $record;
$processedRecordVariables[$key] = \json_decode($recordContentObjectRenderer->cObjGetSingle($objName, $objConf), true);
$processedRecordVariables[$key] = $this->contentDataProcessor->process($recordContentObjectRenderer, $processorConfiguration, $processedRecordVariables[$key]);

if (isset($processorConfiguration['overrideFields.'])) {
Expand All @@ -148,9 +145,6 @@ private function processRecordVariables(array $records, string $tableName, array
return $processedRecordVariables;
}

/**
* @return ContentObjectRenderer
*/
protected function createContentObjectRenderer(): ContentObjectRenderer
{
return GeneralUtility::makeInstance(ContentObjectRenderer::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,17 @@ public function testMenuContentElement()
self::assertIsArray($contentElement['content']['menu'][1]);

$firstCategorizedContentElement = $contentElement['content']['menu'][0];
self::assertEquals('17', $firstCategorizedContentElement['uid']);
self::assertEquals('1', $firstCategorizedContentElement['pid']);
self::assertEquals('1', $firstCategorizedContentElement['sorting']);
self::assertEquals('header', $firstCategorizedContentElement['CType']);
self::assertEquals('default', $firstCategorizedContentElement['frame_class']);
self::assertEquals('17', $firstCategorizedContentElement['id']);
self::assertEquals('header', $firstCategorizedContentElement['type']);
self::assertEquals('default', $firstCategorizedContentElement['appearance']['frameClass']);
self::assertEquals('1', $firstCategorizedContentElement['colPos']);
self::assertEquals('3', $firstCategorizedContentElement['categories']);
self::assertEquals('SysCategory3Title', $firstCategorizedContentElement['categories']);

$secondCategorizedContentElement = $contentElement['content']['menu'][1];
self::assertEquals('18', $secondCategorizedContentElement['uid']);
self::assertEquals('1', $secondCategorizedContentElement['pid']);
self::assertEquals('1', $secondCategorizedContentElement['sorting']);
self::assertEquals('textpic', $secondCategorizedContentElement['CType']);
self::assertEquals('default', $secondCategorizedContentElement['frame_class']);
self::assertEquals('18', $secondCategorizedContentElement['id']);
self::assertEquals('textpic', $secondCategorizedContentElement['type']);
self::assertEquals('default', $secondCategorizedContentElement['appearance']['frameClass']);
self::assertEquals('1', $secondCategorizedContentElement['colPos']);
self::assertEquals('3', $secondCategorizedContentElement['categories']);
self::assertEquals('SysCategory3Title', $secondCategorizedContentElement['categories']);
}
}
11 changes: 11 additions & 0 deletions Tests/Unit/DataProcessing/DatabaseQueryProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

use function json_encode;

class DatabaseQueryProcessorTest extends UnitTestCase
{
use ProphecyTrait;
Expand Down Expand Up @@ -50,6 +53,7 @@ protected function setUp(): void
$this->typoScriptService = $this->prophesize(TypoScriptService::class);
$this->contentDataProcessor = $this->prophesize(ContentDataProcessor::class);
$this->contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
$this->contentObjectRenderer->getRequest()->willReturn(new ServerRequest());
$this->subject = $this->getAccessibleMock(DatabaseQueryProcessor::class, ['createContentObjectRenderer'], [
$this->contentDataProcessor->reveal(),
$this->typoScriptService->reveal(),
Expand Down Expand Up @@ -114,6 +118,10 @@ public function processWithoutAdditionalFields(): void
$this->contentObjectRenderer->stdWrapValue('as', $processorConfigurationWithoutTable, 'records')->shouldBeCalledOnce()->willReturn('records');

$contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
$contentObjectRenderer->getRequest()->willReturn(new ServerRequest());
$contentObjectRenderer->setRequest(Argument::type(ServerRequest::class))->shouldBeCalledOnce();
$contentObjectRenderer->cObjGetSingle(Argument::any(), [])->willReturn(json_encode([ 'uid' => 1]));
$contentObjectRenderer->start($records[0], 'tt_content')->shouldBeCalledOnce();
$this->subject->expects(self::any())->method('createContentObjectRenderer')->willReturn($contentObjectRenderer->reveal());

$this->contentDataProcessor->process($contentObjectRenderer, $processorConfigurationWithoutTable, $records[0])->shouldBeCalledOnce()->willReturn($records[0]);
Expand Down Expand Up @@ -158,6 +166,8 @@ public function processWithAdditionalFields(): void
$this->contentObjectRenderer->stdWrapValue('as', $processorConfigurationWithoutTable, 'records')->shouldBeCalledOnce()->willReturn('records');

$contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
$contentObjectRenderer->getRequest()->willReturn(new ServerRequest());
$contentObjectRenderer->setRequest(Argument::type(ServerRequest::class))->shouldBeCalledOnce();
$this->subject->expects(self::any())->method('createContentObjectRenderer')->willReturn($contentObjectRenderer->reveal());

$expectedRecords = [
Expand All @@ -173,6 +183,7 @@ public function processWithAdditionalFields(): void
$this->typoScriptService->convertPlainArrayToTypoScriptArray(['fields' => $fields, '_typoScriptNodeValue' => 'JSON'])->shouldBeCalledOnce()->willReturn($jsonCE);

$contentObjectRenderer->start($records[0], $processorConfiguration['table'])->shouldBeCalledOnce();
$contentObjectRenderer->setRequest(Argument::type(ServerRequest::class))->shouldBeCalledOnce();
$contentObjectRenderer->cObjGetSingle('JSON', $jsonCE)->willReturn('{"title":"title"}');

$processedData['records'] = $expectedRecords;
Expand Down

0 comments on commit 64bb9af

Please sign in to comment.