|
33 | 33 | namespace GlpiPlugin\Carbon\Tests; |
34 | 34 |
|
35 | 35 | use GlpiPlugin\Carbon\UsageInfo; |
| 36 | +use GlpiPlugin\Carbon\EmbodiedImpact; |
36 | 37 | use Computer as GlpiComputer; |
37 | 38 | use Contact; |
38 | 39 | use DBmysql; |
@@ -177,18 +178,87 @@ public function testShowForItem() |
177 | 178 |
|
178 | 179 | public function testShowcharts() |
179 | 180 | { |
180 | | - // Test that the charts shows for a computer |
181 | | - $glpi_computer = $this->createItem(GlpiComputer::class); |
182 | | - $instance = $this->createItem(UsageInfo::class, [ |
183 | | - 'itemtype' => $glpi_computer::getType(), |
184 | | - 'items_id' => $glpi_computer->getID(), |
185 | | - ]); |
186 | | - ob_start(); |
187 | | - UsageInfo::showCharts($glpi_computer); |
188 | | - $output = ob_get_clean(); |
189 | | - $crawler = new Crawler($output); |
190 | | - $usage_profile_dropdown = $crawler->filter('select[name="plugin_carbon_computerusageprofiles_id"]'); |
191 | | - $this->assertEquals(1, $usage_profile_dropdown->count()); |
| 181 | + $itemtypes = PLUGIN_CARBON_TYPES; |
| 182 | + foreach ($itemtypes as $itemtype) { |
| 183 | + // Test charts and data are visible for an asset |
| 184 | + $item = $this->createItem($itemtype); |
| 185 | + $instance = $this->createItem(UsageInfo::class, [ |
| 186 | + 'itemtype' => $item::getType(), |
| 187 | + 'items_id' => $item->getID(), |
| 188 | + ]); |
| 189 | + $embodied_impact = $this->createItem(EmbodiedImpact::class, [ |
| 190 | + 'itemtype' => $item::getType(), |
| 191 | + 'items_id' => $item->getID(), |
| 192 | + 'gwp' => 10, |
| 193 | + 'adp' => 11, |
| 194 | + 'pe' => 12, |
| 195 | + ]); |
| 196 | + ob_start(); |
| 197 | + UsageInfo::showCharts($item); |
| 198 | + $output = ob_get_clean(); |
| 199 | + $crawler = new Crawler($output); |
| 200 | + $monthlyCarbonEmissionChart = $crawler->filter('#carbonEmissionPerMonthChart'); |
| 201 | + $this->assertEquals(1, $monthlyCarbonEmissionChart->count()); |
| 202 | + $this->assertTrue($this->testEmbodiedGwp($crawler)); |
| 203 | + $this->assertTrue($this->testEmbodiedAdp($crawler)); |
| 204 | + $this->assertTrue($this->testEmbodiedPe($crawler)); |
192 | 205 |
|
| 206 | + // Test charts are visible for an asset - no embodied data |
| 207 | + $item = $this->createItem($itemtype); |
| 208 | + $instance = $this->createItem(UsageInfo::class, [ |
| 209 | + 'itemtype' => $item::getType(), |
| 210 | + 'items_id' => $item->getID(), |
| 211 | + ]); |
| 212 | + ob_start(); |
| 213 | + UsageInfo::showCharts($item); |
| 214 | + $output = ob_get_clean(); |
| 215 | + $crawler = new Crawler($output); |
| 216 | + $monthlyCarbonEmissionChart = $crawler->filter('#carbonEmissionPerMonthChart'); |
| 217 | + $this->assertEquals(1, $monthlyCarbonEmissionChart->count()); |
| 218 | + $this->assertFalse($this->testEmbodiedGwp($crawler)); |
| 219 | + $this->assertFalse($this->testEmbodiedAdp($crawler)); |
| 220 | + $this->assertFalse($this->testEmbodiedPe($crawler)); |
| 221 | + |
| 222 | + // Test charts are visible for an asset - empty embodied data |
| 223 | + $item = $this->createItem($itemtype); |
| 224 | + $instance = $this->createItem(UsageInfo::class, [ |
| 225 | + 'itemtype' => $item::getType(), |
| 226 | + 'items_id' => $item->getID(), |
| 227 | + ]); |
| 228 | + $embodied_impact = $this->createItem(EmbodiedImpact::class, [ |
| 229 | + 'itemtype' => $item::getType(), |
| 230 | + 'items_id' => $item->getID(), |
| 231 | + 'gwp' => null, |
| 232 | + 'adp' => null, |
| 233 | + 'pe' => null, |
| 234 | + ]); |
| 235 | + ob_start(); |
| 236 | + UsageInfo::showCharts($item); |
| 237 | + $output = ob_get_clean(); |
| 238 | + $crawler = new Crawler($output); |
| 239 | + $monthlyCarbonEmissionChart = $crawler->filter('#carbonEmissionPerMonthChart'); |
| 240 | + $this->assertEquals(1, $monthlyCarbonEmissionChart->count()); |
| 241 | + $this->assertFalse($this->testEmbodiedGwp($crawler)); |
| 242 | + $this->assertFalse($this->testEmbodiedAdp($crawler)); |
| 243 | + $this->assertFalse($this->testEmbodiedPe($crawler)); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + private function testEmbodiedGwp(Crawler $crawler): bool |
| 248 | + { |
| 249 | + $items = $crawler->filter('#plugin_carbon_embodied_impacts #embodied_carbon_emission_tip'); |
| 250 | + return $items->count() === 1; |
| 251 | + } |
| 252 | + |
| 253 | + private function testEmbodiedAdp(Crawler $crawler): bool |
| 254 | + { |
| 255 | + $items = $crawler->filter('#plugin_carbon_embodied_impacts #embodied_abiotic_depletion_tip'); |
| 256 | + return $items->count() === 1; |
| 257 | + } |
| 258 | + |
| 259 | + private function testEmbodiedPe(Crawler $crawler): bool |
| 260 | + { |
| 261 | + $items = $crawler->filter('#plugin_carbon_embodied_impacts #embodied_primary_energy_tip'); |
| 262 | + return $items->count() === 1; |
193 | 263 | } |
194 | 264 | } |
0 commit comments