Skip to content

Commit 2e83385

Browse files
committed
test(itil): Add cypress test on the readonly field logic
1 parent 8a9438d commit 2e83385

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

src/CommonITILObject.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,6 @@ public function enforceReadonlyFields(array $input, bool $isAdd = false): array
23452345
{
23462346
$tt = $this->getITILTemplateFromInput($input);
23472347
if (!$tt) {
2348-
dump('Template not found');
23492348
return $input;
23502349
}
23512350

@@ -8300,7 +8299,7 @@ public function getITILTemplateFromInput(array $input = []): ?ITILTemplate
83008299
$type = $this->fields['type'];
83018300
}
83028301

8303-
$categid = $input['itilcategories_id'] ?? $this->fields['itilcategories_id'];
8302+
$categid = $input['itilcategories_id'] ?? $this->fields['itilcategories_id'] ?? null;
83048303
if (is_null($categid)) {
83058304
return null;
83068305
}

tests/cypress/e2e/ITILObject/ticket_form.cy.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,52 @@ describe("Ticket Form", () => {
334334
cy.findByRole('cell').should('contain.text', 'No results found');
335335
});
336336
});
337+
338+
it('Create/update a ticket using a template with readonly fields', () => {
339+
const ticket_template_name = `test template ${rand}`;
340+
cy.createWithAPI('TicketTemplate', {
341+
'name': ticket_template_name,
342+
}).as('ticket_template_id');
343+
344+
cy.get('@ticket_template_id').then((ticket_template_id) => {
345+
cy.createWithAPI('TicketTemplatePredefinedField', {
346+
'tickettemplates_id': ticket_template_id, // Default template
347+
'num': 10, // Urgency
348+
'value': 4, // High
349+
});
350+
351+
cy.createWithAPI('TicketTemplateReadonlyField', {
352+
'tickettemplates_id': ticket_template_id,
353+
'num': 10,
354+
});
355+
356+
cy.createWithAPI('ITILCategory', {
357+
'name':ticket_template_name,
358+
'tickettemplates_id': ticket_template_id,
359+
'tickettemplates_id_incident': ticket_template_id,
360+
'tickettemplates_id_demand': ticket_template_id,
361+
'changetemplates_id': ticket_template_id,
362+
'problemtemplates_id': ticket_template_id,
363+
});
364+
});
365+
366+
// Create form
367+
cy.visit(`/front/ticket.form.php`);
368+
369+
// intercept form submit
370+
cy.intercept('POST', '/front/ticket.form.php').as('submit');
371+
372+
cy.getDropdownByLabelText('Category').selectDropdownValue(${ticket_template_name}`);
373+
374+
// We change the value of a readonly field, it should be ignored
375+
cy.get('input[name="urgency"]').invoke('val', '1');
376+
cy.findByRole('button', {'name': 'Add'}).click();
377+
cy.wait('@submit').its('response.statusCode').should('eq', 200);
378+
cy.get('input[name="urgency"]').should('have.value', '4'); // Should be the template 4 value
379+
380+
// We try updating it
381+
cy.get('input[name="urgency"]').invoke('val', '1');
382+
cy.findByRole('button', {'name': 'Save'}).click();
383+
cy.get('input[name="urgency"]').should('have.value', '4'); // Should be the template 4 value
384+
});
337385
});

tests/src/AbstractITILTemplateReadonlyFieldTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ public function setUp(): void
148148
$this->login();
149149
}
150150

151+
public function testHandleReadonlyFieldsWithNoTemplate(): void
152+
{
153+
$itil_object = $this->getITILClass();
154+
$input = [
155+
'urgency' => Urgency::LOW->value,
156+
'name' => 'Some content',
157+
'status' => CommonITILObject::INCOMING,
158+
'entities_id' => 0,
159+
];
160+
if ($itil_object instanceof Ticket) {
161+
$input['type'] = Ticket::INCIDENT_TYPE;
162+
}
163+
164+
$processed_input = $itil_object->enforceReadonlyFields($input, true);
165+
166+
$this->assertEquals(Urgency::LOW->value, $processed_input['urgency']);
167+
$this->assertEquals('Some content', $processed_input['name']);
168+
}
151169

152170
public function testHandleReadonlyFieldsOnAddWithPredefined(): void
153171
{

0 commit comments

Comments
 (0)