From 155d8609bcb355aed056c78b81900259712af8ec Mon Sep 17 00:00:00 2001 From: Torben Hansen Date: Sun, 1 Sep 2024 12:20:47 +0200 Subject: [PATCH] [TASK] Avoid TSFE usage in PrefillViewHelper --- Classes/ViewHelpers/PrefillViewHelper.php | 17 ++-- .../ViewHelpers/PrefillViewHelperTest.php | 89 ++++++++++--------- 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/Classes/ViewHelpers/PrefillViewHelper.php b/Classes/ViewHelpers/PrefillViewHelper.php index f8bc8323a..326b080a0 100644 --- a/Classes/ViewHelpers/PrefillViewHelper.php +++ b/Classes/ViewHelpers/PrefillViewHelper.php @@ -11,11 +11,10 @@ namespace DERHANSEN\SfEventMgt\ViewHelpers; +use Psr\Http\Message\ServerRequestInterface; use TYPO3\CMS\Extbase\Mvc\Request; +use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; -/** - * Prefill ViewHelper - */ class PrefillViewHelper extends AbstractPrefillViewHelper { public function initializeArguments(): void @@ -35,18 +34,24 @@ public function render(): string $prefillSettings = $this->arguments['prefillSettings']; /** @var Request $request */ - $request = $this->renderingContext->getRequest(); + $request = $this->renderingContext->getAttribute(ServerRequestInterface::class); $registrationData = $request->getParsedBody()[$this->getPluginNamespace($request)] ?? []; if (isset($registrationData['registration'][$fieldname])) { return $registrationData['registration'][$fieldname]; } - if (!isset($GLOBALS['TSFE']) || !$GLOBALS['TSFE']->fe_user->user || empty($prefillSettings) || + $frontendUser = $this->getFrontendUser(); + if (!$frontendUser->user || empty($prefillSettings) || !array_key_exists($fieldname, $prefillSettings) ) { return ''; } - return (string)($GLOBALS['TSFE']->fe_user->user[$prefillSettings[$fieldname]]); + return (string)($frontendUser->user[$prefillSettings[$fieldname]]); + } + + protected function getFrontendUser(): FrontendUserAuthentication + { + return $this->renderingContext->getAttribute(ServerRequestInterface::class)->getAttribute('frontend.user'); } } diff --git a/Tests/Unit/ViewHelpers/PrefillViewHelperTest.php b/Tests/Unit/ViewHelpers/PrefillViewHelperTest.php index 694a98b19..962f57c30 100644 --- a/Tests/Unit/ViewHelpers/PrefillViewHelperTest.php +++ b/Tests/Unit/ViewHelpers/PrefillViewHelperTest.php @@ -16,19 +16,24 @@ use stdClass; use TYPO3\CMS\Extbase\Mvc\Request; use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext; +use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; +use function PHPUnit\Framework\any; + /** * Test case for prefill viewHelper */ class PrefillViewHelperTest extends UnitTestCase { #[Test] - public function viewHelperReturnsEmptyStringIfTsfeNotAvailabe(): void + public function viewHelperReturnsEmptyStringIfFrontendUserNotAvailable(): void { - $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); - $renderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock(); - $renderingContext->expects(self::any())->method('getRequest')->willReturn($request); + $frontendUser = $this->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); @@ -48,14 +53,15 @@ public function viewHelperReturnsCurrentFieldValueIfValueInParsedBodyAvailable() 'registration' => ['firstname' => 'Torben'], ], ]; - $GLOBALS['TSFE'] = new stdClass(); - $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); + $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); - $renderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock(); - $renderingContext->expects(self::any())->method('getRequest')->willReturn($request); + $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); @@ -71,18 +77,18 @@ public function viewHelperReturnsCurrentFieldValueIfValueInParsedBodyAvailable() public function viewHelperReturnsEmptyStringIfPrefillSettingsEmpty(): void { $submittedData = []; - $GLOBALS['TSFE'] = new stdClass(); - $GLOBALS['TSFE']->fe_user = new stdClass(); - $GLOBALS['TSFE']->fe_user->user = [ + + $frontendUser = $this->createMock(FrontendUserAuthentication::class); + $frontendUser->user = [ 'first_name' => 'John', ]; - - $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); + $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); - $renderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock(); - $renderingContext->expects(self::any())->method('getRequest')->willReturn($request); + $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); @@ -98,18 +104,17 @@ public function viewHelperReturnsEmptyStringIfPrefillSettingsEmpty(): void public function viewHelperReturnsEmptyStringIfFieldNotFoundInPrefillSettings(): void { $submittedData = []; - $GLOBALS['TSFE'] = new stdClass(); - $GLOBALS['TSFE']->fe_user = new stdClass(); - $GLOBALS['TSFE']->fe_user->user = [ + $frontendUser = $this->createMock(FrontendUserAuthentication::class); + $frontendUser->user = [ 'first_name' => 'John', ]; - - $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); + $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); - $renderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock(); - $renderingContext->expects(self::any())->method('getRequest')->willReturn($request); + $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); @@ -124,18 +129,16 @@ public function viewHelperReturnsEmptyStringIfFieldNotFoundInPrefillSettings(): #[Test] public function viewHelperReturnsEmptyStringIfFieldNotFoundInFeUser(): void { - $GLOBALS['TSFE'] = new stdClass(); - $GLOBALS['TSFE']->fe_user = new stdClass(); - $GLOBALS['TSFE']->fe_user->user = [ + $frontendUser = $this->createMock(FrontendUserAuthentication::class); + $frontendUser->user = [ 'first_name' => 'John', ]; - - $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); + $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([]); - $renderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock(); - $renderingContext->expects(self::any())->method('getRequest')->willReturn($request); + $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); @@ -150,19 +153,17 @@ public function viewHelperReturnsEmptyStringIfFieldNotFoundInFeUser(): void #[Test] public function viewHelperReturnsFieldvalueIfFound(): void { - $GLOBALS['TSFE'] = new stdClass(); - $GLOBALS['TSFE']->fe_user = new stdClass(); - $GLOBALS['TSFE']->fe_user->user = [ + $frontendUser = $this->createMock(FrontendUserAuthentication::class); + $frontendUser->user = [ 'first_name' => 'John', 'last_name' => 'Doe', ]; - - $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); + $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([]); - $renderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock(); - $renderingContext->expects(self::any())->method('getRequest')->willReturn($request); + $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); @@ -183,12 +184,18 @@ public function viewHelperReturnsSubmittedValueIfValidationError(): void ], ]; - $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); + $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); - $renderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock(); - $renderingContext->expects(self::any())->method('getRequest')->willReturn($request); + $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);