Skip to content

Commit

Permalink
Merge branch '3.x' into feat/headless-user-intv3
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszuznanski committed Jun 23, 2023
2 parents 6645e81 + 730cd11 commit 7368cb8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 28 deletions.
43 changes: 30 additions & 13 deletions Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FriendsOfTYPO3\Headless\Event\EnrichFileDataEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
Expand All @@ -31,7 +32,6 @@ class FileUtility
{
public const RETINA_RATIO = 2;
public const LQIP_RATIO = 0.1;

protected ContentObjectRenderer $contentObjectRenderer;
protected RendererRegistry $rendererRegistry;
protected ImageService $imageService;
Expand All @@ -42,20 +42,23 @@ class FileUtility
* @var array<string, array<string, string>>
*/
protected array $errors = [];
protected Features $features;

public function __construct(
?ContentObjectRenderer $contentObjectRenderer = null,
?RendererRegistry $rendererRegistry = null,
?ImageService $imageService = null,
?ServerRequestInterface $serverRequest = null,
?EventDispatcherInterface $eventDispatcher = null
?EventDispatcherInterface $eventDispatcher = null,
?Features $features = null
) {
$this->contentObjectRenderer = $contentObjectRenderer ??
GeneralUtility::makeInstance(ContentObjectRenderer::class);
$this->rendererRegistry = $rendererRegistry ?? GeneralUtility::makeInstance(RendererRegistry::class);
$this->imageService = $imageService ?? GeneralUtility::makeInstance(ImageService::class);
$this->serverRequest = $serverRequest ?? ($GLOBALS['TYPO3_REQUEST'] ?? null);
$this->eventDispatcher = $eventDispatcher ?? GeneralUtility::makeInstance(EventDispatcher::class);
$this->features = $features ?? GeneralUtility::makeInstance(Features::class);
}

/**
Expand Down Expand Up @@ -122,18 +125,32 @@ public function processFile(
'extension' => $fileReference->getProperty('extension'),
];

return [
'publicUrl' => $publicUrl,
'properties' => $this->eventDispatcher->dispatch(
new EnrichFileDataEvent(
$originalFileReference,
$fileReference,
array_merge(
$originalProperties,
$processedProperties
)
$event = $this->eventDispatcher->dispatch(
new EnrichFileDataEvent(
$originalFileReference,
$fileReference,
array_merge(
$originalProperties,
$processedProperties
)
)->getProperties(),
)
);

$cacheBuster = '';

if ($this->features->isFeatureEnabled('headless.assetsCacheBusting') && $event->getProperties()['type'] !== 'video') {
$modified = $event->getProcessed()->getProperty('modification_date');

if (!$modified) {
$modified = $event->getProcessed()->getProperty('tstamp');
}

$cacheBuster = '?' . $modified;
}

return [
'publicUrl' => $publicUrl . $cacheBuster,
'properties' => $event->getProperties(),
];
}

Expand Down
5 changes: 3 additions & 2 deletions Classes/Utility/UrlUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;

use function array_merge;
use function count;
use function rtrim;
use function strpos;

Expand Down Expand Up @@ -186,7 +185,9 @@ private function resolveWithVariants(
array $variants = [],
string $returnField = 'frontendBase'
): string {
if (count($variants) === 0) {
$frontendUrl = rtrim($frontendUrl, '/');

if ($variants === []) {
return $frontendUrl;
}

Expand Down
3 changes: 3 additions & 0 deletions Classes/ViewHelpers/Format/Json/DecodeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function render()
$json = $this->arguments['json'];
if ($json === null) {
$json = $this->renderChildren();
if ($json !== null) {
$json = trim($json);
}
if (empty($json)) {
return null;
}
Expand Down
10 changes: 5 additions & 5 deletions Classes/XClass/Controller/FormFrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function renderAction(): ResponseInterface
$formFields = $formDefinition['renderables'][$currentPageIndex]['renderables'];

// provides support for custom options providers (dynamic selects/radio/checkboxes)
$formFieldsNames = $this->generateFieldNamesAndReplaceCustomOptions($formFields, $formDefinition['identifier'], $formRuntime->getFormDefinition());
$formFieldsNames = $this->generateFieldNamesAndReplaceCustomOptions($formFields, $formDefinition['identifier'], $formRuntime);

if ($honeyPot) {
$formFields[] = [
Expand Down Expand Up @@ -261,7 +261,7 @@ private function getNextPage(\TYPO3\CMS\Form\Domain\Runtime\FormRuntime $formRun
* @param array<mixed> $formFields
* @return array<int, string>
*/
private function generateFieldNamesAndReplaceCustomOptions(array &$formFields, string $identifier, FormDefinition $definition): array
private function generateFieldNamesAndReplaceCustomOptions(array &$formFields, string $identifier, FormRuntime $formRuntime): array
{
$formFieldsNames = [];

Expand All @@ -271,11 +271,11 @@ private function generateFieldNamesAndReplaceCustomOptions(array &$formFields, s
is_array($field['renderables'])) {
$formFieldsNames = array_merge(
$formFieldsNames,
$this->generateFieldNamesAndReplaceCustomOptions($field['renderables'], $identifier, $definition)
$this->generateFieldNamesAndReplaceCustomOptions($field['renderables'], $identifier, $formRuntime)
);
} else {
if (!empty($field['properties']['customOptions'])) {
$customOptions = GeneralUtility::makeInstance($field['properties']['customOptions']);
$customOptions = GeneralUtility::makeInstance($field['properties']['customOptions'], $field, $formFields, $identifier, $formRuntime);

if ($customOptions instanceof CustomOptionsInterface) {
$field['properties']['options'] = $customOptions->get();
Expand All @@ -284,7 +284,7 @@ private function generateFieldNamesAndReplaceCustomOptions(array &$formFields, s
unset($field['properties']['customOptions']);
}

$defaultValue = $definition->getElementDefaultValueByIdentifier($field['identifier']);
$defaultValue = $formRuntime->getFormDefinition()->getElementDefaultValueByIdentifier($field['identifier']);

if ($defaultValue) {
$field['properties']['defaultValue'] = $defaultValue;
Expand Down
6 changes: 4 additions & 2 deletions Tests/Unit/Utility/FileUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function testGetAbsoluteUrl(): void

public function testProcessFile()
{
$GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['headless.assetsCacheBusting'] = true;
$fileData = [
'uid' => 103,
'pid' => 0,
Expand Down Expand Up @@ -361,7 +362,7 @@ protected function getImageServiceWithProcessedFile($file, $processedFile, $proc
protected function getBaselineResultArrayForFile(): array
{
return [
'publicUrl' => 'https://test-frontend.tld/fileadmin/test-file.jpg',
'publicUrl' => 'https://test-frontend.tld/fileadmin/test-file.jpg?1639061876',
'properties' =>
[
'title' => null,
Expand Down Expand Up @@ -396,7 +397,7 @@ protected function getBaselineResultArrayForFile(): array
protected function getBaselineResultArrayForFileReference(): array
{
return [
'publicUrl' => 'https://test-frontend.tld/fileadmin/test-file.jpg',
'publicUrl' => 'https://test-frontend.tld/fileadmin/test-file.jpg?1639061876',
'properties' =>
[
'title' => null,
Expand Down Expand Up @@ -482,6 +483,7 @@ protected function getFileReferenceBaselineData(): array
'width' => 526,
'uid_local' => 103,
'height' => 526,
'tstamp' => 1639061876,
];
}

Expand Down
12 changes: 6 additions & 6 deletions Tests/Unit/Utility/UrlUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ public function testFrontendUrlsWithBaseProductionAndLocalOverride(): void
{
$site = $this->prophesize(Site::class);
$site->getConfiguration()->shouldBeCalled(3)->willReturn([
'base' => 'https://api.typo3.org',
'frontendBase' => 'https://www.typo3.org',
'frontendApiProxy' => 'https://www.typo3.org/headless',
'frontendFileApi' => 'https://www.typo3.org/headless/fileadmin',
'SpecialSitemapKey' => 'https://www.typo3.org/custom-sitemap',
'base' => 'https://api.typo3.org/',
'frontendBase' => 'https://www.typo3.org/',
'frontendApiProxy' => 'https://www.typo3.org/headless/',
'frontendFileApi' => 'https://www.typo3.org/headless/fileadmin/',
'SpecialSitemapKey' => 'https://www.typo3.org/custom-sitemap/',
'languages' => [],
'baseVariants' => [
[
Expand Down Expand Up @@ -672,7 +672,7 @@ public function testEdgeCases()

$urlUtility = new UrlUtility(null, $resolver->reveal(), $siteFinder, $request->reveal());
self::assertSame('https://frontend-domain-from-lang.tld', $urlUtility->getFrontendUrl());
self::assertSame('https://frontend-domain-from-lang.tld/headless/', $urlUtility->getProxyUrl());
self::assertSame('https://frontend-domain-from-lang.tld/headless', $urlUtility->getProxyUrl());
self::assertSame('https://frontend-domain-from-lang.tld/headless/fileadmin', $urlUtility->getStorageProxyUrl());

// configuration on language lvl with variants
Expand Down
28 changes: 28 additions & 0 deletions Tests/Unit/ViewHelpers/Format/Json/DecodeViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Tests\Unit\ViewHelpers\Format\Json;

use FriendsOfTYPO3\Headless\ViewHelpers\Format\Json\DecodeViewHelper;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class DecodeViewHelperTest extends UnitTestCase
{
public function testRender(): void
{
$GLOBALS['TYPO3_CONF_VARS']['FE']['debug'] = true;
$decodeViewHelper = new DecodeViewHelper();
$decodeViewHelper->setArguments(['json' => null]);
$decodeViewHelper->setRenderChildrenClosure(function () { return "\n \n"; });
$result = $decodeViewHelper->render();
self::assertNull($result);
}
}

0 comments on commit 7368cb8

Please sign in to comment.