Skip to content

Commit

Permalink
test(targetticket): check rules on generated ticket creation
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Sep 25, 2023
1 parent 079a120 commit a003593
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
78 changes: 78 additions & 0 deletions tests/2-integration/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
namespace tests\units;
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
use Entity;
use Group;
use RuleAction;
use User;
use Rule;
use RuleCriteria;
use RuleTicket;
use Session;
use Group_Ticket;
use Group_User;
use CommonITILObject;
use Ticket;

/**
* @engine inline
Expand Down Expand Up @@ -172,4 +183,71 @@ public function testUrgency() {
}
}
}

public function testGenerateTicketAddAssignedByRule() {
$this->isolateInEntity('glpi', 'glpi');
$currentEntity = Session::getActiveEntity();
// Add a group and a user to the group
$group = $this->getGlpiCoreItem(Group::class, [
'name' => 'test group',
]);
$login = $this->getUniqueString();
$this->getGlpiCoreItem(User::class, [
'name' => $login,
'password' => 'superadmin',
'password2' => 'superadmin',
'_profiles_id' => '4', // super admin profile
'_entities_id' => $currentEntity,
'_is_recursive' => 1,
]);
$this->getGlpiCoreItem(Group_User::class, [
'group' => $group->getID(),
'users_id' => User::getIdByName($login),
]);

$rule = $this->getGlpiCoreItem(Rule::class, [
'sub_type' => RuleTicket::class,
'name' => 'add technicians',
'match' => 'AND',
'is_active' => 1,
'condition' => 1,
'entities_id' => $currentEntity,
]);
$ruleCriteria = $this->getGlpiCoreItem(RuleCriteria::class, [
$rule::getForeignKeyField() => $rule->getID(),
'criteria' => 'entities_id',
'condition' => 0,
'pattern' => $currentEntity,
]);
$ruleAction = $this->getGlpiCoreItem(RuleAction::class, [
$rule::getForeignKeyField() => $rule->getID(),
'action_type' => 'assign',
'field' => '_groups_id_assign',
'value' => $group->getID(),
]);

$form = $this->getForm([
'name' => 'test support 29602',
]);

$this->getTargetTicket([
$form::getForeignKeyField() => $form->getID(),
]);

$formAnswer = $this->getFormAnswer([
$form::getForeignKeyField() => $form->getID(),
]);

$targets = $formAnswer->getGeneratedTargets();
$this->array($targets)->hasSize(1);
$ticket = array_pop($targets);
$this->object($ticket)->isInstanceOf(Ticket::class);
$groupTicket = new Group_Ticket();
$groupTicket->getFromDBByCrit([
'type' => CommonITILObject::ASSIGNED,
$ticket::getForeignKeyField() => $ticket->getID(),
$group::getForeignKeyField() => $group->getID(),
]);
$this->boolean($groupTicket->isNewItem())->isFalse();
}
}
28 changes: 26 additions & 2 deletions tests/src/CommonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
use ReflectionMethod;
use PluginFormcreatorCondition;
use PluginFormcreatorQuestion;
use PluginFormcreatorSection;
use PluginFormcreatorForm;
use PluginFormcreatorFormAnswer;
use PluginFormcreatorTargetProblem;
use PluginFormcreatorTargetTicket;
use PluginFormcreatorTargetChange;
use PluginFormcreatorSection;

abstract class CommonTestCase extends atoum
{
Expand Down Expand Up @@ -311,6 +311,12 @@ protected function getTargetChange($input = []) {
return $targetChange;
}

/**
* Undocumented function
*
* @param array $input
* @return PluginFormcreatorFormAnswer|null
*/
protected function getFormAnswer(array $input): ?PluginFormcreatorFormAnswer {
$formAnswer = new PluginFormcreatorFormAnswer();
$formAnswer->add($input);
Expand Down Expand Up @@ -429,7 +435,25 @@ protected function getGlpiCoreItem(string $itemtype, array $input = []): CommonD
return $item;
}

/**
/**
* Create an entity and switch to it
*
* @return void
*/
protected function isolateInEntity($login, $password) {
$entity = new Entity();
$rand = mt_rand();
$entities_id = $entity->add([
'name' => "test formcreator sub entity $rand",
'entities_id' => 0
]);

$this->login($login, $password);
$success = Session::changeActiveEntities($entities_id);
$this->boolean($success)->isTrue('Failed to change active entity');
}

/**
* Handle deprecations in GLPI
* Helps to make unit tests without deprecations warnings, accross 2 version of GLPI
*
Expand Down

0 comments on commit a003593

Please sign in to comment.