<commit_before><?php

/*
   ------------------------------------------------------------------------
   FusionInventory
   Copyright (C) 2010-2016 by the FusionInventory Development Team.

   http://www.fusioninventory.org/   http://forge.fusioninventory.org/
   ------------------------------------------------------------------------

   LICENSE

   This file is part of FusionInventory project.

   FusionInventory is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   FusionInventory is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.

   ------------------------------------------------------------------------

   @package   FusionInventory
   @author    <NAME>
   @co-author
   @copyright Copyright (c) 2010-2016 FusionInventory team
   @license   AGPL License 3.0 or (at your option) any later version
              http://www.gnu.org/licenses/agpl-3.0-standalone.html
   @link      http://www.fusioninventory.org/
   @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
   @since     2015

   ------------------------------------------------------------------------
 */

class RuleLocationTest extends Common_TestCase {


   /**
    * @test
    */
   public function RegexpRuleResultAssignTest() {
      global $DB, $PF_CONFIG;

      $DB->connect();

      $rule     = new Rule();
      $location = new Location();

      $location->deleteByCriteria(['name' => 'Monsols04'], true);
      $rule->deleteByCriteria(['sub_type' => 'PluginFusioninventoryInventoryRuleLocation'], true);
      $input = [
         'name'        => 'Monsols04',
         'entities_id' => 0
      ];
      $locations_id = $location->add($input);

      $input = [
         'is_active' => 1,
         'name'      => 'Location regexp',
         'match'     => 'AND',
         'sub_type'  => 'PluginFusioninventoryInventoryRuleLocation',
         'ranking'   => 1
      ];
      $rules_id = $rule->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = [
         'rules_id'  => $rules_id,
         'criteria'  => "name",
         'pattern'   => "/Item (.*)/",
         'condition' => PluginFusioninventoryInventoryRuleLocation::REGEX_MATCH
      ];
      $rulecriteria->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = [
         'rules_id'  => $rules_id,
         'criteria'  => "itemtype",
         'pattern'   => "/Computer|NetworkEquipment/",
         'condition' => Rule::REGEX_MATCH
      ];
      $rulecriteria->add($input);

      // Add action
      $ruleaction = new RuleAction();
      $input = [
         'rules_id'    => $rules_id,
         'action_type' => 'assign',
         'field'       => 'locations_id',
         'value'       => $locations_id
      ];
      $ruleaction->add($input);

      $input = [
         'name'     => 'Item Monsols04',
         'itemtype' => 'Computer'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;
      $ruleLocation = new PluginFusioninventoryInventoryRuleLocationCollection();
      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $a_references = [
         'locations_id' => $locations_id,
         '_ruleid'      => $rules_id
      ];

      $this->assertEquals($a_references, $loc, 'Location result assign_result');

      //This time it should not match because
      $input = [
         'name'     => 'Printer01',
         'itemtype' => 'Printer'
      ];

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $this->assertEquals($a_references, $loc);
      $rule->delete(['id' => $rules_id], true);
   }


   /**
    * @test
    */
   public function RegexpRuleResultRegexpTest() {
      global $DB, $PF_CONFIG;

      $DB->connect();

      $rule = new Rule();
      $rule->deleteByCriteria(['sub_type' => 'PluginFusioninventoryInventoryRuleLocation'], true);
      $location = new Location;
      $location->getFromDBByCrit(['completename' => 'Monsols04']);;

      $input = [
         'is_active' => 1,
         'name'      => 'Location regexp pc',
         'match'     => 'AND',
         'sub_type'  => 'PluginFusioninventoryInventoryRuleLocation',
         'ranking'   => 1
      ];
      $rules_id = $rule->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = array(
         'rules_id'  => $rules_id,
         'criteria'  => "name",
         'pattern'   => "/pc (.*)/",
         'condition' => PluginFusioninventoryInventoryRuleLocation::REGEX_MATCH
      );
      $rulecriteria->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = array(
         'rules_id'  => $rules_id,
         'criteria'  => "itemtype",
         'pattern'   => "Computer",
         'condition' => Rule::PATTERN_IS
      );
      $rulecriteria->add($input);

      // Add action
      $ruleaction = new RuleAction();
      $input = array(
         'rules_id'    => $rules_id,
         'action_type' => 'regex_result',
         'field'       => 'locations_id',
         'value'       => '#0'
      );
      $ruleaction->add($input);

      $ruleLocation = new PluginFusioninventoryInventoryRuleLocationCollection();

      $input = [
         'name'     => 'pc Monsols04',
         'itemtype' => 'Computer'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;
      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $a_references = array(
         'locations_id' => $location->getID(),
         '_ruleid'      => $rules_id
         ];

      $this->assertEquals($a_references, $loc, 'Location result regexp_result');

      $input = [
         'name'     => 'Monitor Monsols04',
         'itemtype' => 'Monitor'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $this->assertEquals($a_references, $loc);
   }

   /**
    * @test
    */
   public function RegexpRuleByIPTest() {
      global $DB, $PF_CONFIG;

      $DB->connect();

      $rule = new Rule();
      $rule->deleteByCriteria(['sub_type' => 'PluginFusioninventoryInventoryRuleLocation'], true);
      $location = new Location;
      $location->getFromDBByCrit(['completename' => 'Monsols04']);;

      $input = array(
         'is_active' => 1,
         'name'      => 'Location by IP',
         'match'     => 'AND',
         'sub_type'  => 'PluginFusioninventoryInventoryRuleLocation',
         'ranking'   => 1
      );
      $rules_id = $rule->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = array(
         'rules_id'  => $rules_id,
         'criteria'  => "ip",
         'pattern'   => "192.168.0",
         'condition' => Rule::PATTERN_CONTAIN
      );
      $rulecriteria->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = array(
         'rules_id'  => $rules_id,
         'criteria'  => "itemtype",
         'pattern'   => "/Computer|NetworkEquipment|Printer/",
         'condition' => Rule::REGEX_MATCH
      );
      $rulecriteria->add($input);

      // Add action
      $ruleaction = new RuleAction();
      $input = array(
         'rules_id'    => $rules_id,
         'action_type' => 'assign',
         'field'       => 'locations_id',
         'value'       => $location->getID()
      );
      $ruleaction->add($input);

      $ruleLocation = new PluginFusioninventoryInventoryRuleLocationCollection();

      $input = [
         'name'     => '<NAME>',
         'ip'       => '192.168.0.10',
         'itemtype' => 'Computer'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;
      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $a_references = array(
         'locations_id' => $location->getID(),
         '_ruleid'      => $rules_id
      );

      $this->assertEquals($a_references, $loc, 'Location by IP');

      $input = [
         'name'     => '<NAME>',
         'ip'       => '172.16.58.3',
         'itemtype' => 'Computer'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;
      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $this->assertEquals($a_references, $loc, 'Location by IP');

      $input = [
         'name'     => 'Monitor Monsols04',
         'ip'       => '192.168.0.10',
         'itemtype' => 'Monitor'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $this->assertEquals($a_references, $loc);

      //Peripheral is an itemtype that can be targeted by the rules engine
      $input = [
         'name'     => 'Peripheral Monsols04',
         'ip'       => '192.168.0.10',
         'itemtype' => 'Peripheral'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $this->assertEquals($a_references, $loc);

>>>>>>> 70d48a869... add capability to location rule engine to work with snmp asset (#2352)
   }


}<commit_msg>Fix merge location rules<commit_after><?php

/*
   ------------------------------------------------------------------------
   FusionInventory
   Copyright (C) 2010-2016 by the FusionInventory Development Team.

   http://www.fusioninventory.org/   http://forge.fusioninventory.org/
   ------------------------------------------------------------------------

   LICENSE

   This file is part of FusionInventory project.

   FusionInventory is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   FusionInventory is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.

   ------------------------------------------------------------------------

   @package   FusionInventory
   @author    <NAME>
   @co-author
   @copyright Copyright (c) 2010-2016 FusionInventory team
   @license   AGPL License 3.0 or (at your option) any later version
              http://www.gnu.org/licenses/agpl-3.0-standalone.html
   @link      http://www.fusioninventory.org/
   @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
   @since     2015

   ------------------------------------------------------------------------
 */

class RuleLocationTest extends Common_TestCase {


   /**
    * @test
    */
   public function RegexpRuleResultAssignTest() {
      global $DB, $PF_CONFIG;

      $DB->connect();

      $rule     = new Rule();
      $location = new Location();

      $location->deleteByCriteria(['name' => 'Monsols04'], true);
      $rule->deleteByCriteria(['sub_type' => 'PluginFusioninventoryInventoryRuleLocation'], true);
      $input = [
         'name'        => 'Monsols04',
         'entities_id' => 0
      ];
      $locations_id = $location->add($input);

      $input = [
         'is_active' => 1,
         'name'      => 'Location regexp',
         'match'     => 'AND',
         'sub_type'  => 'PluginFusioninventoryInventoryRuleLocation',
         'ranking'   => 1
      ];
      $rules_id = $rule->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = [
         'rules_id'  => $rules_id,
         'criteria'  => "name",
         'pattern'   => "/Item (.*)/",
         'condition' => PluginFusioninventoryInventoryRuleLocation::REGEX_MATCH
      ];
      $rulecriteria->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = [
         'rules_id'  => $rules_id,
         'criteria'  => "itemtype",
         'pattern'   => "/Computer|NetworkEquipment/",
         'condition' => Rule::REGEX_MATCH
      ];
      $rulecriteria->add($input);

      // Add action
      $ruleaction = new RuleAction();
      $input = [
         'rules_id'    => $rules_id,
         'action_type' => 'assign',
         'field'       => 'locations_id',
         'value'       => $locations_id
      ];
      $ruleaction->add($input);

      $input = [
         'name'     => 'Item Monsols04',
         'itemtype' => 'Computer'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;
      $ruleLocation = new PluginFusioninventoryInventoryRuleLocationCollection();
      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $a_references = [
         'locations_id' => $locations_id,
         '_ruleid'      => $rules_id
      ];

      $this->assertEquals($a_references, $loc, 'Location result assign_result');

      //This time it should not match because
      $input = [
         'name'     => 'Printer01',
         'itemtype' => 'Printer'
      ];

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $this->assertEquals($a_references, $loc);
      $rule->delete(['id' => $rules_id], true);
   }


   /**
    * @test
    */
   public function RegexpRuleResultRegexpTest() {
      global $DB, $PF_CONFIG;

      $DB->connect();

      $rule = new Rule();
      $rule->deleteByCriteria(['sub_type' => 'PluginFusioninventoryInventoryRuleLocation'], true);
      $location = new Location;
      $location->getFromDBByCrit(['completename' => 'Monsols04']);;

      $input = [
         'is_active' => 1,
         'name'      => 'Location regexp pc',
         'match'     => 'AND',
         'sub_type'  => 'PluginFusioninventoryInventoryRuleLocation',
         'ranking'   => 1
      ];
      $rules_id = $rule->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = array(
         'rules_id'  => $rules_id,
         'criteria'  => "name",
         'pattern'   => "/pc (.*)/",
         'condition' => PluginFusioninventoryInventoryRuleLocation::REGEX_MATCH
      );
      $rulecriteria->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = array(
         'rules_id'  => $rules_id,
         'criteria'  => "itemtype",
         'pattern'   => "Computer",
         'condition' => Rule::PATTERN_IS
      );
      $rulecriteria->add($input);

      // Add action
      $ruleaction = new RuleAction();
      $input = array(
         'rules_id'    => $rules_id,
         'action_type' => 'regex_result',
         'field'       => 'locations_id',
         'value'       => '#0'
      );
      $ruleaction->add($input);

      $ruleLocation = new PluginFusioninventoryInventoryRuleLocationCollection();

      $input = [
         'name'     => 'pc Monsols04',
         'itemtype' => 'Computer'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;
      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $a_references = [
         'locations_id' => $location->getID(),
         '_ruleid'      => $rules_id
         ];

      $this->assertEquals($a_references, $loc, 'Location result regexp_result');

      $input = [
         'name'     => 'Monitor Monsols04',
         'itemtype' => 'Monitor'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $this->assertEquals($a_references, $loc);
   }

   /**
    * @test
    */
   public function RegexpRuleByIPTest() {
      global $DB, $PF_CONFIG;

      $DB->connect();

      $rule = new Rule();
      $rule->deleteByCriteria(['sub_type' => 'PluginFusioninventoryInventoryRuleLocation'], true);
      $location = new Location;
      $location->getFromDBByCrit(['completename' => 'Monsols04']);;

      $input = array(
         'is_active' => 1,
         'name'      => 'Location by IP',
         'match'     => 'AND',
         'sub_type'  => 'PluginFusioninventoryInventoryRuleLocation',
         'ranking'   => 1
      );
      $rules_id = $rule->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = array(
         'rules_id'  => $rules_id,
         'criteria'  => "ip",
         'pattern'   => "192.168.0",
         'condition' => Rule::PATTERN_CONTAIN
      );
      $rulecriteria->add($input);

      // Add criteria
      $rulecriteria = new RuleCriteria();
      $input = array(
         'rules_id'  => $rules_id,
         'criteria'  => "itemtype",
         'pattern'   => "/Computer|NetworkEquipment|Printer/",
         'condition' => Rule::REGEX_MATCH
      );
      $rulecriteria->add($input);

      // Add action
      $ruleaction = new RuleAction();
      $input = array(
         'rules_id'    => $rules_id,
         'action_type' => 'assign',
         'field'       => 'locations_id',
         'value'       => $location->getID()
      );
      $ruleaction->add($input);

      $ruleLocation = new PluginFusioninventoryInventoryRuleLocationCollection();

      $input = [
         'name'     => '<NAME>',
         'ip'       => '192.168.0.10',
         'itemtype' => 'Computer'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;
      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $a_references = array(
         'locations_id' => $location->getID(),
         '_ruleid'      => $rules_id
      );

      $this->assertEquals($a_references, $loc, 'Location by IP');

      $input = [
         'name'     => '<NAME>',
         'ip'       => '172.16.58.3',
         'itemtype' => 'Computer'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;
      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $this->assertEquals($a_references, $loc, 'Location by IP');

      $input = [
         'name'     => '<NAME>',
         'ip'       => '192.168.0.10',
         'itemtype' => 'Monitor'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $this->assertEquals($a_references, $loc);

      //Peripheral is an itemtype that can be targeted by the rules engine
      $input = [
         'name'     => 'Peripheral Monsols04',
         'ip'       => '192.168.0.10',
         'itemtype' => 'Peripheral'
      ];

      $_SESSION["plugin_fusioninventory_entity"] = 0;

      $a_references = [
         '_no_rule_matches' => 1,
         '_rule_process'    => ''
      ];

      $ruleLocation->getCollectionPart();
      $loc = $ruleLocation->processAllRules($input);

      $this->assertEquals($a_references, $loc);
   }
}
{'fraction_non_alphanumeric': 0.17214313572542903, 'fraction_numerical': 0.010871684867394696, 'mean_word_length': 2.01086158814032, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 311, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5000599', 'n_tokens_mistral': 6364, 'n_tokens_neox': 5961, 'n_words': 1194}