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