|
35 | 35 | namespace tests\units; |
36 | 36 |
|
37 | 37 | use CommonDBTM; |
| 38 | +use CommonITILObject; |
38 | 39 | use Computer; |
39 | 40 | use DbTestCase; |
| 41 | +use Entity; |
40 | 42 | use Generator; |
41 | 43 | use Glpi\Asset\Asset_PeripheralAsset; |
42 | 44 | use Glpi\Features\AssignableItem; |
|
45 | 47 | use Item_DeviceSimcard; |
46 | 48 | use Monitor; |
47 | 49 | use PHPUnit\Framework\Attributes\DataProvider; |
| 50 | +use Profile; |
48 | 51 | use Session; |
49 | 52 | use State; |
50 | 53 | use Symfony\Component\DomCrawler\Crawler; |
| 54 | +use Ticket; |
51 | 55 | use User; |
52 | 56 |
|
53 | 57 | /* Test for inc/dropdown.class.php */ |
@@ -2569,20 +2573,20 @@ public function testSupplierActorDropdownOnlyActive() |
2569 | 2573 | 'is_recursive' => 1, |
2570 | 2574 | ]); |
2571 | 2575 | $params = [ |
2572 | | - 'itemtype' => \Ticket::class, |
| 2576 | + 'itemtype' => Ticket::class, |
2573 | 2577 | 'actortype' => 'assign', |
2574 | 2578 | 'returned_itemtypes' => [\Supplier::class], |
2575 | 2579 | 'searchText' => '', |
2576 | 2580 | ]; |
2577 | | - $results = \Dropdown::getDropdownActors($params + ['_idor_token' => Session::getNewIDORToken(\Ticket::class, $params)], false); |
| 2581 | + $results = \Dropdown::getDropdownActors($params + ['_idor_token' => Session::getNewIDORToken(Ticket::class, $params)], false); |
2578 | 2582 | $this->assertNotEmpty($results['results'][0]['children']); |
2579 | 2583 | $this->assertCount(0, array_filter($results['results'][0]['children'], function ($result) use ($inactive_supplier) { |
2580 | 2584 | return $result['id'] === \Supplier::class . '_' . $inactive_supplier->getID(); |
2581 | 2585 | })); |
2582 | 2586 |
|
2583 | 2587 | // If asking for inactive_deleted, it should return the inactive supplier |
2584 | 2588 | $params['inactive_deleted'] = 1; |
2585 | | - $results = \Dropdown::getDropdownActors($params + ['_idor_token' => Session::getNewIDORToken(\Ticket::class, $params)], false); |
| 2589 | + $results = \Dropdown::getDropdownActors($params + ['_idor_token' => Session::getNewIDORToken(Ticket::class, $params)], false); |
2586 | 2590 | $this->assertNotEmpty($results['results'][0]['children']); |
2587 | 2591 | $this->assertCount(1, array_filter($results['results'][0]['children'], function ($result) use ($inactive_supplier) { |
2588 | 2592 | return $result['id'] === \Supplier::class . '_' . $inactive_supplier->getID(); |
@@ -2685,7 +2689,7 @@ public function testGetDropdownMyDevices() |
2685 | 2689 | ]); |
2686 | 2690 |
|
2687 | 2691 | // Ensure proper permissions and helpdesk types |
2688 | | - $_SESSION["glpiactiveprofile"]["helpdesk_hardware"] = pow(2, \Ticket::HELPDESK_MY_HARDWARE); |
| 2692 | + $_SESSION["glpiactiveprofile"]["helpdesk_hardware"] = pow(2, Ticket::HELPDESK_MY_HARDWARE); |
2689 | 2693 | $_SESSION["glpiactiveprofile"]["helpdesk_item_type"] = ['Computer', 'Monitor', 'Printer']; |
2690 | 2694 |
|
2691 | 2695 | $post = [ |
@@ -2768,4 +2772,82 @@ public function testGetDropdownMyDevices() |
2768 | 2772 | // Test that count is accurate |
2769 | 2773 | $this->assertGreaterThan(0, $result['count']); |
2770 | 2774 | } |
| 2775 | + |
| 2776 | + public static function assetsDropdownForHelpdeskProvider(): iterable |
| 2777 | + { |
| 2778 | + yield 'no rights' => [ |
| 2779 | + 'can_view' => 0, |
| 2780 | + 'itemtypes' => [Computer::class], |
| 2781 | + 'expected' => [], |
| 2782 | + ]; |
| 2783 | + yield 'see his own computers' => [ |
| 2784 | + 'can_view' => 2 ** CommonITILObject::HELPDESK_MY_HARDWARE, |
| 2785 | + 'itemtypes' => [Computer::class], |
| 2786 | + 'expected' => ['My computer'], |
| 2787 | + ]; |
| 2788 | + yield 'see all computers' => [ |
| 2789 | + 'can_view' => 2 ** CommonITILObject::HELPDESK_ALL_HARDWARE, |
| 2790 | + 'itemtypes' => [Computer::class], |
| 2791 | + 'expected' => ['My computer', 'Not my computer'], |
| 2792 | + ]; |
| 2793 | + yield 'see all monitors' => [ |
| 2794 | + 'can_view' => 2 ** CommonITILObject::HELPDESK_ALL_HARDWARE, |
| 2795 | + 'itemtypes' => [Monitor::class], |
| 2796 | + 'expected' => [], |
| 2797 | + ]; |
| 2798 | + } |
| 2799 | + |
| 2800 | + #[DataProvider('assetsDropdownForHelpdeskProvider')] |
| 2801 | + public function testAssetsDropdownForHelpdesk( |
| 2802 | + int $can_view, |
| 2803 | + array $itemtypes, |
| 2804 | + array $expected, |
| 2805 | + ): void { |
| 2806 | + // Arrange: assign a computer to a self-service user and set up the |
| 2807 | + // profile with the given rights. |
| 2808 | + // Wrap items in an entity for better test isolation |
| 2809 | + $this->login(); // Need to be logged in to create an entity |
| 2810 | + $entity = $this->createItem(Entity::class, [ |
| 2811 | + 'name' => 'My entity', |
| 2812 | + 'entities_id' => $this->getTestRootEntity(only_id: true), |
| 2813 | + ]); |
| 2814 | + $this->logOut(); |
| 2815 | + $this->createItem(Computer::class, [ |
| 2816 | + 'name' => 'My computer', |
| 2817 | + 'entities_id' => $entity->getID(), |
| 2818 | + 'users_id' => getItemByTypeName(User::class, "post-only", true), |
| 2819 | + ]); |
| 2820 | + $this->createItem(Computer::class, [ |
| 2821 | + 'name' => 'Not my computer', |
| 2822 | + 'entities_id' => $entity->getID(), |
| 2823 | + ]); |
| 2824 | + $this->updateItem( |
| 2825 | + Profile::class, |
| 2826 | + getItemByTypeName(Profile::class, 'Self-Service', onlyid: true), |
| 2827 | + [ |
| 2828 | + 'helpdesk_hardware' => $can_view, |
| 2829 | + 'helpdesk_item_type' => $itemtypes, |
| 2830 | + ], |
| 2831 | + ['helpdesk_item_type'], |
| 2832 | + ); |
| 2833 | + |
| 2834 | + // Act: get dropdown values for this user |
| 2835 | + $this->login('post-only'); |
| 2836 | + $this->setEntity("My entity", false); |
| 2837 | + $params = [ |
| 2838 | + 'itemtype' => Computer::class, |
| 2839 | + ]; |
| 2840 | + $params['_idor_token'] = Session::getNewIDORToken(Computer::class, $params); |
| 2841 | + $results = \Dropdown::getDropdownValue($params, false); |
| 2842 | + |
| 2843 | + // Assert: only one computer should be count |
| 2844 | + $this->assertEquals(count($expected), $results["count"]); |
| 2845 | + if (!empty($expected)) { |
| 2846 | + $found_items = array_map( |
| 2847 | + fn($data) => $data['text'], |
| 2848 | + $results["results"][1]["children"], |
| 2849 | + ); |
| 2850 | + $this->assertEquals($expected, $found_items); |
| 2851 | + } |
| 2852 | + } |
2771 | 2853 | } |
0 commit comments