3737use Glpi \Api \HL \Controller \AbstractController ;
3838use Glpi \Search \SearchOption ;
3939use Psr \Log \LogLevel ;
40+ use Webhook ;
4041
4142class WebhookTest extends \DbTestCase
4243{
@@ -46,7 +47,7 @@ class WebhookTest extends \DbTestCase
4647 */
4748 public function testWebhookTypesHaveIDOpt ()
4849 {
49- $ supported = \ Webhook::getItemtypesDropdownValues ();
50+ $ supported = Webhook::getItemtypesDropdownValues ();
5051 $ itemtypes = [];
5152 foreach ($ supported as $ types ) {
5253 $ itemtypes = array_merge ($ itemtypes , array_keys ($ types ));
@@ -228,7 +229,7 @@ public function testWebhookHeaderTemplate()
228229 public function testGetResultForPath ()
229230 {
230231 $ this ->login ();
231- /** @var \ Webhook $webhook */
232+ /** @var Webhook $webhook */
232233 $ webhook = $ this ->createItem ('Webhook ' , [
233234 'name ' => 'Test webhook ' ,
234235 'entities_id ' => $ _SESSION ['glpiactive_entity ' ],
@@ -249,7 +250,7 @@ public function testGetAPIItemtypeData()
249250 $ this ->login ();
250251 $ this ->initAssetDefinition ();
251252
252- $ supported_types = \ Webhook::getAPIItemtypeData ();
253+ $ supported_types = Webhook::getAPIItemtypeData ();
253254 foreach ($ supported_types as $ controller => $ type_data ) {
254255 $ this ->assertTrue (is_subclass_of ($ controller , AbstractController::class));
255256 foreach ($ type_data as $ category => $ types ) {
@@ -266,7 +267,7 @@ public function testGetAPIPath()
266267 {
267268 $ this ->login ();
268269
269- $ webhook = new \ Webhook ();
270+ $ webhook = new Webhook ();
270271 $ computer = getItemByTypeName ('Computer ' , '_test_pc01 ' );
271272 $ this ->assertEquals ('/Assets/Computer/ ' . $ computer ->getID (), $ webhook ->getAPIPath ($ computer ));
272273
@@ -283,7 +284,7 @@ public function testWithHLAPIDisabled(): void
283284 global $ CFG_GLPI ;
284285 $ this ->login ();
285286 $ CFG_GLPI ['enable_hlapi ' ] = 0 ;
286- /** @var \ Webhook $webhook */
287+ /** @var Webhook $webhook */
287288 $ webhook = $ this ->createItem ('Webhook ' , [
288289 'name ' => 'Test webhook ' ,
289290 'entities_id ' => $ _SESSION ['glpiactive_entity ' ],
@@ -301,12 +302,12 @@ public function testWithHLAPIDisabled(): void
301302
302303 public function testGetMonacoSuggestions ()
303304 {
304- $ itemtypes = \ Webhook::getItemtypesDropdownValues ();
305+ $ itemtypes = Webhook::getItemtypesDropdownValues ();
305306
306307 foreach ($ itemtypes as $ types ) {
307308 $ this ->assertIsArray ($ types );
308309 foreach ($ types as $ itemtype => $ label ) {
309- $ suggestions = \ Webhook::getMonacoSuggestions ($ itemtype );
310+ $ suggestions = Webhook::getMonacoSuggestions ($ itemtype );
310311 $ this ->assertNotEmpty ($ suggestions , "Missing suggestions for $ itemtype " );
311312 }
312313 }
@@ -316,7 +317,7 @@ public function testWebhookNotBlocker(): void
316317 {
317318 global $ DB ;
318319
319- $ this ->createItem (\ Webhook::class, [
320+ $ this ->createItem (Webhook::class, [
320321 'name ' => 'Test webhook ' ,
321322 'entities_id ' => $ _SESSION ['glpiactive_entity ' ],
322323 'url ' => 'http://localhost ' ,
@@ -360,4 +361,35 @@ public function testWebhookNotBlocker(): void
360361 ]
361362 );
362363 }
364+
365+ public function testItemtypeDropdownExcludesNoReadItemtypes ()
366+ {
367+ $ this ->login ();
368+ $ this ->assertContains ('Computer ' , Webhook::getItemtypesDropdownValues ()['Assets ' ]);
369+ $ this ->assertContains ('Monitor ' , Webhook::getItemtypesDropdownValues ()['Assets ' ]);
370+ $ _SESSION ['glpiactiveprofile ' ]['computer ' ] = ALLSTANDARDRIGHT & ~READ ;
371+ $ this ->assertNotContains ('Computer ' , Webhook::getItemtypesDropdownValues ()['Assets ' ]);
372+ $ this ->assertContains ('Monitor ' , Webhook::getItemtypesDropdownValues ()['Assets ' ]);
373+ }
374+
375+ public function testCreateUpdateNoReadItemtypes ()
376+ {
377+ $ this ->login ();
378+ $ webhook = $ this ->createItem ('Webhook ' , [
379+ 'name ' => 'Test webhook ' ,
380+ 'entities_id ' => $ _SESSION ['glpiactive_entity ' ],
381+ 'url ' => 'http://localhost ' ,
382+ 'itemtype ' => 'Computer ' ,
383+ 'event ' => 'new ' ,
384+ 'is_active ' => 1 ,
385+ 'use_default_payload ' => 1 ,
386+ ]);
387+ $ this ->assertTrue ($ webhook ->canUpdateItem ());
388+ $ _SESSION ['glpiactiveprofile ' ]['computer ' ] = ALLSTANDARDRIGHT & ~READ ;
389+ $ this ->assertFalse ($ webhook ->canUpdateItem ());
390+
391+ $ this ->assertFalse ($ webhook ->canCreateItem ());
392+ $ webhook ->fields ['itemtype ' ] = 'Monitor ' ;
393+ $ this ->assertTrue ($ webhook ->canCreateItem ());
394+ }
363395}
0 commit comments