|
32 | 32 |
|
33 | 33 | namespace GlpiPlugin\Carbon\Tests; |
34 | 34 |
|
| 35 | +use CommonDBTM; |
35 | 36 | use Computer as GlpiComputer; |
36 | 37 | use GlpiPlugin\Carbon\ComputerUsageProfile; |
37 | 38 | use GlpiPlugin\Carbon\Tests\DbTestCase; |
| 39 | +use MassiveAction; |
| 40 | +use Symfony\Component\DomCrawler\Crawler; |
| 41 | +use Ticket; |
38 | 42 |
|
39 | 43 | class ComputerUsageProfileTest extends DbTestCase |
40 | 44 | { |
| 45 | + /** |
| 46 | + * @covers GlpiPlugin\Carbon\ComputerUsageProfile::canView |
| 47 | + * |
| 48 | + * @return void |
| 49 | + */ |
| 50 | + public function testCanView() |
| 51 | + { |
| 52 | + $result = ComputerUsageProfile::canView(); |
| 53 | + $this->assertFalse($result); |
| 54 | + |
| 55 | + $this->login('glpi', 'glpi'); |
| 56 | + $result = ComputerUsageProfile::canView(); |
| 57 | + $this->assertTrue($result); |
| 58 | + } |
| 59 | + |
41 | 60 | /** |
42 | 61 | * @covers GlpiPlugin\Carbon\ComputerUsageProfile::prepareInputForAdd |
43 | 62 | * @covers GlpiPlugin\Carbon\ComputerUsageProfile::inputIntegrityCheck |
@@ -122,16 +141,135 @@ public function testPrepareInputForUpdate() |
122 | 141 | $this->assertEquals($expected, $result); |
123 | 142 | } |
124 | 143 |
|
| 144 | + /** |
| 145 | + * @covers GlpiPlugin\Carbon\ComputerUsageProfile::assignToItem |
| 146 | + * |
| 147 | + * @return void |
| 148 | + */ |
125 | 149 | public function testAssignToItem() |
126 | 150 | { |
| 151 | + $invalid_item = new class extends CommonDBTM { |
| 152 | + }; |
| 153 | + $usage_profile = $this->getItem(ComputerUsageProfile::class, ['name' => 'Test Usage Profile']); |
| 154 | + $result = $usage_profile->assignToItem($invalid_item); |
| 155 | + $this->assertFalse($result); |
| 156 | + |
127 | 157 | $computer = $this->getItem(GlpiComputer::class, ['name' => 'Test Computer']); |
128 | 158 | $usage_profile = $this->getItem(ComputerUsageProfile::class, ['name' => 'Test Usage Profile']); |
129 | 159 |
|
130 | | - $result = ComputerUsageProfile::assignToItem($computer, $usage_profile->getID()); |
| 160 | + $result = $usage_profile->assignToItem($computer); |
131 | 161 | $this->assertTrue($result); |
132 | 162 |
|
133 | 163 | $usage_profile = $this->getItem(ComputerUsageProfile::class, ['name' => 'Test Usage Profile 2']); |
134 | | - $result = ComputerUsageProfile::assignToItem($computer, $usage_profile->getID()); |
| 164 | + $result = $usage_profile->assignToItem($computer); |
135 | 165 | $this->assertTrue($result); |
136 | 166 | } |
| 167 | + |
| 168 | + public function testShowMassiveActionsSubForm() |
| 169 | + { |
| 170 | + // Test power consumption update form |
| 171 | + $massive_action = $this->getMockBuilder(MassiveAction::class) |
| 172 | + ->disableOriginalConstructor() |
| 173 | + ->getMock(); |
| 174 | + $massive_action->method('getAction')->willReturn('MassAssociateItems'); |
| 175 | + $massive_action->method('getItems')->willReturn([ |
| 176 | + GlpiComputer::class => $this->getItem(GlpiComputer::class) |
| 177 | + ]); |
| 178 | + ob_start(function ($buffer) { |
| 179 | + return $buffer; |
| 180 | + }); |
| 181 | + $result = ComputerUsageProfile::showMassiveActionsSubForm($massive_action); |
| 182 | + $output = ob_get_clean(); |
| 183 | + $crawler = new Crawler($output); |
| 184 | + $selector = $crawler->filter('select[name="plugin_carbon_computerusageprofiles_id"]'); |
| 185 | + $this->assertEquals(1, $selector->count()); |
| 186 | + $button = $crawler->filter('button[name="massiveaction"]'); |
| 187 | + $this->assertEquals(1, $button->count()); |
| 188 | + } |
| 189 | + |
| 190 | + public function testProcessMassiveActionsForOneItemtype() |
| 191 | + { |
| 192 | + // Test with invalid usage profile |
| 193 | + $computer = $this->getItem(GlpiComputer::class); |
| 194 | + $massive_action = $this->getMockBuilder(MassiveAction::class) |
| 195 | + ->disableOriginalConstructor() |
| 196 | + ->getMock(); |
| 197 | + $massive_action->method('getAction')->willReturn('MassAssociateItems'); |
| 198 | + $massive_action->expects($this->once())->method('itemDone')->with( |
| 199 | + GlpiComputer::class, |
| 200 | + $computer->getID(), |
| 201 | + MassiveAction::ACTION_KO |
| 202 | + ); |
| 203 | + $usage_profile_fk = ComputerUsageProfile::getForeignKeyField(); |
| 204 | + $usage_profile = $this->getItem(ComputerUsageProfile::class); |
| 205 | + $massive_action->POST[$usage_profile_fk] = -1; |
| 206 | + ComputerUsageProfile::processMassiveActionsForOneItemtype( |
| 207 | + $massive_action, |
| 208 | + new GlpiComputer(), |
| 209 | + [ |
| 210 | + $computer->getID() => $computer->getID(), |
| 211 | + ] |
| 212 | + ); |
| 213 | + |
| 214 | + // Test with invalid and valid computer |
| 215 | + $computer_1 = new GlpiComputer(); |
| 216 | + $computer_2 = $this->getItem(GlpiComputer::class); |
| 217 | + $massive_action = $this->getMockBuilder(MassiveAction::class) |
| 218 | + ->disableOriginalConstructor() |
| 219 | + ->getMock(); |
| 220 | + $massive_action->method('getAction')->willReturn('MassAssociateItems'); |
| 221 | + $matcher = $this->exactly(2); |
| 222 | + $expected_args = [ |
| 223 | + 1 => [GlpiComputer::class, $computer_1->getID(), MassiveAction::ACTION_KO], |
| 224 | + 2 => [GlpiComputer::class, $computer_2->getID(), MassiveAction::ACTION_OK], |
| 225 | + ]; |
| 226 | + $massive_action->expects($matcher)->method('itemDone')->willReturnCallback( |
| 227 | + function (...$parameters) use ($matcher, $expected_args) { |
| 228 | + // TODO: With PHPUnit 10 getInvocationCount becomes numberOfInvocations |
| 229 | + switch ($matcher->getInvocationCount()) { |
| 230 | + case 1: |
| 231 | + $this->assertEquals($expected_args[1], $parameters); |
| 232 | + break; |
| 233 | + case 2: |
| 234 | + $this->assertEquals($expected_args[2], $parameters); |
| 235 | + break; |
| 236 | + } |
| 237 | + } |
| 238 | + ); |
| 239 | + $usage_profile_fk = ComputerUsageProfile::getForeignKeyField(); |
| 240 | + $usage_profile = $this->getItem(ComputerUsageProfile::class); |
| 241 | + $massive_action->POST[$usage_profile_fk] = $usage_profile->getID(); |
| 242 | + ComputerUsageProfile::processMassiveActionsForOneItemtype( |
| 243 | + $massive_action, |
| 244 | + new GlpiComputer(), |
| 245 | + [ |
| 246 | + $computer_1->getID() => $computer_1->getID(), |
| 247 | + $computer_2->getID() => $computer_2->getID(), |
| 248 | + ] |
| 249 | + ); |
| 250 | + } |
| 251 | + |
| 252 | + public function testShowForm() |
| 253 | + { |
| 254 | + $this->login('glpi', 'glpi'); |
| 255 | + $instance = $this->getItem(ComputerUsageProfile::class); |
| 256 | + ob_start(function ($in) { |
| 257 | + return $in; |
| 258 | + }); |
| 259 | + $instance->showForm($instance->getID()); |
| 260 | + $output = ob_get_clean(); |
| 261 | + |
| 262 | + $crawler = new Crawler($output); |
| 263 | + $name_field = $crawler->filter('input[name="name"]'); |
| 264 | + $this->assertEquals(1, $name_field->count()); |
| 265 | + $start_time_field = $crawler->filter('input[name="time_start"]'); |
| 266 | + $this->assertEquals(1, $start_time_field->count()); |
| 267 | + $end_time_field = $crawler->filter('input[name="time_stop"]'); |
| 268 | + $this->assertEquals(1, $end_time_field->count()); |
| 269 | + for ($i = 1; $i <= 7; $i++) { |
| 270 | + $field = $crawler->filter('input[name="day_' . $i . '"]'); |
| 271 | + // 2 inputs : checked and unchecked, one of them is hidden |
| 272 | + $this->assertEquals(2, $field->count()); |
| 273 | + } |
| 274 | + } |
137 | 275 | } |
0 commit comments