Skip to content

Commit

Permalink
even more test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Mar 21, 2024
1 parent 2984938 commit f3f4538
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 61 deletions.
67 changes: 62 additions & 5 deletions src/Api/HL/Controller/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use Location;
use Manufacturer;
use Network;
use Software;
use State;
use User;

Expand Down Expand Up @@ -405,7 +406,7 @@ class: User::class,
if (\Toolbox::hasTrait($asset_type, AssignableAsset::class)) {
// Assignable assets all support multiple groups and the group links are in a separate table
if (in_array($asset_type, $CFG_GLPI['linkgroup_types'], true)) {
$schemas[$schema_name]['properties']['groups'] = [
$schemas[$schema_name]['properties']['group'] = [
'type' => Doc\Schema::TYPE_ARRAY,
'items' => [
'type' => Doc\Schema::TYPE_OBJECT,
Expand Down Expand Up @@ -436,7 +437,7 @@ class: User::class,
];
}
if (in_array($asset_type, $CFG_GLPI['linkgroup_tech_types'], true)) {
$schemas[$schema_name]['properties']['groups_tech'] = [
$schemas[$schema_name]['properties']['group_tech'] = [
'type' => Doc\Schema::TYPE_ARRAY,
'items' => [
'type' => Doc\Schema::TYPE_OBJECT,
Expand Down Expand Up @@ -699,7 +700,7 @@ class: Group::class,
];

$schemas['Software'] = [
'x-itemtype' => \Software::class,
'x-itemtype' => Software::class,
'type' => Doc\Schema::TYPE_OBJECT,
'properties' => [
'id' => [
Expand All @@ -717,9 +718,65 @@ class: Group::class,
'parent' => self::getDropdownTypeSchema(class: \Software::class, full_schema: 'Software'),
'is_helpdesk_visible' => ['type' => Doc\Schema::TYPE_BOOLEAN],
'user' => self::getDropdownTypeSchema(class: User::class, full_schema: 'User'),
'group' => self::getDropdownTypeSchema(class: Group::class, full_schema: 'Group'),
'group' => [
'type' => Doc\Schema::TYPE_ARRAY,
'items' => [
'type' => Doc\Schema::TYPE_OBJECT,
'x-full-schema' => 'Group',
'x-join' => [
'table' => 'glpi_groups', // The table with the desired data
'fkey' => 'groups_id',
'field' => 'id',
'ref-join' => [
'table' => 'glpi_groups_assets',
'fkey' => 'id',
'field' => 'items_id',
'condition' => [
'itemtype' => Software::class,
'type' => 0 // Normal
]
]
],
'properties' => [
'id' => [
'type' => Doc\Schema::TYPE_INTEGER,
'format' => Doc\Schema::FORMAT_INTEGER_INT64,
'description' => 'ID',
],
'name' => ['type' => Doc\Schema::TYPE_STRING],
]
]
],
'user_tech' => self::getDropdownTypeSchema(class: User::class, field: 'users_id_tech', full_schema: 'User'),
'group_tech' => self::getDropdownTypeSchema(class: Group::class, field: 'groups_id_tech', full_schema: 'Group'),
'group_tech' => [
'type' => Doc\Schema::TYPE_ARRAY,
'items' => [
'type' => Doc\Schema::TYPE_OBJECT,
'x-full-schema' => 'Group',
'x-join' => [
'table' => 'glpi_groups', // The table with the desired data
'fkey' => 'groups_id',
'field' => 'id',
'ref-join' => [
'table' => 'glpi_groups_assets',
'fkey' => 'id',
'field' => 'items_id',
'condition' => [
'itemtype' => Software::class,
'type' => 1 // Tech
]
]
],
'properties' => [
'id' => [
'type' => Doc\Schema::TYPE_INTEGER,
'format' => Doc\Schema::FORMAT_INTEGER_INT64,
'description' => 'ID',
],
'name' => ['type' => Doc\Schema::TYPE_STRING],
]
]
],
'is_deleted' => ['type' => Doc\Schema::TYPE_BOOLEAN],
'is_update' => ['type' => Doc\Schema::TYPE_BOOLEAN],
'is_valid' => ['type' => Doc\Schema::TYPE_BOOLEAN],
Expand Down
14 changes: 13 additions & 1 deletion src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Glpi\Application\View\TemplateRenderer;
use Glpi\ContentTemplates\TemplateManager;
use Glpi\DBAL\QueryExpression;
use Glpi\Features\AssignableAsset;
use Glpi\Toolbox\URL;

/**
Expand Down Expand Up @@ -438,10 +439,21 @@ public static function generateLinkContents($link, CommonDBTM $item, bool $safe_
'DOMAIN' => '',
'NETWORK' => $item->isField('networks_id') ? Dropdown::getDropdownName('glpi_networks', $item->getField('networks_id')) : '',
'USER' => $item->isField('users_id') ? Dropdown::getDropdownName('glpi_users', $item->getField('users_id')) : '',
'GROUP' => $item->isField('groups_id') ? Dropdown::getDropdownName('glpi_groups', $item->getField('groups_id')) : '',
'REALNAME' => $item->isField('realname') ? $item->getField('realname') : '',
'FIRSTNAME' => $item->isField('firstname') ? $item->getField('firstname') : '',
];
if (Toolbox::hasTrait($item::class, AssignableAsset::class)) {
if (in_array(0, $item->getGroupTypes(), true)) {
$group_names = array_map(static fn ($group_id) => Dropdown::getDropdownName('glpi_groups', $group_id), $item->fields['groups_id']);
$vars['GROUPS'] = $group_names;
// GROUP - BC for < GLPI 11
$vars['GROUP'] = count($group_names) > 0 ? array_shift($group_names) : '';
}
} else {
$vars['GROUPS'] = [];
// GROUP - BC for < GLPI 11
$vars['GROUP'] = $item->isField('groups_id') ? Dropdown::getDropdownName('glpi_groups', $item->getField('groups_id')) : '';
}
$item_fields = $item->fields;
$item::unsetUndisclosedFields($item_fields);
if (count($item_fields)) {
Expand Down
5 changes: 4 additions & 1 deletion src/Search/Provider/SQLProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,10 @@ public static function getDefaultWhereCriteria(string $itemtype): array

if (Toolbox::hasTrait($itemtype, AssignableAsset::class)) {
/** @var AssignableAsset $itemtype */
$criteria[] = $itemtype::getAssignableVisiblityCriteria();
$visibility_criteria = $itemtype::getAssignableVisiblityCriteria();
if (count($visibility_criteria)) {
$criteria[] = $visibility_criteria;
}
}

/* Hook to restrict user right on current itemtype */
Expand Down
16 changes: 3 additions & 13 deletions tests/functional/CartridgeItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,49 +49,39 @@ public function testAddAndUpdateMultipleGroups()
$cartridgeitem = $this->createItem(\CartridgeItem::class, [
'name' => __FUNCTION__,
'entities_id' => $this->getTestRootEntity(true),
'groups_id' => [1, 2],
'groups_id_tech' => [3, 4],
], ['groups_id', 'groups_id_tech']);
], ['groups_id_tech']);
$cartridgeitems_id_1 = $cartridgeitem->fields['id'];
$this->array($cartridgeitem->fields['groups_id'])->containsValues([1, 2]);
$this->array($cartridgeitem->fields['groups_id_tech'])->containsValues([3, 4]);

$cartridgeitem = $this->createItem(\CartridgeItem::class, [
'name' => __FUNCTION__,
'entities_id' => $this->getTestRootEntity(true),
'groups_id' => null,
'groups_id_tech' => null,
], ['groups_id', 'groups_id_tech']);
], ['groups_id_tech']);
$cartridgeitems_id_2 = $cartridgeitem->fields['id'];
$this->array($cartridgeitem->fields['groups_id'])->isEmpty();
$this->array($cartridgeitem->fields['groups_id_tech'])->isEmpty();

// Update both assets. Asset 1 will have the groups set to null and asset 2 will have the groups set to an array.
$cartridgeitem->getFromDB($cartridgeitems_id_1);
$this->boolean($cartridgeitem->update([
'id' => $cartridgeitems_id_1,
'groups_id' => null,
'groups_id_tech' => null,
]))->isTrue();
$this->array($cartridgeitem->fields['groups_id'])->isEmpty();
$this->array($cartridgeitem->fields['groups_id_tech'])->isEmpty();

$cartridgeitem->getFromDB($cartridgeitems_id_2);
$this->boolean($cartridgeitem->update([
'id' => $cartridgeitems_id_2,
'groups_id' => [5, 6],
'groups_id_tech' => [7, 8],
]))->isTrue();
$this->array($cartridgeitem->fields['groups_id'])->containsValues([5, 6]);
$this->array($cartridgeitem->fields['groups_id_tech'])->containsValues([7, 8]);

// Test updating array to array
$this->boolean($cartridgeitem->update([
'id' => $cartridgeitems_id_2,
'groups_id' => [1, 2],
'groups_id_tech' => [3, 4],
]))->isTrue();
$this->array($cartridgeitem->fields['groups_id'])->containsValues([1, 2]);
$this->array($cartridgeitem->fields['groups_id_tech'])->containsValues([3, 4]);
}

Expand Down Expand Up @@ -125,6 +115,7 @@ public function testLoadOldItemsSingleGroup()
'type' => 1 // Tech
],
);
$cartridgeitem->getFromDB($cartridgeitems_id);
$this->array($cartridgeitem->fields['groups_id_tech'])
->hasSize(1)
->containsValues([2]);
Expand All @@ -138,7 +129,6 @@ public function testGetEmptyMultipleGroups()
{
$cartridgeitem = new \CartridgeItem();
$cartridgeitem->getEmpty();
$this->array($cartridgeitem->fields['groups_id'])->isEmpty();
$this->array($cartridgeitem->fields['groups_id_tech'])->isEmpty();
}
}
15 changes: 2 additions & 13 deletions tests/functional/ConsumableItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,49 +49,39 @@ public function testAddAndUpdateMultipleGroups()
$consumableitem = $this->createItem(\ConsumableItem::class, [
'name' => __FUNCTION__,
'entities_id' => $this->getTestRootEntity(true),
'groups_id' => [1, 2],
'groups_id_tech' => [3, 4],
], ['groups_id', 'groups_id_tech']);
], ['groups_id_tech']);
$consumableitems_id_1 = $consumableitem->fields['id'];
$this->array($consumableitem->fields['groups_id'])->containsValues([1, 2]);
$this->array($consumableitem->fields['groups_id_tech'])->containsValues([3, 4]);

$consumableitem = $this->createItem(\ConsumableItem::class, [
'name' => __FUNCTION__,
'entities_id' => $this->getTestRootEntity(true),
'groups_id' => null,
'groups_id_tech' => null,
], ['groups_id', 'groups_id_tech']);
], ['groups_id_tech']);
$consumableitems_id_2 = $consumableitem->fields['id'];
$this->array($consumableitem->fields['groups_id'])->isEmpty();
$this->array($consumableitem->fields['groups_id_tech'])->isEmpty();

// Update both assets. Asset 1 will have the groups set to null and asset 2 will have the groups set to an array.
$consumableitem->getFromDB($consumableitems_id_1);
$this->boolean($consumableitem->update([
'id' => $consumableitems_id_1,
'groups_id' => null,
'groups_id_tech' => null,
]))->isTrue();
$this->array($consumableitem->fields['groups_id'])->isEmpty();
$this->array($consumableitem->fields['groups_id_tech'])->isEmpty();

$consumableitem->getFromDB($consumableitems_id_2);
$this->boolean($consumableitem->update([
'id' => $consumableitems_id_2,
'groups_id' => [5, 6],
'groups_id_tech' => [7, 8],
]))->isTrue();
$this->array($consumableitem->fields['groups_id'])->containsValues([5, 6]);
$this->array($consumableitem->fields['groups_id_tech'])->containsValues([7, 8]);

// Test updating array to array
$this->boolean($consumableitem->update([
'id' => $consumableitems_id_2,
'groups_id' => [1, 2],
'groups_id_tech' => [3, 4],
]))->isTrue();
$this->array($consumableitem->fields['groups_id'])->containsValues([1, 2]);
$this->array($consumableitem->fields['groups_id_tech'])->containsValues([3, 4]);
}

Expand Down Expand Up @@ -140,7 +130,6 @@ public function testGetEmptyMultipleGroups()
{
$consumableitem = new \ConsumableItem();
$consumableitem->getEmpty();
$this->array($consumableitem->fields['groups_id'])->isEmpty();
$this->array($consumableitem->fields['groups_id_tech'])->isEmpty();
}
}

0 comments on commit f3f4538

Please sign in to comment.