Skip to content

Commit

Permalink
feat(targetproblem): target problem
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Dec 16, 2021
1 parent 58a9b04 commit 4ae2a92
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 74 deletions.
26 changes: 26 additions & 0 deletions inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2473,4 +2473,30 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
$data = array_merge($data, $predefined_fields);
return $data;
}

/**
* get all target problems for a form
*
* @param int $formId
* @return array
*/
public function getTargetsForForm($formId) {
global $DB;

$targets = [];
$rows = $DB->request([
'SELECT' => ['id'],
'FROM' => static::getTable(),
'WHERE' => [
'plugin_formcreator_forms_id' => $formId
],
]);
foreach ($rows as $row) {
$target = new static();
$target->getFromDB($row['id']);
$targets[$row['id']] = $target;
}

return $targets;
}
}
3 changes: 2 additions & 1 deletion inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,8 @@ public function getFields() : array {
public static function getTargetTypes() : array {
return [
PluginFormcreatorTargetTicket::class,
PluginFormcreatorTargetChange::class
PluginFormcreatorTargetChange::class,
PluginFormcreatorTargetProblem::class,
];
}

Expand Down
26 changes: 0 additions & 26 deletions inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -901,30 +901,4 @@ public function save(PluginFormcreatorFormAnswer $formanswer) {

return $change;
}

/**
* get all target changes for a form
*
* @param int $formId
* @return array
*/
public function getTargetsForForm($formId) {
global $DB;

$targets = [];
$rows = $DB->request([
'SELECT' => ['id'],
'FROM' => self::getTable(),
'WHERE' => [
'plugin_formcreator_forms_id' => $formId
],
]);
foreach ($rows as $row) {
$target = new self();
$target->getFromDB($row['id']);
$targets[$row['id']] = $target;
}

return $targets;
}
}
26 changes: 0 additions & 26 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1543,30 +1543,4 @@ private function saveAssociatedItems($input) {
unset($input['items_id']);
return $input;
}

/**
* get all target tickets for a form
*
* @param int $formId
* @return array
*/
public function getTargetsForForm($formId) {
global $DB;

$targets = [];
$rows = $DB->request([
'SELECT' => ['id'],
'FROM' => self::getTable(),
'WHERE' => [
'plugin_formcreator_forms_id' => $formId
],
]);
foreach ($rows as $row) {
$target = new self();
$target->getFromDB($row['id']);
$targets[$row['id']] = $target;
}

return $targets;
}
}
25 changes: 25 additions & 0 deletions install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,31 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` (
INDEX `tickettemplates_id` (`tickettemplates_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targetproblems` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`plugin_formcreator_forms_id` int(11) NOT NULL DEFAULT '0',
`target_name` varchar(255) NOT NULL DEFAULT '',
`problemtemplates_id` int(11) NOT NULL DEFAULT '0',
`content` longtext,
`impactcontent` longtext,
`causecontent` longtext,
`symptomcontent` longtext,
`urgency_rule` int(11) NOT NULL DEFAULT '1',
`urgency_question` int(11) NOT NULL DEFAULT '0',
`destination_entity` int(11) NOT NULL DEFAULT '1',
`destination_entity_value` int(11) DEFAULT NULL,
`tag_type` int(11) NOT NULL DEFAULT '1',
`tag_questions` varchar(255) NOT NULL,
`tag_specifics` varchar(255) NOT NULL,
`category_rule` int(11) NOT NULL DEFAULT '1',
`category_question` int(11) NOT NULL DEFAULT '0',
`show_rule` int(11) NOT NULL DEFAULT '1',
`uuid` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `problemtemplates_id` (`problemtemplates_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targets_actors` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`itemtype` varchar(255) DEFAULT NULL,
Expand Down
33 changes: 29 additions & 4 deletions tests/2-integration/PluginFormcreatorIssue.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,24 @@

class PluginFormcreatorIssue extends CommonTestCase {

public function beforeTestMethod($method) {
switch ($method) {
case 'testAddTicket':
case 'testUpdateTicket':
$this->login('post-only', 'postonly');
break;

case 'testDeleteTicket':
$this->login('glpi', 'glpi');
break;
}
}

public function testAddTicket() {
$this->login('post-only', 'postonly');
global $CFG_GLPI;

$CFG_GLPI['use_notifications'] = '0';

// Create a form with a target ticket
$form = $this->getForm();
$this->getTargetTicket([
Expand All @@ -43,7 +59,10 @@ public function testAddTicket() {

// answer the form
$formAnswer = new \PluginFormcreatorFormAnswer();
$formAnswer->add([\PluginFormcreatorForm::getForeignKeyField() => $form->getID()]);
$formAnswer->add([
\PluginFormcreatorForm::getForeignKeyField() => $form->getID()
]);

// Get the generated ticket
$ticket = array_pop($formAnswer->targetList);
$this->object($ticket);
Expand All @@ -63,7 +82,10 @@ public function testAddTicket() {
}

public function testUpdateTicket() {
$this->login('post-only', 'postonly');
global $CFG_GLPI;

$CFG_GLPI['use_notifications'] = '0';

// Create a form with a target ticket
$form = $this->getForm();
$this->getTargetTicket([
Expand Down Expand Up @@ -104,7 +126,10 @@ public function testUpdateTicket() {
}

public function testDeleteTicket() {
$this->login('glpi', 'glpi');
global $CFG_GLPI;

$CFG_GLPI['use_notifications'] = '0';

$form = $this->getForm();
$this->getTargetTicket([
\PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
Expand Down
16 changes: 8 additions & 8 deletions tests/2-integration/PluginFormcreatorTargetChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,30 @@ public function testTargetChangeActors() {
// Create a form with a target change
$form = $this->getForm();

$targetChange = new \PluginFormcreatorTargetChange();
$targetChange->add([
$instance = new \PluginFormcreatorTargetChange();
$instance->add([
'name' => 'a target',
'plugin_formcreator_forms_id' => $form->getID()
]);
$this->boolean($targetChange->isNewItem())->isFalse();
$this->boolean($instance->isNewItem())->isFalse();

$requesterActor = new \PluginFormcreatorTarget_Actor();
$observerActor = new \PluginFormcreatorTarget_Actor();
$targetChangeId = $targetChange->getID();
$instanceId = $instance->getID();

// find the actors created by default
$requesterActor->getFromDBByCrit([
'AND' => [
'itemtype' => $targetChange->getType(),
'items_id' => $targetChangeId,
'itemtype' => $instance->getType(),
'items_id' => $instanceId,
'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER,
'actor_type' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHOR,
]
]);
$observerActor->getFromDBByCrit([
'AND' => [
'itemtype' => $targetChange->getType(),
'items_id' => $targetChangeId,
'itemtype' => $instance->getType(),
'items_id' => $instanceId,
'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_ROLE_OBSERVER,
'actor_type' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_VALIDATOR
]
Expand Down
18 changes: 9 additions & 9 deletions tests/2-integration/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,31 @@ public function testTargetTicketActors() {
// Create a form with a target ticket
$form = $this->getForm();

$targetTicket = new \PluginFormcreatorTargetTicket();
$targetTicket->add([
$instance = new \PluginFormcreatorTargetTicket();
$instance->add([
'name' => 'a target',
'plugin_formcreator_forms_id' => $form->getID()
]);
$targetTicket->getFromDB($targetTicket->getID());
$this->boolean($targetTicket->isNewItem())->isFalse();
$instance->getFromDB($instance->getID());
$this->boolean($instance->isNewItem())->isFalse();

// find the actors created by default
$requesterActor = new \PluginFormcreatorTarget_Actor();
$observerActor = new \PluginFormcreatorTarget_Actor();
$targetTicketId = $targetTicket->getID();
$instanceId = $instance->getID();

$requesterActor->getFromDBByCrit([
'AND' => [
'itemtype' => $targetTicket->getType(),
'items_id' => $targetTicketId,
'itemtype' => $instance->getType(),
'items_id' => $instanceId,
'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER,
'actor_type' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHOR,
]
]);
$observerActor->getFromDBByCrit([
'AND' => [
'itemtype' => $targetTicket->getType(),
'items_id' => $targetTicketId,
'itemtype' => $instance->getType(),
'items_id' => $instanceId,
'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_ROLE_OBSERVER,
'actor_type' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_VALIDATOR
]
Expand Down
16 changes: 16 additions & 0 deletions tests/src/CommonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ protected function getFormAnswer(array $input): ?\PluginFormcreatorFormAnswer {
return $formAnswer;
}

protected function getTargetProblem($input = []) {
if (!isset($input['name'])) {
$input['name'] = $this->getUniqueString();
}

$formFk = \PluginFormcreatorForm::getForeignKeyField();
if (!isset($input[$formFk])) {
$input[$formFk] = $this->getForm()->getID();
}

$targetProblem = new \PluginFormcreatorTargetProblem();
$targetProblem->add($input);

return $targetProblem;
}

/**
* Tests the session has a specific message
* this may be replaced by a custom asserter for atoum
Expand Down

0 comments on commit 4ae2a92

Please sign in to comment.