Skip to content

Commit

Permalink
fix(targetticket): request source may be unexpected value
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Mar 30, 2023
1 parent dc8c931 commit 2fd6cf5
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
5 changes: 1 addition & 4 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,11 +1031,8 @@ protected function setTargetLocation($data, $formanswer) {
}

protected function setTargetSource(array $data, PluginFormcreatorFormAnswer $formanswer): array {
// do nothing with self::REQUESTSOURCE_NONE
switch ($this->fields['source_rule']) {
case self::REQUESTSOURCE_NONE:
$data['requesttypes_id'] = PluginFormcreatorCommon::getFormcreatorRequestTypeId();
break;

case self::REQUESTSOURCE_FORMCREATOR:
$data['requesttypes_id'] = $this->fields['source_question'];
break;
Expand Down
85 changes: 85 additions & 0 deletions tests/3-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -1727,4 +1727,89 @@ public function testSetTargetLocation($instance, $formanswer, $expected) {

$this->integer((int) $output['locations_id'])->isEqualTo($expected);
}

public function providerRequestSource() {
$testedClassName = $this->getTestedClassName();

$form = $this->getForm();
yield [
'instance' => $this->getTargetTicket([
PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
'source_rule' => $testedClassName::REQUESTSOURCE_FORMCREATOR,
'source_question' => PluginFormcreatorCommon::getFormcreatorRequestTypeId(),
]),
'expected' => PluginFormcreatorCommon::getFormcreatorRequestTypeId()
];

$form = $this->getForm();
$user = $this->getGlpiCoreItem(User::class, [
'name' => 'user' . $this->getUniqueString(),
'password' => 'password',
'password2' => 'password',
'default_requesttypes_id' => 2, // e-mail, see table glpi_requesttypes
]);
$this->login($user->fields['name'], 'password');

yield [
'instance' => $this->getTargetTicket([
PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
'source_rule' => $testedClassName::REQUESTSOURCE_NONE
]),
'expected' => 2
];

$form = $this->getForm();
$user = $this->getGlpiCoreItem(User::class, [
'name' => 'user' . $this->getUniqueString(),
'password' => 'password',
'password2' => 'password',
'default_requesttypes_id' => 0, // unset
]);
$this->login($user->fields['name'], 'password');

yield [
'instance' => $this->getTargetTicket([
PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
'source_rule' => $testedClassName::REQUESTSOURCE_NONE,
]),
'expected' => 0 // Unset (see Setup > General > Default values)
];

$form = $this->getForm();
$ticketTemplate = $this->getGlpiCoreItem(
TicketTemplate::getType(), [
'name' => 'template with predefined request type',
]
);
$this->getGlpiCoreItem(TicketTemplatePredefinedField::getType(), [
'tickettemplates_id' => $ticketTemplate->getID(),
'num' => 9, // RequestType
'value' => 1, // Helpdesk
]);

yield [
'instance' => $this->getTargetTicket([
PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
'source_rule' => $testedClassName::REQUESTSOURCE_NONE,
'tickettemplates_id' => $ticketTemplate->getID(),
]),
'expected' => 1 // Helpdesk (see Setup > General > Default values)
];
}

/**
* @dataProvider providerRequestSource
*/
public function testRequestSource($instance, $expected) {
$form = PluginFormcreatorForm::getByItem($instance);
$formAnswer = $this->getFormAnswer([
PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
]);

$generatedTargets = $formAnswer->targetList;
foreach ($generatedTargets as $target) {
$output = $target->fields['requesttypes_id'];
$this->integer((int) $output)->isEqualTo($expected);
}
}
}

0 comments on commit 2fd6cf5

Please sign in to comment.