3535use PHPUnit \Framework \TestCase ;
3636use Auth ;
3737use CommonDBTM ;
38- use Computer ;
38+ use Computer as GlpiComputer ;
3939use ComputerType as GlpiComputerType ;
4040use DateTime ;
4141use DateInterval ;
4242use DateTimeInterface ;
43+ use ComputerModel as GlpiComputerModel ;
4344use Glpi \Inventory \Conf ;
4445use GlpiPlugin \Carbon \UsageInfo ;
4546use 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 ' ];
0 commit comments