Skip to content

Commit

Permalink
Handle software category rules from inventory
Browse files Browse the repository at this point in the history
closes #16036
  • Loading branch information
trasher committed Apr 11, 2024
1 parent 188dc5c commit c6ee99a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Inventory/Asset/Software.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ private function storeSoftware()
if (!isset($this->softwares[$skey])) {
$stmt_columns = $this->cleanInputToPrepare((array)$val, $soft_fields);

$software->handleCategoryRules($stmt_columns);
$software->handleCategoryRules($stmt_columns, true);
//set create date
$stmt_columns['date_creation'] = $_SESSION["glpi_currenttime"];

Expand Down
6 changes: 3 additions & 3 deletions src/Software.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,10 +1159,10 @@ public static function getIcon()
return "ti ti-apps";
}

public function handleCategoryRules(array &$input)
public function handleCategoryRules(array &$input, bool $is_dynamic = false)
{
//If category was not set by user (when manually adding a user)
if (!isset($input["softwarecategories_id"]) || !$input["softwarecategories_id"]) {
//If category was not set by user (when manually adding a user)
if ($is_dynamic || !isset($input["softwarecategories_id"]) || !$input["softwarecategories_id"]) {
$softcatrule = new RuleSoftwareCategoryCollection();
$result = $softcatrule->processAllRules(null, null, Toolbox::stripslashes_deep($input));

Expand Down
81 changes: 81 additions & 0 deletions tests/functional/Glpi/Inventory/Assets/Software.php
Original file line number Diff line number Diff line change
Expand Up @@ -1519,4 +1519,85 @@ public function testManufacturerSpecialCharacters()
]))->isTrue();
$this->integer($item_versions_id)->isIdenticalTo($item_version->fields['id']);
}

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

$rule = new \RuleSoftwareCategory();
$rulecriteria = new \RuleCriteria();
$ruleaction = new \RuleAction();

$category = new \SoftwareCategory();
$parent_categories_id = $category->add(['name' => 'Parent']);
$categories_id = $category->add(['name' => 'Child', 'categories_id' => $parent_categories_id]);

$rules_id = $rule->add([
'is_active' => 1,
'name' => 'Sub category',
'match' => 'AND',
'sub_type' => \RuleSoftwareCategory::class,
'is_recursive' => 0,
'ranking' => 1,
]);
$this->integer((int) $rules_id)->isGreaterThan(0);

$this->integer((int) $rulecriteria->add([
'rules_id' => $rules_id,
'criteria' => 'name',
'condition' => \Rule::PATTERN_IS,
'pattern' => 'firefox'
]))->isGreaterThan(0);

$this->integer((int) $ruleaction->add([
'rules_id' => $rules_id,
'action_type' => 'assign',
'field' => 'softwarecategories_id',
'value' => $categories_id,
]))->isGreaterThan(0);


$xml_source = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<REQUEST>
<CONTENT>
<SOFTWARES>
<ARCH>x86_64</ARCH>
<COMMENTS>Mozilla Firefox Web browser</COMMENTS>
<FILESIZE>258573684</FILESIZE>
<FROM>rpm</FROM>
<INSTALLDATE>23/12/2020</INSTALLDATE>
<NAME>firefox</NAME>
<PUBLISHER>Fedora Project</PUBLISHER>
<SYSTEM_CATEGORY>Unspecified</SYSTEM_CATEGORY>
<VERSION>84.0-6.fc32</VERSION>
</SOFTWARES>
<HARDWARE>
<NAME>pc_test_entity</NAME>
</HARDWARE>
<BIOS>
<SSN>ssnexample</SSN>
</BIOS>
<VERSIONCLIENT>test-agent</VERSIONCLIENT>
<ACCOUNTINFO>
<KEYNAME>TAG</KEYNAME>
<KEYVALUE>testtag_2</KEYVALUE>
</ACCOUNTINFO>
</CONTENT>
<DEVICEID>pc_test_entity</DEVICEID>
<QUERY>INVENTORY</QUERY>
</REQUEST>";

$this->doInventory($xml_source, true);

$computer = new \Computer();
$found_computers = $computer->find(['name' => "pc_test_entity"]);
$this->integer(count($found_computers))->isIdenticalTo(1);

$soft = new \Software();
$softs = $soft->find(['name' => "firefox"]);
$this->integer(count($softs))->isIdenticalTo(1);
$first_soft = array_pop($softs);

$this->integer($first_soft['softwarecategories_id'])->isIdenticalTo($categories_id);
}
}

0 comments on commit c6ee99a

Please sign in to comment.