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