Skip to content

Commit 62c7b98

Browse files
authored
Webhook: Can't edit the webhook configuration for ITILFollowup - multiple array exceptions (#21181)
* Fix arrays exception (key not existing or value was an array) * Cherry pick test from Seb/fix-webhook-form + merge condition for schema_name * Code cleanup * Fix Document_Item missing api schema
1 parent efc0cbc commit 62c7b98

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

phpunit/functional/WebhookTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,17 @@ public function testWithHLAPIDisabled(): void
297297
// The main purpose is to test the internal authentication middleware.
298298
$this->assertNotNull($webhook->getResultForPath('/Administration/User/' . $users_id, 'new', 'User', $users_id));
299299
}
300+
301+
public function testGetMonacoSuggestions()
302+
{
303+
$itemtypes = \Webhook::getItemtypesDropdownValues();
304+
305+
foreach ($itemtypes as $types) {
306+
$this->assertIsArray($types);
307+
foreach ($types as $itemtype => $label) {
308+
$suggestions = \Webhook::getMonacoSuggestions($itemtype);
309+
$this->assertNotEmpty($suggestions, "Missing suggestions for $itemtype");
310+
}
311+
}
312+
}
300313
}

src/Webhook.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ public static function getAPIItemtypeData(): array
374374
ChangeTask::class => ['parent' => Change::class],
375375
ProblemTask::class => ['parent' => Problem::class],
376376
ITILFollowup::class => [], // All main types can be the parent
377-
Document_Item::class => [],
378377
ITILSolution::class => [],
379378
TicketValidation::class => ['parent' => Ticket::class],
380379
],
@@ -385,6 +384,9 @@ public static function getAPIItemtypeData(): array
385384
Contract::class, Database::class, Datacenter::class, Document::class, Domain::class,
386385
SoftwareLicense::class, Line::class, Supplier::class,
387386
],
387+
'subtypes' => [
388+
Document_Item::class => ['parent' => Document::class],
389+
],
388390
],
389391
];
390392

@@ -438,9 +440,9 @@ public static function getAPIItemtypeData(): array
438440
}
439441

440442
/**
441-
* Return a list of GLPI itemtypes availabel through HL API.
443+
* Return a list of GLPI itemtypes available through HL API.
442444
*
443-
* @return array
445+
* @return array<array>
444446
*/
445447
public static function getItemtypesDropdownValues(): array
446448
{
@@ -917,10 +919,19 @@ public static function getAPISchemaBySupportedItemtype(string $itemtype): ?array
917919
$controller_class = $controller;
918920
break;
919921
}
922+
920923
if (isset($categories['subtypes']) && array_key_exists($itemtype, $categories['subtypes'])) {
921-
$schema_name = $categories['subtypes'][$itemtype]['name'];
922-
$schema_name = $categories['main'][$categories['subtypes'][$itemtype]['parent']]['name'] . $schema_name;
923924
$controller_class = $controller;
925+
$schema_name = $categories['subtypes'][$itemtype]['name'];
926+
927+
if (
928+
array_key_exists('parent', $categories['subtypes'][$itemtype])
929+
&& array_key_exists($categories['subtypes'][$itemtype]['parent'], $categories['main'])
930+
&& array_key_exists('name', $categories['main'][$categories['subtypes'][$itemtype]['parent']])
931+
) {
932+
$schema_name = $categories['main'][$categories['subtypes'][$itemtype]['parent']]['name'] . $schema_name;
933+
}
934+
924935
break;
925936
}
926937
}
@@ -939,6 +950,9 @@ public static function getMonacoSuggestions(?string $itemtype): array
939950
return [];
940951
}
941952
$schema = self::getAPISchemaBySupportedItemtype($itemtype);
953+
if (is_null($schema)) {
954+
return [];
955+
}
942956
$props = Doc\Schema::flattenProperties($schema['properties'], 'item.');
943957
$parent_schema = self::getParentItemSchema($itemtype);
944958
$parent_props = $parent_schema !== [] ? Doc\Schema::flattenProperties($parent_schema['properties'], 'parent_item.') : [];
@@ -953,7 +967,7 @@ public static function getMonacoSuggestions(?string $itemtype): array
953967
$subtype_labels = [];
954968
if (isset($parent_schema['x-subtypes'])) {
955969
foreach ($parent_schema['x-subtypes'] as $subtype) {
956-
$subtype_labels[$subtype] = $subtype['itemtype']::getTypeName(1);
970+
$subtype_labels[$subtype['itemtype']] = $subtype['itemtype']::getTypeName(1);
957971
}
958972
}
959973
foreach ($props as $prop_name => $prop_data) {

0 commit comments

Comments
 (0)