diff --git a/Classes/DataProcessing/DatabaseQueryProcessor.php b/Classes/DataProcessing/DatabaseQueryProcessor.php index fead8a87..aac95c33 100644 --- a/Classes/DataProcessing/DatabaseQueryProcessor.php +++ b/Classes/DataProcessing/DatabaseQueryProcessor.php @@ -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) { @@ -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; @@ -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.'])) { @@ -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); diff --git a/Tests/Functional/ContentTypes/MenuCategorizedContentElementTest.php b/Tests/Functional/ContentTypes/MenuCategorizedContentElementTest.php index de27593e..ba3a9096 100644 --- a/Tests/Functional/ContentTypes/MenuCategorizedContentElementTest.php +++ b/Tests/Functional/ContentTypes/MenuCategorizedContentElementTest.php @@ -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']); } } diff --git a/Tests/Unit/DataProcessing/DatabaseQueryProcessorTest.php b/Tests/Unit/DataProcessing/DatabaseQueryProcessorTest.php index 053b8dfe..ef93d28e 100644 --- a/Tests/Unit/DataProcessing/DatabaseQueryProcessorTest.php +++ b/Tests/Unit/DataProcessing/DatabaseQueryProcessorTest.php @@ -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; @@ -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(), @@ -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]); @@ -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 = [ @@ -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;