Skip to content

Commit 9c57fe6

Browse files
Allow to add user when editing a team without user
Fixes #2535.
1 parent 754d456 commit 9c57fe6

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

webapp/src/Controller/Jury/TeamController.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ public function editAction(Request $request, int $teamId): Response
322322
$form->handleRequest($request);
323323

324324
if ($form->isSubmitted() && $form->isValid()) {
325+
$this->possiblyAddUser($team);
325326
$this->assetUpdater->updateAssets($team);
326327
$this->saveEntity($this->em, $this->eventLogService, $this->dj, $team,
327328
$team->getTeamid(), false);
@@ -358,21 +359,7 @@ public function addAction(Request $request): Response
358359
$form->handleRequest($request);
359360

360361
if ($form->isSubmitted() && $form->isValid()) {
361-
/** @var User $user */
362-
$user = $team->getUsers()->first();
363-
if ($team->getAddUserForTeam() === Team::CREATE_NEW_USER) {
364-
// Create a user for the team.
365-
$user = new User();
366-
$user->setUsername($team->getNewUsername());
367-
$team->addUser($user);
368-
// Make sure the user has the team role to make validation work.
369-
$role = $this->em->getRepository(Role::class)->findOneBy(['dj_role' => 'team']);
370-
$user->addUserRole($role);
371-
// Set the user's name to the team name when creating a new user.
372-
$user->setName($team->getEffectiveName());
373-
} elseif ($team->getAddUserForTeam() === Team::ADD_EXISTING_USER) {
374-
$team->addUser($team->getExistingUser());
375-
}
362+
$this->possiblyAddUser($team);
376363
$this->em->persist($team);
377364
$this->assetUpdater->updateAssets($team);
378365
$this->saveEntity($this->em, $this->eventLogService, $this->dj, $team, null, true);
@@ -384,4 +371,28 @@ public function addAction(Request $request): Response
384371
'form' => $form,
385372
]);
386373
}
374+
375+
/**
376+
* Add an existing or new user to a team if configured to do so
377+
*/
378+
protected function possiblyAddUser(Team $team): void
379+
{
380+
if ($team->getAddUserForTeam() === Team::CREATE_NEW_USER) {
381+
// Create a user for the team.
382+
$user = new User();
383+
$user->setUsername($team->getNewUsername());
384+
// Set the external ID if we need to do so.
385+
if ($this->eventLogService->externalIdFieldForEntity(User::class)) {
386+
$user->setExternalid($team->getNewUsername());
387+
}
388+
$team->addUser($user);
389+
// Make sure the user has the team role to make validation work.
390+
$role = $this->em->getRepository(Role::class)->findOneBy(['dj_role' => 'team']);
391+
$user->addUserRole($role);
392+
// Set the user's name to the team name when creating a new user.
393+
$user->setName($team->getEffectiveName());
394+
} elseif ($team->getAddUserForTeam() === Team::ADD_EXISTING_USER) {
395+
$team->addUser($team->getExistingUser());
396+
}
397+
}
387398
}

webapp/tests/Unit/Controller/Jury/TeamControllerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Tests\Unit\Controller\Jury;
44

55
use App\Entity\Team;
6+
use App\Entity\User;
7+
use Doctrine\ORM\EntityManagerInterface;
68

79
class TeamControllerTest extends JuryControllerTestCase
810
{
@@ -91,4 +93,35 @@ class TeamControllerTest extends JuryControllerTestCase
9193
'Only letters, numbers, dashes and underscores are allowed.' => [['icpcid' => '|viol', 'name' => 'icpcid violation-1'],
9294
['icpcid' => '&viol', 'name' => 'icpcid violation-2']],
9395
'This value should not be blank.' => [['name' => '', 'displayName' => 'Teams should have a name']]];
96+
97+
/**
98+
* Test that adding a team without a user and then editing it to add a user works.
99+
*/
100+
public function testAddWithoutUserThenEdit(): void
101+
{
102+
$teamToAdd = static::$addEntities[0];
103+
$this->roles = ['admin'];
104+
$this->logOut();
105+
$this->logIn();
106+
$this->verifyPageResponse('GET', static::$baseUrl, 200);
107+
$this->helperSubmitFields($teamToAdd);
108+
$viewPage = $this->client->followRedirect()->getUri();
109+
$editPage = $viewPage . static::$edit;
110+
$this->verifyPageResponse('GET', $editPage, 200);
111+
$formFields = [
112+
static::$addForm . 'addUserForTeam]' => Team::CREATE_NEW_USER,
113+
static::$addForm . 'newUsername]' => 'somelinkeduser',
114+
];
115+
$button = $this->client->getCrawler()->selectButton('Save');
116+
$form = $button->form($formFields, 'POST');
117+
$this->client->submit($form);
118+
self::assertNotEquals(500, $this->client->getResponse()->getStatusCode());
119+
120+
/** @var EntityManagerInterface $em */
121+
$em = $this->getContainer()->get(EntityManagerInterface::class);
122+
$user = $em->getRepository(User::class)->findOneBy(['username' => 'somelinkeduser']);
123+
124+
static::assertNotNull($user);
125+
static::assertEquals('New Team', $user->getTeam()->getName());
126+
}
94127
}

0 commit comments

Comments
 (0)