Skip to content

Commit

Permalink
refactor: Remove strategy options
Browse files Browse the repository at this point in the history
  • Loading branch information
ccailly committed Sep 24, 2024
1 parent ed0fd9e commit 984d637
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,13 @@
use DbTestCase;
use Entity;
use Glpi\Form\AnswersHandler\AnswersHandler;
use Glpi\Form\Destination\CommonITILField\EntityField;
use Glpi\Form\Destination\CommonITILField\EntityFieldConfig;
use Glpi\Form\Destination\CommonITILField\EntityFieldStrategy;
use Glpi\Form\Destination\FormDestinationTicket;
use Glpi\Form\Form;
use Glpi\Form\QuestionType\QuestionTypeItem;
use Glpi\Form\QuestionType\QuestionTypeRequester;
use Glpi\Tests\FormBuilder;
use Glpi\Tests\FormTesterTrait;
use Group;
use Group_User;
use Session;
use User;

final class EntityFieldTest extends DbTestCase
{
Expand All @@ -71,13 +65,6 @@ private function getAnswers()
['name' => 'Entity 4', 'entities_id' => $this->getTestRootEntity(true)],
['name' => 'Entity 5', 'entities_id' => $this->getTestRootEntity(true)],
]);
$users = $this->createItems(User::class, [
['name' => 'User 1', 'entities_id' => $entities[2]->getId()],
['name' => 'User 2', 'entities_id' => $entities[3]->getId()],
]);
$groups = $this->createItems(Group::class, [
['name' => "Group 1", 'entities_id' => $entities[4]->getId()],
]);

return [
'answers' => [
Expand All @@ -88,21 +75,9 @@ private function getAnswers()
"Entity 2" => [
'itemtype' => Entity::getType(),
'items_id' => $entities[1]->getId(),
],
"Requester 1" => [
'users_id-' . $users[0]->getId(),
],
"Requester 2" => [
'groups_id-' . $groups[0]->getId(),
],
"Requester 3" => [
'users_id-' . $users[1]->getId(),
'groups_id-' . $groups[0]->getId(),
],
]
],
'entities' => $entities,
'users' => $users,
'groups' => $groups,
'entities' => $entities
];
}

Expand Down Expand Up @@ -149,73 +124,6 @@ public function testEntityFromFormWithSpecificEntityId()
);
}

public function testEntityFromFirstRequester()
{
$form = $this->createAndGetFormWithMultipleEntityAndRequesterQuestions();
$answers = $this->getAnswers();

$this->sendFormAndAssertTicketEntity(
form: $form,
config: new EntityFieldConfig(
EntityFieldStrategy::FROM_FIRST_REQUESTER
),
answers: $answers['answers'],
expected_entity_id: $answers['users'][0]->fields['entities_id']
);
}

public function testEntityFromFirstRequesterGroup()
{
$form = $this->createAndGetFormWithMultipleEntityAndRequesterQuestions();
$answers = $this->getAnswers();

// Drop the "Requester 1" answer
unset($answers['answers']["Requester 1"]);

$this->sendFormAndAssertTicketEntity(
form: $form,
config: new EntityFieldConfig(
EntityFieldStrategy::FROM_FIRST_REQUESTER
),
answers: $answers['answers'],
expected_entity_id: $answers['groups'][0]->fields['entities_id']
);
}

public function testEntityFromFirstRequesters()
{
$form = $this->createAndGetFormWithMultipleEntityAndRequesterQuestions();
$answers = $this->getAnswers();

// Drop the "Requester 1" and "Requester 2" answers
unset($answers['answers']["Requester 1"]);
unset($answers['answers']["Requester 2"]);

$this->sendFormAndAssertTicketEntity(
form: $form,
config: new EntityFieldConfig(
EntityFieldStrategy::FROM_FIRST_REQUESTER
),
answers: $answers['answers'],
expected_entity_id: $answers['users'][1]->fields['entities_id']
);
}

public function testEntityFromUser()
{
$form = $this->createAndGetFormWithMultipleEntityAndRequesterQuestions();
$answers = $this->getAnswers();

$this->sendFormAndAssertTicketEntity(
form: $form,
config: new EntityFieldConfig(
EntityFieldStrategy::FROM_USER
),
answers: $answers['answers'],
expected_entity_id: Session::getActiveEntity()
);
}

public function testEntityFromSpecificValue()
{
$form = $this->createAndGetFormWithMultipleEntityAndRequesterQuestions();
Expand Down Expand Up @@ -364,9 +272,6 @@ private function createAndGetFormWithMultipleEntityAndRequesterQuestions(): Form
$builder->addQuestion("Entity 2", QuestionTypeItem::class, [
'itemtype' => Entity::getType(),
]);
$builder->addQuestion("Requester 1", QuestionTypeRequester::class);
$builder->addQuestion("Requester 2", QuestionTypeRequester::class);
$builder->addQuestion("Requester 3", QuestionTypeRequester::class);
$builder->addDestination(
FormDestinationTicket::class,
"My ticket",
Expand Down
45 changes: 0 additions & 45 deletions src/Glpi/Form/Destination/CommonITILField/EntityFieldStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,10 @@
use Entity;
use Glpi\Form\AnswersSet;
use Glpi\Form\QuestionType\QuestionTypeItem;
use Glpi\Form\QuestionType\QuestionTypeRequester;
use Group;
use Group_User;
use Session;
use User;

enum EntityFieldStrategy: string
{
case FROM_FORM = 'from_form';
case FROM_FIRST_REQUESTER = 'from_first_requester';
case FROM_USER = 'from_user';
case SPECIFIC_VALUE = 'specific_value';
case SPECIFIC_ANSWER = 'specific_answer';
case LAST_VALID_ANSWER = 'last_valid_answer';
Expand All @@ -57,8 +50,6 @@ public function getLabel(): string
{
return match ($this) {
self::FROM_FORM => __("From form"),
self::FROM_FIRST_REQUESTER => __("From first requester"),
self::FROM_USER => __("From user"),
self::SPECIFIC_VALUE => __("Specific entity"),
self::SPECIFIC_ANSWER => __("Answer from a specific question"),
self::LAST_VALID_ANSWER => __('Answer to last "Entity" item question'),
Expand All @@ -71,8 +62,6 @@ public function getEntityID(
): ?int {
return match ($this) {
self::FROM_FORM => $answers_set->getItem()->fields['entities_id'],
self::FROM_FIRST_REQUESTER => $this->getEntityIDForFirstRequester($answers_set),
self::FROM_USER => Session::getActiveEntity(),
self::SPECIFIC_VALUE => $config->getSpecificEntityId(),
self::SPECIFIC_ANSWER => $this->getEntityIDForSpecificAnswer(
$config->getSpecificQuestionId(),
Expand All @@ -82,40 +71,6 @@ public function getEntityID(
};
}

private function getEntityIDForFirstRequester(
AnswersSet $answers_set,
): ?int {
$valid_answers = $answers_set->getAnswersByType(
QuestionTypeRequester::class
);

if (count($valid_answers) == 0) {
return null;
}

$answer = current($valid_answers);
$requester_type = explode('-', $answer->getRawAnswer()[0])[0];
$requester_id = explode('-', $answer->getRawAnswer()[0])[1];
if (!is_numeric($requester_id)) {
return null;
}

switch ($requester_type) {
case 'users_id':
$requester = new User();
break;
case 'groups_id':
$requester = new Group();
break;
}

if (!isset($requester) || !$requester->getFromDB($requester_id)) {
return null;
}

return $requester->fields['entities_id'];
}

private function getEntityIDForSpecificAnswer(
?int $question_id,
AnswersSet $answers_set,
Expand Down
12 changes: 0 additions & 12 deletions tests/cypress/e2e/form/destination_config_fields/entity.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,6 @@ describe('Entity configuration', () => {
cy.checkAndCloseAlert('Item successfully updated');
cy.get('@entity_dropdown').should('have.text', 'From form');

// Switch to "From first requester"
cy.get('@entity_dropdown').selectDropdownValue('From first requester');
cy.findByRole('button', { 'name': 'Update item' }).click();
cy.checkAndCloseAlert('Item successfully updated');
cy.get('@entity_dropdown').should('have.text', 'From first requester');

// Switch to "From user"
cy.get('@entity_dropdown').selectDropdownValue('From user');
cy.findByRole('button', { 'name': 'Update item' }).click();
cy.checkAndCloseAlert('Item successfully updated');
cy.get('@entity_dropdown').should('have.text', 'From user');

// Switch to "Specific entity"
cy.get('@entity_dropdown').selectDropdownValue('Specific entity');
cy.get('@config').getDropdownByLabelText('Select an entity...').as('specific_entity_dropdown');
Expand Down

0 comments on commit 984d637

Please sign in to comment.