Skip to content

Commit a847457

Browse files
committed
test(ComputerUsageProfile): add tests
1 parent 115aaf7 commit a847457

File tree

3 files changed

+166
-23
lines changed

3 files changed

+166
-23
lines changed

setup.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use Glpi\Plugin\Hooks;
3636
use GlpiPlugin\Carbon\Config;
3737
use GlpiPlugin\Carbon\UsageInfo;
38-
use GlpiPlugin\Carbon\Location;
3938
use GlpiPlugin\Carbon\Profile;
4039
use GlpiPlugin\Carbon\Report;
4140
use Location as GlpiLocation;

src/ComputerUsageProfile.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ public static function canView(): bool
5757
return Entity::canView();
5858
}
5959

60-
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
61-
{
62-
$env = new self();
63-
/** @var \CommonDBTM $item */
64-
$found_env = $env->find([static::getForeignKeyField() => $item->getID()]);
65-
$nb = $_SESSION['glpishow_count_on_tabs'] ? count($found_env) : 0;
66-
return self::createTabEntry(self::getTypeName($nb), $nb);
67-
}
68-
6960
public function showForm($ID, array $options = [])
7061
{
7162
$this->initForm($ID, $options);
@@ -238,14 +229,26 @@ public static function processMassiveActionsForOneItemtype(MassiveAction $ma, Co
238229
switch ($ma->getAction()) {
239230
case 'MassAssociateItems':
240231
$usage_profile_fk = ComputerUsageProfile::getForeignKeyField();
241-
$usage_profile_id = $ma->POST[$usage_profile_fk];
232+
$usage_profile_id = (int) $ma->POST[$usage_profile_fk];
242233
foreach ($ids as $id) {
243-
if ($item->getFromDB($id) && self::assignToItem($item, $usage_profile_id)) {
244-
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
245-
} else {
246-
// Example of ko count
234+
$usage_profile = self::getById($usage_profile_id);
235+
if ($usage_profile === false) {
236+
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
237+
continue;
238+
}
239+
240+
$computer = GlpiComputer::getById($id);
241+
if ($computer === false) {
247242
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
243+
continue;
248244
}
245+
246+
if (!$usage_profile->assignToItem($computer)) {
247+
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
248+
continue;
249+
}
250+
251+
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
249252
}
250253
return;
251254
}
@@ -255,14 +258,16 @@ public static function processMassiveActionsForOneItemtype(MassiveAction $ma, Co
255258
* Assign an usage profile to an item
256259
*
257260
* @param CommonDBTM $item A computer to assign to
258-
* @param integer $usage_profile_id usage profile to assign
259261
* @return bool
260262
*/
261-
public static function assignToItem(CommonDBTM $item, int $usage_profile_id): bool
263+
public function assignToItem(CommonDBTM $item): bool
262264
{
265+
if ($item->getType() !== GlpiComputer::class) {
266+
return false;
267+
}
263268
$usage_info = new UsageInfo();
264269
$computers_id = $item->getID();
265-
$usage_profile_fk = ComputerUsageProfile::getForeignKeyField();
270+
$usage_profile_fk = self::getForeignKeyField();
266271
$usage_info->getFromDBByCrit([
267272
'itemtype' => GlpiComputer::class,
268273
'items_id' => $computers_id,
@@ -271,14 +276,15 @@ public static function assignToItem(CommonDBTM $item, int $usage_profile_id): bo
271276
$usage_info->add([
272277
'itemtype' => GlpiComputer::class,
273278
'items_id' => $computers_id,
274-
$usage_profile_fk => $usage_profile_id,
279+
$usage_profile_fk => $this->getID(),
275280
]);
276-
return true;
281+
/** @phpstan-ignore booleanNot.alwaysFalse */
282+
return !$usage_info->isNewItem();
277283
}
278284

279285
return $usage_info->update([
280286
'id' => $usage_info->getID(),
281-
$usage_profile_fk => $usage_profile_id,
287+
$usage_profile_fk => $this->getID(),
282288
]);
283289
}
284290
}

tests/units/ComputerUsageProfileTest.php

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,31 @@
3232

3333
namespace GlpiPlugin\Carbon\Tests;
3434

35+
use CommonDBTM;
3536
use Computer as GlpiComputer;
3637
use GlpiPlugin\Carbon\ComputerUsageProfile;
3738
use GlpiPlugin\Carbon\Tests\DbTestCase;
39+
use MassiveAction;
40+
use Symfony\Component\DomCrawler\Crawler;
41+
use Ticket;
3842

3943
class ComputerUsageProfileTest extends DbTestCase
4044
{
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+
4160
/**
4261
* @covers GlpiPlugin\Carbon\ComputerUsageProfile::prepareInputForAdd
4362
* @covers GlpiPlugin\Carbon\ComputerUsageProfile::inputIntegrityCheck
@@ -122,16 +141,135 @@ public function testPrepareInputForUpdate()
122141
$this->assertEquals($expected, $result);
123142
}
124143

144+
/**
145+
* @covers GlpiPlugin\Carbon\ComputerUsageProfile::assignToItem
146+
*
147+
* @return void
148+
*/
125149
public function testAssignToItem()
126150
{
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+
127157
$computer = $this->getItem(GlpiComputer::class, ['name' => 'Test Computer']);
128158
$usage_profile = $this->getItem(ComputerUsageProfile::class, ['name' => 'Test Usage Profile']);
129159

130-
$result = ComputerUsageProfile::assignToItem($computer, $usage_profile->getID());
160+
$result = $usage_profile->assignToItem($computer);
131161
$this->assertTrue($result);
132162

133163
$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);
135165
$this->assertTrue($result);
136166
}
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+
}
137275
}

0 commit comments

Comments
 (0)