Add quiz slide type
CI / Deploy / test (push) Successful in 41s
CI / Deploy / deploy (push) Skipped

Quiz slides have predefined answers with one or more marked as correct.
The presenter starts the quiz, which then runs until its optional time
limit expires or the presenter ends it. Counts and correct answers are
only revealed once the quiz is finished; participants then see whether
their answer was right.

- migration: slide_options.is_correct, slides.voting_started_at/voting_ends_at
- Slide::isVotingOpen() includes the quiz deadline, expired quizzes are
  closed lazily when the state is read
- presenter countdown, quiz tiles and reveal with correct-answer summary
- CSV export marks quiz answers as right/wrong

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 14:39:21 +01:00
1 parent 04cbc5bcd3
commit fd8da4c16c
18 files changed
+999 -43

No files matched your search

@@ -88,6 +88,67 @@ class AdminControllerTest extends TestCase
$this->assertSame(1, $slide->position);
}
public function testAddQuizSlide(): void
{
[, $token] = $this->createPoll();
$data = [
'type' => 'quiz',
'question' => 'Hauptstadt von Frankreich?',
'settings' => ['time_limit' => '20'],
'is_voting_open' => '1',
'quiz_options' => [
['label' => 'Lyon', 'correct' => '0'],
['label' => 'Paris', 'correct' => '1'],
['label' => ''],
],
];
$this->get("/admin/$token/slides/add?type=quiz");
$this->assertResponseOk();
$this->assertResponseContains('name="quiz_options[0][label]"');
$this->assertResponseNotContains('name="is_voting_open"');
$this->post("/admin/$token/slides/add?type=quiz", ['quiz_options' => [['label' => 'A'], ['label' => 'B']]] + $data);
$this->assertResponseOk();
$this->assertResponseContains('Bitte markiere mindestens eine Antwort als richtig.');
$this->post("/admin/$token/slides/add?type=quiz", $data);
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$slide = $this->slidesTable()->find()->contain(['SlideOptions'])->firstOrFail();
$this->assertSame(['Lyon', 'Paris'], array_map(fn($o) => $o->label, $slide->slide_options));
$this->assertSame([false, true], array_map(fn($o) => $o->is_correct, $slide->slide_options));
$this->assertSame(['time_limit' => 20], $slide->settings);
$this->assertFalse($slide->is_voting_open, 'quiz starts closed until the presenter starts it');
}
public function testPresenterStartsAndEndsQuiz(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QUIZ, ['is_voting_open' => false], ['Richtig', 'Falsch']);
$this->goLive($poll, $slide);
$control = function (string $action) use ($token): array {
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => $action]);
$this->assertResponseOk();
return json_decode((string)$this->_response->getBody(), true);
};
$state = $control('toggle');
$this->assertSame('running', $state['results']['phase']);
$this->assertGreaterThan(25, $state['results']['remaining']);
$this->assertStringNotContainsString('correct', (string)$this->_response->getBody());
$this->assertNotNull($this->slidesTable()->get($slide->id)->voting_ends_at);
$state = $control('toggle');
$this->assertSame('finished', $state['results']['phase']);
$this->assertTrue($state['results']['options'][0]['correct']);
$this->post("/admin/$token/slides/{$slide->id}/reset");
$slide = $this->slidesTable()->get($slide->id);
$this->assertSame(Slide::QUIZ_READY, $slide->quizPhase());
}
public function testEditKeepsVotesOfUnchangedOptions(): void
{
[$poll, $token] = $this->createPoll();
+5 -1
View File
@@ -32,7 +32,7 @@ trait PlenarioTestTrait
* @param \App\Model\Entity\Poll $poll Poll
* @param string $type Slide type
* @param array<string, mixed> $data Extra slide data
* @param list<string> $options Option labels (choice)
* @param list<string> $options Option labels (choice, quiz: the first one is correct)
* @return \App\Model\Entity\Slide Slide incl. slide_options
*/
protected function createSlide(Poll $poll, string $type = Slide::TYPE_CHOICE, array $data = [], array $options = ['Ja', 'Nein']): Slide
@@ -49,6 +49,10 @@ trait PlenarioTestTrait
if ($type === Slide::TYPE_CHOICE) {
$slides->syncOptions($slide, $options);
}
if ($type === Slide::TYPE_QUIZ) {
// First option is the correct one.
$slides->syncOptions($slide, $options, [$options[0]]);
}
return $slides->get($slide->id, contain: ['SlideOptions']);
}
+205
View File
@@ -0,0 +1,205 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Service;
use App\Model\Entity\Poll;
use App\Model\Entity\Slide;
use App\Service\PollStateService;
use App\Service\ResultsService;
use App\Service\VoteException;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\I18n\DateTime;
use Cake\TestSuite\TestCase;
/**
* Quiz slides: phases, time limit, hidden results until the quiz is finished.
*/
class QuizTest extends TestCase
{
use PlenarioTestTrait;
/**
* @var list<string>
*/
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
/**
* @return array{0: \App\Model\Entity\Poll, 1: \App\Model\Entity\Slide}
*/
protected function createQuiz(int $timeLimit = 30): array
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QUIZ, [
'is_voting_open' => false,
'settings' => ['time_limit' => $timeLimit],
], ['Berlin', 'Hamburg', 'München']);
return [$this->goLive($poll, $slide), $slide];
}
protected function setOpen(Slide $slide, bool $open, ?DateTime $now = null): Slide
{
$slide->setVotingOpen($open, $now);
$this->slidesTable()->saveOrFail($slide);
return $this->slidesTable()->get($slide->id, contain: ['SlideOptions']);
}
protected function optionId(Slide $slide, string $label): int
{
foreach ($slide->slide_options as $option) {
if ($option->label === $label) {
return $option->id;
}
}
$this->fail('Unknown option ' . $label);
}
protected function participantState(Poll $poll, string $participant): array
{
return (new PollStateService())->forParticipant($this->pollsTable()->get($poll->id), $participant);
}
public function testSanitizesTimeLimit(): void
{
[, $slide] = $this->createQuiz();
$this->assertSame(30, $slide->setting('time_limit'));
$slide = $this->slidesTable()->patchEntity($slide, ['type' => 'quiz', 'settings' => ['time_limit' => '7']]);
$this->assertSame(30, $slide->setting('time_limit'), 'unknown limits fall back to the default');
$slide = $this->slidesTable()->patchEntity($slide, ['type' => 'quiz', 'settings' => ['time_limit' => '0']]);
$this->assertSame(0, $slide->setting('time_limit'));
}
public function testNotStartedQuizRejectsVotes(): void
{
[$poll, $slide] = $this->createQuiz();
$this->assertSame(Slide::QUIZ_READY, $slide->quizPhase());
$this->expectExceptionMessage('Das Quiz hat noch nicht begonnen.');
(new VoteService())->submit($poll, $slide, 'p1', ['option_id' => $this->optionId($slide, 'Berlin')]);
}
public function testStartingSetsTimerAndReopeningRunningQuizKeepsIt(): void
{
[, $slide] = $this->createQuiz(30);
$now = DateTime::now();
$slide = $this->setOpen($slide, true, $now);
$this->assertSame(Slide::QUIZ_RUNNING, $slide->quizPhase());
$this->assertNotNull($slide->voting_ends_at);
$endsAt = $slide->voting_ends_at->getTimestamp();
$this->assertSame($now->getTimestamp() + 30, $endsAt);
// Idempotent: "open" on a running quiz does not restart the timer.
$slide = $this->setOpen($slide, true, $now->addSeconds(10));
$this->assertSame($endsAt, $slide->voting_ends_at->getTimestamp());
}
public function testQuizWithoutTimeLimitRunsUntilClosed(): void
{
[, $slide] = $this->createQuiz(0);
$slide = $this->setOpen($slide, true);
$this->assertNull($slide->voting_ends_at);
$this->assertNull($slide->remainingSeconds());
$this->assertTrue($slide->isVotingOpen(DateTime::now()->addHours(5)));
$slide = $this->setOpen($slide, false);
$this->assertSame(Slide::QUIZ_FINISHED, $slide->quizPhase());
}
public function testRunningQuizHidesCountsAndCorrectAnswers(): void
{
[$poll, $slide] = $this->createQuiz();
$slide = $this->setOpen($slide, true);
(new VoteService())->submit($poll, $slide, 'p1', ['option_id' => $this->optionId($slide, 'Berlin')]);
$results = (new ResultsService())->forSlide($slide);
$this->assertSame('running', $results['phase']);
$this->assertFalse($results['revealed']);
$this->assertSame(1, $results['participants']);
$this->assertArrayNotHasKey('correctCount', $results);
foreach ($results['options'] as $option) {
$this->assertSame(['id', 'label'], array_keys($option));
}
$state = $this->participantState($poll, 'p1');
$this->assertTrue($state['hasResponded']);
$this->assertNull($state['results']);
$this->assertNull($state['quiz']['answerId']);
$this->assertNull($state['quiz']['isCorrect']);
$this->assertStringNotContainsString('is_correct', (string)json_encode($state));
}
public function testAnswerOnceAndOnlyValidSingleOption(): void
{
[$poll, $slide] = $this->createQuiz();
$slide = $this->setOpen($slide, true);
$service = new VoteService();
foreach ([[], ['option_id' => 999999], ['option_id' => ['1', '2']]] as $data) {
try {
$service->submit($poll, $slide, 'p1', $data);
$this->fail('VoteException expected');
} catch (VoteException $e) {
$this->assertSame('Bitte wähle eine Antwort aus.', $e->getMessage());
}
}
$service->submit($poll, $slide, 'p1', ['option_id' => $this->optionId($slide, 'Hamburg')]);
$this->expectExceptionMessage('Du hast bereits abgestimmt.');
$service->submit($poll, $slide, 'p1', ['option_id' => $this->optionId($slide, 'Berlin')]);
}
public function testFinishedQuizRevealsResults(): void
{
[$poll, $slide] = $this->createQuiz();
$slide = $this->setOpen($slide, true);
$service = new VoteService();
$service->submit($poll, $slide, 'p1', ['option_id' => $this->optionId($slide, 'Berlin')]);
$service->submit($poll, $slide, 'p2', ['option_id' => $this->optionId($slide, 'Hamburg')]);
$service->submit($poll, $slide, 'p3', ['option_id' => $this->optionId($slide, 'Berlin')]);
$slide = $this->setOpen($slide, false);
$results = (new ResultsService())->forSlide($slide);
$this->assertTrue($results['revealed']);
$this->assertSame(2, $results['correctCount']);
$this->assertSame(3, $results['participants']);
$this->assertSame(['Berlin', true, 2], [$results['options'][0]['label'], $results['options'][0]['correct'], $results['options'][0]['count']]);
$this->assertFalse($results['options'][1]['correct']);
// Revealed to participants even without show_results_to_participants.
$poll->show_results_to_participants = false;
$this->pollsTable()->saveOrFail($poll);
$state = $this->participantState($poll, 'p2');
$this->assertSame('finished', $state['quiz']['phase']);
$this->assertFalse($state['quiz']['isCorrect']);
$this->assertSame($this->optionId($slide, 'Hamburg'), $state['quiz']['answerId']);
$this->assertNotNull($state['results']);
$this->assertTrue($this->participantState($poll, 'p1')['quiz']['isCorrect']);
$this->assertNull($this->participantState($poll, 'nobody')['quiz']['isCorrect']);
}
public function testExpiredQuizIsClosedAndRejectsVotes(): void
{
[$poll, $slide] = $this->createQuiz(30);
$slide = $this->setOpen($slide, true, DateTime::now()->subSeconds(60));
$this->assertTrue($slide->is_voting_open);
$this->assertFalse($slide->isVotingOpen());
$this->assertSame(Slide::QUIZ_FINISHED, $slide->quizPhase());
try {
(new VoteService())->submit($poll, $slide, 'p1', ['option_id' => $this->optionId($slide, 'Berlin')]);
$this->fail('VoteException expected');
} catch (VoteException $e) {
$this->assertSame('Die Zeit für dieses Quiz ist abgelaufen.', $e->getMessage());
}
// Reading the state persists the end of the quiz.
$state = $this->participantState($poll, 'p1');
$this->assertFalse($state['isVotingOpen']);
$this->assertFalse($this->slidesTable()->get($slide->id)->is_voting_open);
}
}