Skip to content

Commit 81457ae

Browse files
authored
Add test on mail collector rules with known domain (#21563)
* Add test on mail collector rules with known domain Possibly fix an issue closes #21561 * CS
1 parent e9eafdc commit 81457ae

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/RuleMailCollectorCollection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public function prepareInputDataForProcess($input, $params)
5757
if (isset($params['_users_id_requester'])) {
5858
$input['_users_id_requester'] = $params['_users_id_requester'];
5959
}
60+
if (isset($params['from'])) {
61+
$input['from'] = $params['from'];
62+
}
6063

6164
$fields = $this->getFieldsToLookFor();
6265

@@ -125,7 +128,6 @@ public function prepareInputDataForProcess($input, $params)
125128
}
126129
}
127130

128-
// Store the number of profiles of which the user belongs to
129131
if (in_array('known_domain', $fields, true)) {
130132
if (preg_match("/@(.*)/", $input['from'], $results)) {
131133
if (Entity::getEntityIDByDomain($results[1]) !== -1) {

tests/functional/RuleMailCollectorTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,97 @@ public function testAssignEntityBasedOnGroup()
140140
);
141141
}
142142

143+
public function testAssignEntityBasedOnKnowDomain()
144+
{
145+
$this->login();
146+
147+
$entity = getItemByTypeName('Entity', '_test_child_1');
148+
$entity_id = $entity->getID();
149+
$this->assertTrue(
150+
$entity->update([
151+
'id' => $entity_id,
152+
'mail_domain' => 'glpi-project.org',
153+
])
154+
);
155+
$entity->getFromDB($entity_id);
156+
$this->assertSame('glpi-project.org', $entity->getField('mail_domain'));
157+
$normal_user_id = getItemByTypeName('User', 'normal', true);
158+
159+
// Delete all existing rule
160+
$rule = new Rule();
161+
$rule->deleteByCriteria(['sub_type' => 'RuleMailCollector']);
162+
163+
// Create rule
164+
$rule = new \RuleMailCollector();
165+
$rule_id = $rule->add($rule_input = [
166+
'name' => 'test assign entity based on known domain',
167+
'match' => 'AND',
168+
'is_active' => 1,
169+
'sub_type' => 'RuleMailCollector',
170+
]);
171+
$this->checkInput($rule, $rule_id, $rule_input);
172+
173+
// Create criteria to check if requester group matches a specific group
174+
$criteria = new RuleCriteria();
175+
$criteria_id = $criteria->add($criteria_input = [
176+
'rules_id' => $rule_id,
177+
'criteria' => 'KNOWN_DOMAIN',
178+
'condition' => Rule::PATTERN_IS,
179+
'pattern' => 1,
180+
]);
181+
$this->checkInput($criteria, $criteria_id, $criteria_input);
182+
183+
// Create action to assign entity
184+
$action = new RuleAction();
185+
$action_id = $action->add($action_input = [
186+
'rules_id' => $rule_id,
187+
'action_type' => 'assign',
188+
'field' => 'entities_id',
189+
'value' => $entity_id,
190+
]);
191+
$this->checkInput($action, $action_id, $action_input);
192+
193+
// Check rules output: no rule should match
194+
$rulecollection = new RuleMailCollectorCollection();
195+
$output = $rulecollection->processAllRules(
196+
[],
197+
[],
198+
[
199+
'mailcollector' => 0,
200+
'_users_id_requester' => $normal_user_id,
201+
'from' => '[email protected]',
202+
]
203+
);
204+
205+
$this->assertEquals(
206+
[
207+
'_no_rule_matches' => '1',
208+
'_rule_process' => '',
209+
],
210+
$output
211+
);
212+
213+
// Check rules output: new rule should match
214+
$rulecollection = new RuleMailCollectorCollection();
215+
$output = $rulecollection->processAllRules(
216+
[],
217+
[],
218+
[
219+
'mailcollector' => 0,
220+
'_users_id_requester' => $normal_user_id,
221+
'from' => '[email protected]',
222+
]
223+
);
224+
225+
$this->assertEquals(
226+
[
227+
'entities_id' => $entity_id,
228+
'_ruleid' => $rule_id,
229+
],
230+
$output
231+
);
232+
}
233+
143234
public function testExternalID()
144235
{
145236
$entity_id = getItemByTypeName('Entity', '_test_child_1', true);

0 commit comments

Comments
 (0)