Unverified Commit bf46e02a authored by Stanislas's avatar Stanislas Committed by GitHub
Browse files

Rule action appliance (#9063)

* feat(rule): assign appliance as linked item

* feat(rule): assign appliance as linked item with regex

* fix(rule): retrive id

* refactor(code): remove useless debug

* fix(CS):fix CS

* feat(rule): append appliance as linked item

* fix(tu): handle only if input = 0 or not exist

* fix(rule): restrict dropdown  items to is_helpdesk_visible

* fix(tu): fix TU
parent 058ceca1
......@@ -248,6 +248,13 @@ class RuleTicket extends Rule {
}
}
// special case of appliance
if ($action->fields["field"] == "assign_appliance") {
if (!array_key_exists("items_id", $output) || $output['items_id'] == '0') {
$output["items_id"] = [];
}
$output["items_id"][Appliance::getType()][] = $action->fields["value"];
}
break;
case "append" :
......@@ -259,7 +266,16 @@ class RuleTicket extends Rule {
$value[$actions[$action->fields["field"]]["appendtoarrayfield"]]
= $action->fields["value"];
}
$output[$actions[$action->fields["field"]]["appendto"]][] = $value;
// special case of appliance
if ($action->fields["field"] === "assign_appliance") {
if (!array_key_exists("items_id", $output) || $output['items_id'] == '0') {
$output["items_id"] = [];
}
$output["items_id"][Appliance::getType()][] = $value;
} else {
$output[$actions[$action->fields["field"]]["appendto"]][] = $value;
}
// Special case of users_id_requester
if ($action->fields["field"] === '_users_id_requester') {
......@@ -271,7 +287,6 @@ class RuleTicket extends Rule {
$output['_groups_id_of_requester'][$g['id']] = $g['id'];
}
}
break;
case 'fromuser' :
......@@ -377,6 +392,28 @@ class RuleTicket extends Rule {
}
}
}
if ($action->fields["field"] == "assign_appliance") {
if (isset($this->regex_results[0])) {
$regexvalue = RuleAction::getRegexResultById($action->fields["value"],
$this->regex_results[0]);
} else {
$regexvalue = $action->fields["value"];
}
if (!is_null($regexvalue)) {
$appliances = new Appliance();
$target_appliances = $appliances->find(["name" => $regexvalue, "is_helpdesk_visible" => true]);
if ((!array_key_exists("items_id", $output) || $output['items_id'] == '0') && count($target_appliances) > 0) {
$output["items_id"] = [];
}
foreach ($target_appliances as $value) {
$output["items_id"][Appliance::getType()][] = $value['id'];
}
}
}
break;
}
}
......@@ -709,6 +746,14 @@ class RuleTicket extends Rule {
$actions['affectobject']['force_actions'] = ['affectbyip', 'affectbyfqdn',
'affectbymac'];
$actions['assign_appliance']['name'] = _n('Associated element', 'Associated elements', Session::getPluralNumber())." : ".Appliance::getTypeName(1);
$actions['assign_appliance']['type'] = 'dropdown';
$actions['assign_appliance']['table'] = 'glpi_appliances';
$actions['assign_appliance']['condition'] = ['is_helpdesk_visible' => 1];
$actions['assign_appliance']['permitseveral'] = ['append'];
$actions['assign_appliance']['force_actions'] = ['assign','regex_result', 'append'];
$actions['assign_appliance']['appendto'] = 'items_id';
$actions['slas_id_ttr']['table'] = 'glpi_slas';
$actions['slas_id_ttr']['field'] = 'name';
$actions['slas_id_ttr']['name'] = sprintf(__('%1$s %2$s'), __('SLA'),
......
......@@ -1434,6 +1434,9 @@ class Ticket extends CommonITILObject {
function post_updateItem($history = 1) {
global $CFG_GLPI;
//for items added from rule
$this->handleItemsIdInput();
parent::post_updateItem($history);
//Action for send_validation rule : do validation before clean
......
......@@ -241,7 +241,7 @@ class Rule extends DbTestCase {
$this->integer($rule->maxActionsCount())->isIdenticalTo(0);
$rule = new \RuleTicket();
$this->integer($rule->maxActionsCount())->isIdenticalTo(31);
$this->integer($rule->maxActionsCount())->isIdenticalTo(32);
$rule = new \RuleDictionnarySoftware();
$this->integer($rule->maxActionsCount())->isIdenticalTo(4);
......
......@@ -1596,4 +1596,306 @@ class RuleTicket extends DbTestCase {
$this->boolean($ticket_2->getFromDB($tickets_id_2))->isTrue();
$this->integer($ticket_2->fields['impact'])->isEqualTo($rule_value);
}
public function testAssignAppliance() {
$this->login();
//create appliance "appliance"
$applianceTest1 = new \Appliance();
$appliancetest1_id = $applianceTest1->add($applianceTest1_input = [
"name" => "appliance",
"is_helpdesk_visible" => true
]);
$this->checkInput($applianceTest1, $appliancetest1_id, $applianceTest1_input);
//add appliance to ticket type
$CFG_GLPI["ticket_types"][] = \Appliance::getType();
// Add rule for create / update trigger (and assign action)
$ruleticket = new \RuleTicket();
$rulecrit = new \RuleCriteria();
$ruleaction = new \RuleAction();
$ruletid = $ruleticket->add($ruletinput = [
'name' => 'test associated element : appliance',
'match' => 'AND',
'is_active' => 1,
'sub_type' => 'RuleTicket',
'condition' => \RuleTicket::ONUPDATE + \RuleTicket::ONADD,
'is_recursive' => 1,
]);
$this->checkInput($ruleticket, $ruletid, $ruletinput);
// Create criteria to check if content contain key word
$crit_id = $rulecrit->add($crit_input = [
'rules_id' => $ruletid,
'criteria' => 'content',
'condition' => \Rule::PATTERN_CONTAIN,
'pattern' => 'appliance',
]);
$this->checkInput($rulecrit, $crit_id, $crit_input);
// Create action to add appliance
$action_id = $ruleaction->add($action_input = [
'rules_id' => $ruletid,
'action_type' => 'assign',
'field' => 'assign_appliance',
'value' => $appliancetest1_id,
]);
$this->checkInput($ruleaction, $action_id, $action_input);
//create ticket to match rule on create
$ticketCreate = new \Ticket();
$ticketsCreate_id = $ticketCreate->add($ticketCreate_input = [
'name' => 'test appliance',
'content' => 'test appliance'
]);
$this->checkInput($ticketCreate, $ticketsCreate_id, $ticketCreate_input);
//check for one associated element
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'items_id' => $appliancetest1_id,
'tickets_id' => $ticketsCreate_id]
))->isEqualTo(1);
//create ticket to match rule on update
$ticketUpdate = new \Ticket();
$ticketsUpdate_id = $ticketUpdate->add($ticketUpdate_input = [
'name' => 'test',
'content' => 'test'
]);
$this->checkInput($ticketUpdate, $ticketsUpdate_id, $ticketUpdate_input);
//no appliance associated
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'items_id' => $appliancetest1_id,
'tickets_id' => $ticketsUpdate_id]
))->isEqualTo(0);
//update ticket content to match rule
$ticketUpdate->update(
[
'id' => $ticketsUpdate_id,
'name' => 'test erp',
'content' => 'appliance'
]);
//check for one associated element
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'items_id' => $appliancetest1_id,
'tickets_id' => $ticketsUpdate_id]
))->isEqualTo(1);
}
public function testRegexAppliance() {
$this->login();
//create appliance "erp"
$applianceTest1 = new \Appliance();
$appliancetest1_id = $applianceTest1->add($applianceTest1_input = [
"name" => "erp",
"is_helpdesk_visible" => true
]);
$this->checkInput($applianceTest1, $appliancetest1_id, $applianceTest1_input);
// Create rule for create / update trigger (and regex action)
$ruleticket = new \RuleTicket();
$rulecrit = new \RuleCriteria();
$ruleaction = new \RuleAction();
$ruletid = $ruleticket->add($ruletinput = [
'name' => 'test associated element with regex : erp',
'match' => 'AND',
'is_active' => 1,
'sub_type' => 'RuleTicket',
'condition' => \RuleTicket::ONUPDATE + \RuleTicket::ONADD,
'is_recursive' => 1,
]);
$this->checkInput($ruleticket, $ruletid, $ruletinput);
// Create criteria to match regex
$crit_id = $rulecrit->add($crit_input = [
'rules_id' => $ruletid,
'criteria' => 'name',
'condition' => \Rule::REGEX_MATCH,
'pattern' => '/(erp)/',
]);
$this->checkInput($rulecrit, $crit_id, $crit_input);
// Create action to add appliance
$action_id = $ruleaction->add($action_input = [
'rules_id' => $ruletid,
'action_type' => 'regex_result',
'field' => 'assign_appliance',
'value' => '#0',
]);
$this->checkInput($ruleaction, $action_id, $action_input);
//create ticket to match rule on create
//create ticket to match rule on create
$ticketCreate = new \Ticket();
$ticketsCreate_id = $ticketCreate->add($ticketCreate_input = [
'name' => 'test erp',
'content' => 'test erp'
]);
$this->checkInput($ticketCreate, $ticketsCreate_id, $ticketCreate_input);
$this->integer($ticketsCreate_id)->isGreaterThan(0);
//check for one associated element
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'items_id' => $appliancetest1_id,
'tickets_id' => $ticketsCreate_id]
))->isEqualTo(1);
//create ticket to match rule on update
$ticketUpdate = new \Ticket();
$ticketsUpdate_id = $ticketUpdate->add($ticketUpdate_input = [
'name' => 'test',
'content' => 'test'
]);
$this->checkInput($ticketUpdate, $ticketsUpdate_id, $ticketUpdate_input);
//no appliance associated
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'items_id' => $appliancetest1_id,
'tickets_id' => $ticketsUpdate_id]
))->isEqualTo(0);
//update ticket content to match rule
$ticketUpdate->update(
[
'id' => $ticketsUpdate_id,
'name' => 'erp'
]);
//check for one associated element
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'items_id' => $appliancetest1_id,
'tickets_id' => $ticketsUpdate_id]
))->isEqualTo(1);
}
public function testAppendAppliance() {
$this->login();
//create appliance "erp"
$applianceTest1 = new \Appliance();
$appliancetest1_id = $applianceTest1->add($applianceTest1_input = [
"name" => "erp",
"is_helpdesk_visible" => true
]);
$this->checkInput($applianceTest1, $appliancetest1_id, $applianceTest1_input);
//create appliance "glpi"
$applianceTest2 = new \Appliance();
$appliancetest2_id = $applianceTest2->add($applianceTest2_input = [
"name" => "glpi",
"is_helpdesk_visible" => true
]);
$this->checkInput($applianceTest2, $appliancetest2_id, $applianceTest2_input);
// Create rule for create / update trigger (and regex action)
$ruleticket = new \RuleTicket();
$rulecrit = new \RuleCriteria();
$ruleaction = new \RuleAction();
$ruletid = $ruleticket->add($ruletinput = [
'name' => 'test associated element with : erp',
'match' => 'AND',
'is_active' => 1,
'sub_type' => 'RuleTicket',
'condition' => \RuleTicket::ONUPDATE + \RuleTicket::ONADD,
'is_recursive' => 1,
]);
$this->checkInput($ruleticket, $ruletid, $ruletinput);
// Create criteria to match regex
$crit_id = $rulecrit->add($crit_input = [
'rules_id' => $ruletid,
'criteria' => 'name',
'condition' => \Rule::PATTERN_CONTAIN,
'pattern' => 'erp',
]);
$this->checkInput($rulecrit, $crit_id, $crit_input);
// Create action to add appliance1
$action_id1 = $ruleaction->add($action_input = [
'rules_id' => $ruletid,
'action_type' => 'append',
'field' => 'assign_appliance',
'value' => $appliancetest1_id,
]);
$this->checkInput($ruleaction, $action_id1, $action_input);
//Create action to add appliance2
$action_id2 = $ruleaction->add($action_input = [
'rules_id' => $ruletid,
'action_type' => 'append',
'field' => 'assign_appliance',
'value' => $appliancetest2_id,
]);
$this->checkInput($ruleaction, $action_id2, $action_input);
//create ticket to match rule on create
$ticketCreate = new \Ticket();
$ticketsCreate_id = $ticketCreate->add($ticketCreate_input = [
'name' => 'test erp',
'content' => 'test erp'
]);
$this->checkInput($ticketCreate, $ticketsCreate_id, $ticketCreate_input);
$this->integer($ticketsCreate_id)->isGreaterThan(0);
//check for one associated element
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'tickets_id' => $ticketsCreate_id]
))->isEqualTo(2);
//create ticket to match rule on update
$ticketUpdate = new \Ticket();
$ticketsUpdate_id = $ticketUpdate->add($ticketUpdate_input = [
'name' => 'test',
'content' => 'test'
]);
$this->checkInput($ticketUpdate, $ticketsUpdate_id, $ticketUpdate_input);
//no appliance associated
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'items_id' => $appliancetest1_id,
'tickets_id' => $ticketsUpdate_id]
))->isEqualTo(0);
//update ticket content to match rule
$ticketUpdate->update(
[
'id' => $ticketsUpdate_id,
'name' => 'test erp'
]);
//check for one associated element
$this->integer(countElementsInTable(
\Item_Ticket::getTable(),
['itemtype' => \Appliance::getType(),
'tickets_id' => $ticketsUpdate_id]
))->isEqualTo(2);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment