Skip to content

Commit

Permalink
feat(forms): Adding missing questions and destination configs for def…
Browse files Browse the repository at this point in the history
…ault forms
  • Loading branch information
ccailly authored and cedric-anne committed Nov 13, 2024
1 parent 608ec96 commit 42b861f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 13 deletions.
109 changes: 100 additions & 9 deletions phpunit/functional/Glpi/Helpdesk/DefaultDataManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@

namespace test\units\Glpi\Helpdesk;

use Auth;
use CommonITILActor;
use Computer;
use DbTestCase;
use Glpi\Form\AccessControl\FormAccessControlManager;
use Glpi\Form\AccessControl\FormAccessParameters;
use Glpi\Form\AnswersSet;
use Glpi\Helpdesk\DefaultDataManager;
use Glpi\Form\Form;
use Glpi\Session\SessionInfo;
use Glpi\Tests\FormTesterTrait;
use ITILCategory;
use Location;
use Monitor;
use Session;
use Ticket;
use User;

Expand Down Expand Up @@ -113,19 +119,38 @@ public function testRequestFormProperties(): void

public function testIncidentFormQuestions(): void
{
$this->login();

// Arrange: Get default incident form and fetch test data
$rows = (new Form())->find(['name' => 'Report an issue']);
$row = current($rows);
$form = Form::getById($row['id']);

// Create test categories and locations
// Create test categories, locations and user devices
$category = $this->createItem(ITILCategory::class, [
'name' => 'Test category',
'is_incident' => true,
]);
$location = $this->createItem(Location::class, [
'name' => 'Test location',
]);
$computer = $this->createItem(Computer::class, [
'name' => 'Test User Computer',
'users_id' => Session::getLoginUserID(),
'entities_id' => $this->getTestRootEntity(true),
]);
$monitors = $this->createItems(Monitor::class, [
[
'name' => 'Test Monitor 1',
'users_id' => Session::getLoginUserID(),
'entities_id' => $this->getTestRootEntity(true),
],
[
'name' => 'Test Monitor 2',
'users_id' => Session::getLoginUserID(),
'entities_id' => $this->getTestRootEntity(true),
]
]);

// Fetch test users
$tech_user_id = getItemByTypeName(User::class, "tech", true);
Expand All @@ -145,8 +170,13 @@ public function testIncidentFormQuestions(): void
],
'Urgency' => 5, // Very high
'Watchers' => [
User::class . '-' . $tech_user_id,
User::class . '-' . $normal_user_id,
User::getForeignKeyField() . '-' . $tech_user_id,
User::getForeignKeyField() . '-' . $normal_user_id,
],
'User devices' => [
Computer::class . '_' . $computer->getID(),
Monitor::class . '_' . $monitors[0]->getID(),
Monitor::class . '_' . $monitors[1]->getID(),
]
]);

Expand All @@ -156,25 +186,62 @@ public function testIncidentFormQuestions(): void
$this->assertEquals('My ticket content', $ticket->fields['content']);
$this->assertEquals($category->getID(), $ticket->fields['itilcategories_id']);
$this->assertEquals($location->getID(), $ticket->fields['locations_id']);
$this->assertEquals(5, $ticket->fields['urgency']);
// TODO: check observers, not possible yet as there are not configurable.
$this->assertEquals(
[$tech_user_id, $normal_user_id],
array_column($ticket->getActorsForType(CommonITILActor::OBSERVER), 'items_id')
);
$this->assertEquals(
[User::class, User::class],
array_column($ticket->getActorsForType(CommonITILActor::OBSERVER), 'itemtype')
);
$this->assertEquals(
[Computer::class, Monitor::class, AnswersSet::class],
array_keys($ticket->getLinkedItems())
);
$this->assertEquals(
[$monitors[0]->getID(), $monitors[1]->getID()],
array_values($ticket->getLinkedItems()[Monitor::class])
);
$this->assertEquals(
[$computer->getID()],
array_values($ticket->getLinkedItems()[Computer::class])
);
}

public function testRequestFormQuestions(): void
{
$this->login();

// Arrange: Get default request form and fetch test data
$rows = (new Form())->find(['name' => 'Request a service']);
$row = current($rows);
$form = Form::getById($row['id']);

// Create test categories and locations
// Create test categories, locations and user devices
$category = $this->createItem(ITILCategory::class, [
'name' => 'Test category',
'is_incident' => true,
]);
$location = $this->createItem(Location::class, [
'name' => 'Test location',
]);
$computer = $this->createItem(Computer::class, [
'name' => 'Test User Computer',
'users_id' => Session::getLoginUserID(),
'entities_id' => $this->getTestRootEntity(true),
]);
$monitors = $this->createItems(Monitor::class, [
[
'name' => 'Test Monitor 1',
'users_id' => Session::getLoginUserID(),
'entities_id' => $this->getTestRootEntity(true),
],
[
'name' => 'Test Monitor 2',
'users_id' => Session::getLoginUserID(),
'entities_id' => $this->getTestRootEntity(true),
]
]);

// Fetch test users
$tech_user_id = getItemByTypeName(User::class, "tech", true);
Expand All @@ -194,8 +261,13 @@ public function testRequestFormQuestions(): void
],
'Urgency' => 5, // Very high
'Watchers' => [
User::class . '-' . $tech_user_id,
User::class . '-' . $normal_user_id,
User::getForeignKeyField() . '-' . $tech_user_id,
User::getForeignKeyField() . '-' . $normal_user_id,
],
'User devices' => [
Computer::class . '_' . $computer->getID(),
Monitor::class . '_' . $monitors[0]->getID(),
Monitor::class . '_' . $monitors[1]->getID(),
]
]);

Expand All @@ -206,7 +278,26 @@ public function testRequestFormQuestions(): void
$this->assertEquals($category->getID(), $ticket->fields['itilcategories_id']);
$this->assertEquals($location->getID(), $ticket->fields['locations_id']);
$this->assertEquals(5, $ticket->fields['urgency']);
// TODO: check observers, not possible yet as there are not configurable.
$this->assertEquals(
[$tech_user_id, $normal_user_id],
array_column($ticket->getActorsForType(CommonITILActor::OBSERVER), 'items_id')
);
$this->assertEquals(
[User::class, User::class],
array_column($ticket->getActorsForType(CommonITILActor::OBSERVER), 'itemtype')
);
$this->assertEquals(
[Computer::class, Monitor::class, AnswersSet::class],
array_keys($ticket->getLinkedItems())
);
$this->assertEquals(
[$monitors[0]->getID(), $monitors[1]->getID()],
array_values($ticket->getLinkedItems()[Monitor::class])
);
$this->assertEquals(
[$computer->getID()],
array_values($ticket->getLinkedItems()[Computer::class])
);
}

public function testIncidentFormShouldBeAccessibleBySelfServiceUsers(): void
Expand Down
35 changes: 31 additions & 4 deletions src/Glpi/Helpdesk/DefaultDataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@
use Glpi\Form\AccessControl\ControlType\AllowList;
use Glpi\Form\AccessControl\ControlType\AllowListConfig;
use Glpi\Form\AccessControl\FormAccessControl;
use Glpi\Form\Destination\CommonITILField\AssociatedItemsField;
use Glpi\Form\Destination\CommonITILField\AssociatedItemsFieldConfig;
use Glpi\Form\Destination\CommonITILField\AssociatedItemsFieldStrategy;
use Glpi\Form\Destination\CommonITILField\ContentField;
use Glpi\Form\Destination\CommonITILField\ITILActorFieldConfig;
use Glpi\Form\Destination\CommonITILField\ITILActorFieldStrategy;
use Glpi\Form\Destination\CommonITILField\ObserverField;
use Glpi\Form\Destination\CommonITILField\RequestTypeField;
use Glpi\Form\Destination\CommonITILField\RequestTypeFieldConfig;
use Glpi\Form\Destination\CommonITILField\RequestTypeFieldStrategy;
Expand All @@ -54,6 +60,7 @@
use Glpi\Form\QuestionType\QuestionTypeObserver;
use Glpi\Form\QuestionType\QuestionTypeShortText;
use Glpi\Form\QuestionType\QuestionTypeUrgency;
use Glpi\Form\QuestionType\QuestionTypeUserDevice;
use Glpi\Form\Section;
use Glpi\Form\Tag\AnswerTagProvider;
use ITILCategory;
Expand Down Expand Up @@ -105,7 +112,7 @@ private function createIncidentForm(): void
// Add questions
$this->addQuestion($section, $this->getUrgencyQuestionData());
$this->addQuestion($section, $this->getCategoryQuestionData());
// TODO: associated items (not possible yet)
$this->addQuestion($section, $this->getUserDevicesQuestionData());
$this->addQuestion($section, $this->getWatchersQuestionData());
$this->addQuestion($section, $this->getLocationQuestionData());
$title_question = $this->addQuestion($section, $this->getTitleQuestionData());
Expand Down Expand Up @@ -136,7 +143,12 @@ private function createIncidentForm(): void
RequestTypeField::getKey() => (new RequestTypeFieldConfig(
strategy: RequestTypeFieldStrategy::SPECIFIC_VALUE,
specific_request_type: Ticket::INCIDENT_TYPE,
))->jsonSerialize()
))->jsonSerialize(),

// Set last valid answer as observer
ObserverField::getKey() => (new ITILActorFieldConfig(
strategy: ITILActorFieldStrategy::LAST_VALID_ANSWER,
))->jsonSerialize(),
];

// Add ticket destination
Expand All @@ -161,7 +173,7 @@ private function createRequestForm(): void
// Add questions
$this->addQuestion($section, $this->getUrgencyQuestionData());
$this->addQuestion($section, $this->getCategoryQuestionData());
// TODO: associated items (not possible yet)
$this->addQuestion($section, $this->getUserDevicesQuestionData());
$this->addQuestion($section, $this->getWatchersQuestionData());
$this->addQuestion($section, $this->getLocationQuestionData());
$title_question = $this->addQuestion($section, $this->getTitleQuestionData());
Expand Down Expand Up @@ -192,7 +204,12 @@ private function createRequestForm(): void
RequestTypeField::getKey() => (new RequestTypeFieldConfig(
strategy: RequestTypeFieldStrategy::SPECIFIC_VALUE,
specific_request_type: Ticket::DEMAND_TYPE,
))->jsonSerialize()
))->jsonSerialize(),

// Set last valid answer as observer
ObserverField::getKey() => (new ITILActorFieldConfig(
strategy: ITILActorFieldStrategy::LAST_VALID_ANSWER,
))->jsonSerialize(),
];

// Add ticket destination
Expand Down Expand Up @@ -276,6 +293,16 @@ private function getCategoryQuestionData(): array
];
}

private function getUserDevicesQuestionData(): array
{
return [
'type' => QuestionTypeUserDevice::class,
'name' => __("User devices"),
'default_value' => 0,
'extra_data' => json_encode(['is_multiple_devices' => false]),
];
}

private function getLocationQuestionData(): array
{
return [
Expand Down

0 comments on commit 42b861f

Please sign in to comment.