Skip to content

Commit

Permalink
[TASK] Migrate PrefillFieldViewHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
derhansen committed Sep 4, 2024
1 parent c1be25c commit 6cee057
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 156 deletions.
41 changes: 15 additions & 26 deletions Classes/ViewHelpers/Registration/Field/PrefillFieldViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@

use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
use DERHANSEN\SfEventMgt\ViewHelpers\AbstractPrefillViewHelper;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* PrefillField ViewHelper for registration fields
*/
class PrefillFieldViewHelper extends AbstractPrefillViewHelper
{
public function initializeArguments(): void
Expand All @@ -41,17 +39,20 @@ public function render(): string
$registrationField = $this->arguments['registrationField'];

// If mapping errors occurred for form, return value that has been submitted from POST data
/** @var Request $request */
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
/** @var ExtbaseRequestParameters $extbaseRequestParameters */
$extbaseRequestParameters = $this->renderingContext->getRequest()->getAttribute('extbase');
$extbaseRequestParameters = $request->getAttribute('extbase');
$originalRequest = $extbaseRequestParameters->getOriginalRequest();

if ($originalRequest) {
$registrationData = $originalRequest->getParsedBody()[$this->getPluginNamespace($originalRequest)] ?? [];
return $this->getFieldValueFromSubmittedData($registrationData, $registrationField->getUid());
}

$frontendUser = $request->getAttribute('frontend.user');
$value = $registrationField->getDefaultValue();
return $this->prefillFromFeuserData($registrationField, $value);
return $this->prefillFromFeuserData($frontendUser, $registrationField, $value);
}

/**
Expand All @@ -71,31 +72,19 @@ protected function getFieldValueFromSubmittedData(array $submittedData, int $fie

/**
* Prefills $value with fe_users data if configured in registration field
*
* @param Field $field
* @param string $value
* @return string
*/
protected function prefillFromFeuserData(Field $field, string $value): string
{
if (!$this->getTypoScriptFrontendController() ||
!$this->getFrontendUser()->user ||
protected function prefillFromFeuserData(
FrontendUserAuthentication $frontendUser,
Field $field,
string $value
): string {
if (!$frontendUser->user ||
$field->getFeuserValue() === '' ||
!array_key_exists($field->getFeuserValue(), $this->getFrontendUser()->user)
!array_key_exists($field->getFeuserValue(), $frontendUser->user)
) {
return $value;
}

return (string)$this->getFrontendUser()->user[$field->getFeuserValue()];
}

protected function getFrontendUser(): FrontendUserAuthentication
{
return $this->getTypoScriptFrontendController()->fe_user;
}

protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
{
return $GLOBALS['TSFE'] ?? null;
return (string)$frontendUser->user[$field->getFeuserValue()];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

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

namespace DERHANSEN\SfEventMgt\Tests\Functional\ViewHelpers\Registration\Field;

use DateTime;
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
use DERHANSEN\SfEventMgt\ViewHelpers\Registration\Field\PrefillFieldViewHelper;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use TYPO3Fluid\Fluid\View\TemplateView;

class PrefillFieldViewHelperTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = ['typo3conf/ext/sf_event_mgt'];

#[Test]
public function viewHelperReturnsFieldDefaultValue(): void
{
$field = new Field();
$field->setDefaultValue('Default');

$frontendUser = new FrontendUserAuthentication();
$extbaseRequestParameters = new ExtbaseRequestParameters();
$serverRequest = new ServerRequest();
$serverRequest = $serverRequest->withAttribute('extbase', $extbaseRequestParameters)
->withAttribute('frontend.user', $frontendUser)
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE);
$extbaseRequest = (new Request($serverRequest));
$context = $this->get(RenderingContextFactory::class)->create([], $extbaseRequest);
$context->getViewHelperResolver()->addNamespace('e', 'DERHANSEN\\SfEventMgt\\ViewHelpers');
$context->getTemplatePaths()->setTemplateSource('<e:registration.field.prefillField registrationField="{field}" />');
$context->getVariableProvider()->add('field', $field);
$this->assertEquals('Default', (new TemplateView($context))->render());
}

#[Test]
public function viewHelperReturnsFieldFeUserValue(): void
{
$field = new Field();
$field->setFeuserValue('first_name');

$frontendUser = new FrontendUserAuthentication();
$frontendUser->user = [
'first_name' => 'John',
];

$extbaseRequestParameters = new ExtbaseRequestParameters();
$serverRequest = new ServerRequest();
$serverRequest = $serverRequest->withAttribute('extbase', $extbaseRequestParameters)
->withAttribute('frontend.user', $frontendUser)
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE);
$extbaseRequest = (new Request($serverRequest));
$context = $this->get(RenderingContextFactory::class)->create([], $extbaseRequest);
$context->getViewHelperResolver()->addNamespace('e', 'DERHANSEN\\SfEventMgt\\ViewHelpers');
$context->getTemplatePaths()->setTemplateSource('<e:registration.field.prefillField registrationField="{field}" />');
$context->getVariableProvider()->add('field', $field);
$this->assertEquals('John', (new TemplateView($context))->render());
}

public static function viewHelperReturnsSubmittedValueIfOriginalRequestExistDataProvider(): array
{
return [
'submitted value returned' => [
1,
[
'1' => 'Submitted value',
],
'Submitted value',
],
'empty value returned if not found' => [
2,
[
'1' => 'Submitted value',
],
'',
],
];
}

#[DataProvider('viewHelperReturnsSubmittedValueIfOriginalRequestExistDataProvider')]
#[Test]
public function viewHelperReturnsExpectedValueIfOriginalRequestExist(
int $fieldUid,
array $fieldValues,
string $expected
): void {
$submittedData = [
'tx_sfeventmgt_pievent' => [
'registration' => [
'fields' => $fieldValues,
],
],
];

$frontendUser = new FrontendUserAuthentication();
$frontendUser->user = [
'first_name' => 'John',
];

$field = $this->createMock(Field::class);
$field->expects(self::any())->method('getUid')->willReturn($fieldUid);

$originalExtbaseRequestParameters = new ExtbaseRequestParameters();
$originalExtbaseRequestParameters->setPluginName('Pievent');
$originalExtbaseRequestParameters->setControllerExtensionName('SfEventMgt');

$originalServerRequest = new ServerRequest();
$originalServerRequest = $originalServerRequest->withAttribute('extbase', $originalExtbaseRequestParameters)
->withParsedBody($submittedData)
->withAttribute('frontend.user', $frontendUser)
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE);
$originalExtbaseRequest = (new Request($originalServerRequest));

$extbaseRequestParameters = new ExtbaseRequestParameters();
$extbaseRequestParameters->setPluginName('Pievent');
$extbaseRequestParameters->setControllerExtensionName('SfEventMgt');
$extbaseRequestParameters->setOriginalRequest($originalExtbaseRequest);

$serverRequest = new ServerRequest();
$serverRequest = $serverRequest->withAttribute('extbase', $extbaseRequestParameters)
->withParsedBody($submittedData)
->withAttribute('frontend.user', $frontendUser)
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE);
$extbaseRequest = (new Request($serverRequest));
$context = $this->get(RenderingContextFactory::class)->create([], $extbaseRequest);
$context->getViewHelperResolver()->addNamespace('e', 'DERHANSEN\\SfEventMgt\\ViewHelpers');
$context->getTemplatePaths()->setTemplateSource('<e:registration.field.prefillField registrationField="{field}" />');
$context->getVariableProvider()->add('field', $field);
$this->assertEquals($expected, (new TemplateView($context))->render());
}
}

This file was deleted.

0 comments on commit 6cee057

Please sign in to comment.