diff --git a/.Build/phpunit/FunctionalTests.xml b/.Build/phpunit/FunctionalTests.xml index 56618c733..bf3e76a05 100644 --- a/.Build/phpunit/FunctionalTests.xml +++ b/.Build/phpunit/FunctionalTests.xml @@ -2,17 +2,17 @@ diff --git a/Classes/ViewHelpers/PrefillViewHelper.php b/Classes/ViewHelpers/PrefillViewHelper.php index 326b080a0..4ac106e09 100644 --- a/Classes/ViewHelpers/PrefillViewHelper.php +++ b/Classes/ViewHelpers/PrefillViewHelper.php @@ -40,7 +40,7 @@ public function render(): string return $registrationData['registration'][$fieldname]; } - $frontendUser = $this->getFrontendUser(); + $frontendUser = $request->getAttribute('frontend.user'); if (!$frontendUser->user || empty($prefillSettings) || !array_key_exists($fieldname, $prefillSettings) ) { @@ -49,9 +49,4 @@ public function render(): string return (string)($frontendUser->user[$prefillSettings[$fieldname]]); } - - protected function getFrontendUser(): FrontendUserAuthentication - { - return $this->renderingContext->getAttribute(ServerRequestInterface::class)->getAttribute('frontend.user'); - } } diff --git a/Tests/Functional/ViewHelpers/PrefillViewHelperTest.php b/Tests/Functional/ViewHelpers/PrefillViewHelperTest.php new file mode 100644 index 000000000..a8738c85f --- /dev/null +++ b/Tests/Functional/ViewHelpers/PrefillViewHelperTest.php @@ -0,0 +1,227 @@ +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(''); + $this->assertEquals('', (new TemplateView($context))->render()); + } + + #[Test] + public function viewHelperReturnsCurrentFieldValueIfValueInParsedBodyAvailable(): void + { + $submittedData = [ + 'tx_sfeventmgt_pieventregistration' => [ + 'registration' => ['firstname' => 'Torben'], + ], + ]; + + $frontendUser = new FrontendUserAuthentication(); + $extbaseRequestParameters = new ExtbaseRequestParameters(); + $extbaseRequestParameters->setPluginName('Pieventregistration'); + $extbaseRequestParameters->setControllerExtensionName('SfEventMgt'); + $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(''); + $this->assertEquals('Torben', (new TemplateView($context))->render()); + } + + #[Test] + public function viewHelperReturnsEmptyStringIfPrefillSettingsEmpty(): void + { + $submittedData = []; + $settings = []; + + $frontendUser = new FrontendUserAuthentication(); + $frontendUser->user = [ + 'first_name' => 'John', + ]; + $extbaseRequestParameters = new ExtbaseRequestParameters(); + $extbaseRequestParameters->setPluginName('Pieventregistration'); + $extbaseRequestParameters->setControllerExtensionName('SfEventMgt'); + $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('settings', $settings); + $this->assertEquals('', (new TemplateView($context))->render()); + } + + #[Test] + public function viewHelperReturnsEmptyStringIfFieldNotFoundInPrefillSettings(): void + { + $submittedData = []; + $settings = [ + 'registration' => [ + 'prefillFields' => [ + 'lastname' => 'last_name', + ] + ], + ]; + + $frontendUser = new FrontendUserAuthentication(); + $frontendUser->user = [ + 'first_name' => 'John', + ]; + $extbaseRequestParameters = new ExtbaseRequestParameters(); + $extbaseRequestParameters->setPluginName('Pieventregistration'); + $extbaseRequestParameters->setControllerExtensionName('SfEventMgt'); + $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('settings', $settings); + $this->assertEquals('', (new TemplateView($context))->render()); + } + + #[Test] + public function viewHelperReturnsEmptyStringIfFieldNotFoundInFeUser(): void + { + $submittedData = []; + $settings = [ + 'registration' => [ + 'prefillFields' => [ + 'non_existing' => 'non_existing_field', + ] + ], + ]; + + $frontendUser = new FrontendUserAuthentication(); + $frontendUser->user = [ + 'first_name' => 'John', + ]; + $extbaseRequestParameters = new ExtbaseRequestParameters(); + $extbaseRequestParameters->setPluginName('Pieventregistration'); + $extbaseRequestParameters->setControllerExtensionName('SfEventMgt'); + $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('settings', $settings); + $this->assertEquals('', (new TemplateView($context))->render()); + } + + #[Test] + public function viewHelperReturnsFieldvalueIfFound(): void + { + $submittedData = []; + $settings = [ + 'registration' => [ + 'prefillFields' => [ + 'firstname' => 'first_name', + ] + ], + ]; + + $frontendUser = new FrontendUserAuthentication(); + $frontendUser->user = [ + 'first_name' => 'John', + ]; + $extbaseRequestParameters = new ExtbaseRequestParameters(); + $extbaseRequestParameters->setPluginName('Pieventregistration'); + $extbaseRequestParameters->setControllerExtensionName('SfEventMgt'); + $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('settings', $settings); + $this->assertEquals('John', (new TemplateView($context))->render()); + } + + #[Test] + public function viewHelperReturnsSubmittedValueIfValidationError(): void + { + $submittedData = [ + 'tx_sfeventmgt_pieventregistration' => [ + 'registration' => ['firstname' => 'Torben'], + ], + ]; + + $settings = [ + 'registration' => [ + 'prefillFields' => [ + 'firstname' => 'first_name', + ] + ], + ]; + + $frontendUser = new FrontendUserAuthentication(); + $frontendUser->user = [ + 'first_name' => 'John', + ]; + $extbaseRequestParameters = new ExtbaseRequestParameters(); + $extbaseRequestParameters->setPluginName('Pieventregistration'); + $extbaseRequestParameters->setControllerExtensionName('SfEventMgt'); + $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('settings', $settings); + $this->assertEquals('Torben', (new TemplateView($context))->render()); + } +} diff --git a/Tests/Unit/ViewHelpers/PrefillViewHelperTest.php b/Tests/Unit/ViewHelpers/PrefillViewHelperTest.php deleted file mode 100644 index 962f57c30..000000000 --- a/Tests/Unit/ViewHelpers/PrefillViewHelperTest.php +++ /dev/null @@ -1,209 +0,0 @@ -createMock(FrontendUserAuthentication::class); - $request = $this->createMock(Request::class); - $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 PrefillViewHelper(); - $viewHelper->setRenderingContext($renderingContext); - $viewHelper->setArguments([ - 'fieldname' => 'a field', - 'prefillSettings' => [], - ]); - $actual = $viewHelper->render(); - self::assertSame('', $actual); - } - - #[Test] - public function viewHelperReturnsCurrentFieldValueIfValueInParsedBodyAvailable(): void - { - $submittedData = [ - 'tx_sfeventmgt_pieventregistration' => [ - 'registration' => ['firstname' => 'Torben'], - ], - ]; - - $frontendUser = $this->createMock(FrontendUserAuthentication::class); - $request = $this->createMock(Request::class); - $request->expects(self::any())->method('getControllerExtensionName')->willReturn('SfEventMgt'); - $request->expects(self::any())->method('getPluginName')->willReturn('Pieventregistration'); - $request->expects(self::any())->method('getParsedBody')->willReturn($submittedData); - $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 PrefillViewHelper(); - $viewHelper->setRenderingContext($renderingContext); - $viewHelper->setArguments([ - 'fieldname' => 'firstname', - 'prefillSettings' => [], - ]); - $actual = $viewHelper->render(); - self::assertSame('Torben', $actual); - } - - #[Test] - public function viewHelperReturnsEmptyStringIfPrefillSettingsEmpty(): void - { - $submittedData = []; - - $frontendUser = $this->createMock(FrontendUserAuthentication::class); - $frontendUser->user = [ - 'first_name' => 'John', - ]; - $request = $this->createMock(Request::class); - $request->expects(self::any())->method('getControllerExtensionName')->willReturn('SfEventMgt'); - $request->expects(self::any())->method('getPluginName')->willReturn('Pieventregistration'); - $request->expects(self::any())->method('getParsedBody')->willReturn($submittedData); - $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 PrefillViewHelper(); - $viewHelper->setRenderingContext($renderingContext); - $viewHelper->setArguments([ - 'fieldname' => 'firstname', - 'prefillSettings' => [], - ]); - $actual = $viewHelper->render(); - self::assertSame('', $actual); - } - - #[Test] - public function viewHelperReturnsEmptyStringIfFieldNotFoundInPrefillSettings(): void - { - $submittedData = []; - $frontendUser = $this->createMock(FrontendUserAuthentication::class); - $frontendUser->user = [ - 'first_name' => 'John', - ]; - $request = $this->createMock(Request::class); - $request->expects(self::any())->method('getControllerExtensionName')->willReturn('SfEventMgt'); - $request->expects(self::any())->method('getPluginName')->willReturn('Pieventregistration'); - $request->expects(self::any())->method('getParsedBody')->willReturn($submittedData); - $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 PrefillViewHelper(); - $viewHelper->setRenderingContext($renderingContext); - $viewHelper->setArguments([ - 'fieldname' => 'lastname', - 'prefillSettings' => ['firstname' => 'first_name'], - ]); - $actual = $viewHelper->render(); - self::assertSame('', $actual); - } - - #[Test] - public function viewHelperReturnsEmptyStringIfFieldNotFoundInFeUser(): void - { - $frontendUser = $this->createMock(FrontendUserAuthentication::class); - $frontendUser->user = [ - 'first_name' => 'John', - ]; - $request = $this->createMock(Request::class); - $request->expects(self::any())->method('getControllerExtensionName')->willReturn('SfEventMgt'); - $request->expects(self::any())->method('getPluginName')->willReturn('Pieventregistration'); - $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 PrefillViewHelper(); - $viewHelper->setRenderingContext($renderingContext); - $viewHelper->setArguments([ - 'fieldname' => 'lastname', - 'prefillSettings' => ['firstname' => 'first_name'], - ]); - $actual = $viewHelper->render(); - self::assertSame('', $actual); - } - - #[Test] - public function viewHelperReturnsFieldvalueIfFound(): void - { - $frontendUser = $this->createMock(FrontendUserAuthentication::class); - $frontendUser->user = [ - 'first_name' => 'John', - 'last_name' => 'Doe', - ]; - $request = $this->createMock(Request::class); - $request->expects(self::any())->method('getControllerExtensionName')->willReturn('SfEventMgt'); - $request->expects(self::any())->method('getPluginName')->willReturn('Pieventregistration'); - $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 PrefillViewHelper(); - $viewHelper->setRenderingContext($renderingContext); - $viewHelper->setArguments([ - 'fieldname' => 'lastname', - 'prefillSettings' => ['lastname' => 'last_name'], - ]); - $actual = $viewHelper->render(); - self::assertSame('Doe', $actual); - } - - #[Test] - public function viewHelperReturnsSubmittedValueIfValidationError(): void - { - $submittedData = [ - 'tx_sfeventmgt_pieventregistration' => [ - 'registration' => ['firstname' => 'Torben'], - ], - ]; - - $frontendUser = $this->createMock(FrontendUserAuthentication::class); - $frontendUser->user = [ - 'first_name' => 'John', - 'last_name' => 'Doe', - ]; - $request = $this->createMock(Request::class); - $request->expects(self::any())->method('getControllerExtensionName')->willReturn('SfEventMgt'); - $request->expects(self::any())->method('getPluginName')->willReturn('Pieventregistration'); - $request->expects(self::any())->method('getParsedBody')->willReturn($submittedData); - $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 PrefillViewHelper(); - $viewHelper->setRenderingContext($renderingContext); - $viewHelper->setArguments([ - 'fieldname' => 'firstname', - 'prefillSettings' => ['firstname' => 'first_name'], - ]); - $actual = $viewHelper->render(); - self::assertSame('Torben', $actual); - } -}