Skip to content

Commit de50efe

Browse files
committed
add unit tests
1 parent 725e530 commit de50efe

File tree

9 files changed

+178
-31
lines changed

9 files changed

+178
-31
lines changed

phpunit/abstracts/AbstractCommonItilObject_ItemTest.php

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
namespace tests\units\Glpi\ContentTemplates\Parameters;
43
/**
54
* ---------------------------------------------------------------------
65
*
@@ -35,15 +34,137 @@
3534

3635
namespace tests\units;
3736

37+
use CommonITILObject;
38+
use CommonItilObject_Item;
39+
use Computer;
40+
use User;
41+
3842
abstract class AbstractCommonItilObject_ItemTest extends \DbTestCase
3943
{
4044
/**
4145
* Return the name of the Rule class this test class tests
42-
* @return string
46+
* @return class-string<CommonItilObject_Item>
4347
*/
4448
protected function getTestedClass(): string
4549
{
4650
$test_class = static::class;
4751
return preg_replace('/Test$/', '', substr(strrchr($test_class, '\\'), 1));
4852
}
53+
54+
public function testGetTabNameForItemITIL(): void
55+
{
56+
$this->login();
57+
$itil_itemtype = $this->getTestedClass()::$itemtype_1;
58+
$link = new ($this->getTestedClass())();
59+
60+
$itil_item = $this->createItem($itil_itemtype, [
61+
'name' => __FUNCTION__,
62+
'content' => 'test',
63+
'entities_id' => $this->getTestRootEntity(true),
64+
], ['content']);
65+
66+
$this->createItem($this->getTestedClass(), [
67+
$itil_itemtype::getForeignKeyField() => $itil_item->getID(),
68+
'itemtype' => Computer::class,
69+
'items_id' => getItemByTypeName(Computer::class, '_test_pc01', true),
70+
]);
71+
$this->assertEquals(
72+
'<span class="d-flex align-items-center"><i class="ti ti-package me-2"></i>Items <span class="badge glpi-badge">1</span></span>',
73+
$link->getTabNameForItem($itil_item),
74+
);
75+
76+
$this->createItem($this->getTestedClass(), [
77+
$itil_itemtype::getForeignKeyField() => $itil_item->getID(),
78+
'itemtype' => Computer::class,
79+
'items_id' => getItemByTypeName(Computer::class, '_test_pc02', true),
80+
]);
81+
$this->assertEquals(
82+
'<span class="d-flex align-items-center"><i class="ti ti-package me-2"></i>Items <span class="badge glpi-badge">2</span></span>',
83+
$link->getTabNameForItem($itil_item),
84+
);
85+
}
86+
87+
public function testGetTabNameForItemUser(): void
88+
{
89+
if (!is_subclass_of($this->getTestedClass()::$itemtype_1, CommonITILObject::class)) {
90+
$this->markTestSkipped('This test is only for ITIL items');
91+
}
92+
$this->login();
93+
$itil_itemtype = $this->getTestedClass()::$itemtype_1;
94+
$link = new ($this->getTestedClass())();
95+
96+
$user = getItemByTypeName(User::class, TU_USER);
97+
$tab_label = $link->getTabNameForItem($user);
98+
$this->assertStringContainsString(
99+
$itil_itemtype::getTypeName(\Session::getPluralNumber()),
100+
$tab_label,
101+
);
102+
// Extract count from the inside the .badge element in the label
103+
$original_tab_count = (int) preg_replace(
104+
'/.*<span class="badge glpi-badge">(\d+)<\/span>.*/',
105+
'$1',
106+
$tab_label,
107+
);
108+
109+
$this->createItem($itil_itemtype, [
110+
'name' => __FUNCTION__,
111+
'content' => 'test',
112+
'entities_id' => $this->getTestRootEntity(true),
113+
'_users_id_assign' => $user->getID(),
114+
], ['content']);
115+
$this->assertEquals(
116+
$original_tab_count + 1,
117+
(int) preg_replace(
118+
'/.*<span class="badge glpi-badge">(\d+)<\/span>.*/',
119+
'$1',
120+
$link->getTabNameForItem($user),
121+
),
122+
);
123+
124+
$this->createItem($itil_itemtype, [
125+
'name' => __FUNCTION__,
126+
'content' => 'test',
127+
'entities_id' => $this->getTestRootEntity(true),
128+
'_users_id_assign' => 0,
129+
'_users_id_requester' => $user->getID(),
130+
], ['content']);
131+
$this->assertEquals(
132+
$original_tab_count + 2,
133+
(int) preg_replace(
134+
'/.*<span class="badge glpi-badge">(\d+)<\/span>.*/',
135+
'$1',
136+
$link->getTabNameForItem($user),
137+
),
138+
);
139+
}
140+
141+
public function getGetTabNameForItemAsset(): void
142+
{
143+
$this->login();
144+
$itil_itemtype = $this->getTestedClass()::$itemtype_1;
145+
$link = new ($this->getTestedClass())();
146+
147+
148+
$computer = $this->createItem(Computer::class, [
149+
'name' => __FUNCTION__,
150+
'entities_id' => $this->getTestRootEntity(true),
151+
]);
152+
153+
// Link new computer with a new ITIL
154+
$this->createItem($itil_itemtype, [
155+
'name' => __FUNCTION__,
156+
'content' => 'test',
157+
'entities_id' => $this->getTestRootEntity(true),
158+
'items_id' => [Computer::class => [$computer->getID()]],
159+
], ['content', 'items_id']);
160+
161+
$this->assertEquals(
162+
1,
163+
(int) preg_replace(
164+
'/.*<span class="badge glpi-badge">(\d+)<\/span>.*/',
165+
'$1',
166+
$link->getTabNameForItem($computer),
167+
),
168+
);
169+
}
49170
}

phpunit/functional/Change_ItemTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,4 @@
3434

3535
namespace tests\units;
3636

37-
class Change_ItemTest extends AbstractCommonItilObject_ItemTest
38-
{
39-
40-
}
37+
class Change_ItemTest extends AbstractCommonItilObject_ItemTest {}

phpunit/functional/Item_ProblemTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,4 @@
3434

3535
namespace tests\units;
3636

37-
class Item_ProblemTest extends AbstractCommonItilObject_ItemTest
38-
{
39-
40-
}
37+
class Item_ProblemTest extends AbstractCommonItilObject_ItemTest {}

phpunit/functional/Item_TicketRecurrentTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,4 @@
3434

3535
namespace tests\units;
3636

37-
class Item_TicketRecurrentTest extends AbstractCommonItilObject_ItemTest
38-
{
39-
40-
}
37+
class Item_TicketRecurrentTest extends AbstractCommonItilObject_ItemTest {}

phpunit/functional/ItemTicket_Test.php renamed to phpunit/functional/Item_TicketTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,38 @@
3434

3535
namespace tests\units;
3636

37-
class ItemTicket_Test extends AbstractCommonItilObject_ItemTest
37+
use Computer;
38+
use Ticket;
39+
use TicketCost;
40+
41+
class Item_TicketTest extends AbstractCommonItilObject_ItemTest
3842
{
3943

44+
public function testUpdateItemTCO(): void
45+
{
46+
$this->login();
47+
48+
$computer = $this->createItem(Computer::class, [
49+
'name' => __FUNCTION__,
50+
'entities_id' => $this->getTestRootEntity(true),
51+
]);
52+
53+
// Link new computer with a new ticket
54+
$ticket = $this->createItem(Ticket::class, [
55+
'name' => __FUNCTION__,
56+
'content' => 'test',
57+
'entities_id' => $this->getTestRootEntity(true),
58+
'items_id' => ['Computer' => [$computer->getID()]],
59+
], ['content', 'items_id']);
60+
61+
// Add a cost to the ITIL
62+
$this->createItem(TicketCost::class, [
63+
'tickets_id' => $ticket->getID(),
64+
'cost_fixed' => 100,
65+
]);
66+
67+
// Check that the cost is considered in the TCO
68+
$computer->getFromDB($computer->getID());
69+
$this->assertEquals(100, $computer->fields['ticket_tco']);
70+
}
4071
}

src/Change.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ public static function showListForItem(CommonDBTM $item, $withtemplate = 0)
835835
}
836836

837837
$options = [
838-
'metacriteria' => []
838+
'metacriteria' => [],
839839
];
840840

841841
switch (get_class($item)) {

src/CommonItilObject_Item.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,19 @@ public function prepareInputForAdd($input)
132132
'WHERE' => [
133133
static::$items_id_1 => $input[static::$items_id_1],
134134
static::$itemtype_2 => $input[static::$itemtype_2],
135-
static::$items_id_2 => $input[static::$items_id_2]
135+
static::$items_id_2 => $input[static::$items_id_2],
136136
],
137-
'LIMIT' => 1
137+
'LIMIT' => 1,
138138
]
139139
) > 0
140140
) {
141141
return false;
142142
}
143143

144+
if (!is_subclass_of(static::$itemtype_1, CommonITILObject::class)) {
145+
return parent::prepareInputForAdd($input);
146+
}
147+
144148
/** @var CommonITILObject $itil */
145149
$itil = new static::$itemtype_1();
146150
$item = getItemForItemtype($input["itemtype"]);
@@ -362,11 +366,11 @@ public static function showItemToAdd($object_id, $itemtype, $items_id, $options)
362366
/**
363367
* Print the HTML array for Items linked to a ITIL object
364368
*
365-
* @param CommonITILObject $obj
369+
* @param CommonITILObject|TicketRecurrent $obj
366370
*
367371
* @return bool|void
368372
**/
369-
protected static function showForObject(CommonITILObject $obj)
373+
protected static function showForObject(CommonITILObject|TicketRecurrent $obj)
370374
{
371375
if (!($obj instanceof static::$itemtype_1)) {
372376
return false;
@@ -614,7 +618,7 @@ protected static function countForActor(CommonDBTM $item): int
614618
'FROM' => $link_table,
615619
'WHERE' => [
616620
$item->getForeignKeyField() => $item->fields['id'],
617-
]
621+
],
618622
])->current();
619623
return $result['cpt'] ?? 0;
620624
}
@@ -662,7 +666,7 @@ public static function showListForItem(CommonDBTM $item, $withtemplate = 0, $opt
662666
'searchtype' => 'equals',
663667
'value' => $item->getID(),
664668
'link' => 'AND',
665-
]
669+
],
666670
],
667671
'reset' => 'reset',
668672
];

src/Problem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ public static function showListForItem(CommonDBTM $item, $withtemplate = 0)
13391339
}
13401340

13411341
$options = [
1342-
'metacriteria' => []
1342+
'metacriteria' => [],
13431343
];
13441344

13451345
switch (get_class($item)) {

src/Ticket.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ class Ticket extends CommonITILObject
7676
// Demand type
7777
public const DEMAND_TYPE = 2;
7878

79-
const READGROUP = 2048;
80-
const READASSIGN = 4096;
81-
const ASSIGN = 8192;
82-
const STEAL = 16384;
83-
const OWN = 32768;
84-
const CHANGEPRIORITY = 65536;
85-
const READNEWTICKET = 262144;
79+
public const READGROUP = 2048;
80+
public const READASSIGN = 4096;
81+
public const ASSIGN = 8192;
82+
public const STEAL = 16384;
83+
public const OWN = 32768;
84+
public const CHANGEPRIORITY = 65536;
85+
public const READNEWTICKET = 262144;
8686

8787
#[Override]
8888
public static function supportHelpdeskDisplayPreferences(): bool
@@ -4783,7 +4783,7 @@ public static function showListForItem(CommonDBTM $item, $withtemplate = 0)
47834783
}
47844784

47854785
$options = [
4786-
'metacriteria' => []
4786+
'metacriteria' => [],
47874787
];
47884788

47894789
switch (get_class($item)) {

0 commit comments

Comments
 (0)