Skip to content

Commit

Permalink
Rule assets: Fix process on update (#11716)
Browse files Browse the repository at this point in the history
* Rule assets: Fix process on update

* Update unit tests

* CS + remove unneeded var
  • Loading branch information
AdrienClairembault committed May 31, 2022
1 parent 6b6f319 commit fe57c6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
17 changes: 9 additions & 8 deletions src/CommonDBTM.php
Original file line number Diff line number Diff line change
Expand Up @@ -5532,7 +5532,7 @@ private function assetBusinessRules($condition)
return;
}

//Only process itemtype that are assets
// Only process itemtype that are assets
if (in_array($this->getType(), $CFG_GLPI['asset_types'])) {
$ruleasset = new RuleAssetCollection();
$input = $this->input;
Expand All @@ -5553,22 +5553,23 @@ private function assetBusinessRules($condition)
$input['_default_groups_id_of_user'] = $user->fields['groups_id'];
}

//If _auto is not defined : it's a manual process : set it's value to 0
// If _auto is not defined : it's a manual process : set it's value to 0
if (!isset($this->input['_auto'])) {
$input['_auto'] = 0;
}
//Set the condition (add or update)
$params = [

// Set the condition (add or update)
$output = $ruleasset->processAllRules($input, [], [], [
'condition' => $condition
];
$output = $ruleasset->processAllRules($input, [], $params);
//If at least one rule has matched
]);

// If at least one rule has matched
if (isset($output['_rule_process'])) {
foreach ($output as $key => $value) {
if ($key == '_rule_process' || $key == '_no_rule_matches') {
continue;
}
//Add the rule output to the input array
// Add the rule output to the input array
$this->input[$key] = $value;
}
}
Expand Down
30 changes: 19 additions & 11 deletions tests/units/RuleAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public function testTriggerAdd()

$root_ent_id = getItemByTypeName('Entity', '_test_root_entity', true);

// prepare rule
$this->createRuleComment(\RuleAsset::ONUPDATE);
// prepare rule
$this->createRuleComment(\RuleAsset::ONADD);

// test create ticket (trigger on title)
// test create ticket (trigger on title)
$computer = new \Computer();
$computers_id = $computer->add($computer_input = [
'name' => "computer",
Expand Down Expand Up @@ -98,7 +98,7 @@ public function testTriggerAdd()
]);
$this->boolean((bool)$monitor->getFromDB($monitors_id))->isTrue();

//Rule only apply to computers
//Rule only apply to computers
$this->string((string)$monitor->getField('comment'))->isEqualTo('');
$this->integer((int)$monitor->getField('users_id'))->isEqualTo(0);

Expand All @@ -112,7 +112,7 @@ public function testTriggerAdd()
$this->integer((int)$computers_id)->isGreaterThan(0);
$this->boolean((bool)$computer->getFromDB($computers_id))->isTrue();

//User rule should apply (extract @domain from the name)
//User rule should apply (extract @domain from the name)
$this->integer((int)$computer->getField('users_id'))->isEqualTo(4);
$this->string((string)$computer->getField('comment'))->isEqualTo('comment1');

Expand All @@ -126,7 +126,7 @@ public function testTriggerAdd()
$this->integer((int)$computers_id)->isGreaterThan(0);
$this->boolean((bool)$computer->getFromDB($computers_id))->isTrue();

//User rule should apply (extract the first user from the list)
//User rule should apply (extract the first user from the list)
$this->integer((int)$computer->getField('users_id'))->isEqualTo(4);

$computers_id = $computer->add($computer_input = [
Expand All @@ -139,7 +139,7 @@ public function testTriggerAdd()
$this->integer((int)$computers_id)->isGreaterThan(0);
$this->boolean((bool)$computer->getFromDB($computers_id))->isTrue();

//User rule should apply (extract the first user from the list)
//User rule should apply (extract the first user from the list)
$this->integer((int)$computer->getField('users_id'))->isEqualTo(4);

$computers_id = $computer->add($computer_input = [
Expand All @@ -152,8 +152,8 @@ public function testTriggerAdd()
$this->integer((int)$computers_id)->isGreaterThan(0);
$this->boolean((bool)$computer->getFromDB($computers_id))->isTrue();

//User rule should apply (extract @domain from the name) but should not
//find any user, so users_id is set to 0
//User rule should apply (extract @domain from the name) but should not
//find any user, so users_id is set to 0
$this->integer((int)$computer->getField('users_id'))->isEqualTo(0);
}

Expand All @@ -166,15 +166,14 @@ public function testTriggerUpdate()

$root_ent_id = getItemByTypeName('Entity', '_test_root_entity', true);

// prepare rule
// prepare rule
$this->createRuleComment(\RuleAsset::ONUPDATE);
$this->createRuleLocation(\RuleAsset::ONUPDATE);

foreach ($CFG_GLPI['asset_types'] as $itemtype) {
$item = new $itemtype();
$item_input = [
'name' => "$itemtype 1",
'_auto' => 1,
'entities_id' => $root_ent_id,
'is_dynamic' => 1,
'comment' => 'mycomment'
Expand All @@ -183,6 +182,15 @@ public function testTriggerUpdate()
$item_input['softwares_id'] = 1;
}
$items_id = $item->add($item_input);

// Trigger update
$update = $item->update([
'id' => $item->getID(),
'name' => 'updated name',
'_auto' => 1,
]);
$this->boolean($update)->isTrue();

$this->integer((int)$items_id)->isGreaterThan(0);
$this->boolean((bool)$item->getFromDB($items_id))->isTrue();
if ($itemtype == 'Computer') {
Expand Down

0 comments on commit fe57c6d

Please sign in to comment.