Skip to content

Commit 6def124

Browse files
committed
test(itil): Ensure readonly logic is enforce in backend
1 parent ace94c0 commit 6def124

File tree

5 files changed

+166
-72
lines changed

5 files changed

+166
-72
lines changed

src/CommonITILObject.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
use function Safe\preg_match;
6363
use function Safe\preg_replace;
6464
use function Safe\strtotime;
65-
6665
/**
6766
* CommonITILObject Class
6867
*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace functional;
4+
5+
use Glpi\Tests\AbstractITILTemplateReadonlyFieldTest;
6+
use Change;
7+
8+
class ChangeITILTemplateReadonlyFieldTest extends AbstractITILTemplateReadonlyFieldTest
9+
{
10+
public function getITILClass(): Change
11+
{
12+
return new Change();
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace functional;
4+
5+
use Glpi\Tests\AbstractITILTemplateReadonlyFieldTest;
6+
use Problem;
7+
8+
class ProblemITILTemplateReadonlyFieldTest extends AbstractITILTemplateReadonlyFieldTest
9+
{
10+
public function getITILClass(): Problem
11+
{
12+
return new Problem();
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace functional;
4+
5+
use Glpi\Tests\AbstractITILTemplateReadonlyFieldTest;
6+
use Ticket;
7+
8+
class TicketITILTemplateReadonlyFieldTest extends AbstractITILTemplateReadonlyFieldTest
9+
{
10+
public function getITILClass(): Ticket
11+
{
12+
return new Ticket();
13+
}
14+
}

tests/src/AbstractITILTemplateReadonlyFieldTest.php

Lines changed: 124 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
use CommonITILObject;
66
use DbTestCase;
7+
use Glpi\Urgency;
78
use ITILCategory;
9+
use ITILTemplate;
10+
use ITILTemplatePredefinedField;
11+
use ITILTemplateReadonlyField;
812
use Ticket; // For type constants
913

1014
abstract class AbstractITILTemplateReadonlyFieldTest extends DbTestCase
@@ -21,39 +25,71 @@ abstract public function getITILClass(): CommonITILObject;
2125
*/
2226
protected function createTemplateAndCategory(array $readonly = [], array $predefined = []): int
2327
{
24-
$itil_class = $this->getITILClass();
25-
$templateClass = $itil_class::getTemplateClass();
26-
dump($templateClass);
27-
$template = new $templateClass();
28+
$itil_object = $this->getITILClass();
29+
$itil_type = $itil_object->getType();
2830

29-
// ITILTemplate::add expects `_readonly` to be an array with field names as keys and 1 as value.
30-
$readonly_input = array_fill_keys($readonly, 1);
31+
// Create Template
32+
$template_class = $itil_type . 'Template';
33+
$template = new $template_class();
34+
$this->assertInstanceOf(ITILTemplate::class, $template);
3135

3236
$template_input = [
3337
'name' => 'test_template_' . mt_rand(),
3438
'is_active' => 1,
3539
'entities_id' => 0,
3640
'is_recursive' => 1,
37-
'_readonly' => $readonly_input,
38-
'_predefined' => $predefined,
39-
'_mandatory' => [],
40-
'_hidden' => [],
4141
];
42-
4342
$template_id = $template->add($template_input);
4443
$this->assertGreaterThan(0, $template_id, 'Template creation failed');
4544

45+
// Add Readonly Fields
46+
if (!empty($readonly)) {
47+
$readonly_field_class = $itil_type . 'TemplateReadonlyField';
48+
$readonly_field = new $readonly_field_class();
49+
$this->assertInstanceOf(ITILTemplateReadonlyField::class, $readonly_field);
50+
51+
$foreign_key_field = $readonly_field::$items_id;
52+
53+
foreach ($readonly as $field_name) {
54+
$result = $readonly_field->add([
55+
$foreign_key_field => $template_id,
56+
'num' => $this->getIdFromSearchOptions($field_name),
57+
]);
58+
$this->assertNotFalse($result, "Failed to add readonly field '$field_name'");
59+
}
60+
}
61+
62+
// Add Predefined Fields
63+
if (!empty($predefined)) {
64+
$predefined_field_class = $itil_type . 'TemplatePredefinedField';
65+
$predefined_field = new $predefined_field_class();
66+
$this->assertInstanceOf(ITILTemplatePredefinedField::class, $predefined_field);
67+
68+
$foreign_key_field = $predefined_field::$items_id;
69+
70+
foreach ($predefined as $field_name => $field_value) {
71+
$result = $predefined_field->add([
72+
$foreign_key_field => $template_id,
73+
'num' => $this->getIdFromSearchOptions($field_name),
74+
'value' => $field_value,
75+
]);
76+
$this->assertNotFalse($result, "Failed to add predefined field '$field_name'");
77+
}
78+
}
79+
80+
// Create Category and associate template
4681
$category = new ITILCategory();
4782
$cat_id = $category->add(['name' => 'test_cat_' . mt_rand(), 'entities_id' => 0, 'is_recursive' => 1]);
4883
$this->assertGreaterThan(0, $cat_id, 'Category creation failed');
4984

5085
$type = null;
51-
if ($itil_class instanceof Ticket) {
86+
if ($itil_object instanceof Ticket) {
5287
$type = Ticket::INCIDENT_TYPE;
5388
} else {
54-
$type = true; // For Change and Problem
89+
// For Change and Problem, the template is not type-specific in the same way.
90+
$type = true;
5591
}
56-
$template_field_name = $itil_class->getTemplateFieldName($type);
92+
$template_field_name = $itil_object->getTemplateFieldName($type);
5793

5894
$category->update([
5995
'id' => $cat_id,
@@ -63,122 +99,139 @@ protected function createTemplateAndCategory(array $readonly = [], array $predef
6399
return $cat_id;
64100
}
65101

102+
protected function getIdFromSearchOptions(string $field): ?string
103+
{
104+
$item = $this->getITILClass();
105+
foreach ($item->getSearchOptionsMain() as $option) {
106+
if (isset($option['field']) && $option['field'] === $field) {
107+
return (string) $option['id'];
108+
}
109+
}
110+
return null;
111+
}
112+
113+
public function setUp(): void
114+
{
115+
parent::setUp();
116+
$this->login();
117+
}
118+
119+
66120
public function testHandleReadonlyFieldsOnAddWithPredefined(): void
67121
{
68-
$cat_id = $this->createTemplateAndCategory(
69-
['name'], // readonly
70-
['name' => 'Predefined Name Value'] // predefined
71-
);
122+
$cat_id = $this->createTemplateAndCategory(['urgency'], ['urgency' => Urgency::HIGH->value]);
72123

73-
$itilObject = $this->getITILClass();
124+
$itil_object = $this->getITILClass();
74125
$input = [
75-
'name' => 'User Input Name',
76-
'content' => 'Some content',
126+
'urgency' => Urgency::LOW->value,
127+
'name' => 'Some content',
77128
'status' => CommonITILObject::INCOMING,
78129
'itilcategories_id' => $cat_id,
79130
'entities_id' => 0,
80131
];
81-
if ($itilObject instanceof Ticket) {
132+
if ($itil_object instanceof Ticket) {
82133
$input['type'] = Ticket::INCIDENT_TYPE;
83134
}
84135

85-
$processedInput = $itilObject->prepareInputForAdd($input);
136+
$processed_input = $itil_object->prepareInputForAdd($input);
86137

87-
$this->assertEquals('Predefined Name Value', $processedInput['name']);
88-
$this->assertEquals('Some content', $processedInput['content']);
138+
$this->assertEquals(Urgency::HIGH->value, $processed_input['urgency']);
139+
$this->assertEquals('Some content', $processed_input['name']);
89140
}
90141

91142
public function testHandleReadonlyFieldsOnAddWithoutPredefined(): void
92143
{
93-
$cat_id = $this->createTemplateAndCategory(
94-
['name'] // readonly
95-
);
144+
$cat_id = $this->createTemplateAndCategory(['urgency']);
96145

97-
$itilObject = $this->getITILClass();
146+
$itil_object = $this->getITILClass();
98147
$input = [
99-
'name' => 'User Input Name',
100-
'content' => 'Some content',
148+
'urgency' => Urgency::LOW->value,
149+
'name' => 'Some content',
101150
'status' => CommonITILObject::INCOMING,
102151
'itilcategories_id' => $cat_id,
103152
'entities_id' => 0,
104153
];
105-
if ($itilObject instanceof Ticket) {
154+
if ($itil_object instanceof Ticket) {
106155
$input['type'] = Ticket::INCIDENT_TYPE;
107156
}
108157

109-
$processedInput = $itilObject->prepareInputForAdd($input);
158+
$processed_input = $itil_object->prepareInputForAdd($input);
110159

111-
$this->assertEmpty($processedInput['name']);
112-
$this->assertEquals('Some content', $processedInput['content']);
160+
$this->assertEquals(Urgency::MEDIUM->value, $processed_input['urgency']); // Default value
161+
$this->assertEquals('Some content', $processed_input['name']);
113162
}
114163

115164
public function testHandleReadonlyFieldsOnUpdateWithExistingValue(): void
116165
{
117-
$cat_id = $this->createTemplateAndCategory(
118-
['name'] // readonly
119-
);
120-
121-
$itilObject = $this->getITILClass();
122-
$item_id = $itilObject->add([
123-
'name' => 'Existing Name Value',
124-
'content' => 'Initial content',
166+
$cat_id = $this->createTemplateAndCategory(['urgency']);
167+
168+
$itil_object = $this->getITILClass();
169+
$add_input = [
170+
'name' => 'Initial content',
125171
'status' => CommonITILObject::ASSIGNED,
126172
'itilcategories_id' => $cat_id,
127173
'entities_id' => 0,
128-
]);
174+
];
175+
if ($itil_object instanceof Ticket) {
176+
$add_input['type'] = Ticket::INCIDENT_TYPE;
177+
}
178+
$item_id = $itil_object->add($add_input);
179+
$this->assertGreaterThan(0, $item_id);
129180

130-
$itilObject->getFromDB($item_id);
181+
$itil_object->getFromDB($item_id);
131182

132-
$input = [
183+
$update_input = [
133184
'id' => $item_id,
134-
'name' => 'User Attempted New Name',
135-
'content' => 'Updated content',
185+
'urgency' => Urgency::HIGH->value,
186+
'name' => 'Updated content',
136187
'status' => CommonITILObject::ASSIGNED,
137188
'itilcategories_id' => $cat_id,
138189
'entities_id' => 0,
139190
];
140-
if ($itilObject instanceof Ticket) {
141-
$input['type'] = Ticket::INCIDENT_TYPE;
191+
if ($itil_object instanceof Ticket) {
192+
$update_input['type'] = Ticket::INCIDENT_TYPE;
142193
}
143194

144-
$processedInput = $itilObject->prepareInputForUpdate($input);
195+
$processed_input = $itil_object->prepareInputForUpdate($update_input);
145196

146-
$this->assertEquals('Existing Name Value', $processedInput['name']);
147-
$this->assertEquals('Updated content', $processedInput['content']);
197+
$this->assertEquals(Urgency::MEDIUM->value, $processed_input['urgency']);
198+
$this->assertEquals('Updated content', $processed_input['name']);
148199
}
149200

150201
public function testHandleReadonlyFieldsOnUpdateWithoutExistingValue(): void
151202
{
152-
$cat_id = $this->createTemplateAndCategory(
153-
['name'] // readonly
154-
);
155-
156-
$itilObject = $this->getITILClass();
157-
$item_id = $itilObject->add([
158-
'name' => '', // No initial name
159-
'content' => 'Initial content',
203+
$cat_id = $this->createTemplateAndCategory(['urgency']);
204+
205+
$itil_object = $this->getITILClass();
206+
$add_input = [
207+
'name' => 'Initial content',
160208
'status' => CommonITILObject::ASSIGNED,
161209
'itilcategories_id' => $cat_id,
162210
'entities_id' => 0,
163-
]);
211+
];
212+
if ($itil_object instanceof Ticket) {
213+
$add_input['type'] = Ticket::INCIDENT_TYPE;
214+
}
215+
$item_id = $itil_object->add($add_input);
216+
$this->assertGreaterThan(0, $item_id);
164217

165-
$itilObject->getFromDB($item_id);
218+
$itil_object->getFromDB($item_id);
166219

167-
$input = [
220+
$update_input = [
168221
'id' => $item_id,
169-
'name' => 'User Attempted New Name',
170-
'content' => 'Updated content',
222+
'urgency' => Urgency::LOW->value,
223+
'name' => 'Updated content',
171224
'status' => CommonITILObject::ASSIGNED,
172225
'itilcategories_id' => $cat_id,
173226
'entities_id' => 0,
174227
];
175-
if ($itilObject instanceof Ticket) {
176-
$input['type'] = Ticket::INCIDENT_TYPE;
228+
if ($itil_object instanceof Ticket) {
229+
$update_input['type'] = Ticket::INCIDENT_TYPE;
177230
}
178231

179-
$processedInput = $itilObject->prepareInputForUpdate($input);
232+
$processed_input = $itil_object->prepareInputForUpdate($update_input);
180233

181-
$this->assertArrayNotHasKey('name', $processedInput);
182-
$this->assertEquals('Updated content', $processedInput['content']);
234+
$this->assertEquals(Urgency::MEDIUM->value, $processed_input['urgency']); // Default value
235+
$this->assertEquals('Updated content', $processed_input['name']);
183236
}
184237
}

0 commit comments

Comments
 (0)