From d0941ccd4e4e82f2c138a6641323f1d8bd88fd62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Mon, 13 Feb 2023 20:29:33 -0800 Subject: [PATCH] Update task time spent/estimated when removing a subtask Fixes #3811 --- app/Model/SubtaskModel.php | 8 ++++++- tests/units/Model/SubtaskModelTest.php | 30 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/Model/SubtaskModel.php b/app/Model/SubtaskModel.php index ab9749412..e6da268b2 100644 --- a/app/Model/SubtaskModel.php +++ b/app/Model/SubtaskModel.php @@ -267,7 +267,13 @@ class SubtaskModel extends Base public function remove($subtaskId) { $this->subtaskEventJob->execute($subtaskId, array(self::EVENT_DELETE)); - return $this->db->table(self::TABLE)->eq('id', $subtaskId)->remove(); + + $subtask = $this->getById($subtaskId); + $result = $this->db->table(self::TABLE)->eq('id', $subtaskId)->remove(); + + $this->subtaskTimeTrackingModel->updateTaskTimeTracking($subtask['task_id']); + + return $result; } /** diff --git a/tests/units/Model/SubtaskModelTest.php b/tests/units/Model/SubtaskModelTest.php index d032b0b81..16060a22a 100644 --- a/tests/units/Model/SubtaskModelTest.php +++ b/tests/units/Model/SubtaskModelTest.php @@ -113,6 +113,36 @@ class SubtaskModelTest extends Base $this->assertEmpty($subtask); } + public function testRemoveUpdateTaskTimeTracking() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1))); + + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1, 'time_estimated' => 1, 'time_spent' => 2))); + $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => 3, 'time_spent' => 4))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(4, $task['time_estimated']); + $this->assertEquals(6, $task['time_spent']); + + $this->assertTrue($subtaskModel->remove(1)); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(3, $task['time_estimated']); + $this->assertEquals(4, $task['time_spent']); + + $this->assertTrue($subtaskModel->remove(2)); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(0, $task['time_estimated']); + $this->assertEquals(0, $task['time_spent']); + } + public function testDuplicate() { $taskCreationModel = new TaskCreationModel($this->container);