TYPO3 CMS  TYPO3_7-6
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  * */
23 
28 {
29  protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator::class;
30 
31  public function setup()
32  {
33  $this->validator = $this->getMock($this->validatorClassName, ['translateErrorMessage']);
34  }
35 
39  protected $validator;
40 
45  {
46  $this->assertFalse($this->validator->validate(null)->hasErrors());
47  }
48 
53  {
54  $this->assertFalse($this->validator->validate('')->hasErrors());
55  }
56 
61  {
62  $options = ['minimum' => 0, 'maximum' => 50];
63  $validator = $this->getMock($this->validatorClassName, ['dummy'], [$options]);
64  $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
65  }
66 
71  {
72  $options = ['minimum' => 50, 'maximum' => 100];
73  $validator = $this->getMock($this->validatorClassName, ['translateErrorMessage'], [$options]);
74  $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
75  }
76 
81  {
82  $options = ['minimum' => 5, 'maximum' => 10];
83  $validator = $this->getMock($this->validatorClassName, ['translateErrorMessage'], [$options]);
84  $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
85  }
86 
91  {
92  $options = ['minimum' => 5];
93  $validator = $this->getMock($this->validatorClassName, ['dummy'], [$options]);
94  $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
95  }
96 
101  {
102  $options = ['maximum' => 100];
103  $validator = $this->getMock($this->validatorClassName, ['dummy'], [$options]);
104  $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
105  }
106 
111  {
112  $options = ['maximum' => 10];
113  $validator = $this->getMock($this->validatorClassName, ['dummy'], [$options]);
114  $this->assertFalse($validator->validate('1234567890')->hasErrors());
115  }
116 
121  {
122  $options = ['minimum' => 10];
123  $validator = $this->getMock($this->validatorClassName, ['dummy'], [$options]);
124  $this->assertFalse($validator->validate('1234567890')->hasErrors());
125  }
126 
131  {
132  $options = ['minimum' => 10, 'maximum' => 10];
133  $validator = $this->getMock($this->validatorClassName, ['dummy'], [$options]);
134  $this->assertFalse($validator->validate('1234567890')->hasErrors());
135  }
136 
141  {
142  $options = ['minimum' => 1, 'maximum' => 10];
143  $validator = $this->getMock($this->validatorClassName, ['dummy'], [$options]);
144  $this->assertFalse($validator->validate('1234567890')->hasErrors());
145  }
146 
151  {
152  $options = ['minimum' => 10, 'maximum' => 100];
153  $validator = $this->getMock($this->validatorClassName, ['dummy'], [$options]);
154  $this->assertFalse($validator->validate('1234567890')->hasErrors());
155  }
156 
162  {
163  $options = ['minimum' => 101, 'maximum' => 100];
164  $validator = $this->getMock($this->validatorClassName, ['addError', 'translateErrorMessage'], [$options]);
165  $validator->validate('1234567890');
166  }
167 
172  {
173  $options = ['minimum' => 50, 'maximum' => 100];
174  $validator = $this->getMock($this->validatorClassName, ['translateErrorMessage'], [$options]);
175  $this->assertEquals(1, count($validator->validate('this is a very short string')->getErrors()));
176  }
177 
182  {
183  $options = ['minimum' => 5, 'maximum' => 100];
184  $validator = $this->getMock($this->validatorClassName, ['addError', 'translateErrorMessage'], [$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 
204  {
205  $options = ['minimum' => 5, 'maximum' => 100];
206  $validator = $this->getMock($this->validatorClassName, ['translateErrorMessage'], [$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 
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 }