Skip to content

Commit

Permalink
Fix negated text-match in prop-filter
Browse files Browse the repository at this point in the history
The negate-condition attribute applies to the individual text-match, not
the result of applying the non-negated match against all property
instances of the asked for type.
  • Loading branch information
mstilkerich authored and phil-davis committed Feb 12, 2021
1 parent 7820ac4 commit 505e905
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/CardDAV/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,15 @@ protected function validateTextMatches(array $texts, array $filters, $test)
$success = false;
foreach ($texts as $haystack) {
$success = DAV\StringUtil::textMatch($haystack, $filter['value'], $filter['collation'], $filter['match-type']);
if ($filter['negate-condition']) {
$success = !$success;
}

// Breaking on the first match
if ($success) {
break;
}
}
if ($filter['negate-condition']) {
$success = !$success;
}

if ($success && 'anyof' === $test) {
return true;
Expand Down
15 changes: 13 additions & 2 deletions tests/Sabre/CardDAV/ValidateFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ public function data()
$filter14['text-matches'][0]['value'] = 'bing';
$filter14['text-matches'][0]['negate-condition'] = true;

// Check if there is an EMAIL address that does not have the 111.com domain
$filterEmailWithoutSpecificDomain = $filter11;
$filterEmailWithoutSpecificDomain['name'] = 'email';
$filterEmailWithoutSpecificDomain['text-matches'][0]['value'] = '@111.com';
$filterEmailWithoutSpecificDomain['text-matches'][0]['negate-condition'] = true;

// Param filter with text
// Check there is a TEL;TYPE that contains WORK
$filter15 = $filter5;
Expand All @@ -154,6 +160,9 @@ public function data()

// Param filter + text filter
$filter17 = $filter5;

// Matches if the VCard contains a TEL property that either has a TYPE property defined (-> true),
// or that has a value containing 444 (true)
$filter17['test'] = 'anyof';
$filter17['text-matches'][] = [
'match-type' => 'contains',
Expand All @@ -162,6 +171,8 @@ public function data()
'negate-condition' => false,
];

// Matches if the VCard contains a TEL property that has a TYPE property defined
// AND that has a value NOT containing 444 -> there is 3 properties matching these criteria
$filter18 = $filter17;
$filter18['text-matches'][0]['negate-condition'] = true;

Expand Down Expand Up @@ -198,6 +209,7 @@ public function data()
[$body1, [$filter12], 'anyof', false],
[$body1, [$filter13], 'anyof', false],
[$body1, [$filter14], 'anyof', true],
[$body1, [$filterEmailWithoutSpecificDomain], 'anyof', true, "EMAIL properties with other domain exists, so this should return true"],

// Param filter with text-match
[$body1, [$filter15], 'anyof', true, 'TEL;TYPE with value WORK exists, so this should return true' ],
Expand All @@ -206,8 +218,7 @@ public function data()

// Param filter + text filter
[$body1, [$filter17], 'anyof', true],
[$body1, [$filter18], 'anyof', false],
[$body1, [$filter18], 'anyof', false],
[$body1, [$filter18], 'anyof', true],
];
}
}

0 comments on commit 505e905

Please sign in to comment.