diff --git a/webapp/src/Controller/Jury/TeamController.php b/webapp/src/Controller/Jury/TeamController.php index ccd2b7e55c..19bda2f897 100644 --- a/webapp/src/Controller/Jury/TeamController.php +++ b/webapp/src/Controller/Jury/TeamController.php @@ -322,6 +322,7 @@ public function editAction(Request $request, int $teamId): Response $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $this->possiblyAddUser($team); $this->assetUpdater->updateAssets($team); $this->saveEntity($this->em, $this->eventLogService, $this->dj, $team, $team->getTeamid(), false); @@ -358,21 +359,7 @@ public function addAction(Request $request): Response $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - /** @var User $user */ - $user = $team->getUsers()->first(); - if ($team->getAddUserForTeam() === Team::CREATE_NEW_USER) { - // Create a user for the team. - $user = new User(); - $user->setUsername($team->getNewUsername()); - $team->addUser($user); - // Make sure the user has the team role to make validation work. - $role = $this->em->getRepository(Role::class)->findOneBy(['dj_role' => 'team']); - $user->addUserRole($role); - // Set the user's name to the team name when creating a new user. - $user->setName($team->getEffectiveName()); - } elseif ($team->getAddUserForTeam() === Team::ADD_EXISTING_USER) { - $team->addUser($team->getExistingUser()); - } + $this->possiblyAddUser($team); $this->em->persist($team); $this->assetUpdater->updateAssets($team); $this->saveEntity($this->em, $this->eventLogService, $this->dj, $team, null, true); @@ -384,4 +371,28 @@ public function addAction(Request $request): Response 'form' => $form, ]); } + + /** + * Add an existing or new user to a team if configured to do so + */ + protected function possiblyAddUser(Team $team): void + { + if ($team->getAddUserForTeam() === Team::CREATE_NEW_USER) { + // Create a user for the team. + $user = new User(); + $user->setUsername($team->getNewUsername()); + // Set the external ID if we need to do so. + if ($this->eventLogService->externalIdFieldForEntity(User::class)) { + $user->setExternalid($team->getNewUsername()); + } + $team->addUser($user); + // Make sure the user has the team role to make validation work. + $role = $this->em->getRepository(Role::class)->findOneBy(['dj_role' => 'team']); + $user->addUserRole($role); + // Set the user's name to the team name when creating a new user. + $user->setName($team->getEffectiveName()); + } elseif ($team->getAddUserForTeam() === Team::ADD_EXISTING_USER) { + $team->addUser($team->getExistingUser()); + } + } } diff --git a/webapp/tests/Unit/Controller/Jury/TeamControllerTest.php b/webapp/tests/Unit/Controller/Jury/TeamControllerTest.php index c90d4e8a01..89d30a05fd 100644 --- a/webapp/tests/Unit/Controller/Jury/TeamControllerTest.php +++ b/webapp/tests/Unit/Controller/Jury/TeamControllerTest.php @@ -3,6 +3,8 @@ namespace App\Tests\Unit\Controller\Jury; use App\Entity\Team; +use App\Entity\User; +use Doctrine\ORM\EntityManagerInterface; class TeamControllerTest extends JuryControllerTestCase { @@ -91,4 +93,35 @@ class TeamControllerTest extends JuryControllerTestCase 'Only letters, numbers, dashes and underscores are allowed.' => [['icpcid' => '|viol', 'name' => 'icpcid violation-1'], ['icpcid' => '&viol', 'name' => 'icpcid violation-2']], 'This value should not be blank.' => [['name' => '', 'displayName' => 'Teams should have a name']]]; + + /** + * Test that adding a team without a user and then editing it to add a user works. + */ + public function testAddWithoutUserThenEdit(): void + { + $teamToAdd = static::$addEntities[0]; + $this->roles = ['admin']; + $this->logOut(); + $this->logIn(); + $this->verifyPageResponse('GET', static::$baseUrl, 200); + $this->helperSubmitFields($teamToAdd); + $viewPage = $this->client->followRedirect()->getUri(); + $editPage = $viewPage . static::$edit; + $this->verifyPageResponse('GET', $editPage, 200); + $formFields = [ + static::$addForm . 'addUserForTeam]' => Team::CREATE_NEW_USER, + static::$addForm . 'newUsername]' => 'somelinkeduser', + ]; + $button = $this->client->getCrawler()->selectButton('Save'); + $form = $button->form($formFields, 'POST'); + $this->client->submit($form); + self::assertNotEquals(500, $this->client->getResponse()->getStatusCode()); + + /** @var EntityManagerInterface $em */ + $em = $this->getContainer()->get(EntityManagerInterface::class); + $user = $em->getRepository(User::class)->findOneBy(['username' => 'somelinkeduser']); + + static::assertNotNull($user); + static::assertEquals('New Team', $user->getTeam()->getName()); + } }