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.
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
27 require_once dirname(__FILE__
) . '/../../TestHelper.php';
30 * @see Zend_Filter_Input
32 require_once 'Zend/Filter/Input.php';
37 require_once 'Zend/Loader.php';
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
48 class Zend_Filter_InputTest
extends PHPUnit_Framework_TestCase
51 public function testFilterDeclareSingle()
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()
72 Zend_Loader
::loadClass('Zend_Filter_Digits');
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()
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()
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()
129 'field1' => ' 12abc ',
130 'field2' => ' 24abc '
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()
150 'field1' => array('FOO', 'BAR', 'BaZ')
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()
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()
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()
219 Zend_Loader
::loadClass('Zend_Validate_Digits');
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()
245 array('Between', 1, 12)
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()
272 'field2' => 'abc123',
275 Zend_Loader
::loadClass('Zend_Validate_Between');
276 $btw = new Zend_Validate_Between(1, 100);
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()
303 'field2' => 'abc123',
305 Zend_Loader
::loadClass('Zend_Validate_Between');
312 new Zend_Validate_Between(1, 100),
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()
337 'field1' => '123abc',
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()
358 'field1' => array('abc', 'def', 'ghi'),
359 'field2' => array('abc', '123')
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()
382 'password1' => 'EREIAMJH',
383 'password2' => 'EREIAMJH',
384 'password3' => 'VESPER'
389 'fields' => array('password1', 'password2')
393 'fields' => array('password1', 'password3')
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']));
424 public function testValidatorMultiFieldAllowEmptyProcessing()
427 'password1' => 'EREIAMJH',
428 'password2' => 'EREIAMJH',
435 'fields' => array('password1', 'password2')
438 Zend_Filter_Input
::ALLOW_EMPTY
=> false,
440 'fields' => array('password1', 'password3')
443 Zend_Filter_Input
::ALLOW_EMPTY
=> false,
445 'fields' => array('password3', 'password4')
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'"),
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'"
476 public function testValidatorBreakChain()
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
);
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));
507 current($messages['field1']),
508 'Expected message to break 2 validators, the message of the latter overwriting that of the former'
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()
526 Zend_Filter_Input
::ALLOW_EMPTY
=> false
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']));
552 public function testValidatorAllowEmptyWithOtherValidatersProcessing()
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']));
579 public function testValidatorShouldNotProcessZeroAsEmpty()
584 'presence' => 'required'
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');
609 'email' => 'someemail@server.com'
613 '*' => new Zend_Filter_StringTrim(),
614 'nick' => new Zend_Filter_StripTags()
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.
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()
655 'field1Rule' => array(
656 Zend_Filter_Input
::ALLOW_EMPTY
=> false,
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()
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')),
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');
718 public function testValidatorMissingDefaults()
721 'rule1' => array('presence' => 'required',
722 'fields' => array('field1', 'field2'),
723 'default' => array('field1default'))
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()
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'),
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()
769 'field1' => 'Digits',
774 'field1' => array('Digits'),
775 'field2' => array('Alnum'),
776 'field3' => array('Alnum', 'presence' => 'required')
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';
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');
829 new Zend_Validate_Between(1, 12),
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');
860 new Zend_Validate_Between(1, 12),
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');
890 new Zend_Validate_Between(1, 12),
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';
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');
946 new Zend_Validate_Between(1, 12),
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');
977 new Zend_Validate_Between(1, 12),
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()
1002 $validators = array(
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()
1019 $validators = array(
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()
1036 $validators = array(
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()
1058 $validators = array(
1059 'monthRule' => array(
1061 Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_REQUIRED
,
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()
1086 $validators = array(
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()
1102 $validators = array(
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()
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()
1149 'field1' => 'abc123',
1150 'field2' => 'abcdef'
1152 $validators = array(
1153 'field1' => '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()
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));
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()',
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();
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()',
1261 public function testNamespaceExceptionClassNotFound()
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);
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()
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);
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",
1304 public function testSetDefaultEscapeFilter()
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);
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()
1338 $validators = 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()
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),
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()
1387 'field1' => ' ab&c '
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()
1409 $validators = array(
1410 'field1' => 'MyDigits',
1411 'field2' => 'MyDigits',
1412 'field3' => 'digits'
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()
1444 // field2 is missing deliberately
1446 $validators = array(
1447 'field1' => 'Digits',
1448 'field2' => 'Digits'
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()
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()
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&c', $input->getEscaped('field1'));
1494 $this->assertNull($input->getEscaped('field2'));
1497 public function testGetEscapedAllFields()
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&c'), $input->getEscaped());
1512 public function testMagicGetEscaped()
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&c', $input->field1
);
1525 $this->assertNull($input->field2
);
1528 public function testGetEscapedMultiValue()
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&H', 'B&O', 'AT&T'), $multi);
1546 public function testGetUnescaped()
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()
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()
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()
1597 'field2' => '123abc'
1600 '*' => 'StringTrim',
1601 'field2' => 'digits'
1603 $validators = array(
1604 'field1' => array(Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_OPTIONAL
),
1607 Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_REQUIRED
1610 $input = new Zend_Filter_Input($filters, $validators, $data);
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()
1626 'field2' => '123abc',
1627 'field3' => 'unknown'
1630 '*' => 'StringTrim',
1631 'field2' => 'digits'
1633 $validators = array(
1634 'field1' => array(Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_OPTIONAL
),
1637 Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_REQUIRED
1640 $input = new Zend_Filter_Input($filters, $validators, $data);
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()
1656 'field2' => 'abc' // invalid because no digits
1659 '*' => 'StringTrim',
1660 'field2' => 'digits'
1662 $validators = array(
1663 'field1' => array(Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_OPTIONAL
),
1666 Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_REQUIRED
1669 $input = new Zend_Filter_Input($filters, $validators, $data);
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()
1688 // field2 is missing on purpose for this test
1691 '*' => 'StringTrim',
1692 'field2' => 'digits'
1694 $validators = array(
1696 Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_OPTIONAL
1700 Zend_Filter_Input
::PRESENCE
=> Zend_Filter_Input
::PRESENCE_REQUIRED
1703 $input = new Zend_Filter_Input($filters, $validators, $data);
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');
1721 public function testInsertingNullDoesNotGetEscapedWithDefaultEscapeMethod()
1723 $input = new Zend_Filter_Input(null, null, array('test' => null));
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');
1739 public function testPluginLoaderInputNamespaceWithSameNameFilterAndValidatorLeadsToException()
1742 'date1' => array('Date')
1744 $validators = array(
1745 'date1' => array('Date')
1748 'date1' => '1990-01-01'
1751 'inputNamespace' => array('MyZend_Filter', 'MyZend_Validate'),
1753 $filter = new Zend_Filter_Input($filters, $validators, $data, $options);
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",
1769 public function testPluginLoaderWithFilterValidateNamespaceWithSameNameFilterAndValidatorWorksPerfectly()
1773 'date1' => array('Date')
1775 $validators = array(
1776 'date1' => array('Date')
1779 'date1' => '1990-01-01'
1782 'filterNamespace' => array('MyZend_Filter'),
1783 'validatorNamespace' => array('MyZend_Validate'),
1785 $filter = new Zend_Filter_Input($filters, $validators, $data, $options);
1789 $this->assertEquals("2000-01-01", $filter->date1
);
1790 } catch(Zend_Filter_Exception
$e) {
1796 'filterNamespace' => 'MyZend_Filter',
1797 'validatorNamespace' => 'MyZend_Validate',
1799 $filter = new Zend_Filter_Input($filters, $validators, $data, $options);
1803 $this->assertEquals("2000-01-01", $filter->date1
);
1804 } catch(Zend_Filter_Exception
$e) {
1812 public function testValidatorAllowNull()
1817 $validators = array(
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
);
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(
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.',
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']);
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')
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']);
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'))
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']);
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'))
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)
1977 public function getMessages()
1982 public function getErrors()