‪TYPO3CMS  ‪main
RegularExpressionValidatorTest.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 ‪RegularExpressionValidatorTest 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  #[Test]
41  {
42  $options = ['regularExpression' => '/^simple[0-9]expression$/'];
44  ‪$validator->setOptions($options);
45  self::assertFalse(‪$validator->validate('simple1expression')->hasErrors());
46  self::assertTrue(‪$validator->validate('simple1expressions')->hasErrors());
47  }
48 
49  #[Test]
51  {
52  $options = ['regularExpression' => '/^simple[0-9]expression$/'];
54  ‪$validator->setOptions($options);
55  ‪$errors = ‪$validator->validate('some subject that will not match')->getErrors();
56  // we only test for the error code, after the translation Method for message is mocked anyway
57  self::assertEquals([new ‪Error('The given subject did not match the pattern.', 1221565130)], ‪$errors);
58  }
59 
60  #[Test]
61  public function ‪customErrorMessageIsRespected(): void
62  {
63  $options = [
64  'regularExpression' => '/^simple[0-9]expression$/',
65  'errorMessage' => 'custom message',
66  ];
68  ‪$validator->setOptions($options);
69  $result = ‪$validator->validate('some subject that will not match');
70  self::assertTrue($result->hasErrors());
71  ‪$errors = $result->getErrors();
72  self::assertEquals([new ‪Error('custom message', 1221565130)], ‪$errors);
73  }
74 
75  public static function ‪customErrorMessagesDataProvider(): array
76  {
77  return [
78  'no message' => [
79  '',
80  'The given subject did not match the pattern.',
81  ],
82  'translation key' => [
83  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.recordInformation',
84  'Record information',
85  ],
86  'static message' => [
87  'some static custom message',
88  'some static custom message',
89  ],
90  ];
91  }
92 
93  #[DataProvider('customErrorMessagesDataProvider')]
94  #[Test]
95  public function ‪translatableErrorMessageContainsDefaultValue(string $input, string $expected): void
96  {
97  $options = [
98  'regularExpression' => '/^simple[0-9]expression$/',
99  ];
100  if ($input) {
101  $options['errorMessage'] = $input;
102  }
103  $subject = new ‪RegularExpressionValidator();
104  $subject->setOptions($options);
105  $result = $subject->validate('some subject that will not match');
106  self::assertTrue($result->hasErrors());
107  ‪$errors = $result->getErrors();
108  self::assertEquals([new ‪Error($expected, 1221565130)], ‪$errors);
109  }
110 }
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\regularExpressionValidatorCreatesTheCorrectErrorIfTheExpressionDidNotMatch
‪regularExpressionValidatorCreatesTheCorrectErrorIfTheExpressionDidNotMatch()
Definition: RegularExpressionValidatorTest.php:50
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator
Definition: AlphanumericValidatorTest.php:18
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator
Definition: RegularExpressionValidator.php:26
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\regularExpressionValidatorMatchesABasicExpressionCorrectly
‪regularExpressionValidatorMatchesABasicExpressionCorrectly()
Definition: RegularExpressionValidatorTest.php:40
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Validation\Error
Definition: Error.php:22
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:262
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\customErrorMessageIsRespected
‪customErrorMessageIsRespected()
Definition: RegularExpressionValidatorTest.php:61
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\setUp
‪setUp()
Definition: RegularExpressionValidatorTest.php:31
‪$errors
‪$errors
Definition: annotationChecker.php:116
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest
Definition: RegularExpressionValidatorTest.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\customErrorMessagesDataProvider
‪static customErrorMessagesDataProvider()
Definition: RegularExpressionValidatorTest.php:75
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\translatableErrorMessageContainsDefaultValue
‪translatableErrorMessageContainsDefaultValue(string $input, string $expected)
Definition: RegularExpressionValidatorTest.php:95