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