mirror of
https://github.com/MarijnDoeve/TijdVoorDeTest.git
synced 2026-07-12 21:05:19 +02:00
8382900b9e
* feat: add reset progress action for a candidate's quiz attempt Admins previously had no way to let a candidate retake a quiz once started. Adds a backoffice action that clears a candidate's given answers and start timestamp for a quiz, closes #20. * fix: wrap candidate progress reset in a transaction Avoids leaving a candidate half-reset (answers deleted but started still set) if the second flush fails. Addresses CodeRabbit review comment on PR #204. * feat: open candidate edit/add forms in modals with dirty-aware dismissal Rename-candidate modal always blocked backdrop-click dismissal via a hardcoded static backdrop; wire it into the existing bo--modal controller so outside clicks/Escape only get blocked once the name field has actually been edited. Also move "Add Candidate" from a full-page navigation into the same turbo-frame modal pattern already used for editing quiz questions, keeping the full-page form as a fallback for non-JS/turbo requests. * feat: show a hint for the candidates textarea and gate translations in CI Add a help hint on the add-candidates form explaining one candidate per line, and translate it plus a few strings from the earlier reset-progress feature that had been missed. Also add a CI step that re-runs translation:extract and fails the build if it produces uncommitted changes, so untranslated/stale strings can't slip through review again. * Just links
350 lines
14 KiB
PHP
350 lines
14 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tvdt\Tests\Controller\Backoffice;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use Safe\DateTimeImmutable;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Tvdt\Controller\Backoffice\QuizController;
|
|
use Tvdt\Entity\Answer;
|
|
use Tvdt\Entity\Candidate;
|
|
use Tvdt\Entity\GivenAnswer;
|
|
use Tvdt\Entity\Question;
|
|
use Tvdt\Entity\Quiz;
|
|
use Tvdt\Entity\QuizCandidate;
|
|
use Tvdt\Tests\Controller\AbstractControllerWebTestCase;
|
|
|
|
#[CoversClass(QuizController::class)]
|
|
final class QuizControllerTest extends AbstractControllerWebTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->loginAs('krtek-admin@example.org');
|
|
}
|
|
|
|
private function getCsrfTokenFromOverview(Quiz $quiz, string $formActionContains): string
|
|
{
|
|
return $this->getCsrfTokenFromPage(\sprintf('/backoffice/season/krtek/quiz/%s/overview', $quiz->id), $formActionContains);
|
|
}
|
|
|
|
public function testIndexRedirectsToOverview(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s', $quiz->id));
|
|
|
|
self::assertResponseRedirects(\sprintf('/backoffice/season/krtek/quiz/%s/overview', $quiz->id));
|
|
}
|
|
|
|
public function testOverviewLoadsSuccessfully(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/overview', $quiz->id));
|
|
|
|
self::assertResponseIsSuccessful();
|
|
self::assertSelectorTextContains('body', 'Quiz 1');
|
|
}
|
|
|
|
public function testResultTabLoadsSuccessfully(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/result', $quiz->id));
|
|
|
|
self::assertResponseIsSuccessful();
|
|
}
|
|
|
|
public function testCandidatesTabLoadsSuccessfully(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/candidates-list', $quiz->id));
|
|
|
|
self::assertResponseIsSuccessful();
|
|
}
|
|
|
|
public function testAnswerMappingRedirectsToFirstQuestion(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/answer-mapping', $quiz->id));
|
|
|
|
self::assertResponseRedirects();
|
|
$this->assertStringContainsString('/candidates/', (string) $this->client->getResponse()->headers->get('Location'));
|
|
}
|
|
|
|
public function testCandidatesQuestionTabLoadsSuccessfully(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
$question = $quiz->questions->first();
|
|
$this->assertInstanceOf(Question::class, $question);
|
|
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/candidates/%s', $quiz->id, $question->id));
|
|
|
|
self::assertResponseIsSuccessful();
|
|
}
|
|
|
|
public function testSaveCandidateAnswersPersistsSelection(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
$question = $quiz->questions->first();
|
|
$this->assertInstanceOf(Question::class, $question);
|
|
$answer = $question->answers->first();
|
|
$this->assertInstanceOf(Answer::class, $answer);
|
|
$candidate = $this->getCandidate('Tom');
|
|
|
|
$url = \sprintf('/backoffice/season/krtek/quiz/%s/candidates/%s', $quiz->id, $question->id);
|
|
$crawler = $this->client->request(Request::METHOD_GET, $url);
|
|
self::assertResponseIsSuccessful();
|
|
$token = (string) $crawler->filter('input[name="_token"]')->first()->attr('value');
|
|
|
|
$this->client->request(Request::METHOD_POST, $url, [
|
|
'_token' => $token,
|
|
'candidate_answer' => [
|
|
(string) $candidate->id => [(string) $answer->id],
|
|
],
|
|
]);
|
|
|
|
$this->assertResponseRedirects($url);
|
|
$this->entityManager->clear();
|
|
|
|
$savedAnswer = $this->entityManager->getRepository(Answer::class)->find($answer->id);
|
|
$this->assertInstanceOf(Answer::class, $savedAnswer);
|
|
$this->assertTrue($savedAnswer->candidates->exists(
|
|
static fn (int $key, Candidate $c): bool => $c->id->equals($candidate->id),
|
|
));
|
|
}
|
|
|
|
public function testToggleCandidateCreatesInactiveQuizCandidate(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
$candidate = $this->getCandidate('Tom');
|
|
|
|
$crawler = $this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/candidates-list', $quiz->id));
|
|
self::assertResponseIsSuccessful();
|
|
$token = (string) $crawler->filter(\sprintf('form[action*="/%s/toggle"] input[name="_token"]', $candidate->id))->first()->attr('value');
|
|
|
|
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/candidate/%s/toggle', $quiz->id, $candidate->id), [
|
|
'_token' => $token,
|
|
]);
|
|
|
|
self::assertResponseRedirects();
|
|
$this->entityManager->clear();
|
|
|
|
$quizCandidate = $this->entityManager->getRepository(QuizCandidate::class)->findOneBy([
|
|
'quiz' => $this->getQuizByName('Quiz 1'),
|
|
'candidate' => $this->getCandidate('Tom'),
|
|
]);
|
|
$this->assertInstanceOf(QuizCandidate::class, $quizCandidate);
|
|
$this->assertFalse($quizCandidate->active);
|
|
}
|
|
|
|
public function testToggleCandidateTogglesActiveState(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
$candidate = $this->getCandidate('Tom');
|
|
|
|
$quizCandidate = new QuizCandidate($quiz, $candidate);
|
|
$quizCandidate->active = false;
|
|
|
|
$this->entityManager->persist($quizCandidate);
|
|
$this->entityManager->flush();
|
|
|
|
$crawler = $this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/candidates-list', $quiz->id));
|
|
$token = (string) $crawler->filter(\sprintf('form[action*="/%s/toggle"] input[name="_token"]', $candidate->id))->first()->attr('value');
|
|
|
|
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/candidate/%s/toggle', $quiz->id, $candidate->id), [
|
|
'_token' => $token,
|
|
]);
|
|
|
|
self::assertResponseRedirects();
|
|
$this->entityManager->clear();
|
|
|
|
$updated = $this->entityManager->getRepository(QuizCandidate::class)->findOneBy([
|
|
'quiz' => $this->getQuizByName('Quiz 1'),
|
|
'candidate' => $this->getCandidate('Tom'),
|
|
]);
|
|
$this->assertInstanceOf(QuizCandidate::class, $updated);
|
|
$this->assertTrue($updated->active);
|
|
}
|
|
|
|
public function testResetCandidateProgressDeletesGivenAnswersAndClearsStarted(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
$candidate = $this->getCandidate('Tom');
|
|
|
|
$quizCandidate = new QuizCandidate($quiz, $candidate);
|
|
$quizCandidate->started = new DateTimeImmutable();
|
|
|
|
$this->entityManager->persist($quizCandidate);
|
|
$firstQuestion = $quiz->questions->first();
|
|
$this->assertInstanceOf(Question::class, $firstQuestion);
|
|
$answer = $firstQuestion->answers->first();
|
|
$this->assertInstanceOf(Answer::class, $answer);
|
|
$this->entityManager->persist(new GivenAnswer($candidate, $quiz, $answer));
|
|
$this->entityManager->flush();
|
|
|
|
$crawler = $this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/candidates-list', $quiz->id));
|
|
self::assertResponseIsSuccessful();
|
|
$token = (string) $crawler->filter(\sprintf('form[action*="/%s/reset"] input[name="_token"]', $candidate->id))->first()->attr('value');
|
|
|
|
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/candidate/%s/reset', $quiz->id, $candidate->id), [
|
|
'_token' => $token,
|
|
]);
|
|
|
|
self::assertResponseRedirects();
|
|
$this->entityManager->clear();
|
|
|
|
$updated = $this->entityManager->getRepository(QuizCandidate::class)->findOneBy([
|
|
'quiz' => $this->getQuizByName('Quiz 1'),
|
|
'candidate' => $this->getCandidate('Tom'),
|
|
]);
|
|
$this->assertInstanceOf(QuizCandidate::class, $updated);
|
|
$this->assertNotInstanceOf(\DateTimeImmutable::class, $updated->started);
|
|
|
|
$remainingAnswers = $this->entityManager->getRepository(GivenAnswer::class)->findBy([
|
|
'quiz' => $quiz,
|
|
'candidate' => $candidate,
|
|
]);
|
|
$this->assertCount(0, $remainingAnswers);
|
|
}
|
|
|
|
public function testModifyCorrection(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
$candidate = $this->getCandidate('Tom');
|
|
|
|
// getScores() requires started IS NOT NULL and at least one GivenAnswer
|
|
$quizCandidate = new QuizCandidate($quiz, $candidate);
|
|
$quizCandidate->started = new DateTimeImmutable();
|
|
|
|
$this->entityManager->persist($quizCandidate);
|
|
$firstQuestion = $quiz->questions->first();
|
|
$this->assertInstanceOf(Question::class, $firstQuestion);
|
|
$answer = $firstQuestion->answers->first();
|
|
$this->assertInstanceOf(Answer::class, $answer);
|
|
$this->entityManager->persist(new GivenAnswer($candidate, $quiz, $answer));
|
|
$this->entityManager->flush();
|
|
|
|
$crawler = $this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/result', $quiz->id));
|
|
self::assertResponseIsSuccessful();
|
|
$token = (string) $crawler->filter(\sprintf('form[action*="%s/modify_correction"] input[name="_token"]', $candidate->id))->first()->attr('value');
|
|
|
|
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/candidate/%s/modify_correction', $quiz->id, $candidate->id), [
|
|
'_token' => $token,
|
|
'corrections' => '1.5',
|
|
]);
|
|
|
|
self::assertResponseRedirects();
|
|
$this->entityManager->clear();
|
|
|
|
$updated = $this->entityManager->getRepository(QuizCandidate::class)->findOneBy([
|
|
'quiz' => $this->getQuizByName('Quiz 1'),
|
|
'candidate' => $this->getCandidate('Tom'),
|
|
]);
|
|
$this->assertInstanceOf(QuizCandidate::class, $updated);
|
|
$this->assertEqualsWithDelta(1.5, $updated->corrections, \PHP_FLOAT_EPSILON);
|
|
}
|
|
|
|
public function testModifyPenalty(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
$candidate = $this->getCandidate('Claudia');
|
|
|
|
$quizCandidate = new QuizCandidate($quiz, $candidate);
|
|
$quizCandidate->started = new DateTimeImmutable();
|
|
|
|
$this->entityManager->persist($quizCandidate);
|
|
$firstQuestion = $quiz->questions->first();
|
|
$this->assertInstanceOf(Question::class, $firstQuestion);
|
|
$answer = $firstQuestion->answers->first();
|
|
$this->assertInstanceOf(Answer::class, $answer);
|
|
$this->entityManager->persist(new GivenAnswer($candidate, $quiz, $answer));
|
|
$this->entityManager->flush();
|
|
|
|
$crawler = $this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/result', $quiz->id));
|
|
self::assertResponseIsSuccessful();
|
|
$token = (string) $crawler->filter(\sprintf('form[action*="%s/modify_penalty"] input[name="_token"]', $candidate->id))->first()->attr('value');
|
|
|
|
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/candidate/%s/modify_penalty', $quiz->id, $candidate->id), [
|
|
'_token' => $token,
|
|
'penalty' => '30',
|
|
]);
|
|
|
|
self::assertResponseRedirects();
|
|
$this->entityManager->clear();
|
|
|
|
$updated = $this->entityManager->getRepository(QuizCandidate::class)->findOneBy([
|
|
'quiz' => $this->getQuizByName('Quiz 1'),
|
|
'candidate' => $this->getCandidate('Claudia'),
|
|
]);
|
|
$this->assertInstanceOf(QuizCandidate::class, $updated);
|
|
$this->assertSame(30, $updated->penaltySeconds);
|
|
}
|
|
|
|
public function testDeleteQuiz(): void
|
|
{
|
|
$quiz = $this->getQuizByName('Quiz 2');
|
|
$quizId = $quiz->id;
|
|
$token = $this->getCsrfTokenFromOverview($quiz, '/delete');
|
|
|
|
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/delete', $quiz->id), [
|
|
'_token' => $token,
|
|
]);
|
|
|
|
self::assertResponseRedirects('/backoffice/season/krtek');
|
|
$this->entityManager->clear();
|
|
$this->assertNotInstanceOf(Quiz::class, $this->entityManager->getRepository(Quiz::class)->find($quizId));
|
|
}
|
|
|
|
public function testNonOwnerIsDenied(): void
|
|
{
|
|
$this->loginAs('test@example.org');
|
|
|
|
$quiz = $this->getQuizByName('Quiz 1');
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/overview', $quiz->id));
|
|
|
|
self::assertResponseStatusCodeSame(403);
|
|
}
|
|
|
|
public function testOverviewLoadsForEmptyQuiz(): void
|
|
{
|
|
$season = $this->getSeasonByCode('krtek');
|
|
|
|
$emptyQuiz = new Quiz();
|
|
$emptyQuiz->name = 'Empty Quiz';
|
|
|
|
$season->addQuiz($emptyQuiz);
|
|
$this->entityManager->persist($emptyQuiz);
|
|
$this->entityManager->flush();
|
|
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/overview', $emptyQuiz->id));
|
|
|
|
self::assertResponseIsSuccessful();
|
|
self::assertSelectorTextContains('body', 'Empty Quiz');
|
|
}
|
|
|
|
public function testAnswerMappingRedirectsWithFlashWhenNoQuestions(): void
|
|
{
|
|
$season = $this->getSeasonByCode('krtek');
|
|
|
|
$emptyQuiz = new Quiz();
|
|
$emptyQuiz->name = 'Empty Quiz';
|
|
|
|
$season->addQuiz($emptyQuiz);
|
|
$this->entityManager->persist($emptyQuiz);
|
|
$this->entityManager->flush();
|
|
|
|
$this->client->request(Request::METHOD_GET, \sprintf('/backoffice/season/krtek/quiz/%s/answer-mapping', $emptyQuiz->id));
|
|
|
|
self::assertResponseRedirects(\sprintf('/backoffice/season/krtek/quiz/%s/overview', $emptyQuiz->id));
|
|
$this->client->followRedirect();
|
|
self::assertSelectorTextContains('body', 'Deze test heeft nog geen vragen');
|
|
}
|
|
}
|