Skip to content

Commit 82524f3

Browse files
committed
fix test and implemenation
I will rewrite history (I guess). Some tests still fail, that's intended, their purpose is not fixed.
1 parent 05a41e7 commit 82524f3

File tree

9 files changed

+122
-84
lines changed

9 files changed

+122
-84
lines changed

phpunit/functional/ITILTemplateTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,12 @@ public static function fieldsProvider()
227227
9 => 'Request source',
228228
37 => 'SLA Time to own',
229229
30 => 'SLA Time to resolve',
230-
190 => 'OLA Internal time to own',
231-
191 => 'OLA Internal time to resolve',
230+
// fields removed in ticket, should be replaced ?
231+
// 190 => 'OLA Internal time to own',
232+
// 191 => 'OLA Internal time to resolve',
232233
155 => 'Time to own',
233-
180 => 'Internal time to resolve',
234-
185 => 'Internal time to own',
234+
// 180 => 'Internal time to resolve',
235+
// 185 => 'Internal time to own',
235236
193 => 'Contract',
236237
],
237238
], [

phpunit/functional/OLATest.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -374,26 +374,13 @@ public function testOlaTtoStartsWhenOlaIsAssignedAtUpdate(): void
374374
$this->assertEquals($due_time_datetime->format('Y-m-d H:i:s'), $ola['due_time']);
375375
}
376376

377-
public function testOlaDueTimeIsDelayedWhileTicketStatusIsWaiting()
377+
public function testOlaTTRDueTimeIsDelayedWhileTicketStatusIsWaiting()
378378
{
379379
$this->login();
380-
// @todoseb quelle est la logique technique actuelle ?
381-
// - chercher les occurences de
382-
// x la variable qui contient le prefix internal_
383-
// x 2 occurences de internal_
384-
// - LevelAgreement::$prefixticket -> getFieldNames() : exception dans la méthode dans OLA
385-
// x occurence des 2 variables qui le contiennent :
386-
// - internal_time_to_resolve
387-
// - dans les tests
388-
// - dans le code
389-
// - noter dans ma liste de suppression que fait
390-
// - mise à jour dans preUpdateInDb() en fonction du changement de statut
391-
// - autre chose
392-
393-
// @todoseb implémenter/reprendre
380+
394381
// arrange create ticket with OLA at 09:00:00, status WAITING
395382
$this->setCurrentTime('09:00:00');
396-
['ola' => $ola ] = $this->createOLA();
383+
['ola' => $ola ] = $this->createOLA(ola_type: SLM::TTR);
397384
$ticket = $this->createItem(Ticket::class, ['_la_update' => true, '_olas_id' => [$ola->getID()]] + $this->getValidTicketData());
398385
assert(\CommonITILObject::WAITING === (int) $ticket->fields['status']);
399386
$initial_due_time = $ticket->getOlasData()[0]['due_time'];
@@ -410,6 +397,29 @@ public function testOlaDueTimeIsDelayedWhileTicketStatusIsWaiting()
410397
);
411398
}
412399

400+
public function testOlaTTODueTimeIsNotDelayedWhileTicketStatusIsWaiting()
401+
{
402+
$this->login();
403+
404+
// arrange create ticket with OLA at 09:00:00, status WAITING
405+
$this->setCurrentTime('09:00:00');
406+
['ola' => $ola ] = $this->createOLA(ola_type: SLM::TTO);
407+
$ticket = $this->createItem(Ticket::class, ['_la_update' => true, '_olas_id' => [$ola->getID()]] + $this->getValidTicketData());
408+
assert(\CommonITILObject::WAITING === (int) $ticket->fields['status']);
409+
$initial_due_time = $ticket->getOlasData()[0]['due_time'];
410+
411+
// act : wait one hour and change status to trigger due_time recomputing
412+
$this->setCurrentTime('10:00:00');
413+
$this->updateItem($ticket::class, $ticket->getID(), ['status' => \CommonITILObject::ASSIGNED]);
414+
$new_due_time = $ticket->getOlasData()[0]['due_time'];
415+
416+
$this->assertEquals(
417+
(new \DateTime($initial_due_time))->format('Y-m-d H:i:s'),
418+
$new_due_time,
419+
'Le temps d\'échéance (due time) devrait être retardé d\'une heure après passage du ticket de WAITING à un autre statut'
420+
);
421+
}
422+
413423
public function testOlaTTRWaitingTimeIsIncrementedWhileTicketStatusIsWaiting()
414424
{
415425
$this->login();

phpunit/functional/RuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function testMaxActionsCount()
273273
$this->assertSame(1, $rule->maxActionsCount());
274274

275275
$rule = new \RuleTicket();
276-
$this->assertSame(45, $rule->maxActionsCount());
276+
$this->assertSame(44, $rule->maxActionsCount());
277277

278278
$rule = new \RuleDictionnarySoftware();
279279
$this->assertSame(7, $rule->maxActionsCount());

phpunit/functional/RuleTicketTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ public function testAssignSingleOla()
14701470
$olas_data = $ticket->getOlasData();
14711471
$this->assertCount(1, $olas_data);
14721472
$ola_data = $olas_data[0];
1473-
$this->assertEquals($ola->getID(), $ola_data['id']);
1473+
$this->assertEquals($ola->getID(), $ola_data['olas_id']);
14741474
}
14751475

14761476
public function testAssignMultipleOlas()
@@ -1495,17 +1495,17 @@ public function testAssignMultipleOlas()
14951495
// assert - ola is associated to ticket - getOlasData()
14961496
$olas_data = $ticket->getOlasData();
14971497
$this->assertCount(3, $olas_data);
1498-
$olas_ids = array_column($olas_data, 'id');
1498+
$olas_ids = array_column($olas_data, 'olas_id');
14991499
$this->assertEqualsCanonicalizing($olas_ids, [$ola_tto->getID(), $ola_ttr1->getID(), $ola_ttr2->getID()]); // assert - ola is associated to ticket - getOlasData()
15001500

15011501
$olas_ttr_data = $ticket->getOlasTTRData();
15021502
$this->assertCount(2, $olas_ttr_data);
1503-
$olas_ids = array_column($olas_ttr_data, 'id');
1503+
$olas_ids = array_column($olas_ttr_data, 'olas_id');
15041504
$this->assertEqualsCanonicalizing($olas_ids, [$ola_ttr1->getID(), $ola_ttr2->getID()]); // assert - ola is associated to ticket - getOlasData()
15051505

15061506
$olas_tto_data = $ticket->getOlasTTOData();
15071507
$this->assertCount(1, $olas_tto_data);
1508-
$olas_ids = array_column($olas_tto_data, 'id');
1508+
$olas_ids = array_column($olas_tto_data, 'olas_id');
15091509
$this->assertEqualsCanonicalizing($olas_ids, [$ola_tto->getID()]);
15101510
}
15111511

phpunit/functional/SLMTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ public function testComputationOnOla(
14191419
);
14201420

14211421
// Create escalation level
1422-
$this->createItem($la->getLevelClass(), [
1422+
$level = $this->createItem($la->getLevelClass(), [
14231423
'name' => 'Test escalation level',
14241424
'execution_time' => $escalation_time,
14251425
'is_active' => 1,
@@ -1455,17 +1455,23 @@ public function testComputationOnOla(
14551455

14561456
$ola_data = $ticket->getOlasData()[0];
14571457
$this->assertEquals($waiting_duration, $ola_data['waiting_time']);
1458-
$this->assertEquals($target_date, $ola_data['due_time'])
1459-
;
1458+
$this->assertEquals($target_date, $ola_data['due_time']);
14601459

14611460
// Check escalation date
14621461
$la_level_class = $la->getLevelTicketClass();
14631462
$la_level_ticket = (new $la_level_class())->find([
14641463
'tickets_id' => $ticket->getID(),
14651464
]);
1466-
$this->assertCount(1, $la_level_ticket);
1467-
$escalation_data = array_pop($la_level_ticket)["date"];
1468-
$this->assertEquals($target_escalation_date, $escalation_data);
1465+
1466+
// no level to execute in case of TTO if there were pauses
1467+
// because pauses makes takeintoaccount_delay_stat be > 0 so levels are not executed
1468+
// @see \OlaLevel_Ticket::doLevelForTicket
1469+
$expected_count = (count($pauses) > 0 && $la_params['type'] === \SLM::TTO) ? 0 : 1;
1470+
$this->assertCount($expected_count, $la_level_ticket);
1471+
if ($expected_count > 0) {
1472+
$escalation_data = array_pop($la_level_ticket)["date"];
1473+
$this->assertEquals($target_escalation_date, $escalation_data);
1474+
}
14691475
}
14701476

14711477
/**
@@ -1763,7 +1769,7 @@ public function testLaChangeOnOLA(): void
17631769
->addAction('assign', 'olas_id', $la1->getID());
17641770
$this->createRule($builder);
17651771

1766-
// First OLA is added on update
1772+
// Second OLA is added on update
17671773
$builder = new RuleBuilder('Add second LA on update', RuleTicket::class);
17681774
$builder->setEntity($entity)
17691775
->setCondtion(RuleTicket::ONUPDATE)
@@ -1780,6 +1786,9 @@ public function testLaChangeOnOLA(): void
17801786
'content' => '',
17811787
]);
17821788

1789+
// ticket just created, two ola should be associated, one for TTO and one for TTR
1790+
$this->assertEquals(2, countElementsInTable(\Item_Ola::getTable(), ['items_id' => $ticket->getID()]), 'ajout de ola sur update ne fonctionne pas, fonctionne sur le add - tester sur le commit 8cb37d09567ba071dc74445cebbc84618072bf57 wip');
1791+
17831792
// Create another ticket as a control subject that shouldn't be impacted
17841793
// by changes on the other ticket
17851794
$control_ticket = $this->createItem(Ticket::class, [
@@ -2210,7 +2219,7 @@ public function testEscalationLevelChangesOlaTto()
22102219
$this->assertEquals(5, $ticket->fields['priority']); // level_1 action is applied
22112220
}
22122221

2213-
// @todo write tests to ensure/document that there is No execution of levels
2222+
// @todo write tests to ensure that there is No execution of levels
22142223
// - when ticket status is CLOSED or CLOSED
22152224
// - when levelAgreement is an TTO and takeintoaccount_delay_stat is > 0
22162225

src/CommonITILObject.php

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,38 +2669,37 @@ public function pre_updateInDB()
26692669
$item_olas = $_item_ola->getByIds(array_column($item_olas_array, 'id'));
26702670
$calendars_id = $this->getCalendar();
26712671

2672+
OLA::deleteLevelsToDo($this); // @todoseb peut-être pas utile
26722673
foreach ($item_olas as $item_ola) {
26732674
$ola = $item_ola->getOla();
26742675

26752676
// OLA TTO is not impacted by waiting time, update only for TTR
2676-
if ($ola->fields['type'] == SLM::TTO) {
2677-
continue;
2678-
}
2677+
if ($ola->fields['type'] === SLM::TTR) {
26792678

2680-
$ola->setTicketCalendar($calendars_id);
2681-
$item_ola_data['id'] = $item_ola->getID();
2679+
$ola->setTicketCalendar($calendars_id);
2680+
$item_ola_data['id'] = $item_ola->getID();
26822681

2683-
// update waiting_time
2684-
if ($ola->fields['type'] == SLM::TTR) {
2685-
$item_ola_data['waiting_time'] = $item_ola->fields['waiting_time'] + $ola->getActiveTimeBetween(
2686-
$this->fields['begin_waiting_date'],
2687-
$_SESSION["glpi_currenttime"]
2688-
);
2689-
} else {
2690-
$item_ola_data['waiting_time'] = 0;
2691-
}
2682+
// update waiting_time
2683+
if ($ola->fields['type'] == SLM::TTR) {
2684+
$item_ola_data['waiting_time'] = $item_ola->fields['waiting_time'] + $ola->getActiveTimeBetween(
2685+
$this->fields['begin_waiting_date'],
2686+
$_SESSION["glpi_currenttime"]
2687+
);
2688+
} else {
2689+
$item_ola_data['waiting_time'] = 0;
2690+
}
26922691

2693-
// update due_time (former internal_time_to_own, internal_time_to_resolve)
2694-
$item_ola_data['due_time'] = $ola->computeDate(
2695-
$item_ola->fields['start_time'],
2696-
$item_ola_data['waiting_time']
2697-
);
2692+
// update due_time (former internal_time_to_own, internal_time_to_resolve)
2693+
$item_ola_data['due_time'] = $ola->computeDate(
2694+
$item_ola->fields['start_time'],
2695+
$item_ola_data['waiting_time']
2696+
);
26982697

2699-
if (!(new Item_Ola())->update($item_ola_data)) {
2700-
throw new \Exception('Failed to update item_ola');
2698+
if (!(new Item_Ola())->update($item_ola_data)) {
2699+
throw new \Exception('Failed to update item_ola');
2700+
}
27012701
}
27022702

2703-
OLA::deleteLevelsToDo($this);
27042703
$this->manageOlaLevel($item_ola->fields['olas_id']);
27052704
}
27062705

@@ -2728,10 +2727,7 @@ public function pre_updateInDB()
27282727
SLA::deleteLevelsToDo($this);
27292728
}
27302729

2731-
if (isset($this->fields['olas_id_ttr']) && ($this->fields['olas_id_ttr'] > 0)) {
2732-
OLA::deleteLevelsToDo($this); // // @todoseb probably useless -> exception below
2733-
// throw new \Exception('These lines must be removed, see above in code');
2734-
}
2730+
OLA::deleteLevelsToDo($this);
27352731
}
27362732

27372733
// solve_delay_stat : use delay between opendate and solvedate

src/OLA.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
**/
4040
class OLA extends LevelAgreement
4141
{
42-
protected static $prefix = 'ola';
42+
protected static $prefix = 'ola';
4343

4444
/**
4545
* Remove OLA associations
@@ -55,8 +55,8 @@ public function cleanDBonPurge()
5555
global $DB;
5656

5757
// Clean levels
58-
$ola_fk = getForeignKeyFieldForItemType(static::class);
59-
$level = new static::$levelclass();
58+
$ola_fk = getForeignKeyFieldForItemType(static::class);
59+
$level = new static::$levelclass();
6060
$level->deleteByCriteria([$ola_fk => $this->getID()]);
6161

6262
// Clean levels todo
@@ -66,9 +66,9 @@ public function cleanDBonPurge()
6666
Rule::cleanForItemAction($this);
6767
}
6868

69-
protected static $prefixticket = 'internal_'; // @todoseb pas utilisé selon phpstorm : voir si on peut vraiment le supprimer
70-
protected static $levelclass = 'OlaLevel';
71-
protected static $levelticketclass = 'OlaLevel_Ticket';
69+
protected static $prefixticket = 'internal_'; // @todoseb pas utilisé selon phpstorm : voir si on peut vraiment le supprimer
70+
protected static $levelclass = 'OlaLevel';
71+
protected static $levelticketclass = 'OlaLevel_Ticket';
7272
protected static $forward_entity_to = ['OlaLevel'];
7373

7474
/**
@@ -89,7 +89,7 @@ public static function getFieldNames($subtype)
8989
* Add an entry in slalevels_tickets | olalevels_tickets table
9090
* The level is set by $levels_id parameter or the current level set in slalevels_id_ttr | olalevels_id_ttr (if set)
9191
*
92-
* @param Ticket $ticket
92+
* @param Ticket $ticket
9393
* @param integer $levels_id
9494
* @param integer $olas_id
9595
*
@@ -117,12 +117,17 @@ public function addLevelToDo(Ticket $ticket, $levels_id, $olas_id)
117117
$waiting_duration
118118
);
119119

120-
if ($date !== null) {
120+
$_ticket = new Ticket();
121+
if (
122+
$_ticket->getFromDB($ticket->getID()) &&
123+
$this->levelCanBeAddedInLevelsTodo($ticket, $ola->fields['type']) &&
124+
$date !== null
125+
) {
121126
$toadd = [];
122-
$toadd['date'] = $date;
127+
$toadd['date'] = $date;
123128
$toadd[$pre . 'levels_id'] = $levels_id;
124-
$toadd['tickets_id'] = $ticket->fields["id"];
125-
$levelticket = new static::$levelticketclass();
129+
$toadd['tickets_id'] = $ticket->fields["id"];
130+
$levelticket = new static::$levelticketclass();
126131
$levelticket->add($toadd);
127132
}
128133
}
@@ -148,8 +153,8 @@ public static function deleteLevelsToDo(Ticket $ticket)
148153
$levelticket = new static::$levelticketclass();
149154
$iterator = $DB->request([
150155
'SELECT' => 'id',
151-
'FROM' => $levelticket::getTable(),
152-
'WHERE' => ['tickets_id' => $ticket->fields['id']],
156+
'FROM' => $levelticket::getTable(),
157+
'WHERE' => ['tickets_id' => $ticket->fields['id']],
153158
]);
154159

155160
foreach ($iterator as $data) {
@@ -194,4 +199,18 @@ public function getAddConfirmation(): array
194199
__("Escalations defined in the OLA will be triggered under this new date."),
195200
];
196201
}
202+
203+
private function levelCanBeAddedInLevelsTodo(Ticket $ticket, int $olaType): bool
204+
{
205+
if (
206+
$ticket->isDeleted() ||
207+
$ticket->fields['status'] == CommonITILObject::CLOSED ||
208+
$ticket->fields['status'] == CommonITILObject::SOLVED ||
209+
($olaType == SLM::TTO && $ticket->fields['takeintoaccount_delay_stat'] > 0)
210+
) {
211+
return false;
212+
}
213+
return true;
214+
215+
}
197216
}

src/Ticket.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,9 @@ public function manageSlaLevel($slas_id)
12841284
/**
12851285
* Manage OLA level escalation
12861286
*
1287+
* - add level in todo table XLaLevel_Ticket
1288+
* - replayForTicket
1289+
*
12871290
* @param int $olas_id
12881291
*
12891292
* @return void
@@ -1310,7 +1313,6 @@ public function manageOlaLevel($olas_id)
13101313
OlaLevel_Ticket::replayForTicket($this->getID(), $ola->getField('type'));
13111314
}
13121315

1313-
13141316
public function pre_updateInDB()
13151317
{
13161318

@@ -6583,8 +6585,8 @@ private function updateOlaAssociations(): void
65836585
}
65846586
}
65856587

6588+
OLA::deleteLevelsToDo($this);
65866589
foreach ($added_olas_ids as $olas_id) {
6587-
OLA::deleteLevelsToDo($this);
65886590
$this->manageOlaLevel($olas_id);
65896591
}
65906592
}

0 commit comments

Comments
 (0)