[TASK] Change namespace of testing framework
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / StringLengthValidatorTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
3
4 /* *
5 * This script belongs to the Extbase framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
24
25 /**
26 * Test case
27 */
28 class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
29 {
30 /**
31 * @var string
32 */
33 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator::class;
34
35 public function setup()
36 {
37 $this->validator = $this->getMockBuilder($this->validatorClassName)
38 ->setMethods(['translateErrorMessage'])
39 ->getMock();
40 }
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator
44 */
45 protected $validator;
46
47 /**
48 * @test
49 */
50 public function validateReturnsNoErrorIfTheGivenValueIsNull()
51 {
52 $this->assertFalse($this->validator->validate(null)->hasErrors());
53 }
54
55 /**
56 * @test
57 */
58 public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
59 {
60 $this->assertFalse($this->validator->validate('')->hasErrors());
61 }
62
63 /**
64 * @test
65 */
66 public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
67 {
68 $options = ['minimum' => 0, 'maximum' => 50];
69 $validator = $this->getMockBuilder($this->validatorClassName)
70 ->setMethods(['dummy'])
71 ->setConstructorArgs([$options])
72 ->getMock();
73 $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
74 }
75
76 /**
77 * @test
78 */
79 public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
80 {
81 $options = ['minimum' => 50, 'maximum' => 100];
82 $validator = $this->getMockBuilder($this->validatorClassName)
83 ->setMethods(['translateErrorMessage'])
84 ->setConstructorArgs([$options])
85 ->getMock();
86 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
87 }
88
89 /**
90 * @test
91 */
92 public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
93 {
94 $options = ['minimum' => 5, 'maximum' => 10];
95 $validator = $this->getMockBuilder($this->validatorClassName)
96 ->setMethods(['translateErrorMessage'])
97 ->setConstructorArgs([$options])
98 ->getMock();
99 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
100 }
101
102 /**
103 * @test
104 */
105 public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
106 {
107 $options = ['minimum' => 5];
108 $validator = $this->getMockBuilder($this->validatorClassName)
109 ->setMethods(['dummy'])
110 ->setConstructorArgs([$options])
111 ->getMock();
112 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
113 }
114
115 /**
116 * @test
117 */
118 public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
119 {
120 $options = ['maximum' => 100];
121 $validator = $this->getMockBuilder($this->validatorClassName)
122 ->setMethods(['dummy'])
123 ->setConstructorArgs([$options])
124 ->getMock();
125 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
126 }
127
128 /**
129 * @test
130 */
131 public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
132 {
133 $options = ['maximum' => 10];
134 $validator = $this->getMockBuilder($this->validatorClassName)
135 ->setMethods(['dummy'])
136 ->setConstructorArgs([$options])
137 ->getMock();
138 $this->assertFalse($validator->validate('1234567890')->hasErrors());
139 }
140
141 /**
142 * @test
143 */
144 public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
145 {
146 $options = ['minimum' => 10];
147 $validator = $this->getMockBuilder($this->validatorClassName)
148 ->setMethods(['dummy'])
149 ->setConstructorArgs([$options])
150 ->getMock();
151 $this->assertFalse($validator->validate('1234567890')->hasErrors());
152 }
153
154 /**
155 * @test
156 */
157 public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
158 {
159 $options = ['minimum' => 10, 'maximum' => 10];
160 $validator = $this->getMockBuilder($this->validatorClassName)
161 ->setMethods(['dummy'])
162 ->setConstructorArgs([$options])
163 ->getMock();
164 $this->assertFalse($validator->validate('1234567890')->hasErrors());
165 }
166
167 /**
168 * @test
169 */
170 public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
171 {
172 $options = ['minimum' => 1, 'maximum' => 10];
173 $validator = $this->getMockBuilder($this->validatorClassName)
174 ->setMethods(['dummy'])
175 ->setConstructorArgs([$options])
176 ->getMock();
177 $this->assertFalse($validator->validate('1234567890')->hasErrors());
178 }
179
180 /**
181 * @test
182 */
183 public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
184 {
185 $options = ['minimum' => 10, 'maximum' => 100];
186 $validator = $this->getMockBuilder($this->validatorClassName)
187 ->setMethods(['dummy'])
188 ->setConstructorArgs([$options])
189 ->getMock();
190 $this->assertFalse($validator->validate('1234567890')->hasErrors());
191 }
192
193 /**
194 * @test
195 */
196 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength()
197 {
198 $this->expectException(InvalidValidationOptionsException::class);
199 $this->expectExceptionCode(1238107096);
200 $options = ['minimum' => 101, 'maximum' => 100];
201 $validator = $this->getMockBuilder($this->validatorClassName)
202 ->setMethods(['addError', 'translateErrorMessage'])
203 ->setConstructorArgs([$options])
204 ->getMock();
205 $validator->validate('1234567890');
206 }
207
208 /**
209 * @test
210 */
211 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
212 {
213 $options = ['minimum' => 50, 'maximum' => 100];
214 $validator = $this->getMockBuilder($this->validatorClassName)
215 ->setMethods(['translateErrorMessage'])
216 ->setConstructorArgs([$options])
217 ->getMock();
218 $this->assertEquals(1, count($validator->validate('this is a very short string')->getErrors()));
219 }
220
221 /**
222 * @test
223 */
224 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
225 {
226 $options = ['minimum' => 5, 'maximum' => 100];
227 $validator = $this->getMockBuilder($this->validatorClassName)
228 ->setMethods(['addError', 'translateErrorMessage'])
229 ->setConstructorArgs([$options])
230 ->getMock();
231
232 $className = $this->getUniqueId('TestClass');
233
234 eval('
235 class ' . $className . ' {
236 public function __toString() {
237 return \'some string\';
238 }
239 }
240 ');
241
242 $object = new $className();
243 $this->assertFalse($validator->validate($object)->hasErrors());
244 }
245
246 /**
247 * @test
248 */
249 public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
250 {
251 $options = ['minimum' => 5, 'maximum' => 100];
252 $validator = $this->getMockBuilder($this->validatorClassName)
253 ->setMethods(['translateErrorMessage'])
254 ->setConstructorArgs([$options])
255 ->getMock();
256
257 $className = $this->getUniqueId('TestClass');
258
259 eval('
260 class ' . $className . ' {
261 protected $someProperty;
262 }
263 ');
264
265 $object = new $className();
266 $this->assertTrue($validator->validate($object)->hasErrors());
267 }
268
269 /**
270 * @test
271 */
272 public function validateRegardsMultibyteStringsCorrectly()
273 {
274 $options = ['minimum' => 0, 'maximum' => 8];
275 $validator = $this->getMockBuilder($this->validatorClassName)
276 ->setMethods(['dummy'])
277 ->setConstructorArgs([$options])
278 ->getMock();
279 $this->assertFalse($validator->validate('├╝berlang')->hasErrors());
280 }
281 }