TYPO3 CMS  TYPO3_8-7
StringLengthValidatorTest.php
Go to the documentation of this file.
1 <?php
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  * */
24 
28 class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
29 {
33  protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator::class;
34 
35  public function setup()
36  {
37  $this->validator = $this->getMockBuilder($this->validatorClassName)
38  ->setMethods(['translateErrorMessage'])
39  ->getMock();
40  }
41 
45  protected $validator;
46 
51  {
52  $this->assertFalse($this->validator->validate(null)->hasErrors());
53  }
54 
59  {
60  $this->assertFalse($this->validator->validate('')->hasErrors());
61  }
62 
67  {
68  $options = ['minimum' => 0, 'maximum' => 50];
69  $validator = $this->getMockBuilder($this->validatorClassName)
70  ->setMethods(['dummy'])
71  ->setConstructorArgs([$options])
72  ->getMock();
73  $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
74  }
75 
80  {
81  $options = ['minimum' => 50, 'maximum' => 100];
82  $validator = $this->getMockBuilder($this->validatorClassName)
83  ->setMethods(['translateErrorMessage'])
84  ->setConstructorArgs([$options])
85  ->getMock();
86  $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
87  }
88 
93  {
94  $options = ['minimum' => 5, 'maximum' => 10];
95  $validator = $this->getMockBuilder($this->validatorClassName)
96  ->setMethods(['translateErrorMessage'])
97  ->setConstructorArgs([$options])
98  ->getMock();
99  $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
100  }
101 
106  {
107  $options = ['minimum' => 5];
108  $validator = $this->getMockBuilder($this->validatorClassName)
109  ->setMethods(['dummy'])
110  ->setConstructorArgs([$options])
111  ->getMock();
112  $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
113  }
114 
119  {
120  $options = ['maximum' => 100];
121  $validator = $this->getMockBuilder($this->validatorClassName)
122  ->setMethods(['dummy'])
123  ->setConstructorArgs([$options])
124  ->getMock();
125  $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
126  }
127 
132  {
133  $options = ['maximum' => 10];
134  $validator = $this->getMockBuilder($this->validatorClassName)
135  ->setMethods(['dummy'])
136  ->setConstructorArgs([$options])
137  ->getMock();
138  $this->assertFalse($validator->validate('1234567890')->hasErrors());
139  }
140 
145  {
146  $options = ['minimum' => 10];
147  $validator = $this->getMockBuilder($this->validatorClassName)
148  ->setMethods(['dummy'])
149  ->setConstructorArgs([$options])
150  ->getMock();
151  $this->assertFalse($validator->validate('1234567890')->hasErrors());
152  }
153 
158  {
159  $options = ['minimum' => 10, 'maximum' => 10];
160  $validator = $this->getMockBuilder($this->validatorClassName)
161  ->setMethods(['dummy'])
162  ->setConstructorArgs([$options])
163  ->getMock();
164  $this->assertFalse($validator->validate('1234567890')->hasErrors());
165  }
166 
171  {
172  $options = ['minimum' => 1, 'maximum' => 10];
173  $validator = $this->getMockBuilder($this->validatorClassName)
174  ->setMethods(['dummy'])
175  ->setConstructorArgs([$options])
176  ->getMock();
177  $this->assertFalse($validator->validate('1234567890')->hasErrors());
178  }
179 
184  {
185  $options = ['minimum' => 10, 'maximum' => 100];
186  $validator = $this->getMockBuilder($this->validatorClassName)
187  ->setMethods(['dummy'])
188  ->setConstructorArgs([$options])
189  ->getMock();
190  $this->assertFalse($validator->validate('1234567890')->hasErrors());
191  }
192 
197  {
198  $this->expectException(InvalidValidationOptionsException::class);
199  $this->expectExceptionCode(1238107096);
200  $options = ['minimum' => 101, 'maximum' => 100];
201  $validator = $this->getMockBuilder($this->validatorClassName)
202  ->setMethods(['addError', 'translateErrorMessage'])
203  ->setConstructorArgs([$options])
204  ->getMock();
205  $validator->validate('1234567890');
206  }
207 
212  {
213  $options = ['minimum' => 50, 'maximum' => 100];
214  $validator = $this->getMockBuilder($this->validatorClassName)
215  ->setMethods(['translateErrorMessage'])
216  ->setConstructorArgs([$options])
217  ->getMock();
218  $this->assertEquals(1, count($validator->validate('this is a very short string')->getErrors()));
219  }
220 
225  {
226  $options = ['minimum' => 5, 'maximum' => 100];
227  $validator = $this->getMockBuilder($this->validatorClassName)
228  ->setMethods(['addError', 'translateErrorMessage'])
229  ->setConstructorArgs([$options])
230  ->getMock();
231 
232  $className = $this->getUniqueId('TestClass');
233 
234  eval('
235  class ' . $className . ' {
236  public function __toString() {
237  return \'some string\';
238  }
239  }
240  ');
241 
242  $object = new $className();
243  $this->assertFalse($validator->validate($object)->hasErrors());
244  }
245 
250  {
251  $options = ['minimum' => 5, 'maximum' => 100];
252  $validator = $this->getMockBuilder($this->validatorClassName)
253  ->setMethods(['translateErrorMessage'])
254  ->setConstructorArgs([$options])
255  ->getMock();
256 
257  $className = $this->getUniqueId('TestClass');
258 
259  eval('
260  class ' . $className . ' {
261  protected $someProperty;
262  }
263  ');
264 
265  $object = new $className();
266  $this->assertTrue($validator->validate($object)->hasErrors());
267  }
268 
273  {
274  $options = ['minimum' => 0, 'maximum' => 8];
275  $validator = $this->getMockBuilder($this->validatorClassName)
276  ->setMethods(['dummy'])
277  ->setConstructorArgs([$options])
278  ->getMock();
279  $this->assertFalse($validator->validate('überlang')->hasErrors());
280  }
281 }