ZF-5514: implemented patch and adapted test backend
[zend/radio.git] / tests / Zend / Filter / InputTest.php
blob73c1aa55747d9854457f9885dce28eb8e792819f
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Filter
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
24 /**
25 * Test helper
27 require_once dirname(__FILE__) . '/../../TestHelper.php';
29 /**
30 * @see Zend_Filter_Input
32 require_once 'Zend/Filter/Input.php';
34 /**
35 * @see Zend_Loader
37 require_once 'Zend/Loader.php';
40 /**
41 * @category Zend
42 * @package Zend_Filter
43 * @subpackage UnitTests
44 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
45 * @license http://framework.zend.com/license/new-bsd New BSD License
46 * @group Zend_Filter
48 class Zend_Filter_InputTest extends PHPUnit_Framework_TestCase
51 public function testFilterDeclareSingle()
53 $data = array(
54 'month' => '6abc '
56 $filters = array(
57 'month' => 'digits'
59 $input = new Zend_Filter_Input($filters, null, $data);
61 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
63 $month = $input->month;
64 $this->assertEquals('6', $month);
67 public function testFilterDeclareByObject()
69 $data = array(
70 'month' => '6abc '
72 Zend_Loader::loadClass('Zend_Filter_Digits');
73 $filters = array(
74 'month' => array(new Zend_Filter_Digits())
76 $input = new Zend_Filter_Input($filters, null, $data);
78 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
79 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
80 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
81 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
83 $month = $input->month;
84 $this->assertEquals('6', $month);
87 public function testFilterDeclareByArray()
89 $data = array(
90 'month' => '_6_'
92 $filters = array(
93 'month' => array(
94 array('StringTrim', '_')
97 $input = new Zend_Filter_Input($filters, null, $data);
99 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
100 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
101 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
102 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
104 $month = $input->month;
105 $this->assertEquals('6', $month);
108 public function testFilterDeclareByChain()
110 $data = array(
111 'field1' => ' ABC '
113 $filters = array(
114 'field1' => array('StringTrim', 'StringToLower')
116 $input = new Zend_Filter_Input($filters, null, $data);
118 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
119 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
120 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
121 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
123 $this->assertEquals('abc', $input->field1);
126 public function testFilterWildcardRule()
128 $data = array(
129 'field1' => ' 12abc ',
130 'field2' => ' 24abc '
132 $filters = array(
133 '*' => 'stringTrim',
134 'field1' => 'digits'
136 $input = new Zend_Filter_Input($filters, null, $data);
138 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
139 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
140 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
141 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
143 $this->assertEquals('12', $input->field1);
144 $this->assertEquals('24abc', $input->field2);
147 public function testFilterMultiValue()
149 $data = array(
150 'field1' => array('FOO', 'BAR', 'BaZ')
152 $filters = array(
153 'field1' => 'StringToLower'
155 $input = new Zend_Filter_Input($filters, null, $data);
157 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
158 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
159 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
160 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
162 $f1 = $input->field1;
163 $this->assertType('array', $f1);
164 $this->assertEquals(array('foo', 'bar', 'baz'), $f1);
167 public function testValidatorSingle()
169 $data = array(
170 'month' => '6'
172 $validators = array(
173 'month' => 'digits'
175 $input = new Zend_Filter_Input(null, $validators, $data);
177 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
178 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
179 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
180 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
182 $month = $input->month;
183 $this->assertEquals('6', $month);
186 public function testValidatorSingleInvalid()
188 $data = array(
189 'month' => '6abc '
191 $validators = array(
192 'month' => 'digits'
194 $input = new Zend_Filter_Input(null, $validators, $data);
196 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
197 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
198 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
199 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
201 $messages = $input->getMessages();
202 $this->assertType('array', $messages);
203 $this->assertEquals(array('month'), array_keys($messages));
204 $this->assertType('array', $messages['month']);
205 $this->assertEquals("'6abc ' contains not only digit characters", current($messages['month']));
207 $errors = $input->getErrors();
208 $this->assertType('array', $errors);
209 $this->assertEquals(array('month'), array_keys($errors));
210 $this->assertType('array', $errors['month']);
211 $this->assertEquals("notDigits", $errors['month'][0]);
214 public function testValidatorDeclareByObject()
216 $data = array(
217 'month' => '6'
219 Zend_Loader::loadClass('Zend_Validate_Digits');
220 $validators = array(
221 'month' => array(
222 new Zend_Validate_Digits()
225 $input = new Zend_Filter_Input(null, $validators, $data);
227 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
228 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
229 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
230 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
232 $month = $input->month;
233 $this->assertEquals('6', $month);
236 public function testValidatorDeclareByArray()
238 $data = array(
239 'month' => '6',
240 'month2' => 13
242 $validators = array(
243 'month' => array(
244 'digits',
245 array('Between', 1, 12)
247 'month2' => array(
248 'digits',
249 array('Between', 1, 12)
252 $input = new Zend_Filter_Input(null, $validators, $data);
254 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
255 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
256 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
257 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
259 $month = $input->month;
260 $this->assertEquals('6', $month);
262 $messages = $input->getMessages();
263 $this->assertType('array', $messages);
264 $this->assertEquals(array('month2'), array_keys($messages));
265 $this->assertEquals("'13' is not between '1' and '12', inclusively", current($messages['month2']));
268 public function testValidatorChain()
270 $data = array(
271 'field1' => '50',
272 'field2' => 'abc123',
273 'field3' => 150,
275 Zend_Loader::loadClass('Zend_Validate_Between');
276 $btw = new Zend_Validate_Between(1, 100);
277 $validators = array(
278 'field1' => array('digits', $btw),
279 'field2' => array('digits', $btw),
280 'field3' => array('digits', $btw)
282 $input = new Zend_Filter_Input(null, $validators, $data);
284 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
285 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
286 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
287 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
289 $messages = $input->getMessages();
290 $this->assertType('array', $messages);
291 $this->assertEquals(array('field2', 'field3'), array_keys($messages));
292 $this->assertType('array', $messages['field2']);
293 $this->assertType('array', $messages['field3']);
294 $this->assertEquals("'abc123' contains not only digit characters",
295 current($messages['field2']));
296 $this->assertEquals("'150' is not between '1' and '100', inclusively",
297 current($messages['field3']));
300 public function testValidatorInvalidFieldInMultipleRules()
302 $data = array(
303 'field2' => 'abc123',
305 Zend_Loader::loadClass('Zend_Validate_Between');
306 $validators = array(
307 'field2a' => array(
308 'digits',
309 'fields' => 'field2'
311 'field2b' => array(
312 new Zend_Validate_Between(1, 100),
313 'fields' => 'field2'
316 $input = new Zend_Filter_Input(null, $validators, $data);
318 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
319 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
320 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
321 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
323 $messages = $input->getMessages();
324 $this->assertType('array', $messages);
325 $this->assertEquals(array('field2a', 'field2b'), array_keys($messages));
326 $this->assertType('array', $messages['field2a']);
327 $this->assertType('array', $messages['field2b']);
328 $this->assertEquals("'abc123' contains not only digit characters",
329 current($messages['field2a']));
330 $this->assertEquals("'abc123' is not between '1' and '100', inclusively",
331 current($messages['field2b']));
334 public function testValidatorWildcardRule()
336 $data = array(
337 'field1' => '123abc',
338 'field2' => '246abc'
340 $validators = array(
341 '*' => 'alnum',
342 'field1' => 'digits'
344 $input = new Zend_Filter_Input(null, $validators, $data);
346 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
347 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
348 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
349 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
351 $this->assertNull($input->field1);
352 $this->assertEquals('246abc', $input->field2);
355 public function testValidatorMultiValue()
357 $data = array(
358 'field1' => array('abc', 'def', 'ghi'),
359 'field2' => array('abc', '123')
361 $validators = array(
362 'field1' => 'alpha',
363 'field2' => 'alpha'
365 $input = new Zend_Filter_Input(null, $validators, $data);
367 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
368 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
369 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
370 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
372 $messages = $input->getMessages();
373 $this->assertType('array', $messages);
374 $this->assertEquals(array('field2'), array_keys($messages));
375 $this->assertEquals("'123' contains non alphabetic characters",
376 current($messages['field2']));
379 public function testValidatorMultiField()
381 $data = array(
382 'password1' => 'EREIAMJH',
383 'password2' => 'EREIAMJH',
384 'password3' => 'VESPER'
386 $validators = array(
387 'rule1' => array(
388 'StringEquals',
389 'fields' => array('password1', 'password2')
391 'rule2' => array(
392 'StringEquals',
393 'fields' => array('password1', 'password3')
396 $options = array(
397 Zend_Filter_Input::INPUT_NAMESPACE => 'TestNamespace'
400 $ip = get_include_path();
401 $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
402 $newIp = $dir . PATH_SEPARATOR . $ip;
403 set_include_path($newIp);
405 $input = new Zend_Filter_Input(null, $validators, $data, $options);
407 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
408 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
409 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
410 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
412 set_include_path($ip);
413 $messages = $input->getMessages();
414 $this->assertType('array', $messages);
415 $this->assertEquals(array('rule2'), array_keys($messages));
416 $this->assertEquals("Not all strings in the argument are equal",
417 current($messages['rule2']));
421 * @group ZF-6711
424 public function testValidatorMultiFieldAllowEmptyProcessing()
426 $data = array(
427 'password1' => 'EREIAMJH',
428 'password2' => 'EREIAMJH',
429 'password3' => '',
430 'password4' => ''
432 $validators = array(
433 'rule1' => array(
434 'StringEquals',
435 'fields' => array('password1', 'password2')
437 'rule2' => array(
438 Zend_Filter_Input::ALLOW_EMPTY => false,
439 'StringEquals',
440 'fields' => array('password1', 'password3')
442 'rule3' => array(
443 Zend_Filter_Input::ALLOW_EMPTY => false,
444 'StringEquals',
445 'fields' => array('password3', 'password4')
448 $options = array(
449 Zend_Filter_Input::INPUT_NAMESPACE => 'TestNamespace'
452 $ip = get_include_path();
453 $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
454 $newIp = $dir . PATH_SEPARATOR . $ip;
455 set_include_path($newIp);
457 $input = new Zend_Filter_Input(null, $validators, $data, $options);
459 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
460 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
461 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
462 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
464 set_include_path($ip);
465 $messages = $input->getMessages();
466 $this->assertType('array', $messages);
467 $this->assertEquals(array('rule2', 'rule3'), array_keys($messages));
468 $this->assertEquals(array('isEmpty' => "You must give a non-empty value for field 'password3'"),
469 $messages['rule2']);
470 $this->assertEquals(array('isEmpty' => "You must give a non-empty value for field 'password3'",
471 0 => "You must give a non-empty value for field 'password4'"
473 $messages['rule3']);
476 public function testValidatorBreakChain()
478 $data = array(
479 'field1' => '150',
480 'field2' => '150'
483 Zend_Loader::loadClass('Zend_Validate_Between');
485 $btw1 = new Zend_Validate_Between(1, 100);
487 $btw2 = new Zend_Validate_Between(1, 125);
488 $messageUserDefined = 'Something other than the default message';
489 $btw2->setMessage($messageUserDefined, Zend_Validate_Between::NOT_BETWEEN);
491 $validators = array(
492 'field1' => array($btw1, $btw2),
493 'field2' => array($btw1, $btw2, Zend_Filter_Input::BREAK_CHAIN => true)
495 $input = new Zend_Filter_Input(null, $validators, $data);
497 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
498 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
499 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
500 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
502 $messages = $input->getMessages();
503 $this->assertType('array', $messages);
504 $this->assertEquals(array('field1', 'field2'), array_keys($messages));
505 $this->assertEquals(
506 $messageUserDefined,
507 current($messages['field1']),
508 'Expected message to break 2 validators, the message of the latter overwriting that of the former'
510 $this->assertEquals(
511 "'150' is not between '1' and '100', inclusively",
512 current($messages['field2']),
513 'Expected rule for field2 to break the validation chain at the first validator'
517 public function testValidatorAllowEmpty()
519 $data = array(
520 'field1' => '',
521 'field2' => ''
523 $validators = array(
524 'field1' => array(
525 'alpha',
526 Zend_Filter_Input::ALLOW_EMPTY => false
528 'field2' => array(
529 'alpha',
530 Zend_Filter_Input::ALLOW_EMPTY => true
533 $input = new Zend_Filter_Input(null, $validators, $data);
535 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
536 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
537 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
538 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
540 $this->assertNull($input->field1);
541 $this->assertNotNull($input->field2);
542 $messages = $input->getMessages();
543 $this->assertType('array', $messages);
544 $this->assertEquals(array('field1'), array_keys($messages));
545 $this->assertEquals("You must give a non-empty value for field 'field1'", current($messages['field1']));
549 * @group ZF-6708
550 * @group ZF-1912
552 public function testValidatorAllowEmptyWithOtherValidatersProcessing()
554 $data = array(
555 'field1' => ''
557 $validators = array(
558 'field1' => array(
559 'alpha',
560 Zend_Filter_Input::ALLOW_EMPTY => false
563 $input = new Zend_Filter_Input(null, $validators, $data);
565 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
566 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
567 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
568 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return true');
570 $messages = $input->getMessages();
571 $this->assertType('array', $messages);
572 $this->assertEquals(array('field1'), array_keys($messages));
573 $this->assertEquals("You must give a non-empty value for field 'field1'", current($messages['field1']));
577 * @group ZF-6708
579 public function testValidatorShouldNotProcessZeroAsEmpty()
581 $validation = array(
582 'offset' => array (
583 'digits',
584 'presence' => 'required'
587 $data = array(
588 'offset' => 0,
591 $input = new Zend_Filter_Input(null, $validation, $data);
592 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
593 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
594 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
595 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
597 $messages = $input->getMessages();
598 $this->assertEquals(array(), array_keys($messages));
601 public function testValidatorAllowEmptyNoValidatorChain()
603 Zend_Loader::loadClass('Zend_Filter_StringTrim');
604 Zend_Loader::loadClass('Zend_Filter_StripTags');
605 Zend_Loader::loadClass('Zend_Validate_EmailAddress');
607 $data = array(
608 'nick' => '',
609 'email' => 'someemail@server.com'
612 $filters = array(
613 '*' => new Zend_Filter_StringTrim(),
614 'nick' => new Zend_Filter_StripTags()
617 $validators = array(
618 'email' => array(
619 new Zend_Validate_EmailAddress(),
620 Zend_Filter_Input::ALLOW_EMPTY => true
623 * This is the case we're testing - when presense is required,
624 * but there are no validators besides disallowing empty values.
626 'nick' => array(
627 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED,
628 Zend_Filter_Input::ALLOW_EMPTY => false
632 $input = new Zend_Filter_Input($filters, $validators, $data);
634 if ($input->hasInvalid()) {
635 $input->getMessages();
638 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
639 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
640 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
641 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
643 $messages = $input->getMessages();
644 $this->assertType('array', $messages);
645 $this->assertEquals(array('nick'), array_keys($messages));
646 $this->assertEquals(1, count($messages['nick']));
649 public function testValidatorAllowEmptySetNotEmptyMessage()
651 $data = array(
652 'field1' => '',
654 $validators = array(
655 'field1Rule' => array(
656 Zend_Filter_Input::ALLOW_EMPTY => false,
657 'fields' => 'field1'
661 $options = array(
662 Zend_Filter_Input::NOT_EMPTY_MESSAGE => "You cannot give an empty value for field '%field%', according to rule '%rule%'"
665 $input = new Zend_Filter_Input(null, $validators, $data, $options);
667 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
668 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
669 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
670 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
672 $this->assertNull($input->field1);
673 $messages = $input->getMessages();
674 $this->assertType('array', $messages);
675 $this->assertEquals(array('field1Rule'), array_keys($messages));
676 $this->assertType('array', $messages['field1Rule']);
677 $this->assertEquals("You cannot give an empty value for field 'field1', according to rule 'field1Rule'", current($messages['field1Rule']));
680 public function testValidatorDefault()
682 $validators = array(
683 'field1' => array('presence' => 'required', 'allowEmpty' => false),
684 'field2' => array('presence' => 'optional', 'allowEmpty' => false),
685 'field3' => array('presence' => 'required', 'allowEmpty' => true),
686 'field4' => array('presence' => 'optional', 'allowEmpty' => true),
687 'field5' => array('presence' => 'required', 'allowEmpty' => false, 'default' => 'field5default'),
688 'field6' => array('presence' => 'optional', 'allowEmpty' => false, 'default' => 'field6default'),
689 'field7' => array('presence' => 'required', 'allowEmpty' => true, 'default' => 'field7default'),
690 'field8' => array('presence' => 'optional', 'allowEmpty' => true, 'default' => array('field8default', 'field8default2')),
692 $data = array();
693 $input = new Zend_Filter_Input(null, $validators, $data);
695 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
696 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
697 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
698 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
700 $missing = $input->getMissing();
701 $this->assertType('array', $missing);
702 // make sure field5 and field7 are not counted as missing
703 $this->assertEquals(array('field1', 'field3'), array_keys($missing));
705 $this->assertNull($input->field1);
706 $this->assertNull($input->field2);
707 $this->assertNull($input->field3);
708 $this->assertNull($input->field4);
709 $this->assertEquals('field5default', $input->field5, 'Expected field5 to be non-null');
710 $this->assertEquals('field6default', $input->field6, 'Expected field6 to be non-null');
711 $this->assertEquals('field7default', $input->field7, 'Expected field7 to be non-null');
712 $this->assertEquals('field8default', $input->field8, 'Expected field8 to be non-null');
716 * @group ZF-6761
718 public function testValidatorMissingDefaults()
720 $validators = array(
721 'rule1' => array('presence' => 'required',
722 'fields' => array('field1', 'field2'),
723 'default' => array('field1default'))
725 $data = array();
726 $input = new Zend_Filter_Input(null, $validators, $data);
728 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
729 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
730 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
731 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
733 $missing = $input->getMissing();
734 $this->assertType('array', $missing);
735 $this->assertEquals(array('rule1'), array_keys($missing));
736 $this->assertEquals(array("Field 'field2' is required by rule 'rule1', but the field is missing"), $missing['rule1']);
739 public function testValidatorDefaultDoesNotOverwriteData()
741 $validators = array(
742 'field1' => array('presence' => 'required', 'allowEmpty' => false, 'default' => 'abcd'),
743 'field2' => array('presence' => 'optional', 'allowEmpty' => false, 'default' => 'abcd'),
744 'field3' => array('presence' => 'required', 'allowEmpty' => true, 'default' => 'abcd'),
745 'field4' => array('presence' => 'optional', 'allowEmpty' => true, 'default' => 'abcd'),
747 $data = array(
748 'field1' => 'ABCD',
749 'field2' => 'ABCD',
750 'field3' => 'ABCD',
751 'field4' => 'ABCD'
753 $input = new Zend_Filter_Input(null, $validators, $data);
755 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
756 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
757 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
758 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
760 $this->assertEquals('ABCD', $input->field1);
761 $this->assertEquals('ABCD', $input->field2);
762 $this->assertEquals('ABCD', $input->field3);
763 $this->assertEquals('ABCD', $input->field4);
766 public function testValidatorNotAllowEmpty()
768 $filters = array(
769 'field1' => 'Digits',
770 'field2' => 'Alnum'
773 $validators = array(
774 'field1' => array('Digits'),
775 'field2' => array('Alnum'),
776 'field3' => array('Alnum', 'presence' => 'required')
778 $data = array(
779 'field1' => 'asd1', // Valid data
780 'field2' => '$' // Invalid data
782 $input = new Zend_Filter_Input($filters, $validators, $data);
784 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
785 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
786 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
787 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
789 $messages = $input->getMessages();
790 $this->assertType('array', $messages);
791 $this->assertEquals(array('field2', 'field3'), array_keys($messages));
792 $this->assertType('array', $messages['field2']);
793 $this->assertEquals("You must give a non-empty value for field 'field2'", current($messages['field2']));
796 public function testValidatorMessagesSingle()
798 $data = array('month' => '13abc');
799 $digitsMesg = 'Month should consist of digits';
800 $validators = array(
801 'month' => array(
802 'digits',
803 'messages' => $digitsMesg
806 $input = new Zend_Filter_Input(null, $validators, $data);
808 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
809 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
810 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
811 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
813 $messages = $input->getMessages();
814 $this->assertType('array', $messages);
815 $this->assertEquals(array('month'), array_keys($messages));
816 $this->assertEquals(1, count($messages['month']));
817 $this->assertEquals($digitsMesg, current($messages['month']));
820 public function testValidatorMessagesMultiple()
822 $data = array('month' => '13abc');
823 $digitsMesg = 'Month should consist of digits';
824 $betweenMesg = 'Month should be between 1 and 12';
825 Zend_Loader::loadClass('Zend_Validate_Between');
826 $validators = array(
827 'month' => array(
828 'digits',
829 new Zend_Validate_Between(1, 12),
830 'messages' => array(
831 $digitsMesg,
832 $betweenMesg
836 $input = new Zend_Filter_Input(null, $validators, $data);
838 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
839 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
840 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
841 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
843 $messages = $input->getMessages();
844 $this->assertType('array', $messages);
845 $this->assertEquals(array('month'), array_keys($messages));
846 $this->assertEquals(2, count($messages['month']));
847 $this->assertEquals($digitsMesg, $messages['month']['notDigits']);
848 $this->assertEquals($betweenMesg, $messages['month']['notBetween']);
851 public function testValidatorMessagesFieldsMultiple()
853 $data = array('field1' => array('13abc', '234'));
854 $digitsMesg = 'Field1 should consist of digits';
855 $betweenMesg = 'Field1 should be between 1 and 12';
856 Zend_Loader::loadClass('Zend_Validate_Between');
857 $validators = array(
858 'field1' => array(
859 'digits',
860 new Zend_Validate_Between(1, 12),
861 'messages' => array(
862 $digitsMesg,
863 $betweenMesg
867 $input = new Zend_Filter_Input(null, $validators, $data);
869 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
870 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
871 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
872 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
874 $messages = $input->getMessages();
875 $this->assertType('array', $messages);
876 $this->assertEquals(array('field1'), array_keys($messages));
877 $this->assertEquals(3, count($messages['field1']));
878 $this->assertEquals($digitsMesg, $messages['field1']['notDigits']);
879 $this->assertEquals($betweenMesg, $messages['field1']['notBetween']);
882 public function testValidatorMessagesIntIndex()
884 $data = array('month' => '13abc');
885 $betweenMesg = 'Month should be between 1 and 12';
886 Zend_Loader::loadClass('Zend_Validate_Between');
887 $validators = array(
888 'month' => array(
889 'digits',
890 new Zend_Validate_Between(1, 12),
891 'messages' => array(
892 1 => $betweenMesg
896 $input = new Zend_Filter_Input(null, $validators, $data);
898 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
899 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
900 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
901 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
903 $messages = $input->getMessages();
904 $this->assertType('array', $messages);
905 $this->assertEquals(array('month'), array_keys($messages));
906 $this->assertEquals(2, count($messages['month']));
907 $this->assertEquals("'13abc' contains not only digit characters", current($messages['month']));
909 * @todo $this->assertEquals($betweenMesg, next($messages['month']));
913 public function testValidatorMessagesSingleWithKeys()
915 $data = array('month' => '13abc');
916 $digitsMesg = 'Month should consist of digits';
917 $validators = array(
918 'month' => array(
919 'digits',
920 'messages' => array('notDigits' => $digitsMesg)
923 $input = new Zend_Filter_Input(null, $validators, $data);
925 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
926 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
927 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
928 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
930 $messages = $input->getMessages();
931 $this->assertType('array', $messages);
932 $this->assertEquals(array('month'), array_keys($messages));
933 $this->assertEquals(1, count($messages['month']));
934 // $this->assertEquals($digitsMesg, $messages['month'][0]);
937 public function testValidatorMessagesMultipleWithKeys()
939 $data = array('month' => '13abc');
940 $digitsMesg = 'Month should consist of digits';
941 $betweenMesg = 'Month should be between 1 and 12';
942 Zend_Loader::loadClass('Zend_Validate_Between');
943 $validators = array(
944 'month' => array(
945 'digits',
946 new Zend_Validate_Between(1, 12),
947 'messages' => array(
948 array('notDigits' => $digitsMesg),
949 array('notBetween' => $betweenMesg)
953 $input = new Zend_Filter_Input(null, $validators, $data);
955 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
956 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
957 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
958 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
960 $messages = $input->getMessages();
961 $this->assertType('array', $messages);
962 $this->assertEquals(array('month'), array_keys($messages));
963 $this->assertEquals(2, count($messages['month']));
964 // $this->assertEquals($digitsMesg, $messages['month'][0]);
965 // $this->assertEquals($betweenMesg, $messages['month'][1]);
968 public function testValidatorMessagesMixedWithKeys()
970 $data = array('month' => '13abc');
971 $digitsMesg = 'Month should consist of digits';
972 $betweenMesg = 'Month should be between 1 and 12';
973 Zend_Loader::loadClass('Zend_Validate_Between');
974 $validators = array(
975 'month' => array(
976 'digits',
977 new Zend_Validate_Between(1, 12),
978 'messages' => array(
979 $digitsMesg,
980 array('notBetween' => $betweenMesg)
984 $input = new Zend_Filter_Input(null, $validators, $data);
986 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
987 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
988 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
989 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
991 $messages = $input->getMessages();
992 $this->assertType('array', $messages);
993 $this->assertEquals(array('month'), array_keys($messages));
994 $this->assertEquals(2, count($messages['month']));
995 // $this->assertEquals($digitsMesg, $messages['month'][0]);
996 // $this->assertEquals($betweenMesg, $messages['month'][1]);
999 public function testValidatorHasMissing()
1001 $data = array();
1002 $validators = array(
1003 'month' => array(
1004 'digits',
1005 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
1008 $input = new Zend_Filter_Input(null, $validators, $data);
1010 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
1011 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1012 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1013 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1016 public function testValidatorFieldOptional()
1018 $data = array();
1019 $validators = array(
1020 'month' => array(
1021 'digits',
1022 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL
1025 $input = new Zend_Filter_Input(null, $validators, $data);
1027 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1028 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1029 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1030 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1033 public function testValidatorGetMissing()
1035 $data = array();
1036 $validators = array(
1037 'month' => array(
1038 'digits',
1039 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
1042 $input = new Zend_Filter_Input(null, $validators, $data);
1044 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
1045 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1046 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1047 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1049 $missing = $input->getMissing();
1050 $this->assertType('array', $missing);
1051 $this->assertEquals(array('month'), array_keys($missing));
1052 $this->assertEquals("Field 'month' is required by rule 'month', but the field is missing", $missing['month'][0]);
1055 public function testValidatorSetMissingMessage()
1057 $data = array();
1058 $validators = array(
1059 'monthRule' => array(
1060 'digits',
1061 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED,
1062 'fields' => 'month'
1065 $options = array(
1066 Zend_Filter_Input::MISSING_MESSAGE => 'I looked for %field% but I did not find it; it is required by rule %rule%'
1068 $input = new Zend_Filter_Input(null, $validators, $data, $options);
1070 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
1071 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1072 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1073 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1075 $missing = $input->getMissing();
1076 $this->assertType('array', $missing);
1077 $this->assertEquals(array('monthRule'), array_keys($missing));
1078 $this->assertEquals("I looked for month but I did not find it; it is required by rule monthRule", $missing['monthRule'][0]);
1081 public function testValidatorHasUnknown()
1083 $data = array(
1084 'unknown' => 'xxx'
1086 $validators = array(
1087 'month' => 'digits'
1089 $input = new Zend_Filter_Input(null, $validators, $data);
1091 $this->assertFalse($input->hasMissing(), 'Expecting hasMissing() to return false');
1092 $this->assertFalse($input->hasInvalid(), 'Expecting hasInvalid() to return false');
1093 $this->assertTrue($input->hasUnknown(), 'Expecting hasUnknown() to return true');
1094 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1097 public function testValidatorGetUnknown()
1099 $data = array(
1100 'unknown' => 'xxx'
1102 $validators = array(
1103 'month' => 'digits'
1105 $input = new Zend_Filter_Input(null, $validators, $data);
1107 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1108 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1109 $this->assertTrue($input->hasUnknown(), 'Expected hasUnknown() to retrun true');
1110 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1112 $unknown = $input->getUnknown();
1113 $this->assertType('array', $unknown);
1114 $this->assertThat($unknown, $this->arrayHasKey('unknown'));
1117 public function testValidatorGetInvalid()
1119 $data = array(
1120 'month' => '6abc '
1122 $validators = array(
1123 'month' => 'digits',
1124 'field2' => array('digits', 'presence' => 'required')
1126 $input = new Zend_Filter_Input(null, $validators, $data);
1128 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
1129 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1130 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1131 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1133 $messages = $input->getMessages();
1134 $invalid = $input->getInvalid();
1135 $missing = $input->getMissing();
1137 $this->assertType('array', $messages);
1138 $this->assertEquals(array('month', 'field2'), array_keys($messages));
1139 $this->assertType('array', $invalid);
1140 $this->assertEquals(array('month'), array_keys($invalid));
1141 $this->assertType('array', $missing);
1142 $this->assertEquals(array('field2'), array_keys($missing));
1143 $this->assertEquals(array_merge($invalid, $missing), $messages);
1146 public function testValidatorIsValid()
1148 $data = array(
1149 'field1' => 'abc123',
1150 'field2' => 'abcdef'
1152 $validators = array(
1153 'field1' => 'alpha',
1154 'field2' => 'alpha'
1156 $input = new Zend_Filter_Input(null, $validators, $data);
1158 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1159 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1160 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1161 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1163 $this->assertFalse($input->isValid());
1164 $this->assertFalse($input->isValid('field1'));
1165 $this->assertTrue($input->isValid('field2'));
1167 $input->setData(array('field2' => 'abcdef'));
1169 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1170 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1171 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1172 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1174 $this->assertTrue($input->isValid());
1175 $this->assertFalse($input->isValid('field1'));
1176 $this->assertTrue($input->isValid('field2'));
1179 public function testAddNamespace()
1181 $data = array(
1182 'field1' => 'abc',
1183 'field2' => '123',
1184 'field3' => '123'
1186 $validators = array(
1187 'field1' => 'MyDigits',
1188 'field2' => 'MyDigits',
1189 'field3' => 'digits'
1192 $ip = get_include_path();
1193 $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
1194 $newIp = $dir . PATH_SEPARATOR . $ip;
1195 set_include_path($newIp);
1197 $input = new Zend_Filter_Input(null, $validators, $data);
1198 $input->addNamespace('TestNamespace');
1200 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1201 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1202 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1203 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1204 set_include_path($ip);
1206 $this->assertEquals('123', (string) $input->field2);
1207 $this->assertEquals('123', (string) $input->field3);
1209 $messages = $input->getMessages();
1210 $this->assertType('array', $messages);
1211 $this->assertThat($messages, $this->arrayHasKey('field1'));
1212 $this->assertEquals("'abc' contains not only digit characters", current($messages['field1']));
1215 public function testGetPluginLoader()
1217 $input = new Zend_Filter_Input(null, null);
1219 $loader = $input->getPluginLoader(Zend_Filter_Input::VALIDATE);
1220 $this->assertType('Zend_Loader_PluginLoader', $loader,
1221 'Expected object of type Zend_Loader_PluginLoader, got ' , get_class($loader));
1223 try {
1224 $loader = $input->getPluginLoader('foo');
1225 $this->fail('Expected to catch Zend_Filter_Exception');
1226 } catch (Zend_Exception $e) {
1227 $this->assertType('Zend_Filter_Exception', $e,
1228 'Expected object of type Zend_Filter_Exception, got '.get_class($e));
1229 $this->assertEquals('Invalid type "foo" provided to getPluginLoader()',
1230 $e->getMessage());
1235 public function testSetPluginLoader()
1237 $input = new Zend_Filter_Input(null, null);
1239 $loader = new Zend_Loader_PluginLoader();
1241 $input->setPluginLoader($loader, Zend_Filter_Input::VALIDATE);
1244 public function testSetPluginLoaderInvalidType()
1246 $input = new Zend_Filter_Input(null, null);
1248 $loader = new Zend_Loader_PluginLoader();
1250 try {
1251 $input->setPluginLoader($loader, 'foo');
1252 $this->fail('Expected to catch Zend_Filter_Exception');
1253 } catch (Zend_Exception $e) {
1254 $this->assertType('Zend_Filter_Exception', $e,
1255 'Expected object of type Zend_Filter_Exception, got '.get_class($e));
1256 $this->assertEquals('Invalid type "foo" provided to setPluginLoader()',
1257 $e->getMessage());
1261 public function testNamespaceExceptionClassNotFound()
1263 $data = array(
1264 'field1' => 'abc'
1266 $validators = array(
1267 'field1' => 'MyDigits'
1269 // Do not add namespace on purpose, so MyDigits will not be found
1270 $input = new Zend_Filter_Input(null, $validators, $data);
1271 try {
1272 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1273 $this->fail('Expected to catch Zend_Filter_Exception');
1274 } catch (Zend_Exception $e) {
1275 $this->assertType('Zend_Loader_PluginLoader_Exception', $e,
1276 'Expected object of type Zend_Filter_Exception, got '.get_class($e));
1277 $this->assertContains("not found in the registry", $e->getMessage());
1281 public function testNamespaceExceptionInvalidClass()
1283 $data = array(
1284 'field1' => 'abc'
1286 // Zend_Validate_Exception exists, but does not implement the needed interface
1287 $validators = array(
1288 'field1' => 'Exception'
1291 $input = new Zend_Filter_Input(null, $validators, $data);
1293 try {
1294 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1295 $this->fail('Expected to catch Zend_Filter_Exception');
1296 } catch (Zend_Exception $e) {
1297 $this->assertType('Zend_Filter_Exception', $e,
1298 'Expected object of type Zend_Filter_Exception, got '.get_class($e));
1299 $this->assertEquals("Class 'Zend_Validate_Exception' based on basename 'Exception' must implement the 'Zend_Validate_Interface' interface",
1300 $e->getMessage());
1304 public function testSetDefaultEscapeFilter()
1306 $data = array(
1307 'field1' => ' ab&c '
1309 $input = new Zend_Filter_Input(null, null, $data);
1310 $input->setDefaultEscapeFilter('StringTrim');
1312 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1313 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1314 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1315 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1317 $this->assertEquals('ab&c', $input->field1);
1320 public function testSetDefaultEscapeFilterExceptionWrongClassType()
1322 $input = new Zend_Filter_Input(null, null);
1323 try {
1324 $input->setDefaultEscapeFilter(new StdClass());
1325 $this->fail('Expected to catch Zend_Filter_Exception');
1326 } catch (Zend_Exception $e) {
1327 $this->assertType('Zend_Filter_Exception', $e,
1328 'Expected object of type Zend_Filter_Exception, got '.get_class($e));
1329 $this->assertEquals("Escape filter specified does not implement Zend_Filter_Interface", $e->getMessage());
1333 public function testOptionAllowEmpty()
1335 $data = array(
1336 'field1' => ''
1338 $validators = array(
1339 'field1' => 'alpha'
1341 $options = array(
1342 Zend_Filter_Input::ALLOW_EMPTY => true
1344 $input = new Zend_Filter_Input(null, $validators, $data, $options);
1346 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1347 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1348 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1349 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1351 $this->assertNotNull($input->field1);
1352 $this->assertEquals('', $input->field1);
1355 public function testOptionBreakChain()
1357 $data = array(
1358 'field1' => '150'
1360 Zend_Loader::loadClass('Zend_Validate_Between');
1361 $btw1 = new Zend_Validate_Between(1, 100);
1362 $btw2 = new Zend_Validate_Between(1, 125);
1363 $validators = array(
1364 'field1' => array($btw1, $btw2),
1366 $options = array(
1367 Zend_Filter_Input::BREAK_CHAIN => true
1369 $input = new Zend_Filter_Input(null, $validators, $data, $options);
1371 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1372 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1373 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1374 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1376 $messages = $input->getMessages();
1377 $this->assertType('array', $messages);
1378 $this->assertEquals(array('field1'), array_keys($messages));
1379 $this->assertEquals(1, count($messages['field1']), 'Expected rule for field1 to break 1 validator');
1380 $this->assertEquals("'150' is not between '1' and '100', inclusively",
1381 current($messages['field1']));
1384 public function testOptionEscapeFilter()
1386 $data = array(
1387 'field1' => ' ab&c '
1389 $options = array(
1390 Zend_Filter_Input::ESCAPE_FILTER => 'StringTrim'
1392 $input = new Zend_Filter_Input(null, null, $data, $options);
1394 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1395 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1396 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1397 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1399 $this->assertEquals('ab&c', $input->field1);
1402 public function testOptionNamespace()
1404 $data = array(
1405 'field1' => 'abc',
1406 'field2' => '123',
1407 'field3' => '123'
1409 $validators = array(
1410 'field1' => 'MyDigits',
1411 'field2' => 'MyDigits',
1412 'field3' => 'digits'
1414 $options = array(
1415 Zend_Filter_Input::INPUT_NAMESPACE => 'TestNamespace'
1418 $ip = get_include_path();
1419 $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
1420 $newIp = $dir . PATH_SEPARATOR . $ip;
1421 set_include_path($newIp);
1423 $input = new Zend_Filter_Input(null, $validators, $data, $options);
1425 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1426 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1427 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1428 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1429 set_include_path($ip);
1431 $this->assertEquals('123', (string) $input->field2);
1432 $this->assertEquals('123', (string) $input->field3);
1434 $messages = $input->getMessages();
1435 $this->assertType('array', $messages);
1436 $this->assertThat($messages, $this->arrayHasKey('field1'));
1437 $this->assertEquals("'abc' contains not only digit characters", current($messages['field1']));
1440 public function testOptionPresence()
1442 $data = array(
1443 'field1' => '123'
1444 // field2 is missing deliberately
1446 $validators = array(
1447 'field1' => 'Digits',
1448 'field2' => 'Digits'
1450 $options = array(
1451 Zend_Filter_Input::PRESENCE => true
1453 $input = new Zend_Filter_Input(null, $validators, $data, $options);
1455 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return false');
1456 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1457 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1458 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1460 $missing = $input->getMissing();
1461 $this->assertType('array', $missing);
1462 $this->assertEquals(array('field2'), array_keys($missing));
1463 $this->assertEquals("Field 'field2' is required by rule 'field2', but the field is missing", $missing['field2'][0]);
1466 public function testOptionExceptionUnknown()
1468 $options = array(
1469 'unknown' => 'xxx'
1471 try {
1472 $input = new Zend_Filter_Input(null, null, null, $options);
1473 $this->fail('Expected to catch Zend_Filter_Exception');
1474 } catch (Zend_Exception $e) {
1475 $this->assertType('Zend_Filter_Exception', $e,
1476 'Expected object of type Zend_Filter_Exception, got '.get_class($e));
1477 $this->assertEquals("Unknown option 'unknown'", $e->getMessage());
1481 public function testGetEscaped()
1483 $data = array(
1484 'field1' => 'ab&c'
1486 $input = new Zend_Filter_Input(null, null, $data);
1488 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1489 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1490 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1491 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1493 $this->assertEquals('ab&amp;c', $input->getEscaped('field1'));
1494 $this->assertNull($input->getEscaped('field2'));
1497 public function testGetEscapedAllFields()
1499 $data = array(
1500 'field1' => 'ab&c'
1502 $input = new Zend_Filter_Input(null, null, $data);
1504 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1505 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1506 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1507 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1509 $this->assertEquals(array('field1' => 'ab&amp;c'), $input->getEscaped());
1512 public function testMagicGetEscaped()
1514 $data = array(
1515 'field1' => 'ab&c'
1517 $input = new Zend_Filter_Input(null, null, $data);
1519 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1520 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1521 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1522 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1524 $this->assertEquals('ab&amp;c', $input->field1);
1525 $this->assertNull($input->field2);
1528 public function testGetEscapedMultiValue()
1530 $data = array(
1531 'multiSelect' => array('C&H', 'B&O', 'AT&T')
1533 $input = new Zend_Filter_Input(null, null, $data);
1535 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1536 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1537 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1538 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1540 $multi = $input->getEscaped('multiSelect');
1541 $this->assertType('array', $multi);
1542 $this->assertEquals(3, count($multi));
1543 $this->assertEquals(array('C&amp;H', 'B&amp;O', 'AT&amp;T'), $multi);
1546 public function testGetUnescaped()
1548 $data = array(
1549 'field1' => 'ab&c'
1551 $input = new Zend_Filter_Input(null, null, $data);
1553 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1554 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1555 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1556 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1558 $this->assertEquals('ab&c', $input->getUnescaped('field1'));
1559 $this->assertNull($input->getUnescaped('field2'));
1562 public function testGetUnescapedAllFields()
1564 $data = array(
1565 'field1' => 'ab&c'
1567 $input = new Zend_Filter_Input(null, null, $data);
1569 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1570 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1571 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1572 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1574 $this->assertEquals(array('field1' => 'ab&c'), $input->getUnescaped());
1577 public function testMagicIsset()
1579 $data = array(
1580 'field1' => 'ab&c'
1582 $input = new Zend_Filter_Input(null, null, $data);
1584 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1585 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1586 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1587 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1589 $this->assertTrue(isset($input->field1));
1590 $this->assertFalse(isset($input->field2));
1593 public function testProcess()
1595 $data = array(
1596 'field1' => 'ab&c',
1597 'field2' => '123abc'
1599 $filters = array(
1600 '*' => 'StringTrim',
1601 'field2' => 'digits'
1603 $validators = array(
1604 'field1' => array(Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL),
1605 'field2' => array(
1606 'digits',
1607 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
1610 $input = new Zend_Filter_Input($filters, $validators, $data);
1611 try {
1612 $input->process();
1613 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1614 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1615 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1616 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1617 } catch (Zend_Exception $e) {
1618 $this->fail('Received Zend_Exception where none was expected');
1622 public function testProcessUnknownThrowsNoException()
1624 $data = array(
1625 'field1' => 'ab&c',
1626 'field2' => '123abc',
1627 'field3' => 'unknown'
1629 $filters = array(
1630 '*' => 'StringTrim',
1631 'field2' => 'digits'
1633 $validators = array(
1634 'field1' => array(Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL),
1635 'field2' => array(
1636 'digits',
1637 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
1640 $input = new Zend_Filter_Input($filters, $validators, $data);
1641 try {
1642 $input->process();
1643 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1644 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1645 $this->assertTrue($input->hasUnknown(), 'Expected hasUnknown() to retrun true');
1646 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1647 } catch (Zend_Exception $e) {
1648 $this->fail('Received Zend_Exception where none was expected');
1652 public function testProcessInvalidThrowsException()
1654 $data = array(
1655 'field1' => 'ab&c',
1656 'field2' => 'abc' // invalid because no digits
1658 $filters = array(
1659 '*' => 'StringTrim',
1660 'field2' => 'digits'
1662 $validators = array(
1663 'field1' => array(Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL),
1664 'field2' => array(
1665 'digits',
1666 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
1669 $input = new Zend_Filter_Input($filters, $validators, $data);
1670 try {
1671 $input->process();
1672 $this->fail('Expected to catch Zend_Filter_Exception');
1673 } catch (Zend_Exception $e) {
1674 $this->assertType('Zend_Filter_Exception', $e,
1675 'Expected object of type Zend_Filter_Exception, got '.get_class($e));
1676 $this->assertEquals("Input has invalid fields", $e->getMessage());
1677 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1678 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1679 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1680 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1684 public function testProcessMissingThrowsException()
1686 $data = array(
1687 'field1' => 'ab&c'
1688 // field2 is missing on purpose for this test
1690 $filters = array(
1691 '*' => 'StringTrim',
1692 'field2' => 'digits'
1694 $validators = array(
1695 'field1' => array(
1696 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL
1698 'field2' => array(
1699 'digits',
1700 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
1703 $input = new Zend_Filter_Input($filters, $validators, $data);
1704 try {
1705 $input->process();
1706 $this->fail('Expected to catch Zend_Filter_Exception');
1707 } catch (Zend_Exception $e) {
1708 $this->assertType('Zend_Filter_Exception', $e,
1709 'Expected object of type Zend_Filter_Exception, got '.get_class($e));
1710 $this->assertEquals("Input has missing fields", $e->getMessage());
1711 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
1712 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1713 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1714 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1719 * @group ZF-3004
1721 public function testInsertingNullDoesNotGetEscapedWithDefaultEscapeMethod()
1723 $input = new Zend_Filter_Input(null, null, array('test' => null));
1724 $input->process();
1726 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1727 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1728 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1729 $this->assertTrue($input->hasValid(), 'Expected hasValid() to return true');
1731 $this->assertNull($input->getUnescaped('test'), 'getUnescaped of test fails to return null');
1732 $this->assertNull($input->getEscaped('test'), 'getEscaped of test fails to return null');
1733 $this->assertNull($input->test, 'magic get of test fails to return null');
1737 * @group ZF-3100
1739 public function testPluginLoaderInputNamespaceWithSameNameFilterAndValidatorLeadsToException()
1741 $filters = array(
1742 'date1' => array('Date')
1744 $validators = array(
1745 'date1' => array('Date')
1747 $data = array(
1748 'date1' => '1990-01-01'
1750 $options = array(
1751 'inputNamespace' => array('MyZend_Filter', 'MyZend_Validate'),
1753 $filter = new Zend_Filter_Input($filters, $validators, $data, $options);
1755 try {
1756 $filter->process();
1757 $this->fail();
1758 } catch(Zend_Filter_Exception $e) {
1759 $this->assertEquals(
1760 "Class 'MyZend_Validate_Date' based on basename 'Date' must implement the 'Zend_Filter_Interface' interface",
1761 $e->getMessage()
1767 * @group ZF-3100
1769 public function testPluginLoaderWithFilterValidateNamespaceWithSameNameFilterAndValidatorWorksPerfectly()
1771 // Array
1772 $filters = array(
1773 'date1' => array('Date')
1775 $validators = array(
1776 'date1' => array('Date')
1778 $data = array(
1779 'date1' => '1990-01-01'
1781 $options = array(
1782 'filterNamespace' => array('MyZend_Filter'),
1783 'validatorNamespace' => array('MyZend_Validate'),
1785 $filter = new Zend_Filter_Input($filters, $validators, $data, $options);
1787 try {
1788 $filter->process();
1789 $this->assertEquals("2000-01-01", $filter->date1);
1790 } catch(Zend_Filter_Exception $e) {
1791 $this->fail();
1794 // String notation
1795 $options = array(
1796 'filterNamespace' => 'MyZend_Filter',
1797 'validatorNamespace' => 'MyZend_Validate',
1799 $filter = new Zend_Filter_Input($filters, $validators, $data, $options);
1801 try {
1802 $filter->process();
1803 $this->assertEquals("2000-01-01", $filter->date1);
1804 } catch(Zend_Filter_Exception $e) {
1805 $this->fail();
1810 * @group ZF-7135
1812 public function testValidatorAllowNull()
1814 $data = array(
1815 'field1' => null
1817 $validators = array(
1818 'field1' => array(
1819 'notEmpty'
1822 $input = new Zend_Filter_Input(null, $validators, $data);
1824 $this->assertFalse($input->hasMissing(), 'Expected hasMissing() to return false');
1825 $this->assertTrue($input->hasInvalid(), 'Expected hasInvalid() to return true');
1826 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1827 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return true');
1829 $this->assertNull($input->field1);
1833 * @group ZF-7034
1835 public function testSettingNotEmptyMessageAndMessagePerKeyAndMessagePerArray()
1837 require_once 'Zend/Validate/NotEmpty.php';
1838 require_once 'Zend/Validate/Regex.php';
1839 require_once 'Zend/Validate/StringLength.php';
1841 $filters = array( );
1842 $validators = array(
1843 'street' => array (
1844 new Zend_Validate_NotEmpty (),
1845 new Zend_Validate_Regex ( '/^[a-zA-Z0-9]{1,30}$/u' ),
1846 new Zend_Validate_StringLength ( 0, 10 ),
1847 Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED,
1848 Zend_Filter_Input::DEFAULT_VALUE => '',
1849 Zend_Filter_Input::BREAK_CHAIN => true,
1850 'messages' => array (
1851 0 => 'Bitte geben Sie Ihre Straße ein.',
1852 'Verwenden Sie bitte keine Sonderzeichen bei der Eingabe.',
1853 array (
1854 Zend_Validate_StringLength::TOO_LONG => 'Bitte beschränken Sie sich auf %max% Zeichen'
1860 $filter = new Zend_Filter_Input($filters, $validators, array('street' => ''));
1861 $this->assertFalse($filter->isValid());
1862 $message = $filter->getMessages();
1863 $this->assertContains('Bitte geben Sie Ihre Straße ein.', $message['street']['isEmpty']);
1865 $filter2 = new Zend_Filter_Input($filters, $validators, array('street' => 'Str!!'));
1866 $this->assertFalse($filter2->isValid());
1867 $message = $filter2->getMessages();
1868 $this->assertContains('Verwenden Sie bitte keine Sonderzeichen', $message['street']['regexNotMatch']);
1870 $filter3 = new Zend_Filter_Input($filters, $validators, array('street' => 'Str1234567890'));
1871 $this->assertFalse($filter3->isValid());
1872 $message = $filter3->getMessages();
1873 $this->assertContains('Bitte beschränken Sie sich auf', $message['street']['stringLengthTooLong']);
1877 * @group ZF-7394
1879 public function testSettingMultipleNotEmptyMessages()
1881 require_once 'Zend/Validate/NotEmpty.php';
1882 require_once 'Zend/Validate/Regex.php';
1883 require_once 'Zend/Validate/StringLength.php';
1885 $filters = array( );
1886 $validators = array(
1887 'name' => array('NotEmpty','messages' => 'Please enter your name'),
1888 'subject' => array('NotEmpty','messages' => 'Please enter a subject'),
1889 'email' => array('EmailAddress','messages' => 'Please enter a valid Email address'),
1890 'content' => array('NotEmpty','messages' => 'Please enter message contents')
1893 $data = array(
1894 'name' => '',
1895 'subject' => '',
1896 'content' => ''
1899 $filter = new Zend_Filter_Input($filters, $validators, $data);
1900 $this->assertFalse($filter->isValid());
1901 $message = $filter->getMessages();
1902 $this->assertContains('Please enter your name', $message['name']['isEmpty']);
1903 $this->assertContains('Please enter a subject', $message['subject']['isEmpty']);
1904 $this->assertContains('Please enter message contents', $message['content']['isEmpty']);
1908 * @group ZF-3736
1910 public function testTranslateNotEmptyMessages()
1912 require_once 'Zend/Translate/Adapter/Array.php';
1913 $translator = new Zend_Translate_Adapter_Array(array('missingMessage' => 'Still missing'));
1915 $validators = array(
1916 'rule1' => array('presence' => 'required',
1917 'fields' => array('field1', 'field2'),
1918 'default' => array('field1default'))
1920 $data = array();
1921 $input = new Zend_Filter_Input(null, $validators, $data);
1922 $input->setTranslator($translator);
1924 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
1926 $missing = $input->getMissing();
1927 $this->assertType('array', $missing);
1928 $this->assertEquals(array('rule1'), array_keys($missing));
1929 $this->assertEquals(array("Still missing"), $missing['rule1']);
1933 * @group ZF-3736
1935 public function testTranslateNotEmptyMessagesByUsingRegistry()
1937 require_once 'Zend/Translate/Adapter/Array.php';
1938 $translator = new Zend_Translate_Adapter_Array(array('missingMessage' => 'Still missing'));
1939 require_once 'Zend/Registry.php';
1940 Zend_Registry::set('Zend_Translate', $translator);
1942 $validators = array(
1943 'rule1' => array('presence' => 'required',
1944 'fields' => array('field1', 'field2'),
1945 'default' => array('field1default'))
1947 $data = array();
1948 $input = new Zend_Filter_Input(null, $validators, $data);
1950 $this->assertTrue($input->hasMissing(), 'Expected hasMissing() to return true');
1951 $this->assertFalse($input->hasInvalid(), 'Expected hasInvalid() to return false');
1952 $this->assertFalse($input->hasUnknown(), 'Expected hasUnknown() to return false');
1953 $this->assertFalse($input->hasValid(), 'Expected hasValid() to return false');
1955 $missing = $input->getMissing();
1956 $this->assertType('array', $missing);
1957 $this->assertEquals(array('rule1'), array_keys($missing));
1958 $this->assertEquals(array("Still missing"), $missing['rule1']);
1962 class MyZend_Filter_Date implements Zend_Filter_Interface
1964 public function filter($value)
1966 return "2000-01-01";
1970 class MyZend_Validate_Date implements Zend_Validate_Interface
1972 public function isValid($value)
1974 return true;
1977 public function getMessages()
1979 return array();
1982 public function getErrors()
1984 return array();