Skip to content

Commit

Permalink
[TASK] Migrate PrefillViewHelper tests to functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
derhansen committed Sep 1, 2024
1 parent efe0d74 commit 340883b
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 223 deletions.
16 changes: 8 additions & 8 deletions .Build/phpunit/FunctionalTests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
backupGlobals="true"
bootstrap="../../.Build/vendor/typo3/testing-framework/Resources/Core/Build/FunctionalTestsBootstrap.php"
cacheDirectory=".phpunit.cache"
cacheResult="false"
colors="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
beStrictAboutTestsThatDoNotTestAnything="false"
failOnWarning="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
failOnDeprecation="true"
failOnNotice="true"
failOnRisky="true"
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="false"
failOnWarning="true"
>
<coverage/>
<testsuites>
Expand Down
7 changes: 1 addition & 6 deletions Classes/ViewHelpers/PrefillViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
) {
Expand All @@ -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');
}
}
227 changes: 227 additions & 0 deletions Tests/Functional/ViewHelpers/PrefillViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?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;

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\RenderingContextFactory;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use TYPO3Fluid\Fluid\View\TemplateView;

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

#[Test]
public function viewHelperReturnsEmptyStringIfFrontendUserNotAvailable(): void
{
$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:prefill fieldname="firstname" prefillSettings="{settings.registration.prefillFields}" />');
$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('<e:prefill fieldname="firstname" prefillSettings="{settings.registration.prefillFields}" />');
$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('<e:prefill fieldname="firstname" prefillSettings="{settings.registration.prefillFields}" />');
$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('<e:prefill fieldname="firstname" prefillSettings="{settings.registration.prefillFields}" />');
$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('<e:prefill fieldname="non_existing" prefillSettings="{settings.registration.prefillFields}" />');
$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('<e:prefill fieldname="firstname" prefillSettings="{settings.registration.prefillFields}" />');
$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('<e:prefill fieldname="firstname" prefillSettings="{settings.registration.prefillFields}" />');
$context->getVariableProvider()->add('settings', $settings);
$this->assertEquals('Torben', (new TemplateView($context))->render());
}
}
Loading

0 comments on commit 340883b

Please sign in to comment.