|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * ------------------------------------------------------------------------- |
| 5 | + * Carbon plugin for GLPI |
| 6 | + * |
| 7 | + * @copyright Copyright (C) 2024-2025 Teclib' and contributors. |
| 8 | + * @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+ |
| 9 | + * @link https://github.com/pluginsGLPI/carbon |
| 10 | + * |
| 11 | + * ------------------------------------------------------------------------- |
| 12 | + * |
| 13 | + * LICENSE |
| 14 | + * |
| 15 | + * This file is part of Carbon plugin for GLPI. |
| 16 | + * |
| 17 | + * This program is free software: you can redistribute it and/or modify |
| 18 | + * it under the terms of the GNU General Public License as published by |
| 19 | + * the Free Software Foundation, either version 3 of the License, or |
| 20 | + * (at your option) any later version. |
| 21 | + * |
| 22 | + * This program is distributed in the hope that it will be useful, |
| 23 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | + * GNU General Public License for more details. |
| 26 | + * |
| 27 | + * You should have received a copy of the GNU General Public License |
| 28 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 29 | + * |
| 30 | + * ------------------------------------------------------------------------- |
| 31 | + */ |
| 32 | + |
| 33 | +namespace GlpiPlugin\Carbon\Tests\Impact\Usage\Boavizta; |
| 34 | + |
| 35 | +use CommonDBTM; |
| 36 | +use DBmysql; |
| 37 | +use GlpiPlugin\Carbon\ComputerType; |
| 38 | +use GlpiPlugin\Carbon\ComputerUsageProfile; |
| 39 | +use GlpiPlugin\Carbon\Impact\Usage\AbstractUsageImpact; |
| 40 | +use GlpiPlugin\Carbon\Location; |
| 41 | +use GlpiPlugin\Carbon\Tests\DbTestCase; |
| 42 | +use GlpiPlugin\Carbon\UsageImpact; |
| 43 | +use GlpiPlugin\Carbon\UsageInfo; |
| 44 | +use Infocom; |
| 45 | +use Location as GlpiLocation; |
| 46 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 47 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 48 | + |
| 49 | +#[CoversClass(AbstractUsageImpact::class)] |
| 50 | +abstract class AbstractAsset extends DbTestCase |
| 51 | +{ |
| 52 | + protected static string $instance_type = ''; |
| 53 | + |
| 54 | + /** @var class-string<CommonDBTM> itemtype of the an asset (i.e. Computer, Monotir, ...) */ |
| 55 | + protected static string $itemtype = ''; |
| 56 | + |
| 57 | + /** @var class-string<CommonDBTM> itemtype of the type of an asset (i.e. ComputerType, MonitorType) */ |
| 58 | + protected static string $itemtype_type = ''; |
| 59 | + |
| 60 | + /** @var class-string<CommonDBTM> itemtype of the model of an asset (i.e. ComputerModel, MonitorModel) */ |
| 61 | + protected static string $itemtype_model = ''; |
| 62 | + |
| 63 | + /** |
| 64 | + * Create an asset with all required data to make it evaluable |
| 65 | + * |
| 66 | + * @return array<CommonDBTM> An asset and related objects |
| 67 | + */ |
| 68 | + protected function getEvaluableAsset(): array |
| 69 | + { |
| 70 | + $glpi_location = $this->createItem(GlpiLocation::class); |
| 71 | + $location = $this->createItem(Location::class, [ |
| 72 | + $glpi_location->getForeignKeyField() => $glpi_location->getID(), |
| 73 | + 'boavizta_zone' => 'FRA' |
| 74 | + ]); |
| 75 | + $glpi_asset_type = $this->createItem(static::$itemtype_type); |
| 76 | + $asset_type = $this->createItem('GlpiPlugin\\Carbon\\' . static::$itemtype_type, [ |
| 77 | + $glpi_asset_type->getForeignKeyField() => $glpi_asset_type->getID(), |
| 78 | + 'power_consumption' => 42, |
| 79 | + 'category' => ComputerType::CATEGORY_DESKTOP |
| 80 | + ]); |
| 81 | + $asset = $this->createItem(static::$itemtype, [ |
| 82 | + $glpi_asset_type->getForeignKeyField() => $glpi_asset_type->getID(), |
| 83 | + $glpi_location->getForeignKeyField() => $glpi_location->getID() |
| 84 | + ]); |
| 85 | + $infocom = $this->createItem(Infocom::class, [ |
| 86 | + 'itemtype' => get_class($asset), |
| 87 | + 'items_id' => $asset->getID(), |
| 88 | + 'use_date' => '2024-01-01', |
| 89 | + ]); |
| 90 | + $usage_info = $this->createItem(UsageInfo::class, [ |
| 91 | + 'itemtype' => get_class($asset), |
| 92 | + 'items_id' => $asset->getID(), |
| 93 | + ComputerUsageProfile::getForeignKeyField() => 2 // Office hours ID |
| 94 | + ]); |
| 95 | + |
| 96 | + return [ |
| 97 | + $asset, |
| 98 | + $glpi_location, |
| 99 | + $location, |
| 100 | + $glpi_asset_type, |
| 101 | + $asset_type, |
| 102 | + $infocom, |
| 103 | + $usage_info, |
| 104 | + ]; |
| 105 | + } |
| 106 | + |
| 107 | + public function testGetItemsToEvaluate() |
| 108 | + { |
| 109 | + if (static::$itemtype === '' || static::$itemtype_type === '' || static::$itemtype_model === '') { |
| 110 | + // Ensure that the inherited test class is properly implemented for this test |
| 111 | + $this->fail('Itemtype propertiy not set in ' . static::class); |
| 112 | + } |
| 113 | + |
| 114 | + // Test the asset is evaluable when no impact is in the DB |
| 115 | + [$asset] = $this->getEvaluableAsset(); |
| 116 | + $instance = new static::$instance_type($asset); |
| 117 | + $iterator = $instance->getItemsToEvaluate(static::$itemtype, [ |
| 118 | + $asset->getTableField('id') => $asset->getID(), |
| 119 | + ]); |
| 120 | + $this->assertEquals(1, $iterator->count()); |
| 121 | + |
| 122 | + // Test the asset is no longer evaluable when there is impact in the DB |
| 123 | + [$asset] = $this->getEvaluableAsset(); |
| 124 | + $usage_impact = $this->createItem(UsageImpact::class, [ |
| 125 | + 'itemtype' => $asset->getType(), |
| 126 | + 'items_id' => $asset->getID(), |
| 127 | + 'recalculate' => 0, |
| 128 | + ]); |
| 129 | + $iterator = $instance->getItemsToEvaluate(static::$itemtype, [ |
| 130 | + $asset::getTableField('id') => $asset->getID(), |
| 131 | + ]); |
| 132 | + $this->assertEquals(0, $iterator->count()); |
| 133 | + |
| 134 | + // Test the asset is evaluable when there is impact in the DB but recamculate is set |
| 135 | + [$asset] = $this->getEvaluableAsset(); |
| 136 | + $usage_impact = $this->createItem(UsageImpact::class, [ |
| 137 | + 'itemtype' => $asset->getType(), |
| 138 | + 'items_id' => $asset->getID(), |
| 139 | + 'recalculate' => 1, |
| 140 | + ]); |
| 141 | + $iterator = $instance->getItemsToEvaluate(static::$itemtype, [ |
| 142 | + $asset::getTableField('id') => $asset->getID(), |
| 143 | + ]); |
| 144 | + $this->assertEquals(1, $iterator->count()); |
| 145 | + } |
| 146 | + |
| 147 | + public function testGetEvaluableQuery() |
| 148 | + { |
| 149 | + /** @var DBmysql $DB */ |
| 150 | + global $DB; |
| 151 | + |
| 152 | + // Test an asset with all requirements |
| 153 | + [$asset] = $this->getEvaluableAsset(); |
| 154 | + $instance = new static::$instance_type($asset); |
| 155 | + $request = $instance->getEvaluableQuery( |
| 156 | + get_class($asset), |
| 157 | + [ |
| 158 | + $asset::getTableField('id') => $asset->getID(), |
| 159 | + ] |
| 160 | + ); |
| 161 | + $iterator = $DB->request($request); |
| 162 | + $this->assertEquals(1, $iterator->count()); |
| 163 | + |
| 164 | + // Test an asset without a location |
| 165 | + [$asset, $glpi_location, $location, $glpi_asset_type, $asset_type, $infocom, $usage_info] = $this->getEvaluableAsset(); |
| 166 | + $asset->update(['locations_id' => 0] + $asset->fields); |
| 167 | + $instance = new static::$instance_type($asset); |
| 168 | + $request = $instance->getEvaluableQuery( |
| 169 | + get_class($asset), |
| 170 | + [ |
| 171 | + $asset::getTableField('id') => $asset->getID(), |
| 172 | + ] |
| 173 | + ); |
| 174 | + $iterator = $DB->request($request); |
| 175 | + $this->assertEquals(0, $iterator->count()); |
| 176 | + |
| 177 | + // Test an asset without a boavizta_zone |
| 178 | + [$asset, $glpi_location, $location, $glpi_asset_type, $asset_type, $infocom, $usage_info] = $this->getEvaluableAsset(); |
| 179 | + $location->update(['boavizta_zone' => ''] + $location->fields); |
| 180 | + $instance = new static::$instance_type($asset); |
| 181 | + $request = $instance->getEvaluableQuery( |
| 182 | + get_class($asset), |
| 183 | + [ |
| 184 | + $asset::getTableField('id') => $asset->getID(), |
| 185 | + ] |
| 186 | + ); |
| 187 | + $iterator = $DB->request($request); |
| 188 | + $this->assertEquals(0, $iterator->count()); |
| 189 | + |
| 190 | + |
| 191 | + // Test an asset without usage info |
| 192 | + [$asset, $glpi_location, $location, $glpi_asset_type, $asset_type, $infocom, $usage_info] = $this->getEvaluableAsset(); |
| 193 | + $usage_info->delete($usage_info->fields, true); |
| 194 | + $instance = new static::$instance_type($asset); |
| 195 | + $request = $instance->getEvaluableQuery( |
| 196 | + get_class($asset), |
| 197 | + [ |
| 198 | + $asset::getTableField('id') => $asset->getID(), |
| 199 | + ] |
| 200 | + ); |
| 201 | + $iterator = $DB->request($request); |
| 202 | + $this->assertEquals(0, $iterator->count()); |
| 203 | + |
| 204 | + // Test an asset without usage profile |
| 205 | + [$asset, $glpi_location, $location, $glpi_asset_type, $asset_type, $infocom, $usage_info] = $this->getEvaluableAsset(); |
| 206 | + $usage_info->update(['plugin_carbon_computerusageprofiles_id' => 0] + $usage_info->fields); |
| 207 | + $instance = new static::$instance_type($asset); |
| 208 | + $request = $instance->getEvaluableQuery( |
| 209 | + get_class($asset), |
| 210 | + [ |
| 211 | + $asset::getTableField('id') => $asset->getID(), |
| 212 | + ] |
| 213 | + ); |
| 214 | + $iterator = $DB->request($request); |
| 215 | + $this->assertEquals(0, $iterator->count()); |
| 216 | + |
| 217 | + // Test an asset in the bin |
| 218 | + [$asset, $glpi_location, $location, $glpi_asset_type, $asset_type, $infocom, $usage_info] = $this->getEvaluableAsset(); |
| 219 | + $asset->delete($asset->fields, false); |
| 220 | + $instance = new static::$instance_type($asset); |
| 221 | + $request = $instance->getEvaluableQuery( |
| 222 | + get_class($asset), |
| 223 | + [ |
| 224 | + $asset::getTableField('id') => $asset->getID(), |
| 225 | + ] |
| 226 | + ); |
| 227 | + $iterator = $DB->request($request); |
| 228 | + $this->assertEquals(0, $iterator->count()); |
| 229 | + |
| 230 | + // Test an asset set as a template |
| 231 | + [$asset, $glpi_location, $location, $glpi_asset_type, $asset_type, $infocom, $usage_info] = $this->getEvaluableAsset(); |
| 232 | + $asset->update(['is_template' => 1] + $asset->fields); |
| 233 | + $instance = new static::$instance_type($asset); |
| 234 | + $request = $instance->getEvaluableQuery( |
| 235 | + get_class($asset), |
| 236 | + [ |
| 237 | + $asset::getTableField('id') => $asset->getID(), |
| 238 | + ] |
| 239 | + ); |
| 240 | + $iterator = $DB->request($request); |
| 241 | + $this->assertEquals(0, $iterator->count()); |
| 242 | + |
| 243 | + // Test an asset without a power consumption |
| 244 | + [$asset, $glpi_location, $location, $glpi_asset_type, $asset_type, $infocom, $usage_info] = $this->getEvaluableAsset(); |
| 245 | + $asset_type->update(['power_consumption' => 0] + $asset_type->fields); |
| 246 | + $instance = new static::$instance_type($asset); |
| 247 | + $request = $instance->getEvaluableQuery( |
| 248 | + get_class($asset), |
| 249 | + [ |
| 250 | + $asset::getTableField('id') => $asset->getID(), |
| 251 | + ] |
| 252 | + ); |
| 253 | + $iterator = $DB->request($request); |
| 254 | + $this->assertEquals(0, $iterator->count()); |
| 255 | + |
| 256 | + // Test an asset without a infocom date |
| 257 | + [$asset, $glpi_location, $location, $glpi_asset_type, $asset_type, $infocom, $usage_info] = $this->getEvaluableAsset(); |
| 258 | + $infocom->update(['use_date' => null] + $infocom->fields); |
| 259 | + $instance = new static::$instance_type($asset); |
| 260 | + $request = $instance->getEvaluableQuery( |
| 261 | + get_class($asset), |
| 262 | + [ |
| 263 | + $asset::getTableField('id') => $asset->getID(), |
| 264 | + ] |
| 265 | + ); |
| 266 | + $iterator = $DB->request($request); |
| 267 | + $this->assertEquals(0, $iterator->count()); |
| 268 | + } |
| 269 | + |
| 270 | + // public function testResetForItem() |
| 271 | + // { |
| 272 | + // $asset = $this->createItem(static::$itemtype); |
| 273 | + // $instance = $this->createItem(UsageImpact::class, [ |
| 274 | + // 'itemtype' => get_class($asset), |
| 275 | + // 'items_id' => $asset->getID(), |
| 276 | + // ]); |
| 277 | + |
| 278 | + // $result = AbstractUsageImpact::resetForItem($asset); |
| 279 | + // $this->assertTrue($result); |
| 280 | + // $result = UsageImpact::getById($instance->getID()); |
| 281 | + // $this->assertFalse($result); |
| 282 | + // } |
| 283 | +} |
0 commit comments