Skip to content

Commit

Permalink
Fix(Rule): do not import locations in preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz authored and trasher committed Jun 18, 2024
1 parent e622dbc commit 37179dc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Rule extends CommonDBTM
public $criterias = [];
/// Rules can be sorted ?
public $can_sort = false;
// preview context ?
protected $is_preview = false;
/// field used to order rules
public $orderby = 'ranking';

Expand Down Expand Up @@ -2195,11 +2197,13 @@ public function showMinimalActionForm($fields, $canedit, $rand)
**/
public function showRulePreviewResultsForm($target, $input, $params)
{

$actions = $this->getAllActions();
$check_results = [];
$output = [];

// specify that we are in a test context
$this->is_preview = true;

//Test all criteria, without stopping at the first good one
$this->testCriterias($input, $check_results);
//Process the rule
Expand Down Expand Up @@ -2690,6 +2694,13 @@ public function getActionValue($ID, $type, $value)
return $this->displayAdditionRuleActionValue($value);
}

if ($this->is_preview && !is_numeric($value)) {
// In preview mode, if the value corresponds to a string
// that does not match an existing dropdown entry,
// it will not be imported and therefore tha value will not correspond to a dropdown valid ID.
return $value;
}

// $type == assign
$name = Dropdown::getDropdownName($action["table"], $value);
return (($name == ' ') ? NOT_AVAILABLE : $name);
Expand Down
12 changes: 9 additions & 3 deletions src/RuleLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ public function executeActions($output, $params, array $input = [])
$action->fields["value"],
$regex_result
);
$compute_entities_id = $input['entities_id'] ?? 0;
$location = new Location();
$output['locations_id'] = $location->importExternal($regexvalue, $compute_entities_id);

// from rule test context just assign regex value to key
if ($this->is_preview) {
$output['locations_id'] = $regexvalue;
} else {
$compute_entities_id = $input['entities_id'] ?? 0;
$location = new Location();
$output['locations_id'] = $location->importExternal($regexvalue, $compute_entities_id);
}
}
}
break;
Expand Down
58 changes: 58 additions & 0 deletions tests/functional/RuleLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,62 @@ public function testSubIPCIDR()
$all_locations = getAllDataFromTable($location->getTable());
$this->integer(count($all_locations))->isIdenticalTo($count_locations);
}


public function testActionsFromTestContext()
{
$this->login();

$location = new \Location();
$locations_id = $location->add([
'name' => 'Location_Test',
]);
$this->integer($locations_id)->isGreaterThan(0);

$rule = new \Rule();
$input = [
'is_active' => 1,
'name' => 'location rule test context',
'match' => 'AND',
'sub_type' => 'RuleLocation',
'ranking' => 1
];
$rules_id = $rule->add($input);
$this->integer($rules_id)->isGreaterThan(0);

$rulecriteria = new \RuleCriteria();
$input = [
'rules_id' => $rules_id,
'criteria' => "tag",
'pattern' => "/(.*)/",
'condition' => \RuleImportEntity::REGEX_MATCH
];
$this->integer($rulecriteria->add($input))->isGreaterThan(0);

$ruleaction = new \RuleAction();
$input = [
'rules_id' => $rules_id,
'action_type' => 'regex_result',
'field' => 'locations_id',
'value' => "#0"
];
$this->integer($ruleaction->add($input))->isGreaterThan(0);

// test rule like rule.test.php
$rule = new \RuleLocation();
$rule->getRuleWithCriteriasAndActions($rules_id, 1, 1);

$params = $rule->addSpecificParamsForPreview([]);
$input = $rule->prepareAllInputDataForProcess(['tag' => 'testtag'], $params);

// intercepts the output of echo functions
// as showRulePreviewResultsForm is also in charge of displaying the result (in addition to testing the rule)
ob_start();
$rule->showRulePreviewResultsForm($_SERVER['PHP_SELF'], $input, $params);
ob_end_clean();

// check that location was not created
$location = new \Location();
$this->boolean($location->getFromDBByCrit(['name' => 'testtag']))->isNotTrue();
}
}

0 comments on commit 37179dc

Please sign in to comment.