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