diff --git a/Classes/ViewHelpers/Registration/Field/PrefillFieldViewHelper.php b/Classes/ViewHelpers/Registration/Field/PrefillFieldViewHelper.php
index 461e3bd29..8d88c93f6 100644
--- a/Classes/ViewHelpers/Registration/Field/PrefillFieldViewHelper.php
+++ b/Classes/ViewHelpers/Registration/Field/PrefillFieldViewHelper.php
@@ -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
@@ -41,8 +39,10 @@ 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) {
@@ -50,8 +50,9 @@ public function render(): string
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);
}
/**
@@ -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()];
}
}
diff --git a/Tests/Functional/ViewHelpers/Registration/Field/PrefillFieldViewHelperTest.php b/Tests/Functional/ViewHelpers/Registration/Field/PrefillFieldViewHelperTest.php
new file mode 100644
index 000000000..63dc33290
--- /dev/null
+++ b/Tests/Functional/ViewHelpers/Registration/Field/PrefillFieldViewHelperTest.php
@@ -0,0 +1,148 @@
+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('');
+ $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('');
+ $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('');
+ $context->getVariableProvider()->add('field', $field);
+ $this->assertEquals($expected, (new TemplateView($context))->render());
+ }
+}
diff --git a/Tests/Unit/ViewHelpers/Registration/Field/PrefillFieldViewHelperTest.php b/Tests/Unit/ViewHelpers/Registration/Field/PrefillFieldViewHelperTest.php
deleted file mode 100644
index 6847951a3..000000000
--- a/Tests/Unit/ViewHelpers/Registration/Field/PrefillFieldViewHelperTest.php
+++ /dev/null
@@ -1,130 +0,0 @@
-setDefaultValue('Default');
-
- $frontendUser = $this->createMock(FrontendUserAuthentication::class);
- $extbaseRequestParameters = $this->createMock(ExtbaseRequestParameters::class);
- $request = $this->createMock(Request::class);
- $request->expects(self::any())->method('getAttribute')->with('extbase')->willReturn($extbaseRequestParameters);
- $request->expects(self::any())->method('getAttribute')->with('frontend.user')->willReturn($frontendUser);
- $renderingContext = $this->createMock(RenderingContext::class);
- $renderingContext->expects(self::any())->method('getAttribute')->willReturn($request);
-
- $viewHelper = new PrefillFieldViewHelper();
- $viewHelper->setRenderingContext($renderingContext);
- $viewHelper->setArguments(['registrationField' => $field]);
-
- self::assertSame('Default', $viewHelper->render());
- }
-
- #[Test]
- public function viewHelperReturnsFieldFeUserValue(): void
- {
- $field = new Field();
- $field->setFeuserValue('first_name');
-
- $frontendUser = $this->createMock(FrontendUserAuthentication::class);
- $frontendUser->user = [
- 'first_name' => 'John',
- ];
-
- $extbaseRequestParameters = $this->createMock(ExtbaseRequestParameters::class);
- $request = $this->createMock(Request::class);
- $request->expects(self::any())->method('getAttribute')->with('extbase')->willReturn($extbaseRequestParameters);
- $request->expects(self::any())->method('getAttribute')->with('frontend.user')->willReturn($frontendUser);
- $renderingContext = $this->createMock(RenderingContext::class);
- $renderingContext->expects(self::any())->method('getAttribute')->willReturn($request);
-
- $viewHelper = new PrefillFieldViewHelper();
- $viewHelper->setRenderingContext($renderingContext);
- $viewHelper->setArguments(['registrationField' => $field]);
-
- self::assertSame('John', $viewHelper->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 {
- $field = $this->createMock(Field::class);
- $field->expects(self::any())->method('getUid')->willReturn($fieldUid);
-
- $submittedData = [
- 'tx_sfeventmgt_pievent' => [
- 'registration' => [
- 'fields' => $fieldValues,
- ],
- ],
- ];
-
- $originalRequest = $this->createMock(Request::class);
- $originalRequest->expects(self::any())->method('getControllerExtensionName')->willReturn('SfEventMgt');
- $originalRequest->expects(self::any())->method('getPluginName')->willReturn('Pievent');
- $originalRequest->expects(self::any())->method('getParsedBody')->willReturn($submittedData);
-
- $extbaseRequestParameters = $this->createMock(ExtbaseRequestParameters::class);
- $extbaseRequestParameters->expects(self::once())->method('getOriginalRequest')->willReturn($originalRequest);
- $request = $this->createMock(Request::class);
- $request->expects(self::once())->method('getAttribute')->with('extbase')->willReturn($extbaseRequestParameters);
- $renderingContext = $this->createMock(RenderingContext::class);
- $renderingContext->expects(self::any())->method('getAttribute')->willReturn($request);
-
- $viewHelper = new PrefillFieldViewHelper();
- $viewHelper->setRenderingContext($renderingContext);
- $viewHelper->setArguments(['registrationField' => $field]);
-
- self::assertSame($expected, $viewHelper->render());
- }
-}