@@ -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+ 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+ 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