Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ProjectTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -1419,11 +1419,14 @@ public static function showFor($item)
'delete' => _x('button', 'Put in trashbin'),
'restore' => _x('button', 'Restore'),
'purge' => _x('button', 'Delete permanently'),
ProjectTaskTeam::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'affect_to_team' => _x('button', 'Affect to team'),
ProjectTaskTeam::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'unaffect_to_team' => _x('button', 'Unaffect to team'),
],
],
]);
}


public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
$nb = 0;
Expand Down
94 changes: 94 additions & 0 deletions src/ProjectTaskTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,100 @@ public function getForbiddenStandardMassiveAction()
return $forbidden;
}

public static function showMassiveActionsSubForm(MassiveAction $ma)
{
global $CFG_GLPI;

switch ($ma->getAction()) {
case 'affect_to_team':
case 'unaffect_to_team':
$rand = Dropdown::showItemTypes('itemtype', static::$available_types);
echo '<br>';
$params = [
'idtable' => '__VALUE__',
'display_emptychoice' => true,
'name' => 'items_id',
'entity_restrict' => Session::getActiveEntity(),
'rand' => $rand,
];
Ajax::updateItemOnSelectEvent(
"dropdown_itemtype$rand",
"results_itemtype$rand",
$CFG_GLPI['root_doc'] . '/ajax/dropdownAllItems.php',
$params
);
echo "<span id='results_itemtype$rand'></span>";
echo '<br>';

echo Html::submit(_x('button', 'Post'), ['name' => 'massiveaction']);
return true;
}
return parent::showMassiveActionsSubForm($ma);
}

/**
* @param int[] $ids
*/
public static function processMassiveActionsForOneItemtype(
MassiveAction $ma,
CommonDBTM $item,
array $ids
): void {
$action = $ma->getAction();
$input = $ma->getInput();

if (!in_array($action, ['affect_to_team', 'unaffect_to_team'], true)) {
parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
return;
}

if (
empty($input['itemtype'])
|| !isset($input['items_id'])
|| (int) $input['items_id'] <= 0
) {
foreach ($ids as $id) {
$ma->itemDone($item->getType(), $id, MassiveAction::NO_ACTION);
}
return;
}

$team = new self();

foreach ($ids as $id) {
if (!$item->can($id, UPDATE)) {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
continue;
}

$criteria = [
'projecttasks_id' => $id,
'itemtype' => $input['itemtype'],
'items_id' => (int) $input['items_id'],
];

if ($action === 'affect_to_team') {
if (countElementsInTable(self::getTable(), $criteria) > 0) {
$ma->itemDone($item->getType(), $id, MassiveAction::NO_ACTION);
continue;
}
$result = $team->add($criteria);
} else {
if (countElementsInTable(self::getTable(), $criteria) === 0) {
$ma->itemDone($item->getType(), $id, MassiveAction::NO_ACTION);
continue;
}
$result = $team->deleteByCriteria($criteria);
}

if ($result) {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
}
}
}


public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
Expand Down
79 changes: 71 additions & 8 deletions tests/functional/ProjectTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Glpi\Tests\DbTestCase;
use Project;
use ProjectTask;
use ProjectTaskTeam;

/* Test for inc/projecttask.class.php */

Expand Down Expand Up @@ -79,7 +80,7 @@ public function testPlanningConflict()
$this->hasNoSessionMessages([ERROR, WARNING]);
$task_id = $ptask->fields['id'];

$team = new \ProjectTaskTeam();
$team = new ProjectTaskTeam();
$tid = (int) $team->add([
'projecttasks_id' => $ptask->fields['id'],
'itemtype' => \User::getType(),
Expand All @@ -100,7 +101,7 @@ public function testPlanningConflict()
);
$this->hasNoSessionMessages([ERROR, WARNING]);

$team = new \ProjectTaskTeam();
$team = new ProjectTaskTeam();
$tid = (int) $team->add([
'projecttasks_id' => $ptask->fields['id'],
'itemtype' => \User::getType(),
Expand Down Expand Up @@ -130,7 +131,7 @@ public function testPlanningConflict()
);
$this->hasNoSessionMessages([ERROR, WARNING]);

$team = new \ProjectTaskTeam();
$team = new ProjectTaskTeam();
$tid = (int) $team->add([
'projecttasks_id' => $ptask->fields['id'],
'itemtype' => \User::getType(),
Expand Down Expand Up @@ -405,7 +406,7 @@ public function testCloneProjectTask()
$this->assertEquals($task->fields['name'] . ' (copy)', $clonedTask->fields['name']);

// Load task team
$project_task_team = new \ProjectTaskTeam();
$project_task_team = new ProjectTaskTeam();
$team = [];
foreach ($project_task_team->find(['projecttasks_id' => $task->fields['id']]) as $row) {
$team[] = [
Expand Down Expand Up @@ -857,7 +858,7 @@ public function testGetActiveProjectTaskIDsForUser()
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForUser([$user->getID()]));

// Create user team
$user_team = $this->createItem(\ProjectTaskTeam::getType(), [
$user_team = $this->createItem(ProjectTaskTeam::getType(), [
'projecttasks_id' => $project_task->getID(),
'itemtype' => \User::class,
'items_id' => $user->getID(),
Expand All @@ -876,14 +877,14 @@ public function testGetActiveProjectTaskIDsForUser()
$this->createItem(\Group_User::getType(), ['groups_id' => $group->getID(), 'users_id' => $user->getID()]);

// Create group team
$this->createItem(\ProjectTaskTeam::getType(), [
$this->createItem(ProjectTaskTeam::getType(), [
'projecttasks_id' => $project_task->getID(),
'itemtype' => \Group::class,
'items_id' => $group->getID(),
]);

// Remove user team
$this->deleteItem(\ProjectTaskTeam::getType(), $user_team->getID());
$this->deleteItem(ProjectTaskTeam::getType(), $user_team->getID());

// Check if a user with a project with tasks, where the user is a member of the group and the group is a member of the team, returns an array with the task ID if $search_in_groups is true
$this->assertEquals(
Expand Down Expand Up @@ -934,7 +935,7 @@ public function testGetActiveProjectTaskIDsForGroup(): void
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForGroup([$group->getID()]));

// Create group team
$this->createItem(\ProjectTaskTeam::getType(), [
$this->createItem(ProjectTaskTeam::getType(), [
'projecttasks_id' => $project_task->getID(),
'itemtype' => \Group::class,
'items_id' => $group->getID(),
Expand Down Expand Up @@ -1003,4 +1004,66 @@ public function testAutoSetDate(): void
$result = $task2->autoSetDate($input);
$this->assertNull($result['real_end_date']);
}

public function testAffectAndUnaffectTeamMember(): void
{
$this->login();

$project = $this->createItem(Project::class, ['name' => __FUNCTION__]);
$task = $this->createItem(ProjectTask::class, [
'name' => __FUNCTION__,
'projects_id' => $project->getID(),
]);
$group = getItemByTypeName('Group', '_test_group_1');

$criteria = [
'projecttasks_id' => $task->getID(),
'itemtype' => 'Group',
'items_id' => $group->getID(),
];

// Ajout d'un membre
$team_entry = $this->createItem(ProjectTaskTeam::class, $criteria);
$this->assertSame(1, countElementsInTable(ProjectTaskTeam::getTable(), $criteria));

// Vérification via getTeamFor
$team = ProjectTaskTeam::getTeamFor($task->getID());
$this->assertCount(1, $team['Group']);
$this->assertSame($group->getID(), $team['Group'][0]['items_id']);

// Suppression du membre
$this->deleteItem(ProjectTaskTeam::class, $team_entry->getID());
$this->assertSame(0, countElementsInTable(ProjectTaskTeam::getTable(), $criteria));

$team = ProjectTaskTeam::getTeamFor($task->getID());
$this->assertCount(0, $team['Group']);
}

public function testAffectTeamMemberDuplicatePrevented(): void
{
$this->login();

$project = $this->createItem(Project::class, ['name' => __FUNCTION__]);
$task = $this->createItem(ProjectTask::class, [
'name' => __FUNCTION__,
'projects_id' => $project->getID(),
]);
$group = getItemByTypeName('Group', '_test_group_1');

$criteria = [
'projecttasks_id' => $task->getID(),
'itemtype' => 'Group',
'items_id' => $group->getID(),
];

$this->createItem(ProjectTaskTeam::class, $criteria);
$this->assertSame(1, countElementsInTable(ProjectTaskTeam::getTable(), $criteria));

// Le doublon est détecté par countElementsInTable avant tout INSERT
$already_exists = countElementsInTable(ProjectTaskTeam::getTable(), $criteria) > 0;
$this->assertTrue($already_exists);

// Toujours une seule entrée en base
$this->assertSame(1, countElementsInTable(ProjectTaskTeam::getTable(), $criteria));
}
}
Loading