Skip to content

Commit 5d5cbea

Browse files
Fix task template using "Current logged in user" option
1 parent 0326b74 commit 5d5cbea

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/CommonITILTask.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,14 +673,19 @@ public function prepareInputForAdd($input)
673673
return false;
674674
}
675675
$input['tasktemplates_id'] = $input['_tasktemplates_id'];
676+
$user = $template->fields['users_id_tech'];
677+
if ($user == -1) {
678+
$user = Session::getLoginUserID();
679+
}
680+
676681
$input = array_replace(
677682
[
678683
'content' => $template->getRenderedContent($parent_item),
679684
'taskcategories_id' => $template->fields['taskcategories_id'],
680685
'actiontime' => $template->fields['actiontime'],
681686
'state' => $template->fields['state'],
682687
'is_private' => $template->fields['is_private'],
683-
'users_id_tech' => $template->fields['users_id_tech'],
688+
'users_id_tech' => $user,
684689
'groups_id_tech' => $template->fields['groups_id_tech'],
685690
],
686691
$input

tests/functional/TaskTemplateTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
use AbstractITILChildTemplate;
3838
use Glpi\Tests\AbstractITILChildTemplateTest;
3939
use TaskTemplate;
40+
use Ticket;
41+
use TicketTask;
42+
use User;
4043

4144
class TaskTemplateTest extends AbstractITILChildTemplateTest
4245
{
@@ -167,4 +170,32 @@ public function testSearchAbility()
167170
$condition = TaskTemplate::addWhere('AND', 0, TaskTemplate::class, 7, 'equals', 0);
168171
$this->assertEquals(' AND (`glpi_tasktemplates`.`use_current_user` = 0 AND `glpi_tasktemplates`.`users_id_tech` = 0)', $condition);
169172
}
173+
174+
public function testTemplateWithCurrentUserAsAuthor(): void
175+
{
176+
// Arrange: create a task template with the "current user" as author
177+
$template = $this->createItem(TaskTemplate::class, [
178+
'name' => 'my template',
179+
'content' => '<p>test</p>',
180+
'users_id_tech' => -1,
181+
]);
182+
183+
// Act: use the template to create a ticket with this task
184+
$this->login();
185+
$ticket = $this->createItem(Ticket::class, [
186+
'name' => 'My ticket',
187+
'content' => 'My content',
188+
'_tasktemplates_id' => [$template->getID()],
189+
]);
190+
191+
// Assert: task is created with the current user
192+
$task = new TicketTask();
193+
$task->getFromDBByCrit([
194+
'tickets_id' => $ticket->getID(),
195+
]);
196+
$this->assertEquals(
197+
getItemByTypeName(User::class, TU_USER, true),
198+
$task->fields['users_id_tech']
199+
);
200+
}
170201
}

0 commit comments

Comments
 (0)