From 984d6379f76b84573f989e18acd15b64c3868482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Cailly?= Date: Mon, 23 Sep 2024 15:44:49 +0200 Subject: [PATCH] refactor: Remove strategy options --- .../CommonITILField/EntityFieldTest.php | 99 +------------------ .../CommonITILField/EntityFieldStrategy.php | 45 --------- .../destination_config_fields/entity.cy.js | 12 --- 3 files changed, 2 insertions(+), 154 deletions(-) diff --git a/phpunit/functional/Glpi/Form/Destination/CommonITILField/EntityFieldTest.php b/phpunit/functional/Glpi/Form/Destination/CommonITILField/EntityFieldTest.php index d866fc46543..4626b535ae8 100644 --- a/phpunit/functional/Glpi/Form/Destination/CommonITILField/EntityFieldTest.php +++ b/phpunit/functional/Glpi/Form/Destination/CommonITILField/EntityFieldTest.php @@ -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 { @@ -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' => [ @@ -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 ]; } @@ -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(); @@ -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", diff --git a/src/Glpi/Form/Destination/CommonITILField/EntityFieldStrategy.php b/src/Glpi/Form/Destination/CommonITILField/EntityFieldStrategy.php index c45b23779e0..024fa3cbb33 100644 --- a/src/Glpi/Form/Destination/CommonITILField/EntityFieldStrategy.php +++ b/src/Glpi/Form/Destination/CommonITILField/EntityFieldStrategy.php @@ -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'; @@ -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'), @@ -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(), @@ -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, diff --git a/tests/cypress/e2e/form/destination_config_fields/entity.cy.js b/tests/cypress/e2e/form/destination_config_fields/entity.cy.js index 5419f47adc4..eb9c3a19387 100644 --- a/tests/cypress/e2e/form/destination_config_fields/entity.cy.js +++ b/tests/cypress/e2e/form/destination_config_fields/entity.cy.js @@ -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');