[TASK] Fix typos all over the core
[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
24 /**
25 * Testcase for the string length validator
26 *
27 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
28 */
29 class StringLengthValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\AbstractValidatorTestcase {
30
31 protected $validatorClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator';
32
33 /**
34 * @var \TYPO3\Flow\Validation\Validator\StringLengthValidator
35 */
36 protected $validator;
37
38 /**
39 * @test
40 */
41 public function validateReturnsNoErrorIfTheGivenValueIsNull() {
42 $this->assertFalse($this->validator->validate(NULL)->hasErrors());
43 }
44
45 /**
46 * @test
47 */
48 public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString() {
49 $this->assertFalse($this->validator->validate('')->hasErrors());
50 }
51
52 /**
53 * @test
54 */
55 public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength() {
56 $this->validatorOptions(array('minimum' => 0, 'maximum' => 50));
57 $this->assertFalse($this->validator->validate('this is a very simple string')->hasErrors());
58 }
59
60 /**
61 * @test
62 */
63 public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength() {
64 $this->validatorOptions(array('minimum' => 50, 'maximum' => 100));
65 $this->assertTrue($this->validator->validate('this is a very short string')->hasErrors());
66 }
67
68 /**
69 * @test
70 */
71 public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength() {
72 $this->validatorOptions(array('minimum' => 5, 'maximum' => 10));
73 $this->assertTrue($this->validator->validate('this is a very short string')->hasErrors());
74 }
75
76 /**
77 * @test
78 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
79 */
80 public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified() {
81 $this->validatorOptions(array('minimum' => 5));
82 $this->assertFalse($this->validator->validate('this is a very short string')->hasErrors());
83 }
84
85 /**
86 * @test
87 */
88 public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified() {
89 $this->validatorOptions(array('maximum' => 100));
90 $this->assertFalse($this->validator->validate('this is a very short string')->hasErrors());
91 }
92
93 /**
94 * @test
95 */
96 public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified() {
97 $this->validatorOptions(array('maximum' => 10));
98 $this->assertFalse($this->validator->validate('1234567890')->hasErrors());
99 }
100
101 /**
102 * @test
103 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
104 */
105 public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified() {
106 $this->validatorOptions(array('minimum' => 10));
107 $this->assertFalse($this->validator->validate('1234567890')->hasErrors());
108 }
109
110 /**
111 * @test
112 */
113 public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue() {
114 $this->validatorOptions(array('minimum' => 10, 'maximum' => 10));
115 $this->assertFalse($this->validator->validate('1234567890')->hasErrors());
116 }
117
118 /**
119 * @test
120 */
121 public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength() {
122 $this->validatorOptions(array('minimum' => 1, 'maximum' => 10));
123 $this->assertFalse($this->validator->validate('1234567890')->hasErrors());
124 }
125
126 /**
127 * @test
128 */
129 public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength() {
130 $this->validatorOptions(array('minimum' => 10, 'maximum' => 100));
131 $this->assertFalse($this->validator->validate('1234567890')->hasErrors());
132 }
133
134 /**
135 * @test
136 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
137 */
138 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength() {
139 $this->validator = $this->getMock('TYPO3\Flow\Validation\Validator\StringLengthValidator', array('addError'), array(), '', FALSE);
140 $this->validatorOptions(array('minimum' => 101, 'maximum' => 100));
141 $this->validator->validate('1234567890');
142 }
143
144 /**
145 * @test
146 */
147 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails() {
148 $this->validatorOptions(array('minimum' => 50, 'maximum' => 100));
149
150 $this->assertEquals(1, count($this->validator->validate('this is a very short string')->getErrors()));
151 }
152
153 /**
154 * @test
155 */
156 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod() {
157 $this->validator = $this->getMock('TYPO3\Flow\Validation\Validator\StringLengthValidator', array('addError'), array(), '', FALSE);
158 $this->validatorOptions(array('minimum' => 5, 'maximum' => 100));
159
160 $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
161
162 eval('
163 class ' . $className . ' {
164 public function __toString() {
165 return \'some string\';
166 }
167 }
168 ');
169
170 $object = new $className();
171 $this->assertFalse($this->validator->validate($object)->hasErrors());
172 }
173
174 /**
175 * @test
176 */
177 public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString() {
178 $this->validator = $this->getMock('TYPO3\Flow\Validation\Validator\StringLengthValidator', array('addError'), array(), '', FALSE);
179 $this->validatorOptions(array('minimum' => 5, 'maximum' => 100));
180
181 $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
182
183 eval('
184 class ' . $className . ' {
185 protected $someProperty;
186 }
187 ');
188
189 $object = new $className();
190 $this->assertTrue($this->validator->validate($object)->hasErrors());
191 }
192
193 /**
194 * @test
195 */
196 public function validateRegardsMultibyteStringsCorrectly() {
197 // $this->validatorOptions(array('maximum' => 8));
198 // $this->assertFalse($this->validator->validate('├╝berlang')->hasErrors());
199 $this->markTestSkipped('Validator needs to be adjusted regarding multibyte char lengths.');
200 }
201 }