Files
TijdVoorDeTest/tests/Controller/Backoffice/QuizFinalizeTest.php
T
Marijn 2fd15ba8fa test: expand coverage, dedupe data-driven tests, extract shared WebTestCase base (#201)
* test: expand coverage, dedupe data-driven tests, extract shared WebTestCase base

- Add #[CoversClass] to Base64Test and FilenameSanitizerTest
- Merge near-duplicate test methods into #[DataProvider] cases across
  ResetPasswordControllerTest, SettingsControllerTest, ClaimSeasonCommandTest,
  FilenameSanitizerTest, Base64Test, and SeasonRepositoryTest
- Add integration tests for previously untested controllers: public
  QuizController (quiz-taking flow), LoginController, RegistrationController,
  EliminationController, and PrepareEliminationController
- Add unit tests for Elimination and BankQuestion entity logic
- Extract shared WebTestCase setup/helpers (client, entityManager, login,
  entity lookups, CSRF token scraping) into AbstractControllerWebTestCase,
  removing duplicated boilerplate from all 14 WebTestCase files

* test: address PR review feedback

- Scope AbstractControllerWebTestCase::getCandidate/getQuizByName by
  season code (both Candidate and Quiz are only unique per season, not
  system-wide) and add a CandidateRepository regression test guarding
  against same-named candidates in different seasons
- Add missing entityManager->clear() before verifying DB state after a
  POST in PrepareEliminationControllerTest and QuestionBankControllerTest
- Add non-owner denial tests for BackofficeController::exportQuiz and
  QuizQuestionController::edit/reorder, which had IsGranted checks with
  no test coverage

* ci: publish PHPUnit coverage to GitHub code coverage

- Generate a Cobertura report alongside the existing JUnit report and
  upload it with actions/upload-code-coverage so coverage shows up on
  PRs and the default branch via GitHub's code coverage feature
- Add a step to copy both reports out of the php container before
  publishing them, since var/ is a Docker volume (see the Dockerfile's
  VOLUME /app/var/) and isn't bind-mounted to the runner — this also
  fixes the existing JUnit report publishing step, which was silently
  looking at a path that never had contets on the runner

* ci: fix coverage report paths and tolerate Code Quality not yet enabled

- Write PHPUnit's JUnit and Cobertura reports to reports/ instead of
  var/, since var/ is a Docker volume (Dockerfile's VOLUME /app/var/)
  that isn't bind-mounted to the runner - reports written there never
  reached the host, which is also why the prior junit.xml publish step
  had nothing to read. reports/ is a plain path under the project's
  bind mount, so no extra copy-out step is needed
- Set fail-on-error: false on the coverage upload step: it 404s until
  "Code Quality" is turned on for the repo under Settings > Code
  security > Code quality, a one-time manual step
2026-07-10 18:06:48 +02:00

200 lines
7.2 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\Question;
use Tvdt\Entity\Quiz;
use Tvdt\Entity\QuizCandidate;
use Tvdt\Tests\Controller\AbstractControllerWebTestCase;
#[CoversClass(QuizController::class)]
final class QuizFinalizeTest 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 testFinalizeSetsFinalizedAt(): void
{
$quiz = $this->getQuizByName('Quiz 2');
$this->assertFalse($quiz->isFinalized);
$token = $this->getCsrfTokenFromOverview($quiz, '/finalize');
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/finalize', $quiz->id), ['_token' => $token]);
$this->assertResponseRedirects();
$this->entityManager->clear();
$this->assertTrue($this->getQuizByName('Quiz 2')->isFinalized);
}
public function testFinalizeRefusedWhenQuizHasErrors(): void
{
$season = $this->getSeasonByCode('krtek');
$invalidQuiz = new Quiz();
$invalidQuiz->name = 'Invalid Quiz';
$question = new Question();
$question->question = 'Vraag zonder goed antwoord';
$question->ordering = 1;
$question->addAnswer(new Answer('Fout'));
$question->addAnswer(new Answer('Ook fout'));
$invalidQuiz->addQuestion($question);
$season->addQuiz($invalidQuiz);
$this->entityManager->persist($invalidQuiz);
$this->entityManager->flush();
// Token intention is shared, so any quiz overview provides it
$token = $this->getCsrfTokenFromOverview($invalidQuiz, '/finalize');
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/finalize', $invalidQuiz->id), ['_token' => $token]);
$this->assertResponseRedirects();
$this->entityManager->clear();
$this->assertFalse($this->getQuizByName('Invalid Quiz')->isFinalized);
}
public function testEnableRefusedWhenNotFinalized(): void
{
$quiz = $this->getQuizByName('Quiz 2');
$this->assertFalse($quiz->isFinalized);
$token = $this->getCsrfTokenFromOverview($quiz, '/enable');
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/season/krtek/quiz/%s/enable', $quiz->id), ['_token' => $token]);
$this->assertResponseRedirects();
$this->entityManager->clear();
$season = $this->getSeasonByCode('krtek');
$this->assertInstanceOf(Quiz::class, $season->activeQuiz);
$this->assertSame('Quiz 1', $season->activeQuiz->name);
}
public function testEnableAllowedWhenFinalized(): void
{
$quiz = $this->getQuizByName('Quiz 2');
$quiz->finalizedAt = new DateTimeImmutable();
$this->entityManager->flush();
$token = $this->getCsrfTokenFromOverview($quiz, '/enable');
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/season/krtek/quiz/%s/enable', $quiz->id), ['_token' => $token]);
$this->assertResponseRedirects();
$this->entityManager->clear();
$season = $this->getSeasonByCode('krtek');
$this->assertInstanceOf(Quiz::class, $season->activeQuiz);
$this->assertSame('Quiz 2', $season->activeQuiz->name);
}
public function testUnfinalize(): void
{
$quiz = $this->getQuizByName('Quiz 2');
$quiz->finalizedAt = new DateTimeImmutable();
$this->entityManager->flush();
$token = $this->getCsrfTokenFromOverview($quiz, '/unfinalize');
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/unfinalize', $quiz->id), ['_token' => $token]);
$this->assertResponseRedirects();
$this->entityManager->clear();
$this->assertFalse($this->getQuizByName('Quiz 2')->isFinalized);
}
public function testUnfinalizeRefusedWhenQuizIsActive(): void
{
// Quiz 1 is finalized and active in the fixtures; scrape a token from Quiz 2 (same intention)
$quiz2 = $this->getQuizByName('Quiz 2');
$quiz2->finalizedAt = new DateTimeImmutable();
$this->entityManager->flush();
$token = $this->getCsrfTokenFromOverview($quiz2, '/unfinalize');
$quiz1 = $this->getQuizByName('Quiz 1');
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/unfinalize', $quiz1->id), ['_token' => $token]);
$this->assertResponseRedirects();
$this->entityManager->clear();
$this->assertTrue($this->getQuizByName('Quiz 1')->isFinalized);
}
public function testUnfinalizeRefusedWhenCandidatesStarted(): void
{
$quiz = $this->getQuizByName('Quiz 2');
$quiz->finalizedAt = new DateTimeImmutable();
$this->entityManager->flush();
// Scrape the token before a candidate starts, since the button disappears afterwards
$token = $this->getCsrfTokenFromOverview($quiz, '/unfinalize');
$candidate = $this->getCandidate('Tom');
$quizCandidate = new QuizCandidate($quiz, $candidate);
$quizCandidate->started = new DateTimeImmutable();
$this->entityManager->persist($quizCandidate);
$this->entityManager->flush();
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/unfinalize', $quiz->id), ['_token' => $token]);
$this->assertResponseRedirects();
$this->entityManager->clear();
$this->assertTrue($this->getQuizByName('Quiz 2')->isFinalized);
}
public function testClearQuizResetsFinalization(): void
{
$quiz = $this->getQuizByName('Quiz 1');
$this->assertTrue($quiz->isFinalized);
$token = $this->getCsrfTokenFromOverview($quiz, '/clear');
$this->client->request(Request::METHOD_POST, \sprintf('/backoffice/quiz/%s/clear', $quiz->id), ['_token' => $token]);
$this->assertResponseRedirects();
$this->entityManager->clear();
$this->assertFalse($this->getQuizByName('Quiz 1')->isFinalized);
}
public function testDeactivateWithRedirectQuizStaysOnQuizOverview(): void
{
// Quiz 1 is active in fixtures; deactivate while viewing Quiz 2 → should redirect to Quiz 2 overview
$this->getQuizByName('Quiz 1');
$quiz2 = $this->getQuizByName('Quiz 2');
$token = $this->getCsrfTokenFromOverview($quiz2, '/enable');
$this->client->request(
Request::METHOD_POST,
'/backoffice/season/krtek/quiz/null/enable',
['_token' => $token, 'redirect_quiz' => (string) $quiz2->id],
);
self::assertResponseRedirects(\sprintf('/backoffice/season/krtek/quiz/%s/overview', $quiz2->id));
$this->entityManager->clear();
$this->assertNotInstanceOf(Quiz::class, $this->getSeasonByCode('krtek')->activeQuiz);
}
}