Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1951 from fusioninventory/fix-#1925
Browse files Browse the repository at this point in the history
Fix sql error on import rules and models. closes #1925
  • Loading branch information
David Durieux authored Nov 2, 2016
2 parents ef47bd2 + af62c6a commit 338860e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
20 changes: 12 additions & 8 deletions inc/inventoryruleimport.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,10 @@ function findWithGlobalCriteria($input) {
$sql_from = '';
$sql_where_computer= '';
$sql_where_domain = '';
$sql_where_model = '';
$sql_from_computer = '';
$sql_from_domain = '';
$sql_from_model = '';
$continue = TRUE;
$entityRestrict = FALSE;
$global_criteria = array('model',
Expand Down Expand Up @@ -477,15 +479,11 @@ function findWithGlobalCriteria($input) {
switch ($criteria->fields['criteria']) {

case 'model':
$sql_from_temp = " LEFT JOIN `glpi_".strtolower("[typename]")."models`
ON (`glpi_".strtolower("[typename]")."models`.`id` = ".
"`[typetable]`.`".strtolower("[typename]models_id")."`
AND `glpi_networkports`.`itemtype` = '[typename]') ";
$sql_where_temp = " AND `[typetable]`.`".strtolower("[typename]")."models_id` = '".
$sql_from_model = " LEFT JOIN `glpi_[typenamefortable]models`
ON (`glpi_[typenamefortable]models`.`id` = ".
"`[typetable]`.`[typenamefortable]models_id`) ";
$sql_where_model = " AND `[typetable]`.`[typenamefortable]models_id` = '".
$input["model"]."'";

$sql_from .= $sql_from_temp;
$sql_where .= $sql_where_temp;
break;

case 'mac':
Expand Down Expand Up @@ -603,6 +601,11 @@ function findWithGlobalCriteria($input) {
$sql_where_temp .= $sql_where_domain;
}

if ($itemtype != 'PluginFusioninventoryUnmanaged') {
$sql_from_temp .= $sql_from_model;
$sql_where_temp .= $sql_where_model;
}

if ($entityRestrict) {
if (isset($_SESSION['plugin_fusioninventory_entityrestrict'])) {
$sql_where_temp .= " AND `[typetable]`.`entities_id`='".
Expand All @@ -626,6 +629,7 @@ function findWithGlobalCriteria($input) {
}
$sql_glpi = str_replace("[typetable]", $item->getTable(), $sql_glpi);
$sql_glpi = str_replace("[typename]", $itemtype, $sql_glpi);
$sql_glpi = str_replace("[typenamefortable]", strtolower($itemtype), $sql_glpi);

PluginFusioninventoryToolbox::logIfExtradebug(
"pluginFusioninventory-rules",
Expand Down
34 changes: 31 additions & 3 deletions phpunit/2_Integration/RuleImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,43 @@ protected function setUp() {
SET `is_active`='0'
WHERE `sub_type`='PluginFusioninventoryInventoryRuleImport'");

$rule = new Rule();
// Add a rule test check model
$input = array();
$input['is_active']=1;
$input['name']='Printer model';
$input['match']='AND';
$input['sub_type'] = 'PluginFusioninventoryInventoryRuleImport';
$input['ranking'] = 198;
$rule_id = $rule->add($input);

// Add criteria
$rulecriteria = new RuleCriteria();
$input = array();
$input['rules_id'] = $rule_id;
$input['criteria'] = "model";
$input['pattern']= '1';
$input['condition']=10;
$rulecriteria->add($input);

// Add action
$ruleaction = new RuleAction();
$input = array();
$input['rules_id'] = $rule_id;
$input['action_type'] = 'assign';
$input['field'] = '_fusion';
$input['value'] = '1';
$ruleaction->add($input);


// Add a rule to ignore import
// Create rule for import into unknown devices
$rule = new Rule();
$input = array();
$input['is_active']=1;
$input['name']='Import pinter';
$input['match']='AND';
$input['sub_type'] = 'PluginFusioninventoryInventoryRuleImport';
$input['ranking'] = 198;
$input['ranking'] = 199;
$rule_id = $rule->add($input);

// Add criteria
Expand Down Expand Up @@ -113,7 +141,7 @@ protected function setUp() {
$input['name']='Import pinter';
$input['match']='AND';
$input['sub_type'] = 'PluginFusioninventoryInventoryRuleImport';
$input['ranking'] = 199;
$input['ranking'] = 200;
$rule_id = $rule->add($input);

// Add criteria
Expand Down
4 changes: 2 additions & 2 deletions phpunit/2_Integration/RuleLocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function RegexpRuleResultAssignTest() {

$a_references = array(
'locations_id' => 1,
'_ruleid' => 50
'_ruleid' => $rules_id
);

$this->assertEquals($a_references, $loc, 'Location result assign_result');
Expand Down Expand Up @@ -159,7 +159,7 @@ public function RegexpRuleResultRegexpTest() {

$a_references = array(
'locations_id' => 1,
'_ruleid' => 51
'_ruleid' => $rules_id
);

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

0 comments on commit 338860e

Please sign in to comment.