Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -4639,12 +4639,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Item_SoftwareVersion.php',
];
$ignoreErrors[] = [
'message' => '#^Loose comparison using \\!\\= between \'Monitor\'\\|\'Peripheral\'\\|\'Phone\'\\|\'Printer\' and \'Software\' will always evaluate to true\\.$#',
'identifier' => 'notEqual.alwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Item_Ticket.php',
];
$ignoreErrors[] = [
'message' => '#^Method Item_Ticket\\:\\:dropdown\\(\\) should return int\\|string\\|false but empty return statement found\\.$#',
'identifier' => 'return.empty',
Expand All @@ -4663,12 +4657,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Item_Ticket.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#2 \\$right of static method Session\\:\\:haveRight\\(\\) expects int, string given\\.$#',
'identifier' => 'argument.type',
'count' => 1,
'path' => __DIR__ . '/src/Item_Ticket.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#3 \\$itemtype of static method Item_Ticket\\:\\:dropdownMyDevices\\(\\) expects string, null given\\.$#',
'identifier' => 'argument.type',
Expand Down
45 changes: 45 additions & 0 deletions ajax/getDropdownMyDevices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

/**
* List of user devices for dropdown with lazy loading.
*/

include('../inc/includes.php');

header("Content-Type: application/json; charset=UTF-8");
Html::header_nocache();

Session::checkLoginUser();
echo Dropdown::getDropdownMyDevices($_POST);
123 changes: 123 additions & 0 deletions phpunit/functional/DropdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2085,4 +2085,127 @@ public function testGetLanguages()

$this->assertCount(count($CFG_GLPI['languages']), \Dropdown::getLanguages());
}

public function testGetDropdownMyDevices()
{
$this->login();

// Use existing admin user (ID 2) from test DB
$userID = 2;
$entity_id = $this->getTestRootEntity(true); // true to get ID instead of object

// Create test equipment owned by user using GLPI's createItem helper
$computer = $this->createItem('Computer', [
'name' => 'Test Laptop',
'users_id' => $userID,
'entities_id' => $entity_id,
'serial' => 'LAP123',
'otherserial' => 'INV456',
]);

$monitor = $this->createItem('Monitor', [
'name' => 'Test Monitor 24"',
'users_id' => $userID,
'entities_id' => $entity_id,
'serial' => 'MON789',
]);

$printer = $this->createItem('Printer', [
'name' => 'Test Printer HP',
'users_id' => $userID,
'entities_id' => $entity_id,
'serial' => 'PRT321',
]);

// Connect monitor to computer to test connected devices
$this->createItem('Computer_Item', [
'computers_id' => $computer->getID(),
'itemtype' => 'Monitor',
'items_id' => $monitor->getID(),
]);

// Ensure proper permissions and helpdesk types
$_SESSION["glpiactiveprofile"]["helpdesk_hardware"] = pow(2, \Ticket::HELPDESK_MY_HARDWARE);
$_SESSION["glpiactiveprofile"]["helpdesk_item_type"] = ['Computer', 'Monitor', 'Printer'];

$post = [
'userID' => $userID,
'entity_restrict' => $entity_id,
'page' => 1,
'page_limit' => 20, // Increase limit to see all items
];

$result = \Dropdown::getDropdownMyDevices($post, false);

// Basic structure validation
$this->assertIsArray($result);
$this->assertArrayHasKey('count', $result);
$this->assertArrayHasKey('results', $result);
$this->assertIsArray($result['results']);

// Should have empty value plus our devices
$this->assertGreaterThan(1, count($result['results']));
$this->assertEquals('', $result['results'][0]['id']);
$this->assertEquals('-----', $result['results'][0]['text']);

// Verify hierarchical structure and find our devices
$found_devices = [
'computer' => false,
'monitor' => false,
'printer' => false,
'connected_monitor' => false,
];

foreach ($result['results'] as $group) {
if (isset($group['text']) && isset($group['children'])) {
// This is a hierarchical group
$this->assertIsString($group['text']);
$this->assertIsArray($group['children']);

foreach ($group['children'] as $item) {
// Each item should have required structure
$this->assertArrayHasKey('id', $item);
$this->assertArrayHasKey('text', $item);
$this->assertArrayHasKey('itemtype', $item);
$this->assertArrayHasKey('items_id', $item);

// Check for our specific devices
if ($item['itemtype'] === 'Computer' && $item['items_id'] == $computer->getID()) {
$found_devices['computer'] = true;
$this->assertStringContainsString('Test Laptop', $item['text']);
$this->assertStringContainsString('LAP123', $item['text']);
}

if ($item['itemtype'] === 'Monitor' && $item['items_id'] == $monitor->getID()) {
if (strpos($group['text'], 'Connected') !== false) {
$found_devices['connected_monitor'] = true;
} else {
$found_devices['monitor'] = true;
}
$this->assertStringContainsString('Test Monitor', $item['text']);
$this->assertStringContainsString('MON789', $item['text']);
}

if ($item['itemtype'] === 'Printer' && $item['items_id'] == $printer->getID()) {
$found_devices['printer'] = true;
$this->assertStringContainsString('Test Printer', $item['text']);
$this->assertStringContainsString('PRT321', $item['text']);
}
}
}
}

// Verify all devices were found
$this->assertTrue($found_devices['computer'], 'Computer should be found in "My devices" section');
$this->assertTrue($found_devices['printer'], 'Printer should be found in "My devices" section');

// Monitor can be in either "My devices" or "Connected devices" section
$this->assertTrue(
$found_devices['monitor'] || $found_devices['connected_monitor'],
'Monitor should be found in either "My devices" or "Connected devices" section'
);

// Test that count is accurate
$this->assertGreaterThan(0, $result['count']);
}
}
Loading