Skip to content

Commit 4d64dbd

Browse files
committed
test(Dashboard\Provider): update tests
and ehnance tests maintaniability
1 parent d4c434c commit 4d64dbd

File tree

2 files changed

+248
-16
lines changed

2 files changed

+248
-16
lines changed

tests/src/CommonTestCase.php

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
use PHPUnit\Framework\TestCase;
3636
use Auth;
3737
use CommonDBTM;
38-
use Computer;
38+
use Computer as GlpiComputer;
3939
use ComputerType as GlpiComputerType;
4040
use DateTime;
4141
use DateInterval;
4242
use DateTimeInterface;
43+
use ComputerModel as GlpiComputerModel;
4344
use Glpi\Inventory\Conf;
4445
use GlpiPlugin\Carbon\UsageInfo;
4546
use GlpiPlugin\Carbon\ComputerUsageProfile;
@@ -242,20 +243,20 @@ public function updateItem(CommonDBTM $item, array $input): CommonDBTM
242243
return $item;
243244
}
244245

245-
protected function createComputerUsageProfile(array $usage_profile_params): Computer
246+
protected function createComputerUsageProfile(array $usage_profile_params): GlpiComputer
246247
{
247248
$usage_profile = $this->createItem(ComputerUsageProfile::class, $usage_profile_params);
248-
$glpi_computer = $this->createItem(Computer::class);
249+
$glpi_computer = $this->createItem(GlpiComputer::class);
249250
$impact = $this->createItem(UsageInfo::class, [
250-
'itemtype' => Computer::class,
251+
'itemtype' => GlpiComputer::class,
251252
'items_id' => $glpi_computer->getId(),
252253
ComputerUsageProfile::getForeignKeyField() => $usage_profile->getID(),
253254
]);
254255

255256
return $glpi_computer;
256257
}
257258

258-
protected function createComputerUsageProfilePower(array $usage_profile_params, int $type_power): Computer
259+
protected function createComputerUsageProfilePower(array $usage_profile_params, int $type_power): GlpiComputer
259260
{
260261
$glpi_computer = $this->createComputerUsageProfile($usage_profile_params);
261262
$glpiComputerType = $this->createItem(GlpiComputerType::class);
@@ -271,7 +272,7 @@ protected function createComputerUsageProfilePower(array $usage_profile_params,
271272
return $glpi_computer;
272273
}
273274

274-
protected function createComputerUsageProfilePowerLocation(array $usage_profile_params, int $type_power, Source_Zone $source_zone): Computer
275+
protected function createComputerUsageProfilePowerLocation(array $usage_profile_params, int $type_power, Source_Zone $source_zone): GlpiComputer
275276
{
276277
$glpi_computer = $this->createComputerUsageProfilePower($usage_profile_params, $type_power);
277278

@@ -345,6 +346,113 @@ protected function createCarbonEmissionData(CommonDBTM $item, DateTime $start, D
345346
}
346347
}
347348

349+
/**
350+
* Build a computer and all its data to make it historiable
351+
* except the data provided in argument
352+
*
353+
* @param array $skip
354+
* @return GlpiComputer
355+
*/
356+
protected function createHistorizableComputer(array $skip = []): GlpiComputer
357+
{
358+
if (!in_array(GlpiComputerType::class, $skip)) {
359+
$glpi_computer_type = $this->createItem(GlpiComputerType::class);
360+
}
361+
if (!in_array(ComputerType::class, $skip)) {
362+
$computer_type = $this->createItem(ComputerType::class, [
363+
'computertypes_id' => isset($glpi_computer_type) ? $glpi_computer_type->getID() : 0,
364+
'power_consumption' => !in_array(ComputerType::class . '_power', $skip) ? 90 : 0,
365+
]);
366+
}
367+
if (!in_array(GlpiComputerModel::class, $skip)) {
368+
$glpi_computer_model = $this->createItem(GlpiComputerModel::class, [
369+
'power_consumption' => !in_array(GlpiComputerModel::class . '_power', $skip) ? 150 : 0,
370+
]);
371+
}
372+
if (!in_array(Zone::class, $skip)) {
373+
$zone = $this->createItem(Zone::class);
374+
}
375+
if (!in_array(Source::class, $skip)) {
376+
$source = $this->createItem(Source::class, [
377+
'is_carbon_intensity_source' => 1,
378+
'fallback_level' => 0,
379+
]);
380+
}
381+
if (!in_array(Source_Zone::class, $skip)) {
382+
$source_zone = $this->createItem(Source_Zone::class, [
383+
'plugin_carbon_sources_id' => isset($source) ? $source->getID() : 0,
384+
'plugin_carbon_zones_id' => isset($zone) ? $zone->getID() : 0,
385+
]);
386+
}
387+
if (!in_array(CarbonIntensity::class, $skip)) {
388+
$carbon_intensity = $this->createItem(CarbonIntensity::class, [
389+
'plugin_carbon_sources_id' => isset($source) ? $source->getID() : 0,
390+
'plugin_carbon_zones_id' => isset($zone) ? $zone->getID() : 0,
391+
]);
392+
}
393+
if (!in_array('fallback_' . Source::class, $skip)) {
394+
$fallback_source = $this->createItem(Source::class, [
395+
'is_carbon_intensity_source' => 1,
396+
'fallback_level' => 1,
397+
]);
398+
}
399+
if (!in_array('fallback_' . Source_Zone::class, $skip)) {
400+
$fallback_source_zone = $this->createItem(Source_Zone::class, [
401+
'plugin_carbon_sources_id' => isset($fallback_source) ? $fallback_source->getID() : 0,
402+
'plugin_carbon_zones_id' => isset($zone) ? $zone->getID() : 0,
403+
]);
404+
}
405+
if (!in_array('fallback_' . CarbonIntensity::class, $skip)) {
406+
$fallback_carbon_intensity = $this->createItem(CarbonIntensity::class, [
407+
'plugin_carbon_sources_id' => isset($fallback_source) ? $fallback_source->getID() : 0,
408+
'plugin_carbon_zones_id' => isset($zone) ? $zone->getID() : 0,
409+
]);
410+
}
411+
if (!in_array('2nd_fallback_' . Source::class, $skip)) {
412+
$fallback_source = $this->createItem(Source::class, [
413+
'is_carbon_intensity_source' => 1,
414+
'fallback_level' => 2,
415+
]);
416+
}
417+
if (!in_array('2nd_fallback_' . Source_Zone::class, $skip)) {
418+
$fallback_source_zone = $this->createItem(Source_Zone::class, [
419+
'plugin_carbon_sources_id' => isset($fallback_source) ? $fallback_source->getID() : 0,
420+
'plugin_carbon_zones_id' => isset($zone) ? $zone->getID() : 0,
421+
]);
422+
}
423+
if (!in_array('2nd_fallback_' . CarbonIntensity::class, $skip)) {
424+
$fallback_carbon_intensity = $this->createItem(CarbonIntensity::class, [
425+
'plugin_carbon_sources_id' => isset($fallback_source) ? $fallback_source->getID() : 0,
426+
'plugin_carbon_zones_id' => isset($zone) ? $zone->getID() : 0,
427+
]);
428+
}
429+
if (!in_array(GlpiLocation::class, $skip)) {
430+
$glpi_location = $this->createItem(GlpiLocation::class);
431+
}
432+
if (!in_array(Location::class, $skip)) {
433+
$location = $this->createItem(Location::class, [
434+
'locations_id' => isset($glpi_location) ? $glpi_location->getID() : 0,
435+
Source_Zone::getForeignKeyField() => isset($source_zone) ? $source_zone->getID() : 0,
436+
]);
437+
}
438+
$glpi_computer = $this->createItem(GlpiComputer::class, [
439+
GlpiLocation::getForeignKeyField() => isset($glpi_location) ? $glpi_location->getID() : 0,
440+
GlpiComputerType::getForeignKeyField() => isset($glpi_computer_type) ? $glpi_computer_type->getID() : 0,
441+
GlpiComputerModel::getForeignKeyField() => isset($glpi_computer_model) ? $glpi_computer_model->getID() : 0,
442+
]);
443+
if (!in_array(ComputerUsageProfile::class, $skip)) {
444+
$usage_profile = $this->createItem(ComputerUsageProfile::class);
445+
}
446+
if (!in_array(UsageInfo::class, $skip)) {
447+
$impact = $this->createItem(UsageInfo::class, [
448+
'itemtype' => $glpi_computer::getType(),
449+
'items_id' => $glpi_computer->getID(),
450+
'plugin_carbon_computerusageprofiles_id' => isset($usage_profile) ? $usage_profile->getID() : 0,
451+
]);
452+
}
453+
return $glpi_computer;
454+
}
455+
348456
protected function getSessionMessage(): string
349457
{
350458
if (
@@ -377,7 +485,7 @@ private function handleDeprecations(&$itemtype, &$input): void
377485
if (version_compare(GLPI_VERSION, '11.0.0-beta') >= 0) {
378486
if ($itemtype === \Computer_Item::class) {
379487
$itemtype = \Glpi\Asset\Asset_PeripheralAsset::class;
380-
$input['itemtype_asset'] = Computer::class;
488+
$input['itemtype_asset'] = GlpiComputer::class;
381489
$input['items_id_asset'] = $input['computers_id'];
382490
$input['itemtype_peripheral'] = $input['itemtype'];
383491
$input['items_id_peripheral'] = $input['items_id'];

tests/units/Dashboard/ProviderTest.php

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use DateTime;
4040
use Glpi\Asset\Asset_PeripheralAsset;
4141
use GlpiPlugin\Carbon\CarbonEmission;
42+
use GlpiPlugin\Carbon\CarbonIntensity;
4243
use GlpiPlugin\Carbon\ComputerType;
4344
use GlpiPlugin\Carbon\ComputerUsageProfile;
4445
use GlpiPlugin\Carbon\Dashboard\Provider;
@@ -68,7 +69,7 @@ public function setUp(): void
6869
$this->login('glpi', 'glpi');
6970
}
7071

71-
protected function handledComputersCountFixture(): int
72+
protected function handledComputersCountFixture_old(): int
7273
{
7374
// Switch to an empty entity
7475
$entities_id = $this->isolateInEntity('glpi', 'glpi');
@@ -97,12 +98,18 @@ protected function handledComputersCountFixture(): int
9798

9899
$glpi_location_empty = $this->createItem(GlpiLocation::class);
99100
$glpi_location = $this->createItem(GlpiLocation::class);
100-
$source = $this->createItem(Source::class);
101+
$source = $this->createItem(Source::class, [
102+
'is_carbon_intensity_source' => 1
103+
]);
101104
$zone = $this->createItem(Zone::class);
102105
$source_zone = $this->createItem(Source_Zone::class, [
103106
'plugin_carbon_sources_id' => $source->getID(),
104107
'plugin_carbo_zones_id' => $zone->getID(),
105108
]);
109+
$carbon_intensity = $this->createItem(CarbonIntensity::class, [
110+
'plugin_carbon_sources_id' => $source->getID(),
111+
'plugin_carbon_zones_id' => $zone->getID(),
112+
]);
106113
$location = $this->createItem(Location::class, [
107114
'locations_id' => $glpi_location->getID(),
108115
$source_zone::getForeignKeyField() => $source_zone->getID(),
@@ -173,31 +180,148 @@ protected function handledComputersCountFixture(): int
173180
return $total_count;
174181
}
175182

183+
protected function handledComputersCountFixture(): int
184+
{
185+
$glpi_computers = [];
186+
187+
// Handled computer with all requirments
188+
$glpi_computer = $this->createHistorizableComputer();
189+
$glpi_computers[] = $glpi_computer;
190+
191+
// Computer without
192+
// - fallback carbon intensity
193+
$glpi_computer = $this->createHistorizableComputer([
194+
'fallback_' . CarbonIntensity::class,
195+
'2nd_fallback_' . CarbonIntensity::class,
196+
]);
197+
$glpi_computers[] = $glpi_computer;
198+
199+
// Computer without
200+
// - fallback source_zone
201+
$glpi_computer = $this->createHistorizableComputer([
202+
'fallback_' . Source_Zone::class,
203+
'fallback_' . CarbonIntensity::class,
204+
'2nd_fallback_' . Source_Zone::class,
205+
'2nd_fallback_' . CarbonIntensity::class,
206+
]);
207+
$glpi_computers[] = $glpi_computer;
208+
209+
// Computer without
210+
// - realtime carbon intensity
211+
$glpi_computer = $this->createHistorizableComputer([CarbonIntensity::class]);
212+
$glpi_computers[] = $glpi_computer;
213+
214+
// Computer without
215+
// - Type power consumption
216+
$glpi_computer = $this->createHistorizableComputer([ComputerType::class . '_power']);
217+
$glpi_computers[] = $glpi_computer;
218+
219+
// Computer without
220+
// - plugin data for type
221+
$glpi_computer = $this->createHistorizableComputer([ComputerType::class]);
222+
$glpi_computers[] = $glpi_computer;
223+
224+
// Computer without
225+
// - type
226+
$glpi_computer = $this->createHistorizableComputer([
227+
GlpiComputerType::class,
228+
ComputerType::class
229+
]);
230+
$glpi_computers[] = $glpi_computer;
231+
232+
// Computer without
233+
// - model power conssumption
234+
$glpi_computer = $this->createHistorizableComputer([GlpiComputerModel::class . '_power']);
235+
$glpi_computers[] = $glpi_computer;
236+
237+
// Computer without
238+
// - model power conssumption
239+
// - type power consumption
240+
$glpi_computer = $this->createHistorizableComputer([
241+
ComputerType::class . '_power',
242+
GlpiComputerModel::class . '_power',
243+
]);
244+
$glpi_computers[] = $glpi_computer;
245+
246+
// Computer without
247+
// - model
248+
// - type
249+
$glpi_computer = $this->createHistorizableComputer([
250+
ComputerType::class,
251+
GlpiComputerModel::class,
252+
]);
253+
$glpi_computers[] = $glpi_computer;
254+
255+
// Computer without
256+
// - model
257+
$glpi_computer = $this->createHistorizableComputer([GlpiComputerModel::class]);
258+
$glpi_computers[] = $glpi_computer;
259+
260+
// Computer without
261+
// - plugin location data
262+
$glpi_computer = $this->createHistorizableComputer([Location::class]);
263+
$glpi_computers[] = $glpi_computer;
264+
265+
// Computer without
266+
// - any source_zone
267+
$glpi_computer = $this->createHistorizableComputer([
268+
Source_Zone::class,
269+
CarbonIntensity::class,
270+
'fallback_' . Source_Zone::class,
271+
'fallback_' . CarbonIntensity::class,
272+
'2nd_fallback_' . Source_Zone::class,
273+
'2nd_fallback_' . CarbonIntensity::class,
274+
]);
275+
$glpi_computers[] = $glpi_computer;
276+
277+
// Computer without
278+
// - location
279+
$glpi_computer = $this->createHistorizableComputer([GlpiLocation::class]);
280+
$glpi_computers[] = $glpi_computer;
281+
282+
// Computer without
283+
// - Usage profile
284+
$glpi_computer = $this->createHistorizableComputer([ComputerUsageProfile::class]);
285+
$glpi_computers[] = $glpi_computer;
286+
287+
// Computer
288+
// - as template
289+
$glpi_computer = $this->createHistorizableComputer();
290+
$glpi_computers[] = $glpi_computer;
291+
$this->assertTrue($glpi_computer->update(['is_template' => 1] + $glpi_computer->fields));
292+
293+
// Computer
294+
// - deleted
295+
$glpi_computer = $this->createHistorizableComputer();
296+
$glpi_computers[] = $glpi_computer;
297+
$this->assertTrue($glpi_computer->update(['is_deleted' => 1] + $glpi_computer->fields));
298+
299+
return count($glpi_computers);
300+
}
301+
176302
public function testGetHandledComputersCount()
177303
{
178304
$total_count = $this->handledComputersCountFixture();
179305

180-
// 3 computers are complete
181-
// 1 having both power_consumption from computer type and computer model
182-
// 1 having both power_consumption from computer type only
183-
// 1 having both power_consumption from computer model only
306+
// 9 computers fill historization requirements
184307
$handled_count = Provider::getHandledAssetCount(GlpiComputer::class, true);
185-
$this->assertEquals(3, $handled_count['number']);
308+
$this->assertEquals(9, $handled_count['number']);
186309
}
187310

188311
public function testGetUnhandledComputersCount()
189312
{
190313
$total_count = $this->handledComputersCountFixture();
191314

192315
$unhandled_count = Provider::getHandledAssetCount(GlpiComputer::class, false);
193-
$this->assertEquals($total_count - 3, $unhandled_count['number']);
316+
$deleted_or_template_count = 2;
317+
$this->assertEquals($total_count - 9 - $deleted_or_template_count, $unhandled_count['number']);
194318
}
195319

196320
public function testGetHandledAssetsRatio()
197321
{
198322
$total_count = $this->handledComputersCountFixture();
199323
$result = Provider::getHandledAssetsRatio([GlpiComputer::class]);
200-
$expected = 19; // This is a percentage
324+
$expected = 60; // This is a percentage
201325
$this->assertEquals($expected, $result['data'][0]['number']);
202326
}
203327

0 commit comments

Comments
 (0)