*/ 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 testReadyQuizHidesAnswerOptions(): void { [$poll, $slide] = $this->createQuiz(); $results = (new ResultsService())->forSlide($slide); $this->assertSame('ready', $results['phase']); $this->assertSame([], $results['options']); $state = $this->participantState($poll, 'p1'); $this->assertStringNotContainsString('Berlin', (string)json_encode($state)); $slide = $this->setOpen($slide, true); $results = (new ResultsService())->forSlide($slide); $this->assertSame(['Berlin', 'Hamburg', 'München'], array_column($results['options'], 'label')); } 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']); // Participants get the solution and their own verdict, but no distribution. $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->assertSame([true, false], array_slice(array_column($state['results']['options'], 'correct'), 0, 2)); $this->assertArrayNotHasKey('correctCount', $state['results']); $this->assertArrayNotHasKey('count', $state['results']['options'][0]); $this->assertArrayNotHasKey('percent', $state['results']['options'][0]); $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); } }