| 1 |
<?php |
| 2 |
|
| 3 |
require_once __DIR__ . '/classes/wp_cassify_rule_solver.php'; |
| 4 |
|
| 5 |
function run_tests() { |
| 6 |
|
| 7 |
require_once __DIR__ . '/config.php'; |
| 8 |
|
| 9 |
$mock_cas_object = [ |
| 10 |
'first_name' => 'Maria2', |
| 11 |
'email' => 'awoods1a@toplist.cz', |
| 12 |
'note' => 'team -OR blue', |
| 13 |
'pattern_like' => 'a.b[c](d)+^$', |
| 14 |
'complex_note' => 'prefix (-AND) .* suffix', |
| 15 |
'eduPersonAffiliation' => ['alumn', 'student', 'affiliate'] |
| 16 |
]; |
| 17 |
|
| 18 |
$solver = new \wp_cassify\wp_cassify_rule_solver(); |
| 19 |
|
| 20 |
$solver->match_first_level_parenthesis_group_pattern = $wp_cassify_match_first_level_parenthesis_group_pattern; |
| 21 |
$solver->match_second_level_parenthesis_group_pattern = $wp_cassify_match_second_level_parenthesis_group_pattern; |
| 22 |
$solver->match_cas_variable_pattern = $wp_cassify_match_cas_variable_pattern; |
| 23 |
$solver->allowed_operators = $wp_cassify_allowed_operators; |
| 24 |
$solver->operator_prefix = $wp_cassify_operator_prefix; |
| 25 |
$solver->allowed_parenthesis = $wp_cassify_allowed_parenthesis; |
| 26 |
$solver->error_messages = $wp_cassify_error_messages; |
| 27 |
$solver->cas_user_datas = $mock_cas_object; |
| 28 |
|
| 29 |
$tests = [ |
| 30 |
'(CAS{first_name} -EQ "Maria") -AND (CAS{email} -CONTAINS "mhawkins0@mashable.com")' => false, |
| 31 |
'(CAS{first_name} -EQ "Maria2") -AND (CAS{email} -CONTAINS "awoods1a@toplist.cz")' => true, |
| 32 |
'(CAS{first_name} -NEQ "Maria2") -AND (CAS{email} -CONTAINS "awoods1a@toplist.cz")' => false, |
| 33 |
'(CAS{first_name} -NEQ "Maria2") -OR (CAS{email} -CONTAINS "awoods1a@toplist.cz")' => true, |
| 34 |
'[(CAS{eduPersonAffiliation} -CONTAINS "student") -AND (CAS{email} -ENDWITH "toplist.cz")]' => true, |
| 35 |
'(CAS{eduPersonAffiliation} -IN "alumn")' => true, |
| 36 |
'(CAS{eduPersonAffiliation} -IN "al")' => false, |
| 37 |
'(CAS{eduPersonAffiliation} -NOTIN "alumn")' => false, |
| 38 |
'(CAS{eduPersonAffiliation} -NOTIN "student")' => false, |
| 39 |
'(CAS{eduPersonAffiliation} -NOTIN "al")' => true, |
| 40 |
'(CAS{eduPersonAffiliation} -CONTAINS "al")' => true, |
| 41 |
'(CAS{eduPersonAffiliation} -NCONTAINS "la")' => true, |
| 42 |
'(CAS{eduPersonAffiliation} -NCONTAINS "al")' => false, |
| 43 |
'(CAS{first_name} -STARTWITH "Mari"))' => true, |
| 44 |
'(CAS{first_name} -STARTWITH "ari"))' => false, |
| 45 |
'(CAS{first_name} -ENDWITH "2"))' => true, |
| 46 |
'(CAS{first_name} -ENDWITH "aria"))' => false, |
| 47 |
'(CAS{first_name} -NCONTAINS "Ma")' => false, |
| 48 |
'(CAS{note} -CONTAINS "-OR")' => true, |
| 49 |
'(CAS{pattern_like} -CONTAINS "a.b[c]")' => true, |
| 50 |
'(CAS{pattern_like} -CONTAINS "a.*")' => false, |
| 51 |
'(CAS{complex_note} -CONTAINS "(-AND) .*")' => true, |
| 52 |
'(CAS{missing_attr} -EQ "")' => true, |
| 53 |
'(CAS{missing_attr} -NEQ "")' => false, |
| 54 |
]; |
| 55 |
|
| 56 |
$all_tests_passed = true; |
| 57 |
foreach ($tests as $condition => $expected) { |
| 58 |
$result = $solver->solve($condition); |
| 59 |
assert($result === $expected, "Test failed: '$condition' expected " . var_export($expected, true) . " but got " . var_export($result, true)); |
| 60 |
if($result === $expected) { |
| 61 |
echo "\tTest OK : '$condition' => " . var_export($expected, true) . "\n"; |
| 62 |
} else { |
| 63 |
$all_tests_passed = false; |
| 64 |
echo "\tTest KO : '$condition' => " . var_export($result, true) . " but " . var_export($expected, true) . " was expected\n"; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if (!$all_tests_passed) { |
| 69 |
exit(1); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
run_tests(); |
| 74 |
|
| 75 |
echo "All tests executed.\n"; |
| 76 |
|