diff --git a/phpunit/functional/WebhookTest.php b/phpunit/functional/WebhookTest.php index 14f812f7cd4..077336f1060 100644 --- a/phpunit/functional/WebhookTest.php +++ b/phpunit/functional/WebhookTest.php @@ -297,4 +297,17 @@ public function testWithHLAPIDisabled(): void // The main purpose is to test the internal authentication middleware. $this->assertNotNull($webhook->getResultForPath('/Administration/User/' . $users_id, 'new', 'User', $users_id)); } + + public function testGetMonacoSuggestions() + { + $itemtypes = \Webhook::getItemtypesDropdownValues(); + + foreach ($itemtypes as $types) { + $this->assertIsArray($types); + foreach ($types as $itemtype => $label) { + $suggestions = \Webhook::getMonacoSuggestions($itemtype); + $this->assertNotEmpty($suggestions, "Missing suggestions for $itemtype"); + } + } + } } diff --git a/src/Webhook.php b/src/Webhook.php index d1e79e9c543..e4380455b63 100644 --- a/src/Webhook.php +++ b/src/Webhook.php @@ -374,7 +374,6 @@ public static function getAPIItemtypeData(): array ChangeTask::class => ['parent' => Change::class], ProblemTask::class => ['parent' => Problem::class], ITILFollowup::class => [], // All main types can be the parent - Document_Item::class => [], ITILSolution::class => [], TicketValidation::class => ['parent' => Ticket::class], ], @@ -385,6 +384,9 @@ public static function getAPIItemtypeData(): array Contract::class, Database::class, Datacenter::class, Document::class, Domain::class, SoftwareLicense::class, Line::class, Supplier::class, ], + 'subtypes' => [ + Document_Item::class => ['parent' => Document::class], + ], ], ]; @@ -438,9 +440,9 @@ public static function getAPIItemtypeData(): array } /** - * Return a list of GLPI itemtypes availabel through HL API. + * Return a list of GLPI itemtypes available through HL API. * - * @return array + * @return array */ public static function getItemtypesDropdownValues(): array { @@ -917,10 +919,19 @@ public static function getAPISchemaBySupportedItemtype(string $itemtype): ?array $controller_class = $controller; break; } + if (isset($categories['subtypes']) && array_key_exists($itemtype, $categories['subtypes'])) { - $schema_name = $categories['subtypes'][$itemtype]['name']; - $schema_name = $categories['main'][$categories['subtypes'][$itemtype]['parent']]['name'] . $schema_name; $controller_class = $controller; + $schema_name = $categories['subtypes'][$itemtype]['name']; + + if ( + array_key_exists('parent', $categories['subtypes'][$itemtype]) + && array_key_exists($categories['subtypes'][$itemtype]['parent'], $categories['main']) + && array_key_exists('name', $categories['main'][$categories['subtypes'][$itemtype]['parent']]) + ) { + $schema_name = $categories['main'][$categories['subtypes'][$itemtype]['parent']]['name'] . $schema_name; + } + break; } } @@ -939,6 +950,9 @@ public static function getMonacoSuggestions(?string $itemtype): array return []; } $schema = self::getAPISchemaBySupportedItemtype($itemtype); + if (is_null($schema)) { + return []; + } $props = Doc\Schema::flattenProperties($schema['properties'], 'item.'); $parent_schema = self::getParentItemSchema($itemtype); $parent_props = $parent_schema !== [] ? Doc\Schema::flattenProperties($parent_schema['properties'], 'parent_item.') : []; @@ -953,7 +967,7 @@ public static function getMonacoSuggestions(?string $itemtype): array $subtype_labels = []; if (isset($parent_schema['x-subtypes'])) { foreach ($parent_schema['x-subtypes'] as $subtype) { - $subtype_labels[$subtype] = $subtype['itemtype']::getTypeName(1); + $subtype_labels[$subtype['itemtype']] = $subtype['itemtype']::getTypeName(1); } } foreach ($props as $prop_name => $prop_data) {