diff --git a/tests/2-integration/PluginFormcreatorTargetTicket.php b/tests/2-integration/PluginFormcreatorTargetTicket.php index fbfe297dd..1fb861ece 100644 --- a/tests/2-integration/PluginFormcreatorTargetTicket.php +++ b/tests/2-integration/PluginFormcreatorTargetTicket.php @@ -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 @@ -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(); + } } diff --git a/tests/src/CommonTestCase.php b/tests/src/CommonTestCase.php index 99e29486d..d229ea360 100644 --- a/tests/src/CommonTestCase.php +++ b/tests/src/CommonTestCase.php @@ -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 { @@ -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); @@ -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 *