‪TYPO3CMS  ‪main
TextfieldViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
29 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
30 use TYPO3Fluid\Fluid\View\TemplateView;
31 
32 final class ‪TextfieldViewHelperTest extends FunctionalTestCase
33 {
34  public static function ‪renderDataProvider(): array
35  {
36  return [
37  'renderCorrectlySetsTagName' => [
38  '<f:form.textfield />',
39  '<input type="text" name="" />',
40  ],
41  'renderCorrectlySetsTypeNameAndValueAttributes' => [
42  '<f:form.textfield name="NameOfTextfield" value="Current value" type="text" />',
43  '<input type="text" name="NameOfTextfield" value="Current value" />',
44  ],
45  'renderAddsPlaceholder' => [
46  '<f:form.textfield name="NameOfTextfield" placeholder="SomePlaceholder" />',
47  '<input placeholder="SomePlaceholder" type="text" name="NameOfTextfield" />',
48  ],
49  'renderCorrectlySetsRequiredAttribute' => [
50  '<f:form.textfield required="true" name="NameOfTextfield" />',
51  '<input type="text" name="NameOfTextfield" required="required" />',
52  ],
53  ];
54  }
55 
56  #[DataProvider('renderDataProvider')]
57  #[Test]
58  public function ‪render(string $template, string $expected): void
59  {
60  $context = $this->get(RenderingContextFactory::class)->create();
61  $context->getTemplatePaths()->setTemplateSource($template);
62  $serverRequest = (new ‪ServerRequest())->withAttribute('extbase', new ‪ExtbaseRequestParameters());
63  $context->setRequest(new ‪Request($serverRequest));
64  self::assertSame($expected, (new TemplateView($context))->‪render());
65  }
66 
67  #[Test]
68  public function ‪renderCallsSetErrorClassAttribute(): void
69  {
70  // Create an extbase request that contains mapping results of the form object property we're working with.
71  $mappingResult = new ‪Result();
72  $objectResult = $mappingResult->forProperty('myObjectName');
73  $propertyResult = $objectResult->forProperty('someProperty');
74  $propertyResult->addError(new ‪Error('invalidProperty', 2));
75  $extbaseRequestParameters = new ‪ExtbaseRequestParameters();
76  $extbaseRequestParameters->setOriginalRequestMappingResults($mappingResult);
77  $psr7Request = (new ‪ServerRequest())->withAttribute('extbase', $extbaseRequestParameters)
78  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
79  ‪$GLOBALS['TYPO3_REQUEST'] = $psr7Request;
80  $extbaseRequest = new ‪Request($psr7Request);
81 
82  $formObject = new \stdClass();
83  $context = $this->get(RenderingContextFactory::class)->create();
84  $context->getTemplatePaths()->setTemplateSource('<f:form object="{formObject}" fieldNamePrefix="myFieldPrefix" objectName="myObjectName"><f:form.textfield property="someProperty" errorClass="myError" /></f:form>');
85  $context->setRequest($extbaseRequest);
86  $view = new TemplateView($context);
87  $view->assign('formObject', $formObject);
88  // The point is that 'class="myError"' is added since the form had mapping errors for this property.
89  self::assertStringContainsString('<input type="text" name="myFieldPrefix[myObjectName][someProperty]" class="myError" />', $view->render());
90  }
91 }
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Form\TextfieldViewHelperTest\renderCallsSetErrorClassAttribute
‪renderCallsSetErrorClassAttribute()
Definition: TextfieldViewHelperTest.php:68
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Form\TextfieldViewHelperTest\render
‪render(string $template, string $expected)
Definition: TextfieldViewHelperTest.php:58
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Form
Definition: ButtonViewHelperTest.php:18
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Form\TextfieldViewHelperTest\renderDataProvider
‪static renderDataProvider()
Definition: TextfieldViewHelperTest.php:34
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Form\TextfieldViewHelperTest
Definition: TextfieldViewHelperTest.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters
Definition: ExtbaseRequestParameters.php:35
‪TYPO3\CMS\Extbase\Mvc\Request
Definition: Request.php:35