Skip to content

Commit

Permalink
feat(forms): Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ccailly committed Jan 30, 2025
1 parent 13fe0ed commit 0a236c0
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ protected function sendFormAndAssertTicketActors(

// Check actors
$this->assertEquals(
array_map(fn(array $actor) => $actor['items_id'], $ticket->getActorsForType(CommonITILActor::ASSIGN)),
$expected_actors_ids
$expected_actors_ids,
array_map(fn(array $actor) => $actor['items_id'], $ticket->getActorsForType(CommonITILActor::ASSIGN))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ protected function sendFormAndAssertTicketActors(

// Check actors
$this->assertEquals(
array_map(fn(array $actor) => $actor['items_id'], $ticket->getActorsForType(CommonITILActor::OBSERVER)),
$expected_actors_ids
$expected_actors_ids,
array_map(fn(array $actor) => $actor['items_id'], $ticket->getActorsForType(CommonITILActor::OBSERVER))
);
}

Expand All @@ -432,21 +432,4 @@ private function createAndGetFormWithMultipleActorsQuestions(): Form
);
return $this->createForm($builder);
}

private function createAndGetFormWithItemQuestions(): Form
{
$builder = new FormBuilder();
$builder->addQuestion(
'Computer question',
QuestionTypeItem::class,
'',
json_encode((new QuestionTypeItemExtraDataConfig(Computer::class))->jsonSerialize())
);
$builder->addDestination(
FormDestinationTicket::class,
"My ticket",
);

return $this->createForm($builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ protected function sendFormAndAssertTicketActors(

// Check actors
$this->assertEquals(
array_map(fn(array $actor) => $actor['items_id'], $ticket->getActorsForType(CommonITILActor::REQUESTER)),
$expected_actors_ids
$expected_actors_ids,
array_map(fn(array $actor) => $actor['items_id'], $ticket->getActorsForType(CommonITILActor::REQUESTER))
);
}

Expand All @@ -432,21 +432,4 @@ private function createAndGetFormWithMultipleActorsQuestions(): Form
);
return $this->createForm($builder);
}

private function createAndGetFormWithItemQuestions(): Form
{
$builder = new FormBuilder();
$builder->addQuestion(
'Computer question',
QuestionTypeItem::class,
'',
json_encode((new QuestionTypeItemExtraDataConfig(Computer::class))->jsonSerialize())
);
$builder->addDestination(
FormDestinationTicket::class,
"My ticket",
);

return $this->createForm($builder);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\PHPUnit\Tests\Glpi\Form\Destination\CommonITILField;

use Computer;
Expand Down Expand Up @@ -49,12 +81,12 @@ public function testUserActorsFromSpecificItemQuestions(): void
[
'name' => 'testUserActorsFromSpecificItemQuestions Computer 1',
'entities_id' => $this->getTestRootEntity(true),
'_users_id_tech' => [$users[0]->getID()]
'users_id_tech' => $users[0]->getID()
],
[
'name' => 'testUserActorsFromSpecificItemQuestions Computer 2',
'entities_id' => $this->getTestRootEntity(true),
'_users_id' => [$users[1]->getID()]
'users_id' => $users[1]->getID()
],
]);

Expand Down Expand Up @@ -89,7 +121,7 @@ public function testUserActorsFromSpecificItemQuestions(): void
'items_id' => $computers[1]->getID(),
]
],
expected_actors_ids: []
expected_actors_ids: [$users[1]->getID()]
);
}

Expand All @@ -114,12 +146,12 @@ public function testTechUserActorsFromSpecificItemQuestions(): void
[
'name' => 'testTechUserActorsFromSpecificItemQuestions Computer 1',
'entities_id' => $this->getTestRootEntity(true),
'_users_id_tech' => [$users[0]->getID()]
'users_id_tech' => $users[0]->getID()
],
[
'name' => 'testTechUserActorsFromSpecificItemQuestions Computer 2',
'entities_id' => $this->getTestRootEntity(true),
'_users_id' => [$users[1]->getID()]
'users_id' => $users[1]->getID()
],
]);

Expand Down Expand Up @@ -234,7 +266,7 @@ public function testTechGroupActorsFromSpecificItemQuestions(): void
$form = $this->createAndGetFormWithItemQuestions();
$config = new $config_class(
strategies: [ITILActorFieldStrategy::TECH_GROUP_FROM_OBJECT_ANSWER],
specific_question_ids: [$this->getQuestionId($form, "Computer question")]
specific_question_ids: [$this->getQuestionId($form, 'Computer question')]
);
$groups = $this->createItems(Group::class, [
['name' => 'testGroupActorsFromSpecificItemQuestions Group 1'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,13 @@ private function getActorsIdsFromObjectAnswer(
return null;
}

$actors_ids = $item->fields[$fk_field];
if (!is_array($actors_ids)) {
$actors_ids = [$actors_ids];
}

return [
getItemtypeForForeignKeyField(str_replace('_tech', '', $fk_field)) => [(int) $item->fields[$fk_field]],
getItemtypeForForeignKeyField(str_replace('_tech', '', $fk_field)) => $actors_ids,
];
}
}
197 changes: 197 additions & 0 deletions tests/cypress/e2e/form/destination_config_fields/actors.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

const actorTypes = [
{
name: 'Requester',
type: 'requester',
dataAttr: 'requester',
defaultValue: 'User who filled the form'
},
{
name: 'Assignee',
type: 'assignee',
dataAttr: 'assign',
defaultValue: 'From template'
},
{
name: 'Observer',
type: 'observer',
dataAttr: 'observer',
defaultValue: 'From template'
}
];

describe('Actors configuration', () => {
actorTypes.forEach((actorConfig) => {
describe(`${actorConfig.name} configuration`, () => {
beforeEach(() => {
cy.login();
cy.changeProfile('Super-Admin', true);

cy.createFormWithAPI().as('form_id').visitFormTab('Form');

// Create actor, group and computer test data
cy.get('@form_id').then((form_id) => {
const actor_name = `Test ${actorConfig.name} - ${form_id}`;
const userParams = {
name: actor_name,
};

// Add technician profile for assignees
if (actorConfig.type === 'assignee') {
userParams._profiles_id = 6;
}

cy.createWithAPI('User', userParams).as('actor_id');

// Create a Group
cy.createWithAPI('Group', {
name: `Test Group - ${form_id}`,
}).as('group_id');

// Create a Computer with users and groups
cy.get('@group_id').then((group_id) => {
cy.createWithAPI('Computer', {
name: `Test Computer - ${form_id}`,
users_id: 7,
users_id_tech: 7,
groups_id: group_id,
groups_id_tech: group_id,
}).as('computer_id');
});

// Create actor question
cy.findByRole('button', {'name': "Add a new question"}).click();
cy.focused().type(`My ${actorConfig.name} question`);
cy.getDropdownByLabelText('Question type').selectDropdownValue('Actors');
cy.getDropdownByLabelText('Question sub type').selectDropdownValue(`${actorConfig.name}s`);
cy.getDropdownByLabelText("Select an actor...").selectDropdownValue(actor_name);
cy.findByRole('button', {'name': 'Save'}).click();
cy.checkAndCloseAlert('Item successfully updated');

// Create computer question
cy.findByRole('button', {'name': "Add a new question"}).click();
cy.focused().type("My Computer question");
cy.getDropdownByLabelText('Question type').selectDropdownValue('Item');
cy.getDropdownByLabelText('Question sub type').selectDropdownValue('GLPI Objects');
cy.getDropdownByLabelText("Select an itemtype").selectDropdownValue('Computers');
cy.findByRole('button', {'name': 'Save'}).click();

// Go to destination tab and add ticket
cy.findByRole('tab', { 'name': "Items to create" }).click();
cy.findByRole('button', { 'name': "Add ticket" }).click();
cy.checkAndCloseAlert('Item successfully added');
});
});

it('can use all possibles configuration options', () => {
const regionName = `${actorConfig.name}s configuration`;
const dropdownLabel = `${actorConfig.name}s`;

cy.findByRole('region', { 'name': regionName }).as("config");
cy.get('@config').getDropdownByLabelText(dropdownLabel).as("dropdown");

// Test default value
cy.get('@dropdown').should('have.text', actorConfig.defaultValue);

// Test hidden dropdowns
cy.get('@config').getDropdownByLabelText('Select actors...').should('not.exist');
cy.get('@config').getDropdownByLabelText('Select questions...').should('not.exist');

// Test common options
const commonOptions = [
'From template',
'User who filled the form',
'Specific actors',
'Answer from specific questions',
`Answer to last "${actorConfig.name}s" question`,
'User from GLPI object answer',
'Tech user from GLPI object answer',
'Group from GLPI object answer',
'Tech group from GLPI object answer',
'Supervisor of the user who filled the form'
];

// Test each option
commonOptions.forEach((option) => {
cy.get('@dropdown').selectDropdownValue(option);

// Handle specific cases that need additional input
if (option === 'Specific actors') {
cy.get('@config').getDropdownByLabelText('Select actors...').as('specific_dropdown');
cy.get('@form_id').then((form_id) => {
const actor_name = `Test ${actorConfig.name} - ${form_id}`;
cy.get('@specific_dropdown').selectDropdownValue(actor_name);
});
} else if (option === 'Answer from specific questions') {
cy.get('@config').getDropdownByLabelText('Select questions...').as('question_dropdown');
cy.get('@question_dropdown').selectDropdownValue(`My ${actorConfig.name} question`);
} else if (option.includes('from GLPI object answer')) {
cy.get('@config').getDropdownByLabelText('Select questions...').as('object_dropdown');
cy.get('@object_dropdown').selectDropdownValue('My Computer question');
}

cy.findByRole('button', { 'name': 'Update item' }).click();
cy.checkAndCloseAlert('Item successfully updated');
cy.get('@dropdown').should('have.text', option);
});
});

it('can create ticket using default configuration', () => {
const regionName = `${actorConfig.name}s configuration`;
const dropdownLabel = `${actorConfig.name}s`;

cy.findByRole('region', { 'name': regionName }).as("config");
cy.get('@config').getDropdownByLabelText(dropdownLabel).as("dropdown");

// Set to "User who filled the form"
cy.get('@dropdown').selectDropdownValue('User who filled the form');
cy.findByRole('button', { 'name': 'Update item' }).click();
cy.checkAndCloseAlert('Item successfully updated');

// Preview and submit form
cy.findByRole('tab', { 'name': "Form" }).click();
cy.findByRole('link', { 'name': "Preview" })
.invoke('removeAttr', 'target')
.click();
cy.findByRole('button', { 'name': 'Send form' }).click();
cy.findByRole('link', { 'name': 'My test form' }).click();

// Verify actor in ticket
cy.findByRole('region', { 'name': "Actors" }).within(() => {
cy.get(`select[data-actor-type="${actorConfig.dataAttr}"]`).contains('E2E Tests');
});
});
});
});
});
Loading

0 comments on commit 0a236c0

Please sign in to comment.