Skip to content

Commit

Permalink
test(abstractitiltarget): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Apr 14, 2023
1 parent 0972714 commit 69731d1
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/src/AbstractItilTargetTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@

namespace GlpiPlugin\Formcreator\Tests;

use PluginFormcreatorForm;
use PluginFormcreatorFormanswer;
use PluginFormcreatorTarget_Actor;
use Ticket_User;

abstract class AbstractItilTargetTestCase extends CommonTargetTestCase {
public function beforeTestMethod($method) {
Expand Down Expand Up @@ -120,4 +123,71 @@ public function testSetTargetPriority($formanswerData, $expected) {
$generatedTarget = $formanswer->targetList[0]; // Assume the target has been generated
$generatedTarget->fields['priority'] = $expected;
}

public function providerSetTargetRequesters() {
$question = $this->getQuestion([
'fieldtype' => 'email',
]);
$form = PluginFormcreatorForm::getByItem($question);
$target = $this->newTestedInstance();
$target->add([
'name' => $this->getUniqueString(),
'plugin_formcreator_forms_id' => $form->getID(),
]);
$targetActor = new PluginFormcreatorTarget_Actor();
$targetActor->add([
'itemtype' => $target->getType(),
'items_id' => $target->getID(),
'actor_role' => PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER,
'actor_type' => PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_PERSON,
'actor_value_4' => $question->getID(),
]);
$this->boolean($targetActor->isNewItem())->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));

$this->login('glpi', 'glpi');
yield 'One email requester' => [
'formanswerData' => [
PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
'formcreator_field_' . $question->getID() => '[email protected]',
],
'expected' => [
[
'users_id' => 0,
'alternative_email' => '[email protected]',
],
],
];
}

/**
* @dataProvider providerSetTargetRequesters
*/
public function testSetTargetRequesters($formanswerData, $expected) {
$formanswer = new PluginFormcreatorFormanswer();
$formanswer->add($formanswerData);
$_SESSION['MESSAGE_AFTER_REDIRECT'] = [];
$this->boolean($formanswer->isNewItem())->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
$generatedTarget = $formanswer->targetList[0]; // Assume the target has been generated
$instance = $this->newTestedInstance();
$relationClass = $this->callPrivateMethod($instance, 'getItem_User');
$targetClass = $instance::getTargetItemtypeName();
$ticketUser = new $relationClass;
if (count($expected) === 0) {
$rows = $ticketUser->find([
$targetClass::getForeignKeyField() => $generatedTarget->getID(),
'type' => $relationClass::REQUESTER,
]);
$this->array($rows)->hasSize(0);
} else {
foreach ($expected as $searched) {
$rows = $ticketUser->find([
$targetClass::getForeignKeyField() => $generatedTarget->getID(),
'type' => $relationClass::REQUESTER,
'users_id' => $searched['users_id'],
'alternative_email' => $searched['alternative_email'],
]);
$this->array($rows)->hasSize(1);
}
}
}
}

0 comments on commit 69731d1

Please sign in to comment.