Skip to content

Commit

Permalink
Add process and environment capacities
Browse files Browse the repository at this point in the history
Add test on inventory
  • Loading branch information
trasher committed Sep 17, 2024
1 parent b952a20 commit 61dd02b
Show file tree
Hide file tree
Showing 6 changed files with 682 additions and 0 deletions.
54 changes: 54 additions & 0 deletions phpunit/functional/Glpi/Inventory/Assets/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public static function assetProvider(): array
'xml' => "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<REQUEST>
<CONTENT>
<HARDWARE>
<NAME>pc002</NAME>
</HARDWARE>
<BIOS>
<SSN>ggheb7ne7</SSN>
</BIOS>
<ENVS>
<KEY>LC_ALL</KEY>
<VAL>C</VAL>
Expand Down Expand Up @@ -144,4 +150,52 @@ public function testHandle()
'Environments has not been linked to computer :('
);
}

public function testGenericAssetEnvironment(): void
{
/** @var \DBmysql $DB */
global $DB;

//create generic asset
$definition = $this->initAssetDefinition(
system_name: 'MyAsset' . $this->getUniqueString(),
capacities: array_merge(
[
\Glpi\Asset\Capacity\IsInventoriableCapacity::class,
\Glpi\Asset\Capacity\HasEnvironmentsCapacity::class
]
)
);
$classname = $definition->getAssetClassName();

$this->login();
$conf = new \Glpi\Inventory\Conf();
$this->assertTrue($conf->saveConf([
'import_env' => 1,
]));
$this->logout();

$converter = new \Glpi\Inventory\Converter();
$data = $converter->convert(self::assetProvider()[0]['xml']);
$json = json_decode($data);
//we change itemtype to our asset
$json->itemtype = $classname;
$inventory = $this->doInventory($json);

//check created asset
$assets_id = $inventory->getAgent()->fields['items_id'];
$this->assertGreaterThan(0, $assets_id);
$asset = new $classname();
$this->assertTrue($asset->getFromDB($assets_id));

$this->assertSame(
4,
countElementsInTable(\Item_Environment::getTable(), ['itemtype' => $classname, 'items_id' => $assets_id]),
'Environments has not been linked to asset :('
);

//check for tab presence
$this->login();
$this->assertArrayHasKey('Item_Environment$1', $asset->defineAllTabs());
}
}
54 changes: 54 additions & 0 deletions phpunit/functional/Glpi/Inventory/Assets/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public static function assetProvider(): array
'xml' => "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<REQUEST>
<CONTENT>
<HARDWARE>
<NAME>pc002</NAME>
</HARDWARE>
<BIOS>
<SSN>ggheb7ne7</SSN>
</BIOS>
<PROCESSES>
<CMD>php-fpm: pool www</CMD>
<CPUUSAGE>0.0</CPUUSAGE>
Expand Down Expand Up @@ -157,4 +163,52 @@ public function testHandle()
'Process has not been linked to computer :('
);
}

public function testGenericAssetProcess(): void
{
/** @var \DBmysql $DB */
global $DB;

//create generic asset
$definition = $this->initAssetDefinition(
system_name: 'MyAsset' . $this->getUniqueString(),
capacities: array_merge(
[
\Glpi\Asset\Capacity\IsInventoriableCapacity::class,
\Glpi\Asset\Capacity\HasProcessCapacity::class
]
)
);
$classname = $definition->getAssetClassName();

$this->login();
$conf = new \Glpi\Inventory\Conf();
$this->assertTrue($conf->saveConf([
'import_process' => 1,
]));
$this->logout();

$converter = new \Glpi\Inventory\Converter();
$data = $converter->convert(self::assetProvider()[0]['xml']);
$json = json_decode($data);
//we change itemtype to our asset
$json->itemtype = $classname;
$inventory = $this->doInventory($json);

//check created asset
$assets_id = $inventory->getAgent()->fields['items_id'];
$this->assertGreaterThan(0, $assets_id);
$asset = new $classname();
$this->assertTrue($asset->getFromDB($assets_id));

$this->assertSame(
1,
countElementsInTable(\Item_Process::getTable(), ['itemtype' => $classname, 'items_id' => $assets_id]),
'Process has not been linked to asset :('
);

//check for tab presence
$this->login();
$this->assertArrayHasKey('Item_Process$1', $asset->defineAllTabs());
}
}
86 changes: 86 additions & 0 deletions src/Glpi/Asset/Capacity/HasEnvironmentsCapacity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @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/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Asset\Capacity;

use CommonGLPI;
use Item_Environment;
use Session;

class HasEnvironmentsCapacity extends AbstractCapacity
{
public function getLabel(): string
{
return Item_Environment::getTypeName(Session::getPluralNumber());
}

public function getIcon(): string
{
return Item_Environment::getIcon();
}

public function isUsed(string $classname): bool
{
return parent::isUsed($classname)
&& $this->countAssetsLinkedToPeerItem($classname, Item_Environment::class) > 0;
}

public function getCapacityUsageDescription(string $classname): string
{
return sprintf(
__('%1$s environments attached to %2$s assets'),
$this->countPeerItemsUsage($classname, Item_Environment::class),
$this->countAssetsLinkedToPeerItem($classname, Item_Environment::class)
);
}

public function onClassBootstrap(string $classname): void
{
$this->registerToTypeConfig('environment_types', $classname);
CommonGLPI::registerStandardTab($classname, Item_Environment::class, 85);
}

public function onCapacityDisabled(string $classname): void
{
$this->unregisterFromTypeConfig('environment_types', $classname);

$appliance_item = new Item_Environment();
$appliance_item->deleteByCriteria([
'itemtype' => $classname,
], true, false);

$this->deleteRelationLogs($classname, Item_Environment::class);
}
}
88 changes: 88 additions & 0 deletions src/Glpi/Asset/Capacity/HasProcessCapacity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @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/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Asset\Capacity;

use Appliance;
use Appliance_Item;
use CommonGLPI;
use Item_Process;
use Session;

class HasProcessCapacity extends AbstractCapacity
{
public function getLabel(): string
{
return Item_Process::getTypeName(Session::getPluralNumber());
}

public function getIcon(): string
{
return Item_Process::getIcon();
}

public function isUsed(string $classname): bool
{
return parent::isUsed($classname)
&& $this->countAssetsLinkedToPeerItem($classname, Item_Process::class) > 0;
}

public function getCapacityUsageDescription(string $classname): string
{
return sprintf(
__('%1$s process attached to %2$s assets'),
$this->countPeerItemsUsage($classname, Item_Process::class),
$this->countAssetsLinkedToPeerItem($classname, Item_Process::class)
);
}

public function onClassBootstrap(string $classname): void
{
$this->registerToTypeConfig('process_types', $classname);
CommonGLPI::registerStandardTab($classname, Item_Process::class, 85);
}

public function onCapacityDisabled(string $classname): void
{
$this->unregisterFromTypeConfig('process_types', $classname);

$appliance_item = new Item_Process();
$appliance_item->deleteByCriteria([
'itemtype' => $classname,
], true, false);

$this->deleteRelationLogs($classname, Item_Process::class);
}
}
Loading

0 comments on commit 61dd02b

Please sign in to comment.