Skip to content

Commit

Permalink
[TASK] Migrate ErrorClassViewHelper 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 4c46948 commit 68ff79b
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 163 deletions.
10 changes: 5 additions & 5 deletions Classes/ViewHelpers/Validation/ErrorClassViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace DERHANSEN\SfEventMgt\ViewHelpers\Validation;

use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
Expand All @@ -22,8 +22,8 @@ class ErrorClassViewHelper extends AbstractViewHelper
{
public function initializeArguments(): void
{
$this->registerArgument('fieldname', 'string', 'A fieldname to be checked', false);
$this->registerArgument('registrationField', 'object', 'A registration field record', false);
$this->registerArgument('fieldname', 'string', 'A fieldname to be checked');
$this->registerArgument('registrationField', 'object', 'A registration field record');
$this->registerArgument('class', 'string', 'Classname if the field has an error', false, 'error-class');
parent::initializeArguments();
}
Expand Down Expand Up @@ -54,8 +54,8 @@ public function render(): string

protected function getValidationErrors(): array
{
/** @var ExtbaseRequestParameters $extbaseRequestParameters */
$extbaseRequestParameters = $this->renderingContext->getRequest()->getAttribute('extbase');
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
$extbaseRequestParameters = $request->getAttribute('extbase');
$validationResults = $extbaseRequestParameters->getOriginalRequestMappingResults();

return $validationResults->getFlattenedErrors();
Expand Down
163 changes: 163 additions & 0 deletions Tests/Functional/ViewHelpers/Validation/ErrorClassViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?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\Validation;

use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
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\Error\Error;
use TYPO3\CMS\Extbase\Error\Result;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use TYPO3Fluid\Fluid\View\TemplateView;

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

public static function fieldnameDataProvider(): array
{
return [
'No fieldname' => [
'',
'',
'error-class',
'',
'',
],
'No error for fieldname' => [
'firstname',
'',
'error-class',
'registration.lastname',
'Error message for registration.lastname',
],
'Error for fieldname with default class name' => [
'firstname',
'error-class',
'error-class',
'registration.firstname',
'Error message for registration.firstname',
],
'Error for fieldname with custom class name' => [
'firstname',
'custom-class',
'custom-class',
'registration.firstname',
'Error message for registration.firstname'
],
];
}

#[DataProvider('fieldnameDataProvider')]
#[Test]
public function viewHelperReturnsExpectedStringForFieldname(
string $fieldname,
string $expected,
string $errorClass,
string $errorForProperty,
string $errorMessageForProperty
): void {
$result = new Result();
if ($errorForProperty) {
$result->forProperty($errorForProperty)->addError(new Error($errorMessageForProperty, 1234567890));
}

$extbaseRequestParameters = new ExtbaseRequestParameters();
$extbaseRequestParameters->setOriginalRequestMappingResults($result);

$serverRequest = new ServerRequest();
$serverRequest = $serverRequest->withAttribute('extbase', $extbaseRequestParameters)
->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:validation.errorClass fieldname="{fieldname}" class="{class}" />');
$context->getVariableProvider()->add('fieldname', $fieldname);
$context->getVariableProvider()->add('class', $errorClass);
$result = (new TemplateView($context))->render();

self::assertEquals($expected, $result);
}

public static function registrationFieldDataProvider(): array
{
$field = new Field();
$field->_setProperty('uid', 2);

return [
'No registration field' => [
null,
'',
'error-class',
'',
''
],
'No error for registration field' => [
$field,
'',
'error-class',
'registration.fields.1',
'Error message for registration.fields.1'
],
'Error for fieldname with default class name' => [
$field,
'error-class',
'error-class',
'registration.fields.2',
'Error message for registration.fields.1'
],
'Error for fieldname with custom class name' => [
$field,
'custom-class',
'custom-class',
'registration.fields.2',
'Error message for registration.fields.1'
],
];
}

#[DataProvider('registrationFieldDataProvider')]
#[Test]
public function viewHelperReturnsExpectedStringForRegistrationField(
?Field $registrationField,
string $expected,
string $errorClass,
string $errorForProperty,
string $errorMessageForProperty
): void {
$result = new Result();
if ($errorForProperty) {
$result->forProperty($errorForProperty)->addError(new Error($errorMessageForProperty, 1234567890));
}

$extbaseRequestParameters = new ExtbaseRequestParameters();
$extbaseRequestParameters->setOriginalRequestMappingResults($result);

$serverRequest = new ServerRequest();
$serverRequest = $serverRequest->withAttribute('extbase', $extbaseRequestParameters)
->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:validation.errorClass registrationField="{registrationField}" class="{class}" />');
$context->getVariableProvider()->add('registrationField', $registrationField);
$context->getVariableProvider()->add('class', $errorClass);
$result = (new TemplateView($context))->render();

self::assertEquals($expected, $result);
}
}
158 changes: 0 additions & 158 deletions Tests/Unit/ViewHelpers/Validation/ErrorClassViewHelperTest.php

This file was deleted.

0 comments on commit 68ff79b

Please sign in to comment.