‪TYPO3CMS  ‪main
TextValidatorTest.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;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 final class ‪TextValidatorTest extends FunctionalTestCase
30 {
31  protected function ‪setUp(): void
32  {
33  parent::setUp();
34  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->create('default');
35  $request = (new ‪ServerRequest())->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
36  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
37  }
38 
39  public static function ‪isValidDataProvider(): array
40  {
41  return [
42  'a simple string' => [
43  false, // expectation: no error
44  'this is a very simple string', // test string
45  ],
46  'allow new line character' => [
47  false,
48  'Ierd Frot uechter mä get, Kirmesdag' . chr(10) . 'Ke kille Minutt',
49  ],
50  'allow single quote' => [
51  false,
52  'foo \' bar',
53  ],
54  'allow double quote' => [
55  false,
56  'foo " bar',
57  ],
58  'slash' => [
59  false,
60  'foo/bar',
61  ],
62  'slash with closing angle bracket' => [
63  false,
64  'foo/>bar',
65  ],
66  'closing angle bracket without opening angle bracket' => [
67  false,
68  '>foo',
69  ],
70  'common special characters' => [
71  false,
72  '3% of most people tend to use semikolae; we need to check & allow that. And hashes (#) are not evil either, nor is the sign called \'quote\'.',
73  ],
74  'nul byte' => [
75  true,
76  'foo' . chr(0) . 'bar',
77  ],
78  'a string with html' => [
79  true,
80  '<span style="color: #BBBBBB;">a nice text</span>',
81  ],
82  'not closed html' => [
83  true,
84  '<foo>bar',
85  ],
86  'opening angle bracket' => [
87  true,
88  '<foo', // @todo: This is odd. It means a simple opening bracket makes this validator fail.
89  ],
90  ];
91  }
92 
93  #[DataProvider('isValidDataProvider')]
94  #[Test]
95  public function ‪isValidHasNoError(bool $expectation, string $testString): void
96  {
98  ‪$validator->setOptions([]);
99  self::assertSame($expectation, ‪$validator->validate($testString)->hasErrors());
100  }
101 
102  #[Test]
104  {
106  ‪$validator->setOptions([]);
107  // we only test for the error code, after the translation Method for message is mocked anyway
108  $expected = [new ‪Error('The given subject was not a valid text (e.g. contained XML tags).', 1221565786)];
109  self::assertEquals($expected, ‪$validator->validate('<span style="color: #BBBBBB;">a nice text</span>')->getErrors());
110  }
111 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator
Definition: AlphanumericValidatorTest.php:18
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\TextValidatorTest
Definition: TextValidatorTest.php:30
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Extbase\Validation\Validator\TextValidator
Definition: TextValidator.php:24
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\TextValidatorTest\isValidDataProvider
‪static isValidDataProvider()
Definition: TextValidatorTest.php:39
‪TYPO3\CMS\Extbase\Validation\Error
Definition: Error.php:22
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:262
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\TextValidatorTest\isValidHasNoError
‪isValidHasNoError(bool $expectation, string $testString)
Definition: TextValidatorTest.php:95
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\TextValidatorTest\textValidatorCreatesTheCorrectErrorIfTheSubjectContainsHtmlEntities
‪textValidatorCreatesTheCorrectErrorIfTheSubjectContainsHtmlEntities()
Definition: TextValidatorTest.php:103
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\TextValidatorTest\setUp
‪setUp()
Definition: TextValidatorTest.php:31