[CLEANUP] Rework/simplify copyright header and remove @package
[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 file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 protected $validatorClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator';
23
24 public function setup() {
25 $this->validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'));
26 }
27
28 /**
29 * @var \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator
30 */
31 protected $validator;
32
33 /**
34 * @test
35 */
36 public function validateReturnsNoErrorIfTheGivenValueIsNull() {
37 $this->assertFalse($this->validator->validate(NULL)->hasErrors());
38 }
39
40 /**
41 * @test
42 */
43 public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString() {
44 $this->assertFalse($this->validator->validate('')->hasErrors());
45 }
46
47 /**
48 * @test
49 */
50 public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength() {
51 $options = array('minimum' => 0, 'maximum' => 50);
52 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
53 $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
54 }
55
56 /**
57 * @test
58 */
59 public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength() {
60 $options = array('minimum' => 50, 'maximum' => 100);
61 $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
62 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
63 }
64
65 /**
66 * @test
67 */
68 public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength() {
69 $options = array('minimum' => 5, 'maximum' => 10);
70 $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
71 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
72 }
73
74 /**
75 * @test
76 */
77 public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified() {
78 $options = array('minimum' => 5);
79 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
80 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
81 }
82
83 /**
84 * @test
85 */
86 public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified() {
87 $options = array('maximum' => 100);
88 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
89 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
90 }
91
92 /**
93 * @test
94 */
95 public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified() {
96 $options = array('maximum' => 10);
97 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
98 $this->assertFalse($validator->validate('1234567890')->hasErrors());
99 }
100
101 /**
102 * @test
103 */
104 public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified() {
105 $options = array('minimum' => 10);
106 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
107 $this->assertFalse($validator->validate('1234567890')->hasErrors());
108 }
109
110 /**
111 * @test
112 */
113 public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue() {
114 $options = array('minimum' => 10, 'maximum' => 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 stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength() {
123 $options = array('minimum' => 1, '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 stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength() {
132 $options = array('minimum' => 10, 'maximum' => 100);
133 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
134 $this->assertFalse($validator->validate('1234567890')->hasErrors());
135 }
136
137 /**
138 * @test
139 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
140 */
141 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength() {
142 $options = array('minimum' => 101, 'maximum' => 100);
143 $validator = $this->getMock($this->validatorClassName, array('addError', 'translateErrorMessage'), array($options));
144 $validator->validate('1234567890');
145 }
146
147 /**
148 * @test
149 */
150 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails() {
151 $options = array('minimum' => 50, 'maximum' => 100);
152 $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
153 $this->assertEquals(1, count($validator->validate('this is a very short string')->getErrors()));
154 }
155
156 /**
157 * @test
158 */
159 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod() {
160 $options = array('minimum' => 5, 'maximum' => 100);
161 $validator = $this->getMock($this->validatorClassName, array('addError', 'translateErrorMessage'), array($options));
162
163 $className = $this->getUniqueId('TestClass');
164
165 eval('
166 class ' . $className . ' {
167 public function __toString() {
168 return \'some string\';
169 }
170 }
171 ');
172
173 $object = new $className();
174 $this->assertFalse($validator->validate($object)->hasErrors());
175 }
176
177 /**
178 * @test
179 */
180 public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString() {
181 $options = array('minimum' => 5, 'maximum' => 100);
182 $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
183
184 $className = $this->getUniqueId('TestClass');
185
186 eval('
187 class ' . $className . ' {
188 protected $someProperty;
189 }
190 ');
191
192 $object = new $className();
193 $this->assertTrue($validator->validate($object)->hasErrors());
194 }
195
196 /**
197 * @test
198 */
199 public function validateRegardsMultibyteStringsCorrectly() {
200 // $this->validatorOptions(array('maximum' => 8));
201 // $this->assertFalse($this->validator->validate('├╝berlang')->hasErrors());
202 $this->markTestSkipped('Validator needs to be adjusted regarding multibyte char lengths.');
203 }
204 }