|  | 
|  | 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 | +use DBmysql; | 
|  | 34 | +use Location as GlpiLocation; | 
|  | 35 | +use GlpiPlugin\Carbon\Tests\CommonTestCase; | 
|  | 36 | +use GlpiPlugin\Carbon\Tests\DbTestCase; | 
|  | 37 | +use GlpiPlugin\Carbon\Uninstall; | 
|  | 38 | + | 
|  | 39 | +class migration_to_1_2_0_Test extends CommonTestCase | 
|  | 40 | +{ | 
|  | 41 | +    public static function setUpBeforeClass(): void | 
|  | 42 | +    { | 
|  | 43 | +        global $DB; | 
|  | 44 | + | 
|  | 45 | +        require_once(__DIR__ . '/../../setup.php'); | 
|  | 46 | +        $sql_file = plugin_carbon_getSchemaPath('1.2.0'); | 
|  | 47 | +        $success = $DB->runFile(realpath($sql_file)); | 
|  | 48 | +    } | 
|  | 49 | + | 
|  | 50 | +    public static function tearDownAfterClass(): void | 
|  | 51 | +    { | 
|  | 52 | +        require_once(__DIR__ . '/../../install/Uninstall.php'); | 
|  | 53 | +        $uninstall = new Uninstall(); | 
|  | 54 | +        $uninstall->uninstall(); | 
|  | 55 | +    } | 
|  | 56 | + | 
|  | 57 | +    public function testUpdateCountryLocationZoneRelation() | 
|  | 58 | +    { | 
|  | 59 | +        /** @var DBMysql $DB */ | 
|  | 60 | +        global $DB; | 
|  | 61 | +        return; | 
|  | 62 | + | 
|  | 63 | +        $glpi_location = $this->createItem(GlpiLocation::class, [ | 
|  | 64 | +            'country' => 'France', | 
|  | 65 | +        ]); | 
|  | 66 | +        $DB->insert('glpi_plugin_carbon_zones', [ | 
|  | 67 | +            'name' => 'France', | 
|  | 68 | +            'plugin_carbon_sources_id_historical' => 1, | 
|  | 69 | +        ]); | 
|  | 70 | +        $zone_id = $DB->insertId(); | 
|  | 71 | +        $DB->insert('glpi_plugin_carbon_sources', [ | 
|  | 72 | +            'name' => 'RTE', | 
|  | 73 | +            'is_fallback' => 0, | 
|  | 74 | +            'is_carbon_intensity_source' => 1, | 
|  | 75 | +        ]); | 
|  | 76 | +        $source_id = $DB->insertId(); | 
|  | 77 | +        $DB->insert('glpi_plugin_carbon_sources_zones', [ | 
|  | 78 | +            'plugin_carbon_sources_id' => $source_id, | 
|  | 79 | +            'plugin_carbon_zones_id'   => $zone_id, | 
|  | 80 | +        ]); | 
|  | 81 | +        $source_zone_id = $DB->insertId(); | 
|  | 82 | +        $migration_file = __DIR__ . '/../../install/migration/update_1.1.0_to_1.2.0/04_update_location_zone_relation.php'; | 
|  | 83 | +        $migration_file = realpath($migration_file); | 
|  | 84 | +        require($migration_file); | 
|  | 85 | + | 
|  | 86 | +        $result = $DB->request([ | 
|  | 87 | +            'SELECT' => '*', | 
|  | 88 | +            'FROM' => 'glpi_plugin_carbon_locations', | 
|  | 89 | +            'WHERE' => [ | 
|  | 90 | +                'locations_id' => $glpi_location->getID(), | 
|  | 91 | +            ], | 
|  | 92 | +        ]); | 
|  | 93 | +        $this->assertEquals(1, $result->count()); | 
|  | 94 | +        $expected = [ | 
|  | 95 | +            'id' => $result->current()['id'], | 
|  | 96 | +            'locations_id' => $glpi_location->getID(), | 
|  | 97 | +            'boavizta_zone' => null, | 
|  | 98 | +            'plugin_carbon_sources_zones_id' => $source_zone_id, | 
|  | 99 | +        ]; | 
|  | 100 | +        $this->assertEquals($expected, $result->current()); | 
|  | 101 | +    } | 
|  | 102 | + | 
|  | 103 | +    public function testUpdateStateLocationZoneRelation() | 
|  | 104 | +    { | 
|  | 105 | +        /** @var DBMysql $DB */ | 
|  | 106 | +        global $DB; | 
|  | 107 | + | 
|  | 108 | +        $glpi_location = $this->createItem(GlpiLocation::class, [ | 
|  | 109 | +            'state' => 'Quebec', | 
|  | 110 | +        ]); | 
|  | 111 | +        $DB->insert('glpi_plugin_carbon_zones', [ | 
|  | 112 | +            'name' => 'Quebec', | 
|  | 113 | +            'plugin_carbon_sources_id_historical' => 1, | 
|  | 114 | +        ]); | 
|  | 115 | +        $zone_id = $DB->insertId(); | 
|  | 116 | +        $DB->insert('glpi_plugin_carbon_sources', [ | 
|  | 117 | +            'name' => 'Hydro Quebec', | 
|  | 118 | +            'is_fallback' => 1, | 
|  | 119 | +            'is_carbon_intensity_source' => 1, | 
|  | 120 | +        ]); | 
|  | 121 | +        $source_id = $DB->insertId(); | 
|  | 122 | +        $DB->insert('glpi_plugin_carbon_sources_zones', [ | 
|  | 123 | +            'plugin_carbon_sources_id' => $source_id, | 
|  | 124 | +            'plugin_carbon_zones_id'   => $zone_id, | 
|  | 125 | +        ]); | 
|  | 126 | +        $source_zone_id = $DB->insertId(); | 
|  | 127 | +        $migration_file = __DIR__ . '/../../install/migration/update_1.1.0_to_1.2.0/04_update_location_zone_relation.php'; | 
|  | 128 | +        $migration_file = realpath($migration_file); | 
|  | 129 | +        require($migration_file); | 
|  | 130 | + | 
|  | 131 | +        $result = $DB->request([ | 
|  | 132 | +            'SELECT' => '*', | 
|  | 133 | +            'FROM' => 'glpi_plugin_carbon_locations', | 
|  | 134 | +            'WHERE' => [ | 
|  | 135 | +                'locations_id' => $glpi_location->getID(), | 
|  | 136 | +            ], | 
|  | 137 | +        ]); | 
|  | 138 | +        $this->assertEquals(1, $result->count()); | 
|  | 139 | +        $expected = [ | 
|  | 140 | +            'id' => $result->current()['id'], | 
|  | 141 | +            'locations_id' => $glpi_location->getID(), | 
|  | 142 | +            'boavizta_zone' => null, | 
|  | 143 | +            'plugin_carbon_sources_zones_id' => $source_zone_id, | 
|  | 144 | +        ]; | 
|  | 145 | +        $this->assertEquals($expected, $result->current()); | 
|  | 146 | +    } | 
|  | 147 | +} | 
0 commit comments