Skip to content

Commit

Permalink
Custom Asset Field Unicity
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 authored Aug 20, 2024
1 parent aa7d415 commit ba5d899
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Glpi/Asset/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ public function rawSearchOptions()
return $search_options;
}

public function getUnallowedFieldsForUnicity()
{
$not_allowed = parent::getUnallowedFieldsForUnicity();
$not_allowed[] = AssetDefinition::getForeignKeyField();
return $not_allowed;
}

public static function getSystemSQLCriteria(?string $tablename = null): array
{
$table_prefix = $tablename !== null
Expand Down
1 change: 1 addition & 0 deletions src/Glpi/Asset/AssetDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ private function boostrapConcreteClass(AssetDefinition $definition): void
'location_types',
'state_types',
'ticket_types',
'unicity_types'
];
foreach ($config_keys as $config_key) {
$CFG_GLPI[$config_key][] = $asset_class_name;
Expand Down
12 changes: 10 additions & 2 deletions tests/functional/Glpi/Asset/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected function getByIdProvider(): iterable
for ($i = 0; $i < 2; $i++) {
$fields = [
'name' => 'Foo asset ' . $i,
'entities_id' => $this->getTestRootEntity(true),
];
$asset = $this->createItem($foo_classname, $fields);
yield [
Expand All @@ -62,6 +63,7 @@ protected function getByIdProvider(): iterable

$fields = [
'name' => 'Bar asset ' . $i,
'entities_id' => $this->getTestRootEntity(true),
];
$asset = $this->createItem($bar_classname, $fields);
yield [
Expand Down Expand Up @@ -114,7 +116,10 @@ public function testUpdateWithWrongDefinition(): void
$definition_2 = $this->initAssetDefinition();
$classname_2 = $definition_2->getAssetClassName();

$asset = $this->createItem($classname_1, ['name' => 'new asset']);
$asset = $this->createItem($classname_1, [
'name' => 'new asset',
'entities_id' => $this->getTestRootEntity(true),
]);

$this->exception(
function () use ($asset, $classname_2) {
Expand All @@ -131,7 +136,10 @@ public function testSearchOptionsUnicity(): void
capacities: array_keys($capacities)
);

$asset = $this->createItem($definition->getAssetClassName(), ['name' => 'test asset']);
$asset = $this->createItem($definition->getAssetClassName(), [
'name' => 'test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

$this->when(
function () use ($asset) {
Expand Down
1 change: 1 addition & 0 deletions tests/functional/Glpi/Asset/AssetDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ public function testDelete()
$definition->getAssetClassName(),
[
'name' => 'test',
'entities_id' => $this->getTestRootEntity(true),
]
);

Expand Down
2 changes: 2 additions & 0 deletions tests/functional/Glpi/Asset/AssetDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected function testCommonITILTabRegistrationProvider(): iterable
// Create a test subject without any linked ITIL items
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
yield [
$definition,
Expand Down Expand Up @@ -154,6 +155,7 @@ protected function testCommonITILTabRegistrationProvider(): iterable
// Create a test subject without any linked ITIL items
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
yield [
$definition,
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/Glpi/Asset/Capacity/HasContractsCapacity.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function testContractItemTabRegistration(): void
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Validate that the subject does not have the contract tab, as the
Expand Down Expand Up @@ -139,6 +140,7 @@ public function testContractItemDataDeletion(): void
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Create some contracts that are ready to be assigned to our item
Expand Down Expand Up @@ -204,6 +206,7 @@ public function testHistoryDataDeletion(): void
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Create a contract
Expand Down Expand Up @@ -281,6 +284,7 @@ public function testDisplayPreferencesDataDeletion(): void
// Create our test subject and enable the capacity
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Set display preferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public function testIsUsed(): void
// Create our test subject
$asset = $this->createItem($definition->getAssetClassName(), [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Check that the capacity can't be safely disabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function testOperatingSystemTabRegistration(): void
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Validate that the subject does not have the operating system tab,
Expand Down Expand Up @@ -143,6 +144,7 @@ public function testSearchOptionRegistration(): void
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
$base_search_options_count = count($subject->rawSearchOptions());

Expand Down Expand Up @@ -182,6 +184,7 @@ public function testItemOperatingSystemDataDeletion(): void
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Create an Item_OperatingSystem linked to our asset
Expand Down Expand Up @@ -230,6 +233,7 @@ public function testHistoryDataDeletion(): void
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Create and update the linked OS in order to generate history entries
Expand Down Expand Up @@ -291,6 +295,7 @@ public function testDisplayPreferencesDataDeletion(): void
// Create our test subject and enable the capacity
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);

// Set display preferences
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/Glpi/Asset/Capacity/IsRackableCapacity.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,11 @@ public function testGetCapacityUsageDescription(): void
// Create assets
$asset1 = $this->createItem($definition->getAssetClassName(), [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
$asset2 = $this->createItem($definition->getAssetClassName(), [
'name' => 'Test asset 2',
'entities_id' => $this->getTestRootEntity(true),
]);

// Check that the capacity usage description is correct
Expand Down

0 comments on commit ba5d899

Please sign in to comment.