‪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 
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
27 final class ‪RegularExpressionValidatorTest extends FunctionalTestCase
28 {
29  protected function ‪setUp(): void
30  {
31  parent::setUp();
32  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->create('default');
33  $request = (new ‪ServerRequest())->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
34  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
35  }
36 
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 
53  {
54  $options = ['regularExpression' => '/^simple[0-9]expression$/'];
56  ‪$validator->setOptions($options);
57  ‪$errors = ‪$validator->validate('some subject that will not match')->getErrors();
58  // we only test for the error code, after the translation Method for message is mocked anyway
59  self::assertEquals([new ‪Error('The given subject did not match the pattern.', 1221565130)], ‪$errors);
60  }
61 
65  public function ‪customErrorMessageIsRespected(): void
66  {
67  $options = [
68  'regularExpression' => '/^simple[0-9]expression$/',
69  'errorMessage' => 'custom message',
70  ];
72  ‪$validator->setOptions($options);
73  $result = ‪$validator->validate('some subject that will not match');
74  self::assertTrue($result->hasErrors());
75  ‪$errors = $result->getErrors();
76  self::assertEquals([new ‪Error('custom message', 1221565130)], ‪$errors);
77  }
78 
79  public static function ‪customErrorMessagesDataProvider(): array
80  {
81  return [
82  'no message' => [
83  '',
84  'The given subject did not match the pattern.',
85  ],
86  'translation key' => [
87  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.recordInformation',
88  'Record information',
89  ],
90  'static message' => [
91  'some static custom message',
92  'some static custom message',
93  ],
94  ];
95  }
96 
101  public function ‪translatableErrorMessageContainsDefaultValue(string $input, string $expected): void
102  {
103  $options = [
104  'regularExpression' => '/^simple[0-9]expression$/',
105  ];
106  if ($input) {
107  $options['errorMessage'] = $input;
108  }
109  $subject = new ‪RegularExpressionValidator();
110  $subject->setOptions($options);
111  $result = $subject->validate('some subject that will not match');
112  self::assertTrue($result->hasErrors());
113  ‪$errors = $result->getErrors();
114  self::assertEquals([new ‪Error($expected, 1221565130)], ‪$errors);
115  }
116 }
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\regularExpressionValidatorCreatesTheCorrectErrorIfTheExpressionDidNotMatch
‪regularExpressionValidatorCreatesTheCorrectErrorIfTheExpressionDidNotMatch()
Definition: RegularExpressionValidatorTest.php:52
‪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:65
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\setUp
‪setUp()
Definition: RegularExpressionValidatorTest.php:29
‪$errors
‪$errors
Definition: annotationChecker.php:121
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest
Definition: RegularExpressionValidatorTest.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\customErrorMessagesDataProvider
‪static customErrorMessagesDataProvider()
Definition: RegularExpressionValidatorTest.php:79
‪TYPO3\CMS\Extbase\Tests\Functional\Validation\Validator\RegularExpressionValidatorTest\translatableErrorMessageContainsDefaultValue
‪translatableErrorMessageContainsDefaultValue(string $input, string $expected)
Definition: RegularExpressionValidatorTest.php:101