[FEATURE] Add additional configuration for external URLs
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / StringLengthValidatorTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
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
18 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
19 use TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class StringLengthValidatorTest extends UnitTestCase
26 {
27 /**
28 * @test
29 */
30 public function validateReturnsNoErrorIfTheGivenValueIsNull()
31 {
32 self::assertFalse((new StringLengthValidator())->validate(null)->hasErrors());
33 }
34
35 /**
36 * @test
37 */
38 public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
39 {
40 self::assertFalse((new StringLengthValidator())->validate('')->hasErrors());
41 }
42
43 /**
44 * @test
45 */
46 public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
47 {
48 $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 50]);
49
50 self::assertFalse($validator->validate('this is a very simple string')->hasErrors());
51 }
52
53 /**
54 * @test
55 */
56 public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
57 {
58 /** @var StringLengthValidator $validator */
59 $validator = $this->getMockBuilder(StringLengthValidator::class)
60 ->setMethods(['translateErrorMessage'])
61 ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
62 ->getMock();
63
64 self::assertTrue($validator->validate('this is a very short string')->hasErrors());
65 }
66
67 /**
68 * @test
69 */
70 public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
71 {
72 /** @var StringLengthValidator $validator */
73 $validator = $this->getMockBuilder(StringLengthValidator::class)
74 ->setMethods(['translateErrorMessage'])
75 ->setConstructorArgs([['minimum' => 5, 'maximum' => 10]])
76 ->getMock();
77
78 self::assertTrue($validator->validate('this is a very short string')->hasErrors());
79 }
80
81 /**
82 * @test
83 */
84 public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
85 {
86 $validator = new StringLengthValidator(['minimum' => 5]);
87
88 self::assertFalse($validator->validate('this is a very short string')->hasErrors());
89 }
90
91 /**
92 * @test
93 */
94 public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
95 {
96 $validator = new StringLengthValidator(['maximum' => 100]);
97
98 self::assertFalse($validator->validate('this is a very short string')->hasErrors());
99 }
100
101 /**
102 * @test
103 */
104 public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
105 {
106 $validator = new StringLengthValidator(['maximum' => 10]);
107
108 self::assertFalse($validator->validate('1234567890')->hasErrors());
109 }
110
111 /**
112 * @test
113 */
114 public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
115 {
116 $validator = new StringLengthValidator(['minimum' => 10]);
117
118 self::assertFalse($validator->validate('1234567890')->hasErrors());
119 }
120
121 /**
122 * @test
123 */
124 public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
125 {
126 $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 10]);
127
128 self::assertFalse($validator->validate('1234567890')->hasErrors());
129 }
130
131 /**
132 * @test
133 */
134 public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
135 {
136 $validator = new StringLengthValidator(['minimum' => 1, 'maximum' => 10]);
137
138 self::assertFalse($validator->validate('1234567890')->hasErrors());
139 }
140
141 /**
142 * @test
143 */
144 public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
145 {
146 $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 100]);
147
148 self::assertFalse($validator->validate('1234567890')->hasErrors());
149 }
150
151 /**
152 * @test
153 */
154 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength()
155 {
156 $this->expectException(InvalidValidationOptionsException::class);
157 $this->expectExceptionCode(1238107096);
158
159 /** @var StringLengthValidator $validator */
160 $validator = $this->getMockBuilder(StringLengthValidator::class)
161 ->setMethods(['addError', 'translateErrorMessage'])
162 ->setConstructorArgs([['minimum' => 101, 'maximum' => 100]])
163 ->getMock();
164 $validator->validate('1234567890');
165 }
166
167 /**
168 * @test
169 */
170 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
171 {
172 /** @var StringLengthValidator $validator */
173 $validator = $this->getMockBuilder(StringLengthValidator::class)
174 ->setMethods(['translateErrorMessage'])
175 ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
176 ->getMock();
177
178 self::assertCount(1, $validator->validate('this is a very short string')->getErrors());
179 }
180
181 /**
182 * @test
183 */
184 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
185 {
186 $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
187 $object = new class() {
188 /** @return string */
189 public function __toString()
190 {
191 return 'some string';
192 }
193 };
194
195 self::assertFalse($validator->validate($object)->hasErrors());
196 }
197
198 /**
199 * @test
200 */
201 public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
202 {
203 $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
204 $object = new class() {
205 };
206
207 $result = $validator->validate($object);
208
209 self::assertTrue($result->hasErrors());
210 self::assertCount(1, $result->getErrors());
211
212 /** @var \TYPO3\CMS\Extbase\Validation\Error $error */
213 $error = current($result->getErrors());
214 self::assertSame(1238110957, $error->getCode());
215 }
216
217 /**
218 * @test
219 */
220 public function validateRegardsMultibyteStringsCorrectly()
221 {
222 $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 8]);
223 $result = $validator->validate('├╝berlang');
224
225 self::assertFalse($result->hasErrors());
226 }
227 }