Skip to content

Commit

Permalink
[TASK] Avoid TSFE usage in PrefillViewHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
derhansen committed Sep 1, 2024
1 parent 9901330 commit 155d860
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 47 deletions.
17 changes: 11 additions & 6 deletions Classes/ViewHelpers/PrefillViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}
}
89 changes: 48 additions & 41 deletions Tests/Unit/ViewHelpers/PrefillViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 155d860

Please sign in to comment.