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
41 changes: 26 additions & 15 deletions webapp/src/Controller/Jury/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
}
}
33 changes: 33 additions & 0 deletions webapp/tests/Unit/Controller/Jury/TeamControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}