Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(rule mail collector): add criterion for quarantine released #18943

Open
wants to merge 5 commits into
base: 10.0/bugfixes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions phpunit/functional/RuleMailCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,158 @@ public function testAssignEntityBasedOnGroup()
$output
);
}

public static function headerCriteriasProvider()
{
return [
[
'headers' =>
[
'auto-submitted' => 'auto-replied',
],
'rule_param' => [
'match' => 'AND',
'criterias' => [
[
'criteria' => 'auto-submitted',
'condition' => Rule::REGEX_MATCH,
'pattern' => '/^(?!.*no).+$/i',
],
],
],
'result_match' => true,
],
[
'headers' =>
[
'auto-submitted' => 'no',
],
'rule_param' => [
'match' => 'AND',
'criterias' => [
[
'criteria' => 'auto-submitted',
'condition' => Rule::REGEX_MATCH,
'pattern' => '/^(?!.*no).+$/i',
],
],
],
'result_match' => false,
],
[
'headers' =>
[
'auto-submitted' => 'auto-replied',
'x-ms-exchange-organization-expirationstarttimereason' => 'QuarantineReleased',
],
'rule_param' => [
'match' => 'AND',
'criterias' => [
[
'criteria' => 'auto-submitted',
'condition' => Rule::REGEX_MATCH,
'pattern' => '/^(?!.*no).+$/i',
],
[
'criteria' => '_headers',
'condition' => Rule::PATTERN_NOT_CONTAIN,
'pattern' => 'X-MS-Exchange-Organization-ExpirationStartTimeReason: QuarantineReleased',
],
],
],
'result_match' => false,
],
[
'headers' =>
[
'auto-submitted' => 'auto-replied',
],
'rule_param' => [
'match' => 'AND',
'criterias' => [
[
'criteria' => 'auto-submitted',
'condition' => Rule::REGEX_MATCH,
'pattern' => '/^(?!.*no).+$/i',
],
[
'criteria' => '_headers',
'condition' => Rule::PATTERN_NOT_CONTAIN,
'pattern' => 'X-MS-Exchange-Organization-ExpirationStartTimeReason: QuarantineReleased',
],
],
],
'result_match' => true,
],
];
}

/**
* @dataProvider headerCriteriasProvider
*/
public function testHeaderCriterias(
array $headers,
array $rule_param,
bool $result_match
) {
$this->login();

// Delete all existing rule
$rule = new Rule();
$rule->deleteByCriteria(['sub_type' => 'RuleMailCollector']);

// Create rule
$rule = new \RuleMailCollector();
$rule_id = $rule->add($rule_input = [
'name' => __FUNCTION__,
'match' => $rule_param['match'],
'is_active' => 1,
'sub_type' => 'RuleMailCollector',
]);
$this->checkInput($rule, $rule_id, $rule_input);

// Add criterias
foreach ($rule_param['criterias'] as $criteria_input) {
$criteria = new RuleCriteria();
$criteria_input['rules_id'] = $rule_id;
$criteria_id = $criteria->add($criteria_input);
$this->checkInput($criteria, $criteria_id, $criteria_input);
}

// Create action
$action = new RuleAction();
$action_id = $action->add($action_input = [
'rules_id' => $rule_id,
'action_type' => 'assign',
'field' => '_refuse_email_no_response',
'value' => 1,
]);
$this->checkInput($action, $action_id, $action_input);

// Check rules output: no rule should match
$rulecollection = new RuleMailCollectorCollection();
$output = $rulecollection->processAllRules(
[],
[],
['headers' => $headers]
);

if ($result_match) {
$this->assertEquals(
[
'_refuse_email_no_response' => 1,
'_ruleid' => $rule_id,
],
$output
);
} else {
$this->assertEquals(
[
'_no_rule_matches' => '1',
'_rule_process' => '',
],
$output
);
}
}
}
6 changes: 6 additions & 0 deletions src/RuleMailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public function getCriterias()
$criterias['x-uce-status']['table'] = '';
$criterias['x-uce-status']['type'] = 'text';

$criterias['_headers'] = [
'name' => __('Full email headers'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be more precise:

Suggested change
'name' => __('Full email headers'),
'name' => __('Entire email header string'),

@cconard96 What would be the most relevant text here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just Headers would be fine IMO.

If headers contains XXX then ...

'table' => '',
'type' => 'text',
];

$criterias['received']['name'] = __('Received email header');
$criterias['received']['table'] = '';
$criterias['received']['type'] = 'text';
Expand Down
8 changes: 8 additions & 0 deletions src/RuleMailCollectorCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public function prepareInputDataForProcess($input, $params)
$input[$key] = $value;
}
}
$input['_headers'] = implode(
"\n",
array_map(
fn($k, $v) => is_array($v) ? "$k: " . implode(', ', $v) : "$k: $v",
array_keys($params['headers']),
$params['headers']
)
);
}

//Add all user's groups
Expand Down