Skip to content

Commit

Permalink
feat(targetticket): better template support for request source
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Mar 31, 2022
1 parent 032c4c1 commit 61cf134
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 3 deletions.
54 changes: 53 additions & 1 deletion inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class PluginFormcreatorTargetTicket extends PluginFormcreatorAbstractItilTarget
const REQUESTTYPE_SPECIFIC = 1;
const REQUESTTYPE_ANSWER = 2;

const REQUESTSOURCE_NONE = 0;
const REQUESTSOURCE_SPECIFIC = 1;

public static function getTypeName($nb = 1) {
return _n('Target ticket', 'Target tickets', $nb, 'formcreator');
Expand Down Expand Up @@ -100,6 +102,13 @@ public static function getEnumAssociateRule() {
];
}

public static function getEnumRequestSourceRule(): array {
return [
self::REQUESTSOURCE_NONE => __('Source from template or user default or GLPI default', 'formcreator'),
self::REQUESTSOURCE_SPECIFIC => __('Formcreator', 'formcreator'),
];
}

public static function getEnumRequestTypeRule() {
return [
self::REQUESTTYPE_NONE => __('Default or from a template', 'formcreator'),
Expand Down Expand Up @@ -239,6 +248,7 @@ public static function showProperties(self $item) {
$item->showSLASettings();
$item->showOLASettings();

$item->showSourceSettings($rand);
$item->showTypeSettings($rand);
// -------------------------------------------------------------------------------------------
// associated elements of the target
Expand Down Expand Up @@ -528,6 +538,13 @@ public function prepareInputForAdd($input) {
}
}

if (!isset($input['source_rule'])) {
$input['source_rule'] = self::REQUESTSOURCE_SPECIFIC;
}
$input['source_question'] = 0;
if ($input['source_rule'] == self::REQUESTTYPE_SPECIFIC) {
$input['source_question'] = PluginFormcreatorCommon::getFormcreatorRequestTypeId();
}
return $input;
}

Expand Down Expand Up @@ -613,6 +630,18 @@ public function prepareInputForUpdate($input) {
}
}

if (isset($input['source_rule'])) {
$input['source_question'] = '0';
switch ($input['source_rule']) {
case self::REQUESTSOURCE_NONE:
$input['source_question'] = 0;
break;
case self::REQUESTSOURCE_SPECIFIC:
$input['source_question'] = PluginFormcreatorCommon::getFormcreatorRequestTypeId();
break;
}
}

if (isset($input['category_rule'])) {
switch ($input['category_rule']) {
case self::CATEGORY_RULE_ANSWER:
Expand Down Expand Up @@ -799,7 +828,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
$ticket = new Ticket();
$form = $formanswer->getForm();
$data = $this->getDefaultData($formanswer);
$data['requesttypes_id'] = $data['requesttypes_id'] ?? PluginFormcreatorCommon::getFormcreatorRequestTypeId();

// Parse data
// TODO: generate instances of all answers of the form and use them for the fullform computation
Expand Down Expand Up @@ -852,6 +880,7 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
$data['users_id_lastupdater'] = $lastUpdater != '' ? $lastUpdater : 0;

$data = $this->setTargetType($data, $formanswer);
$data = $this->setTargetSource($data, $formanswer);
$data = $this->setTargetEntity($data, $formanswer, $requesters_id);
$data = $this->setTargetDueDate($data, $formanswer);
$data = $this->setSLA($data, $formanswer);
Expand Down Expand Up @@ -1033,6 +1062,16 @@ protected function setTargetLocation($data, $formanswer) {
return $data;
}

protected function setTargetSource(array $data, PluginFormcreatorFormAnswer $formanswer): array {
switch ($this->fields['source_rule']) {
case self::REQUESTSOURCE_SPECIFIC:
$data['requesttypes_id'] = $this->fields['source_question'];
break;
}

return $data;
}

protected function setTargetType(array $data, PluginFormcreatorFormAnswer $formanswer) {
global $DB;

Expand Down Expand Up @@ -1062,6 +1101,19 @@ protected function setTargetType(array $data, PluginFormcreatorFormAnswer $forma
return $data;
}

protected function showSourceSettings($rand): void {
echo '<tr>';
echo '<td width="15%">' . __('Request source') . '</td>';
echo '<td width="25%">';
Dropdown::showFromArray('source_rule', static::getEnumRequestSourceRule(), [
'value' => $this->fields['source_rule'],
'rand' => $rand,
]);
echo '<td></td><td></td>';
echo '</td>';
echo '</tr>';
}

protected function showTypeSettings($rand) {
echo '<tr>';
echo '<td width="15%">' . __('Request type') . '</td>';
Expand Down
2 changes: 2 additions & 0 deletions install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` (
`name` varchar(255) NOT NULL DEFAULT '',
`plugin_formcreator_forms_id` int unsigned NOT NULL DEFAULT '0',
`target_name` varchar(255) NOT NULL DEFAULT '',
`source_rule` int(11) NOT NULL DEFAULT '0',
`source_question` int(11) NOT NULL DEFAULT '0',
`type_rule` int(11) NOT NULL DEFAULT '0',
`type_question` int unsigned NOT NULL DEFAULT '0',
`tickettemplates_id` int unsigned NOT NULL DEFAULT '0',
Expand Down
8 changes: 6 additions & 2 deletions install/upgrade_to_2.12.5.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ class PluginFormcreatorUpgradeTo2_12_5 {
* @param Migration $migration
*/
public function upgrade(Migration $migration) {
global $DB;

$this->migration = $migration;

$this->addUserRecipient();
}

public function addUserRecipient(): void {
global $DB;

// Add users_id_recipient
$table = 'glpi_plugin_formcreator_issues';
$this->migration->addField($table, 'users_id_recipient', 'integer');
Expand Down
15 changes: 15 additions & 0 deletions install/upgrade_to_2.13.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function upgrade(Migration $migration) {
$this->addTargetValidationSetting();
$this->addFormVisibility();
$this->addDashboardVisibility();
$this->addRequestSourceSeting();
}

public function addFormAnswerTitle() {
Expand Down Expand Up @@ -274,4 +275,18 @@ protected function migrateFkToUnsignedInt() {
}
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null auto_increment');
}

public function addRequestSourceSeting(): void {
global $DB;

$table = 'glpi_plugin_formcreator_targettickets';

if (!$DB->fieldExists($table, 'source_rule')) {
$this->migration->addField($table, 'source_rule', 'integer', ['after' => 'target_name']);
$this->migration->addField($table, 'source_question', 'integer', ['after' => 'source_rule']);
$this->migration->migrationOneTable($table);
$formcreatorSourceId = PluginFormcreatorCommon::getFormcreatorRequestTypeId();
$DB->queryOrDie("UPDATE `$table` SET `source_rule` = '1', `source_question` = '$formcreatorSourceId'");
}
}
}
102 changes: 102 additions & 0 deletions tests/3-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function beforeTestMethod($method) {
case 'testDeleteLinkedTickets':
case 'testSetTargetAssociatedItem':
case 'testSetTargetLocation':
case 'testSetRequestSource':
$this->boolean($this->login('glpi', 'glpi'))->isTrue();
break;
}
Expand Down Expand Up @@ -77,6 +78,23 @@ public function testGetTypeName($number, $expected) {
$this->string($output)->isEqualTo($expected);
}

public function testGetEnumRequestTypeRule(): void {
$output = \PluginFormcreatorTargetTicket::getEnumRequestTypeRule();
$this->array($output)->isEqualTo([
\PluginFormcreatorTargetTicket::REQUESTTYPE_NONE => 'Default or from a template',
\PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC => "Specific type",
\PluginFormcreatorTargetTicket::REQUESTTYPE_ANSWER => "Equals to the answer to the question",
]);
}

public function testGetEnumRequestSourceRule(): void {
$output = \PluginFormcreatorTargetTicket::getEnumRequestSourceRule();
$this->array($output)->isEqualTo([
\PluginFormcreatorTargetTicket::REQUESTTYPE_NONE => 'Source from template or user default or GLPI default',
\PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC => "Formcreator",
]);
}

public function testGetEnumDestinationEntity() {
$output = \PluginFormcreatorTargetTicket::getEnumDestinationEntity();
$this->array($output)->isEqualTo([
Expand Down Expand Up @@ -638,6 +656,8 @@ public function testExport() {
$fieldsWithoutID = [
'name',
'target_name',
'source_rule',
'source_question',
'type_rule',
'type_question',
'content',
Expand Down Expand Up @@ -712,6 +732,8 @@ public function testImport() {
'category_question' => '0',
'associate_rule' => \PluginFormcreatorTargetTicket::ASSOCIATE_RULE_NONE,
'associate_question' => '0',
'source_rule' => 0,
'source_question' => 0,
'type_rule' => 1,
'type_question' => 0,
'uuid' => $uuid,
Expand Down Expand Up @@ -1266,6 +1288,7 @@ public function providerPrepareInputForAdd() {
$formFk = \PluginFormcreatorForm::getForeignKeyField();
$form = $this->getForm();
$name = $this->getUniqueString();
$sourceId = \PluginFormcreatorCommon::getFormcreatorRequestTypeId();
return [
'name is mandatory' => [
'input' => [
Expand All @@ -1287,6 +1310,8 @@ public function providerPrepareInputForAdd() {
'type_rule' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC,
'type_question' => \Ticket::INCIDENT_TYPE,
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS,
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_SPECIFIC,
'source_question' => $sourceId,
],
'message' => null,
],
Expand All @@ -1296,6 +1321,7 @@ public function providerPrepareInputForAdd() {
'name' => $name,
'type_rule' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC,
'type_question' => \Ticket::DEMAND_TYPE,
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE,
],
'expected' => [
$formFk => $form->getID(),
Expand All @@ -1305,6 +1331,8 @@ public function providerPrepareInputForAdd() {
'type_rule' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC,
'type_question' => \Ticket::DEMAND_TYPE,
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS,
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE,
'source_question' => 0,
],
'message' => null,
],
Expand Down Expand Up @@ -1437,4 +1465,78 @@ public function testSetTargetLocation($instance, $formanswer, $expected) {
$this->array($output)->notHasKey('locations_id');
}
}

public function providerSetRequestSource_none(): array {
$form = $this->getForm();
$formanswer = new \PluginFormcreatorFormanswer();
$formanswer->add([
'plugin_formcreator_forms_id' => $form->getID(),
]);
$this->boolean($formanswer->isNewItem())->isFalse();
$targetTicket = new \PluginFormcreatorTargetTicket();
$targetTicket->add([
'name' => 'target ticket',
'target_name' => 'target ticket',
'plugin_formcreator_forms_id' => $form->getID(),
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE,
]);
$this->boolean($targetTicket->isNewItem())->isFalse();

return [
[
'instance' => $targetTicket,
'formanswer' => $formanswer,
'expected' => 0
],
];
}

public function providerSetRequestSource_specific(): array {
$form = $this->getForm();
$formanswer = new \PluginFormcreatorFormanswer();
$formanswer->add([
'plugin_formcreator_forms_id' => $form->getID(),
]);
$this->boolean($formanswer->isNewItem())->isFalse();
$targetTicket = new \PluginFormcreatorTargetTicket();
$targetTicket->add([
'name' => 'target ticket',
'target_name' => 'target ticket',
'plugin_formcreator_forms_id' => $form->getID(),
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_SPECIFIC,
'source_question' => \PluginFormcreatorCommon::getFormcreatorRequestTypeId(),
]);
$this->boolean($targetTicket->isNewItem())->isFalse();

return [
[
'instance' => $targetTicket,
'formanswer' => $formanswer,
'expected' => 0
],
];
}

public function providerSetRequestSource(): array {
return array_merge(
$this->providerSetRequestSource_none(),
$this->providerSetRequestSource_specific()
);
}

/**
* @dataProvider providerSetRequestSource
*/
public function testSetRequestSource($instance, $formanswer, $expected): void {
// Substitute a dummy class to access protected / private methods
$dummyItemtype = 'GlpiPlugin\Formcreator\Tests\\' . $this->getTestedClassName() . 'Dummy';
$dummyInstance = new $dummyItemtype();
/**@var \GlpiPlugin\Formcreator\Tests\PluginFormcreatorTargetTicketDummy */
$instance->getFromDB($instance->getID());
$dummyInstance->fields = $instance->fields;

$data = $dummyInstance->publicGetDefaultData($formanswer);
$output = $dummyInstance->publicSetTargetCategory($data, $formanswer);
$this->integer((int) $output['itilcategories_id'])->isEqualTo($expected);
}
}
4 changes: 4 additions & 0 deletions tests/src/PluginFormcreatorTargetTicketDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ public function publicGetDefaultData($formanswer) {
public function publicSetTargetLocation($data, $formanswer) {
return $this->setTargetLocation($data, $formanswer);
}

public function publicSetTargetSource($data, $formanswer): array {
return $this->setTargetSource($data, $formanswer);
}
}

0 comments on commit 61cf134

Please sign in to comment.