<?php
namespace Liaison\Test\Addon;
/** Test the POSTValidator addon and the InputValidator utility class
*
*/
class POSTValidator extends \Tlf\Tester {
public function testFailureMessagesOnRedirectToForm(){
// NOTE: The session does not seem to work when POSTing from the test
// I do not know how to fix it
// but manually doing the form submission in the browser allows the session to work, and the failure message prints
$response = $this->post('/addon/redirect-to-form/',
['posint' => '-15']
);
$this->str_contains($response,
'<span class="error">Must be greater than \'0\'</span>'
);
echo $response;
}
public function testFailureMessagesOnForm(){
$response = $this->post('/addon/validated-form/',
['posint' => '-15']
);
$this->str_contains($response,
'<span class="error">Must be greater than \'0\'</span>'
);
}
public function testPOSTFailMsg(){
unset($_POST);
$_POST['posint'] = '-15';
$_POST['publish_status'] = 'undrafted';
$pv = new \Lia\Addon\POSTValidator(new \Lia\Package($lia=new \Lia(),'testpackage'));
$pv->activate($lia);
$pv->validate('posint', 'number|int|minval:0');
$pv->validate('publish_status', 'string|oneof:draft,public,archive');
$msgs = $pv->get_failure_messages();
print_r($msgs);
$this->compare_arrays(
[
'posint' => ['Must be greater than \'0\''],
'publish_status' => ['Must be one of: draft,public,archive'],
],
$msgs, true
);
}
public function testFailureList(){
$v = new \Lia\Utility\InputValidator();
$v->validate('string|number|int|oneof:def', "abc");
$failures = $v->failed_rules;
$this->compare_arrays([
0=>['number',null],
1=>['int',null],
2=>['oneof','def'],
], $failures, true);
}
public function testFailMsg(){
$v = new \Lia\Utility\InputValidator();
$msg = $v->get_failure_message('safehtml');
$this->compare("Some disallowed HTML tags were present. h1-h6, p, div, span, summary/details, small, strike, and other basic formatting tags are available.", $msg);
}
public function testValidatorCombinations(){
$v = new \Lia\Utility\InputValidator();
// rule combination tests
$this->is_true($v->validate('string|number', '12345'), "Numeric string is both a string and a number");
$this->is_true($v->validate(['string'=>null, 'number'=>null], '12345'), "Numeric string is both a string and a number (ruleslist is an array)");
$this->is_false($v->validate(['string'=>null, 'number'=>null], 12345), "Actual number is not a string (ruleslist is an array validating string & number)");
$this->is_false($v->validate(['string'=>null, 'number'=>null], "abcd"), "alpha string is not a number (ruleslist is an array validating string & number)");
$this->is_true($v->validate(['number'=>null,'int','oneof'=>'1,2,3'], "2"), "'2' is a number, int, and is oneof 1,2,3");
$this->is_true($v->validate('string|number|oneof:1,2,3|alnum|minval:0|maxlen:1|int', '1'), '1 passes all rules');
}
public function testValidatorTypeCast(){
$v = new \Lia\Utility\InputValidator();
$num = "23";
$v->validate('int', $num);
$this->is_true(is_string($num), "Original variable was NOT typecast when ref was excluded");
$v->validate('int', $num, null, $num);
$this->is_false(is_string($num), "When var passed as value & as ref, typecast to int works");
$v->validate('int', "17", null, $abc);
$this->is_false(is_string($abc), "When value (*not var*) passed as value, and ref passed, ref is set.");
$v->validate('string', "abc", null, $def);
$this->is_true($def=="abc", "When typecast is NOT performed AND ref is passed, ref is set to value.");
}
/** Test the Utility class */
public function testValidatorRules(){
$v = new \Lia\Utility\InputValidator();
$this->is_true($v->validate('string', 'abcdefhasdfjkl'), "actual string is a string");
$this->is_false($v->validate('string', 12345), "actual number is not a string");
$this->is_true($v->validate('number', 12345), "actual number is number");
$this->is_true($v->validate('number', '12345'), "numeric string is number");
$this->is_true($v->validate('int', '1'), '1 is int');
$this->is_true($v->validate('int', '1234'), '1 is int');
$this->is_false($v->validate('int', '1.5'), '1.5 is not int');
$this->is_false($v->validate('int', 'abc'), 'abc is not int');
$this->is_false($v->validate('int', ''), 'empty string is not int');
$this->is_true($v->validate('alnum', 'a'), 'alnum w/ just alpa');
$this->is_true($v->validate('alnum', '1'), 'alnum w/ just num');
$this->is_true($v->validate('alnum', '1a'), 'alnum w/ just num & alpha');
$this->is_false($v->validate('alnum', '1a&'), 'alnum w/ symbol, should fail validation');
$this->is_true($v->validate('minval:5',5), "Minval is 5, actual value is 5, so should be true.");
$this->is_true($v->validate('minval:5',10), "Minval is 5, actual value is 10, so should be true.");
$this->is_false($v->validate('minval:5',4), "Minval is 5, actual value is 4, so should be false.");
$this->is_true($v->validate('maxlen:1', ''), "maxlen 1. empty string.");
$this->is_true($v->validate('maxlen:1', 'a'), "maxlen 1. string of length 1.");
$this->is_false($v->validate('maxlen:1', 'ab'), "maxlen 1. string of length 2 fails validation.");
$this->is_true($v->validate('oneof:a,b,c', "a"), "a is one of abc");
$this->is_true($v->validate('oneof:a,b,c', "b"), "b is one of abc");
$this->is_true($v->validate('oneof:a,b,c', "c"), "c is one of abc");
$this->is_false($v->validate('oneof:a,b,c', ""), "empty string is not one of abc");
$this->is_false($v->validate('oneof:a,b,c', "d"), "d is not one of abc");
$this->is_false($v->validate('oneof:a,b,c', "ab"), "ab is not one of abc");
}
public function testPostValidator(){
$response = $this->post('/addon/post-validator/',
['TEST_POST_VALIDATOR' => true,
'key_is_present' => true,
'alphanum' => "abc1234",
'num' => "1234",
"alpha" => "abcd",
]
);
$this->str_contains($response,
"POST is empty before validation",
"Validating key alone is successful",
"PLACEHOLDER intended to fail",
);
}
}