diff --git a/Classes/DataProcessing/ResponsiveImagesProcessor.php b/Classes/DataProcessing/ResponsiveImagesProcessor.php index bd1608c..5f80fb3 100644 --- a/Classes/DataProcessing/ResponsiveImagesProcessor.php +++ b/Classes/DataProcessing/ResponsiveImagesProcessor.php @@ -26,7 +26,9 @@ use Codappix\ResponsiveImages\Domain\Factory\BreakpointFactory; use Codappix\ResponsiveImages\Domain\Factory\RootlineFactory; use RuntimeException; +use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData; use TYPO3\CMS\Core\Resource\FileInterface; +use TYPO3\CMS\Core\Resource\FileRepository; use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; @@ -42,7 +44,10 @@ final class ResponsiveImagesProcessor implements DataProcessorInterface private array $contentElementSizes = []; + private array $processedData = []; + public function __construct( + private readonly FileRepository $fileRepository, private readonly BreakpointFactory $breakpointFactory, private readonly RootlineFactory $rootlineFactory ) { @@ -54,10 +59,14 @@ public function process( array $processorConfiguration, array $processedData ): array { + $this->calculatedFiles = []; + if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) { return $processedData; } + $this->processedData = $processedData; + $filesDataKey = (string) $cObj->stdWrapValue( 'filesDataKey', $processorConfiguration, @@ -68,10 +77,17 @@ public function process( $processorConfiguration, 'image' ); - if (isset($processedData[$filesDataKey]) && is_array($processedData[$filesDataKey])) { - $this->files = $processedData[$filesDataKey]; - } else { - // Files key is empty or not configured. + $targetFieldName = (string) $cObj->stdWrapValue( + 'as', + $processorConfiguration, + 'responsiveImages' + ); + + $this->files = $this->getFiles($filesDataKey, $fieldName); + + if (empty($this->files)) { + $processedData[$targetFieldName] = []; + return $processedData; } @@ -80,16 +96,10 @@ public function process( throw new RuntimeException('Could not fetch TypoScriptFrontendController from request.', 1712819889); } - $rootline = $this->rootlineFactory->create($processedData['data'], $fieldName, $tsfe); + $rootline = $this->rootlineFactory->create($this->getData(), $fieldName, $tsfe); $this->contentElementSizes = $rootline->getFinalSize(); $this->calculateFileDimensions(); - $targetFieldName = (string) $cObj->stdWrapValue( - 'as', - $processorConfiguration, - 'responsiveImages' - ); - $processedData[$targetFieldName] = $this->calculatedFiles; return $processedData; @@ -126,4 +136,43 @@ private function calculateFileDimensionForBreakpoints(): array return $fileDimensions; } + + private function getData(): array + { + if ( + $this->processedData['data'] instanceof ContentBlockData + ) { + assert(is_array($this->processedData['data']->_raw)); + $data = $this->processedData['data']->_raw; + } else { + assert(is_array($this->processedData['data'])); + $data = $this->processedData['data']; + } + + return $data; + } + + private function getFiles(string $filesDataKey, string $fieldName): array + { + if ( + isset($this->processedData[$filesDataKey]) + && is_array($this->processedData[$filesDataKey]) + ) { + return $this->processedData[$filesDataKey]; + } + + if ($fieldName === '') { + return []; + } + + if ($this->processedData['data'] instanceof ContentBlockData) { + return $this->processedData['data']->{$fieldName}; + } + + return $this->fileRepository->findByRelation( + 'tt_content', + $fieldName, + $this->processedData['data']['uid'] + ); + } } diff --git a/Tests/Fixtures/container_example/Test/Fixtures/0colDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/0colDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/0colDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/0colDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/0colImageFixWidthDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/0colImageFixWidthDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/0colImageFixWidthDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/0colImageFixWidthDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/1col2colDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/1col2colDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/1col2colDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/1col2colDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/1col2colFullWidthDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/1col2colFullWidthDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/1col2colFullWidthDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/1col2colFullWidthDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/1col2colWidthContainerMultiplierDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/1col2colWidthContainerMultiplierDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/1col2colWidthContainerMultiplierDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/1col2colWidthContainerMultiplierDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/1col2colWidthContainerSizeDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/1col2colWidthContainerSizeDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/1col2colWidthContainerSizeDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/1col2colWidthContainerSizeDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/1colDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/1colDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/1colDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/1colDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/1colFullWidthDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/1colFullWidthDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/1colFullWidthDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/1colFullWidthDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/2col2colDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/2col2colDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/2col2colDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/2col2colDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/2col_50_50_Database.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/2col_50_50_Database.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/2col_50_50_Database.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/2col_50_50_Database.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/2col_66_33_ImageLeft_Database.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/2col_66_33_ImageLeft_Database.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/2col_66_33_ImageLeft_Database.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/2col_66_33_ImageLeft_Database.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/2col_66_33_ImageRight_Database.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/2col_66_33_ImageRight_Database.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/2col_66_33_ImageRight_Database.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/2col_66_33_ImageRight_Database.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/3colDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/3colDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/3colDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/3colDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/3colImageFixWidthDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/Content/3colImageFixWidthDatabase.php similarity index 100% rename from Tests/Fixtures/container_example/Test/Fixtures/3colImageFixWidthDatabase.php rename to Tests/Fixtures/container_example/Test/Fixtures/Content/3colImageFixWidthDatabase.php diff --git a/Tests/Fixtures/container_example/Test/Fixtures/ContentBlocks/0colDatabase.php b/Tests/Fixtures/container_example/Test/Fixtures/ContentBlocks/0colDatabase.php new file mode 100644 index 0000000..8430b7f --- /dev/null +++ b/Tests/Fixtures/container_example/Test/Fixtures/ContentBlocks/0colDatabase.php @@ -0,0 +1,11 @@ + + diff --git a/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Assets/Icon.svg b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Assets/Icon.svg new file mode 100644 index 0000000..54568d7 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Assets/Icon.svg @@ -0,0 +1 @@ + diff --git a/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/EditorInterface.yaml b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/EditorInterface.yaml new file mode 100644 index 0000000..d315456 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/EditorInterface.yaml @@ -0,0 +1,12 @@ +name: codappix/image-fixed-width +title: codappix/image-fixed-width +description: 'Description for Content Element codappix/image-fixed-width' +group: common +prefixFields: true +prefixType: full +fields: + - + identifier: image + useExistingField: true + type: File + allowed: common-image-types diff --git a/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Source/Frontend.html b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Source/Frontend.html new file mode 100644 index 0000000..e0f3a22 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Source/Frontend.html @@ -0,0 +1,7 @@ + + + +{size.breakpoint.cropVariant} {size.size} {size.breakpoint.mediaQuery} + + + diff --git a/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Source/Language/Labels.xlf b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Source/Language/Labels.xlf new file mode 100644 index 0000000..43009e5 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image-fixed-width/Source/Language/Labels.xlf @@ -0,0 +1,17 @@ + + + +
+ + + codappix/image-fixed-width + + + Description for Content Element codappix/image-fixed-width + + + Custom image label + + + + diff --git a/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Assets/Icon.svg b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Assets/Icon.svg new file mode 100644 index 0000000..54568d7 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Assets/Icon.svg @@ -0,0 +1 @@ + diff --git a/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/EditorInterface.yaml b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/EditorInterface.yaml new file mode 100644 index 0000000..14b4f30 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/EditorInterface.yaml @@ -0,0 +1,12 @@ +name: codappix/image +title: codappix/image +description: 'Description for Content Element codappix/image' +group: common +prefixFields: true +prefixType: full +fields: + - + identifier: image + useExistingField: true + type: File + allowed: common-image-types diff --git a/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Source/Frontend.html b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Source/Frontend.html new file mode 100644 index 0000000..e0f3a22 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Source/Frontend.html @@ -0,0 +1,7 @@ + + + +{size.breakpoint.cropVariant} {size.size} {size.breakpoint.mediaQuery} + + + diff --git a/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Source/Language/Labels.xlf b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Source/Language/Labels.xlf new file mode 100644 index 0000000..209f583 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/ContentBlocks/ContentElements/image/Source/Language/Labels.xlf @@ -0,0 +1,17 @@ + + + +
+ + + codappix/image + + + Description for Content Element codappix/image + + + Custom image label + + + + diff --git a/Tests/Fixtures/content_blocks_example/Test/Fixtures/0colDatabase.php b/Tests/Fixtures/content_blocks_example/Test/Fixtures/0colDatabase.php new file mode 100644 index 0000000..71d44e4 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/Test/Fixtures/0colDatabase.php @@ -0,0 +1,32 @@ + [ + 1 => [ + 'uid' => '1', + 'pid' => '2', + 'hidden' => '0', + 'sorting' => '1', + 'CType' => 'codappix_image', + 'header' => 'codappix_image', + 'deleted' => '0', + 'starttime' => '0', + 'endtime' => '0', + 'colPos' => '0', + 'sys_language_uid' => '0', + 'image' => '1', + ], + ], + 'sys_file_reference' => [ + 0 => [ + 'uid' => '1', + 'pid' => '2', + 'uid_local' => '1', + 'uid_foreign' => '1', + 'tablenames' => 'tt_content', + 'fieldname' => 'image', + ], + ], +]; diff --git a/Tests/Fixtures/content_blocks_example/Test/Fixtures/0colImageFixWidthDatabase.php b/Tests/Fixtures/content_blocks_example/Test/Fixtures/0colImageFixWidthDatabase.php new file mode 100644 index 0000000..82bd96a --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/Test/Fixtures/0colImageFixWidthDatabase.php @@ -0,0 +1,32 @@ + [ + 1 => [ + 'uid' => '1', + 'pid' => '2', + 'hidden' => '0', + 'sorting' => '1', + 'CType' => 'codappix_imagefixedwidth', + 'header' => 'codappix_imagefixedwidth', + 'deleted' => '0', + 'starttime' => '0', + 'endtime' => '0', + 'colPos' => '0', + 'sys_language_uid' => '0', + 'image' => '1', + ], + ], + 'sys_file_reference' => [ + 0 => [ + 'uid' => '1', + 'pid' => '2', + 'uid_local' => '1', + 'uid_foreign' => '1', + 'tablenames' => 'tt_content', + 'fieldname' => 'image', + ], + ], +]; diff --git a/Tests/Fixtures/content_blocks_example/composer.json b/Tests/Fixtures/content_blocks_example/composer.json new file mode 100644 index 0000000..6221e75 --- /dev/null +++ b/Tests/Fixtures/content_blocks_example/composer.json @@ -0,0 +1,15 @@ +{ + "name": "codappix/content_blocks_example", + "description": "Add a content blocks example for frontend tests", + "type": "typo3-cms-extension", + "license": "GPL-2.0-or-later", + "require": { + "typo3/cms-core": "*", + "codappix/typo3-responsive-images": "*" + }, + "extra": { + "typo3/cms": { + "extension-key": "content_blocks_example" + } + } +} diff --git a/Tests/Functional/ContainerTest.php b/Tests/Functional/ContainerTest.php index 1e69457..f635893 100644 --- a/Tests/Functional/ContainerTest.php +++ b/Tests/Functional/ContainerTest.php @@ -42,8 +42,10 @@ protected function setUp(): void $this->testExtensionsToLoad = [ 'b13/container', 'codappix/typo3-responsive-images', + 'contentblocks/content-blocks', 'typo3conf/ext/responsive_images/Tests/Fixtures/base_example', 'typo3conf/ext/responsive_images/Tests/Fixtures/container_example', + 'typo3conf/ext/responsive_images/Tests/Fixtures/content_blocks_example', ]; $this->pathsToLinkInTestInstance = [ @@ -60,6 +62,8 @@ protected function setUp(): void 'EXT:base_example/Configuration/TypoScript/Setup.typoscript', 'EXT:base_example/Configuration/TypoScript/Rendering.typoscript', 'EXT:container_example/Configuration/TypoScript/Setup.typoscript', + 'EXT:content_blocks_example/Configuration/TypoScript/ContentElements/codappix_image.typoscript', + 'EXT:content_blocks_example/Configuration/TypoScript/ContentElements/codappix_imagefixedwidth.typoscript', ]); } @@ -211,7 +215,25 @@ public static function imageScalingValuesDataProvider(): iterable #[DataProvider(methodName: 'imageScalingValuesDataProvider')] public function imageIsScaledCorrectly(string $phpDataSet, array $expectedValues): void { - $this->importPHPDataSet(__DIR__ . '/../Fixtures/container_example/Test/Fixtures/' . $phpDataSet); + $this->importPHPDataSet(__DIR__ . '/../Fixtures/container_example/Test/Fixtures/Content/' . $phpDataSet); + + $request = new InternalRequest(); + $request = $request->withPageId(2); + + $result = $this->executeFrontendSubRequest($request); + + self::assertSame(200, $result->getStatusCode()); + + foreach ($expectedValues as $expectedValue) { + self::assertStringContainsString($expectedValue, (string) $result->getBody()); + } + } + + #[Test] + #[DataProvider(methodName: 'imageScalingValuesDataProvider')] + public function contentBlocksImageIsScaledCorrectly(string $phpDataSet, array $expectedValues): void + { + $this->importPHPDataSet(__DIR__ . '/../Fixtures/container_example/Test/Fixtures/ContentBlocks/' . $phpDataSet); $request = new InternalRequest(); $request = $request->withPageId(2); diff --git a/Tests/Functional/ContentBlocksTest.php b/Tests/Functional/ContentBlocksTest.php new file mode 100644 index 0000000..7e245e3 --- /dev/null +++ b/Tests/Functional/ContentBlocksTest.php @@ -0,0 +1,100 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use Codappix\Typo3PhpDatasets\TestingFramework; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; +use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest; +use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; + +class ContentBlocksTest extends FunctionalTestCase +{ + use TestingFramework; + + protected function setUp(): void + { + $this->testExtensionsToLoad = [ + 'codappix/typo3-responsive-images', + 'contentblocks/content-blocks', + 'typo3conf/ext/responsive_images/Tests/Fixtures/content_blocks_example', + ]; + + $this->pathsToLinkInTestInstance = [ + 'typo3conf/ext/responsive_images/Tests/Fixtures/fileadmin/test_data' => 'fileadmin/test_data', + 'typo3conf/ext/responsive_images/Tests/Fixtures/config/sites' => 'typo3conf/sites', + ]; + + parent::setUp(); + + $this->importPHPDataSet(__DIR__ . '/../Fixtures/BaseDatabase.php'); + $this->setUpFrontendRootPage(1, [ + 'EXT:responsive_images/Configuration/TypoScript/Setup.typoscript', + 'EXT:content_blocks_example/Configuration/TypoScript/Setup.typoscript', + 'EXT:content_blocks_example/Configuration/TypoScript/Rendering.typoscript', + ]); + } + + public static function imageScalingValuesDataProvider(): iterable + { + yield '0 Column' => [ + '0colDatabase.php', + [ + '0' => 'mobile 734 (max-width: 480px)', + '1' => 'mobile 704 (max-width: 767px)', + '2' => 'tablet 924 (max-width: 991px)', + '3' => 'default 1124 (max-width: 1479px)', + '4' => 'large 562 (min-width: 1480px)', + ], + ]; + yield '0 Column with ImageFixWidth' => [ + '0colImageFixWidthDatabase.php', + [ + '0' => 'mobile 600 (max-width: 480px)', + '1' => 'mobile 900 (max-width: 767px)', + '2' => 'tablet 1200 (max-width: 991px)', + '3' => 'default 1600 (max-width: 1479px)', + '4' => 'large 1600 (min-width: 1480px)', + ], + ]; + } + + #[Test] + #[DataProvider(methodName: 'imageScalingValuesDataProvider')] + public function imageIsScaledCorrectly(string $phpDataSet, array $expectedValues): void + { + $this->importPHPDataSet(__DIR__ . '/../Fixtures/content_blocks_example/Test/Fixtures/' . $phpDataSet); + + $request = new InternalRequest(); + $request = $request->withPageId(2); + + $result = $this->executeFrontendSubRequest($request); + + self::assertSame(200, $result->getStatusCode()); + + foreach ($expectedValues as $expectedValue) { + self::assertStringContainsString($expectedValue, (string) $result->getBody()); + } + } +} diff --git a/composer.json b/composer.json index b61cf62..f869944 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "require-dev": { "b13/container": "^2.3", "codappix/typo3-php-datasets": "^1.5", + "contentblocks/content-blocks": "^0.7", "erickskrauch/php-cs-fixer-custom-fixers": "^1.2", "friendsofphp/php-cs-fixer": "^3.50", "kubawerlos/php-cs-fixer-custom-fixers": "^3.21",