-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Migrate PrefillFieldViewHelper
- Loading branch information
Showing
3 changed files
with
163 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
Tests/Functional/ViewHelpers/Registration/Field/PrefillFieldViewHelperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?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\Registration\Field; | ||
|
||
use DateTime; | ||
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field; | ||
use DERHANSEN\SfEventMgt\ViewHelpers\Registration\Field\PrefillFieldViewHelper; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
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\RenderingContext; | ||
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 PrefillFieldViewHelperTest extends FunctionalTestCase | ||
{ | ||
protected array $testExtensionsToLoad = ['typo3conf/ext/sf_event_mgt']; | ||
|
||
#[Test] | ||
public function viewHelperReturnsFieldDefaultValue(): void | ||
{ | ||
$field = new Field(); | ||
$field->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('<e:registration.field.prefillField registrationField="{field}" />'); | ||
$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('<e:registration.field.prefillField registrationField="{field}" />'); | ||
$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('<e:registration.field.prefillField registrationField="{field}" />'); | ||
$context->getVariableProvider()->add('field', $field); | ||
$this->assertEquals($expected, (new TemplateView($context))->render()); | ||
} | ||
} |
130 changes: 0 additions & 130 deletions
130
Tests/Unit/ViewHelpers/Registration/Field/PrefillFieldViewHelperTest.php
This file was deleted.
Oops, something went wrong.