Skip to content

Commit

Permalink
Fix Task Duration Update Behavior in CommonITILTask
Browse files Browse the repository at this point in the history
  • Loading branch information
ccailly authored Oct 31, 2023
1 parent e0539b1 commit ff969cb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/CommonITILTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ public function post_deleteFromDB()
}
}

/**
* Handle the task duration and planned duration logic.
*
* This function ensures a bi-directional link between the task duration and the planned duration.
* These two fields can be a bit redundant when task planning is enabled.
*
* @param array $input The input array, passed by reference.
* @param int $timestart The start time of the task.
* @param int $timeend The end time of the task.
* @return void
*/
private function handleTaskDuration(array &$input, int $timestart, int $timeend): void
{
// If 'actiontime' is set and different from the current 'actiontime'
if (isset($input['actiontime']) && $this->fields['actiontime'] != $input['actiontime']) {
// Compute the end date based on 'actiontime'
$input["end"] = date("Y-m-d H:i:s", $timestart + $input['actiontime']);
} else {
// If 'actiontime' is not set, compute it based on the start and end times
$input["actiontime"] = $timeend - $timestart;
}
}


public function prepareInputForUpdate($input)
{
Expand Down Expand Up @@ -309,7 +332,8 @@ public function prepareInputForUpdate($input)

$timestart = strtotime($input["begin"]);
$timeend = strtotime($input["end"]);
$input["actiontime"] = $timeend - $timestart;

$this->handleTaskDuration($input, $timestart, $timeend);

unset($input["plan"]);

Expand Down Expand Up @@ -523,7 +547,8 @@ public function prepareInputForAdd($input)

$timestart = strtotime($input["begin"]);
$timeend = strtotime($input["end"]);
$input["actiontime"] = $timeend - $timestart;

$this->handleTaskDuration($input, $timestart, $timeend);

unset($input["plan"]);
if (!$this->test_valid_date($input)) {
Expand Down
70 changes: 70 additions & 0 deletions tests/functional/TicketTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,74 @@ public function testUpdateParentStatus()

$this->integer(\Ticket::getById($ticket_id)->fields['status'])->isEqualTo(\Ticket::WAITING);
}

/**
* Test that the task duration is correctly updated
*
* @return void
*/
public function testTaskDurationUpdate()
{
$this->login();
$ticketId = $this->getNewTicket();
$uid = getItemByTypeName('User', TU_USER, true);

$date_begin = new \DateTime(); // ==> now
$date_begin_string = $date_begin->format('Y-m-d H:i:s');

$date_end = new \DateTime(); // ==> +2days
$date_end->add(new \DateInterval('P2D'));
$date_end_string = $date_end->format('Y-m-d H:i:s');

// Create task with actiontime and without schedule
$task = new \TicketTask();
$task_id = $task->add([
'state' => \Planning::TODO,
'tickets_id' => $ticketId,
'tasktemplates_id' => '0',
'taskcategories_id' => '0',
'content' => "Task with schedule and recall",
'users_id_tech' => $uid,
'actiontime' => 3600,
]);
$this->integer($task_id)->isGreaterThan(0);

// Check that the task duration is correctly updated
$this->integer($task->fields['actiontime'])->isEqualTo(3600);
$this->variable($task->fields['begin'])->isEqualTo(null);
$this->variable($task->fields['end'])->isEqualTo(null);

// Schedule the task
$this->boolean($task->update([
'id' => $task_id,
'tickets_id' => $ticketId,
'users_id_tech' => $uid,
'plan' => [
'begin' => $date_begin_string,
'end' => $date_end_string,
]
]))->isTrue();

// Check that the task duration is correctly updated
$this->integer($task->fields['actiontime'])->isEqualTo(172800);
$this->string($task->fields['begin'])->isEqualTo($date_begin_string);
$this->string($task->fields['end'])->isEqualTo($date_end_string);

// Update the task duration with actiontime
$this->boolean($task->update([
'id' => $task_id,
'tickets_id' => $ticketId,
'users_id_tech' => $uid,
'actiontime' => 7200,
'plan' => [
'begin' => $date_begin_string,
'end' => $date_end_string,
]
]))->isTrue();

// Check that the task duration is correctly updated
$this->integer($task->fields['actiontime'])->isEqualTo(7200);
$this->string($task->fields['begin'])->isEqualTo($date_begin_string);
$this->string($task->fields['end'])->isEqualTo($date_begin->add(new \DateInterval('PT2H'))->format('Y-m-d H:i:s'));
}
}

0 comments on commit ff969cb

Please sign in to comment.