Open Journal Systems  3.3.0
FormValidatorCustomTest.php
1 <?php
2 
17 import('lib.pkp.tests.PKPTestCase');
18 import('lib.pkp.classes.form.Form');
19 
21  private
22  $checkedValue;
23 
28  public function testIsValid() {
29  $form = new Form('some template');
30  $validationFunction = array($this, 'userValidationFunction');
31 
32  // Tests are completely bypassed when the validation type is
33  // "optional" and the test field is empty. We make sure this is the
34  // case by returning 'false' for the custom validation function.
35  $form->setData('testData', '');
36  $validator = new FormValidatorCustom($form, 'testData', FORM_VALIDATOR_OPTIONAL_VALUE, 'some.message.key', $validationFunction, array(false));
37  self::assertTrue($validator->isValid());
38  self::assertSame(null, $this->checkedValue);
39 
40  // Simulate valid data
41  $form->setData('testData', 'xyz');
42  $validator = new FormValidatorCustom($form, 'testData', FORM_VALIDATOR_REQUIRED_VALUE, 'some.message.key', $validationFunction, array(true));
43  self::assertTrue($validator->isValid());
44  self::assertSame('xyz', $this->checkedValue);
45 
46  // Simulate invalid data
47  $form->setData('testData', 'xyz');
48  $validator = new FormValidatorCustom($form, 'testData', FORM_VALIDATOR_REQUIRED_VALUE, 'some.message.key', $validationFunction, array(false));
49  self::assertFalse($validator->isValid());
50  self::assertSame('xyz', $this->checkedValue);
51 
52  // Simulate valid data with negation of the user function return value
53  $form->setData('testData', 'xyz');
54  $validator = new FormValidatorCustom($form, 'testData', FORM_VALIDATOR_REQUIRED_VALUE, 'some.message.key', $validationFunction, array(false), true);
55  self::assertTrue($validator->isValid());
56  self::assertSame('xyz', $this->checkedValue);
57  }
58 
69  public function userValidationFunction($value, $additionalArgument) {
70  $this->checkedValue = $value;
71  return $additionalArgument;
72  }
73 }
74 
PKPTestCase
Class that implements functionality common to all PKP unit test cases.
Definition: PKPTestCase.inc.php:27
FormValidatorCustomTest\userValidationFunction
userValidationFunction($value, $additionalArgument)
Definition: FormValidatorCustomTest.php:69
FormValidatorCustomTest\testIsValid
testIsValid()
Definition: FormValidatorCustomTest.php:28
Form
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
FormValidatorCustom
Form validation check with a custom user function performing the validation check.
Definition: FormValidatorCustom.inc.php:18
FormValidatorCustomTest
Test class for FormValidatorCustom.
Definition: FormValidatorCustomTest.php:20