[TASK] Testing Framework Extraction - Next Steps
[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\Components\TestingFramework\Core\UnitTestCase
29 {
30 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator::class;
31
32 public function setup()
33 {
34 $this->validator = $this->getMockBuilder($this->validatorClassName)
35 ->setMethods(['translateErrorMessage'])
36 ->getMock();
37 }
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator
41 */
42 protected $validator;
43
44 /**
45 * @test
46 */
47 public function validateReturnsNoErrorIfTheGivenValueIsNull()
48 {
49 $this->assertFalse($this->validator->validate(null)->hasErrors());
50 }
51
52 /**
53 * @test
54 */
55 public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
56 {
57 $this->assertFalse($this->validator->validate('')->hasErrors());
58 }
59
60 /**
61 * @test
62 */
63 public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
64 {
65 $options = ['minimum' => 0, 'maximum' => 50];
66 $validator = $this->getMockBuilder($this->validatorClassName)
67 ->setMethods(['dummy'])
68 ->setConstructorArgs([$options])
69 ->getMock();
70 $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
71 }
72
73 /**
74 * @test
75 */
76 public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
77 {
78 $options = ['minimum' => 50, 'maximum' => 100];
79 $validator = $this->getMockBuilder($this->validatorClassName)
80 ->setMethods(['translateErrorMessage'])
81 ->setConstructorArgs([$options])
82 ->getMock();
83 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
84 }
85
86 /**
87 * @test
88 */
89 public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
90 {
91 $options = ['minimum' => 5, 'maximum' => 10];
92 $validator = $this->getMockBuilder($this->validatorClassName)
93 ->setMethods(['translateErrorMessage'])
94 ->setConstructorArgs([$options])
95 ->getMock();
96 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
97 }
98
99 /**
100 * @test
101 */
102 public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
103 {
104 $options = ['minimum' => 5];
105 $validator = $this->getMockBuilder($this->validatorClassName)
106 ->setMethods(['dummy'])
107 ->setConstructorArgs([$options])
108 ->getMock();
109 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
110 }
111
112 /**
113 * @test
114 */
115 public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
116 {
117 $options = ['maximum' => 100];
118 $validator = $this->getMockBuilder($this->validatorClassName)
119 ->setMethods(['dummy'])
120 ->setConstructorArgs([$options])
121 ->getMock();
122 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
123 }
124
125 /**
126 * @test
127 */
128 public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
129 {
130 $options = ['maximum' => 10];
131 $validator = $this->getMockBuilder($this->validatorClassName)
132 ->setMethods(['dummy'])
133 ->setConstructorArgs([$options])
134 ->getMock();
135 $this->assertFalse($validator->validate('1234567890')->hasErrors());
136 }
137
138 /**
139 * @test
140 */
141 public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
142 {
143 $options = ['minimum' => 10];
144 $validator = $this->getMockBuilder($this->validatorClassName)
145 ->setMethods(['dummy'])
146 ->setConstructorArgs([$options])
147 ->getMock();
148 $this->assertFalse($validator->validate('1234567890')->hasErrors());
149 }
150
151 /**
152 * @test
153 */
154 public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
155 {
156 $options = ['minimum' => 10, 'maximum' => 10];
157 $validator = $this->getMockBuilder($this->validatorClassName)
158 ->setMethods(['dummy'])
159 ->setConstructorArgs([$options])
160 ->getMock();
161 $this->assertFalse($validator->validate('1234567890')->hasErrors());
162 }
163
164 /**
165 * @test
166 */
167 public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
168 {
169 $options = ['minimum' => 1, 'maximum' => 10];
170 $validator = $this->getMockBuilder($this->validatorClassName)
171 ->setMethods(['dummy'])
172 ->setConstructorArgs([$options])
173 ->getMock();
174 $this->assertFalse($validator->validate('1234567890')->hasErrors());
175 }
176
177 /**
178 * @test
179 */
180 public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
181 {
182 $options = ['minimum' => 10, 'maximum' => 100];
183 $validator = $this->getMockBuilder($this->validatorClassName)
184 ->setMethods(['dummy'])
185 ->setConstructorArgs([$options])
186 ->getMock();
187 $this->assertFalse($validator->validate('1234567890')->hasErrors());
188 }
189
190 /**
191 * @test
192 */
193 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength()
194 {
195 $this->expectException(InvalidValidationOptionsException::class);
196 $this->expectExceptionCode(1238107096);
197 $options = ['minimum' => 101, 'maximum' => 100];
198 $validator = $this->getMockBuilder($this->validatorClassName)
199 ->setMethods(['addError', 'translateErrorMessage'])
200 ->setConstructorArgs([$options])
201 ->getMock();
202 $validator->validate('1234567890');
203 }
204
205 /**
206 * @test
207 */
208 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
209 {
210 $options = ['minimum' => 50, 'maximum' => 100];
211 $validator = $this->getMockBuilder($this->validatorClassName)
212 ->setMethods(['translateErrorMessage'])
213 ->setConstructorArgs([$options])
214 ->getMock();
215 $this->assertEquals(1, count($validator->validate('this is a very short string')->getErrors()));
216 }
217
218 /**
219 * @test
220 */
221 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
222 {
223 $options = ['minimum' => 5, 'maximum' => 100];
224 $validator = $this->getMockBuilder($this->validatorClassName)
225 ->setMethods(['addError', 'translateErrorMessage'])
226 ->setConstructorArgs([$options])
227 ->getMock();
228
229 $className = $this->getUniqueId('TestClass');
230
231 eval('
232 class ' . $className . ' {
233 public function __toString() {
234 return \'some string\';
235 }
236 }
237 ');
238
239 $object = new $className();
240 $this->assertFalse($validator->validate($object)->hasErrors());
241 }
242
243 /**
244 * @test
245 */
246 public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
247 {
248 $options = ['minimum' => 5, 'maximum' => 100];
249 $validator = $this->getMockBuilder($this->validatorClassName)
250 ->setMethods(['translateErrorMessage'])
251 ->setConstructorArgs([$options])
252 ->getMock();
253
254 $className = $this->getUniqueId('TestClass');
255
256 eval('
257 class ' . $className . ' {
258 protected $someProperty;
259 }
260 ');
261
262 $object = new $className();
263 $this->assertTrue($validator->validate($object)->hasErrors());
264 }
265
266 /**
267 * @test
268 */
269 public function validateRegardsMultibyteStringsCorrectly()
270 {
271 // $this->validatorOptions(array('maximum' => 8));
272 // $this->assertFalse($this->validator->validate('├╝berlang')->hasErrors());
273 $this->markTestSkipped('Validator needs to be adjusted regarding multibyte char lengths.');
274 }
275 }