diff --git a/src/CarbonIntensity.php b/src/CarbonIntensity.php index cb82887f..f5932b2c 100644 --- a/src/CarbonIntensity.php +++ b/src/CarbonIntensity.php @@ -368,8 +368,6 @@ public function save(string $zone_name, string $source_name, array $data): int ); $stmt = $DB->prepare($query); - $in_transaction = $DB->inTransaction() || $DB->beginTransaction(); - foreach ($data as $intensity) { try { $stmt->bind_param( @@ -387,9 +385,6 @@ public function save(string $zone_name, string $source_name, array $data): int continue; } } - if ($in_transaction) { - $DB->commit(); - } $stmt->close(); return $count; diff --git a/src/Dashboard/Provider.php b/src/Dashboard/Provider.php index 311b7a5a..ded89598 100644 --- a/src/Dashboard/Provider.php +++ b/src/Dashboard/Provider.php @@ -951,7 +951,7 @@ public static function getUsageCarbonEmissionPerMonth(array $crit = [], array $p $emissions_table = CarbonEmission::getTable(); $dbUtils = new DbUtils(); - $entityRestrict = $dbUtils->getEntitiesRestrictCriteria($emissions_table, '', '', 'auto'); + $entity_restrict = $dbUtils->getEntitiesRestrictCriteria($emissions_table, '', '', 'auto'); $sql_year_month = "DATE_FORMAT(`date`, '%Y-%m')"; $request = [ 'SELECT' => [ @@ -964,7 +964,7 @@ public static function getUsageCarbonEmissionPerMonth(array $crit = [], array $p 'FROM' => $emissions_table, 'GROUPBY' => @new QueryExpression($sql_year_month), 'ORDER' => @new QueryExpression($sql_year_month), - 'WHERE' => $entityRestrict + $crit, + 'WHERE' => array_merge($entity_restrict, $crit), ]; $filter = self::getFiltersCriteria($emissions_table, $params['apply_filters']); $request = array_merge_recursive($request, $filter); diff --git a/src/Toolbox.php b/src/Toolbox.php index 25f566e0..be946ffb 100644 --- a/src/Toolbox.php +++ b/src/Toolbox.php @@ -426,18 +426,18 @@ public static function getEmbodiedImpactClasses(): array } /** - * Get an array of 2 dates from the beginning of the current year to yesterday + * Get an array of 2 dates matching the 12 complete months before the date argument * * @param DateTimeImmutable $date * @return array */ public function yearToLastMonth(DateTimeImmutable $date): array { - $end_date = DateTime::createFromImmutable($date); - $end_date->setTime(0, 0, 0, 0); - $end_date->setDate((int) $end_date->format('Y'), (int) $end_date->format('m'), 1); // First day of current month (excluded) - $start_date = clone $end_date; - $start_date->setDate((int) $end_date->format('Y') - 1, (int) $end_date->format('m'), 1); + $end_date = DateTime::createFromImmutable($date); + $end_date->setTime(0, 0, 0, 0); + $end_date->setDate((int) $end_date->format('Y'), (int) $end_date->format('m'), 1); // First day of current month (excluded) + $start_date = clone $end_date; + $start_date->setDate((int) $end_date->format('Y') - 1, (int) $end_date->format('m'), 1); return [$start_date, $end_date]; } diff --git a/templates/components/form/fields_macros.html.twig b/templates/components/form/fields_macros.html.twig index 77dea6cf..afc414c1 100644 --- a/templates/components/form/fields_macros.html.twig +++ b/templates/components/form/fields_macros.html.twig @@ -33,12 +33,16 @@ {% macro timeField(name, value, label = '', options = {}) %} {% import 'components/form/fields_macros.html.twig' as fields %} - {% set options = {'rand': random()}|merge(options) %} + {% set options = { + 'rand': random(), + 'disabled': false + }|merge(options) + %} {% if options.disabled %} {% set options = options|merge({specific_tags: {'disabled': 'disabled'}}) %} {% endif %} - {% if options.fields_template.isMandatoryField(name) %} + {% if options.fields_template.isMandatoryField(name)|default(false) %} {% set options = {'specific_tags': {'required': true}}|merge(options) %} {% endif %} @@ -63,12 +67,15 @@ {# Multiple values will be set, input need to be an array #} {% set name = "#{name}[]" %} {% endif %} - {% set options = {'rand': random()}|merge(options) %} + {% set options = { + 'rand': random(), + 'disabled': false}|merge(options) + %} {% if options.fields_template.isMandatoryField(name)|default(false) %} {% set options = {'specific_tags': {'required': true}}|merge(options) %} {% endif %} - {% if options.disabled|default(false) %} + {% if options.disabled %} {% set options = options|merge({specific_tags: {'disabled': 'disabled'}}) %} {% endif %} diff --git a/tests/src/CommonTestCase.php b/tests/src/CommonTestCase.php index 02e1421c..de049b24 100644 --- a/tests/src/CommonTestCase.php +++ b/tests/src/CommonTestCase.php @@ -147,6 +147,12 @@ protected function login($name, $password, $noauto = false) return $result; } + protected function logout() + { + Session::destroy(); + Session::start(); + } + /** * Get a unique random string */ @@ -169,11 +175,11 @@ protected function getItem(string $itemtype, array $input = []): CommonDBTM { global $DB; + $this->handleDeprecations($itemtype, $input); + /** @var CommonDBTM */ $item = new $itemtype(); - $this->handleDeprecations($itemtype, $input); - // set random name if not already set if (!isset($item->fields['name']) && $DB->fieldExists($item->getTable(), 'name')) { if (!isset($input['name'])) { diff --git a/tests/units/Dashboard/ProviderTest.php b/tests/units/Dashboard/ProviderTest.php index 7417e3ca..cf3f5541 100644 --- a/tests/units/Dashboard/ProviderTest.php +++ b/tests/units/Dashboard/ProviderTest.php @@ -400,7 +400,7 @@ public function testGetUsageCarbonEmissionPerMonth() 'apply_filters' => [ 'dates' => [ '2024-02-01T00:00:00.000Z', - '2024-05-31T00:00:00.000Z', + '2024-06-01T00:00:00.000Z', ], ], ], @@ -424,7 +424,7 @@ public function testGetUsageCarbonEmissionPerMonth() ], 3 => [ 'x' => '2024-05', - 'y' => '600.000', + 'y' => '620.000', ], ], 'name' => 'Carbon emission (gCO₂eq)', @@ -446,7 +446,7 @@ public function testGetUsageCarbonEmissionPerMonth() ], 3 => [ 'x' => '2024-05', - 'y' => '30.000', + 'y' => '31.000', ], ], 'name' => 'Consumed energy (KWh)', diff --git a/tests/units/ProfileTest.php b/tests/units/ProfileTest.php index de40695c..89213039 100644 --- a/tests/units/ProfileTest.php +++ b/tests/units/ProfileTest.php @@ -49,7 +49,7 @@ public function testGetTabNameForItem() $profile = new Profile(); $item = new GlpiProfile(); $tabName = $profile->getTabNameForItem($item); - $this->assertEquals(__('Environmental impact', 'carbon'), $tabName); + $this->assertStringContainsString(__('Environmental impact', 'carbon'), $tabName); } /**