*/ protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; protected function setUp(): void { parent::setUp(); $this->enableCsrfToken(); } /** * @return array */ protected function control(string $token, string $action): array { $this->configRequest(['headers' => ['Accept' => 'application/json']]); $this->post("/admin/$token/control", ['action' => $action]); $this->assertResponseOk(); return json_decode((string)$this->_response->getBody(), true); } /** * A fresh slide of the given type with a time limit (waits for the presenter). */ protected function createTimed(Poll $poll, string $type = Slide::TYPE_CHOICE, int $limit = 30): Slide { $slide = $this->createSlide($poll, $type, ['settings' => ['time_limit' => $limit] + Slide::DEFAULT_SETTINGS[$type]]); $slide->startFresh(); return $this->slidesTable()->saveOrFail($slide); } public function testSlideWithoutLimitStaysOpenAndHasNoPhases(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll); $slide->startFresh(); $this->assertFalse($slide->usesPhases()); $this->assertNull($slide->phase()); $this->assertTrue($slide->isVotingOpen()); $this->assertTrue($slide->isFresh()); } public function testTimedSlideWaitsUntilStartedAndShowsNoResults(): void { [$poll] = $this->createPoll(); $slide = $this->createTimed($poll); $poll = $this->goLive($poll, $slide); $this->assertTrue($slide->usesPhases()); $this->assertSame(Slide::QUIZ_READY, $slide->phase()); $this->assertFalse($slide->isVotingOpen()); $this->assertTrue($slide->isFresh()); // Presenter: only the question, no answers, no counts. $state = (new PollStateService())->forPresenter($poll); $this->assertTrue($state['results']['ready']); $this->assertSame(0, $state['results']['participants']); $this->assertArrayNotHasKey('options', $state['results']); $this->assertSame(Slide::QUIZ_READY, $state['results']['timer']['phase']); // Participants know about the timer, can not vote yet. $participant = (new PollStateService())->forParticipant($poll, str_repeat('a', 32)); $this->assertSame(Slide::QUIZ_READY, $participant['timer']['phase']); $this->assertFalse($participant['isVotingOpen']); $this->post('/p/' . $poll->join_code . '/vote', ['slide_id' => $slide->id, 'option_id' => $slide->slide_options[0]->id]); $this->assertRedirect(); $this->assertFlashMessage('Die Abstimmung hat noch nicht begonnen.'); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('Die Abstimmung startet gleich'); $this->assertResponseNotContains('name="option_id"'); } public function testOrderSolutionDoesNotLeakBeforeTheCountdownStarts(): void { [$poll] = $this->createPoll(); $slide = $this->createTimed($poll, Slide::TYPE_ORDER); $poll = $this->goLive($poll, $slide); $this->assertFalse($slide->isOrderRevealed()); $state = (new PollStateService())->forPresenter($poll); $this->assertTrue($state['results']['ready']); $this->assertArrayNotHasKey('items', $state['results']); $participant = (new PollStateService())->forParticipant($poll, str_repeat('b', 32)); $this->assertFalse($participant['order']['revealed']); $this->assertNull($participant['results']); } public function testNextStartsEndsAndMovesOn(): void { [$poll, $token] = $this->createPoll(); $slide = $this->createTimed($poll); $after = $this->createSlide($poll, Slide::TYPE_CONTENT); $this->goLive($poll, $slide); // 1st: start the countdown, the slide stays. $state = $this->control($token, 'next'); $this->assertSame($slide->id, $state['slide']['id']); $running = $this->slidesTable()->get($slide->id); $this->assertSame(Slide::QUIZ_RUNNING, $running->phase()); $this->assertNotNull($running->voting_ends_at); $this->assertSame(Slide::QUIZ_RUNNING, $state['results']['timer']['phase']); $this->assertGreaterThan(0, $state['results']['timer']['remaining']); $this->assertArrayNotHasKey('ready', $state['results']); // 2nd: end it, results stay on stage. $state = $this->control($token, 'next'); $this->assertSame($slide->id, $state['slide']['id']); $this->assertSame(Slide::QUIZ_FINISHED, $this->slidesTable()->get($slide->id)->phase()); // 3rd: next slide. Back goes straight back without restarting. $state = $this->control($token, 'next'); $this->assertSame($after->id, $state['slide']['id']); $state = $this->control($token, 'prev'); $this->assertSame($slide->id, $state['slide']['id']); $this->assertSame(Slide::QUIZ_FINISHED, $this->slidesTable()->get($slide->id)->phase()); } public function testSlideWithoutLimitMovesOnWithTheFirstNext(): void { [$poll, $token] = $this->createPoll(); $slide = $this->createSlide($poll); $after = $this->createSlide($poll, Slide::TYPE_CONTENT); $this->goLive($poll, $slide); $state = $this->control($token, 'next'); $this->assertSame($after->id, $state['slide']['id']); } public function testRestartingTheCountdownWithTheToggle(): void { [$poll, $token] = $this->createPoll(); $slide = $this->createTimed($poll, Slide::TYPE_WORDCLOUD, 10); $this->goLive($poll, $slide); $this->control($token, 'toggle'); $this->assertSame(Slide::QUIZ_RUNNING, $this->slidesTable()->get($slide->id)->phase()); $this->control($token, 'toggle'); $this->assertSame(Slide::QUIZ_FINISHED, $this->slidesTable()->get($slide->id)->phase()); $this->control($token, 'toggle'); $reopened = $this->slidesTable()->get($slide->id); $this->assertSame(Slide::QUIZ_RUNNING, $reopened->phase()); $this->assertLessThanOrEqual(10, $reopened->remainingSeconds()); } public function testRunningVotingResultsAreShownLive(): void { [$poll, $token] = $this->createPoll(); $slide = $this->createTimed($poll); $poll = $this->goLive($poll, $slide); $this->control($token, 'next'); $this->cookie('plenario_pid', str_repeat('c', 32)); $this->post('/p/' . $poll->join_code . '/vote', ['slide_id' => $slide->id, 'option_id' => $slide->slide_options[0]->id]); $this->assertRedirect(); $state = (new PollStateService())->forPresenter($this->pollsTable()->get($poll->id)); $this->assertArrayNotHasKey('ready', $state['results']); $this->assertSame(1, $state['results']['participants']); $this->assertSame(1, $state['results']['options'][0]['count']); } public function testExpiredCountdownClosesTheSlide(): void { [$poll] = $this->createPoll(); $slide = $this->createTimed($poll); $slide->setVotingOpen(true); $slide->voting_ends_at = $slide->voting_started_at->subSeconds(1); $this->slidesTable()->saveOrFail($slide); $poll = $this->goLive($poll, $slide); $state = (new PollStateService())->forPresenter($poll); $this->assertSame(Slide::QUIZ_FINISHED, $state['results']['timer']['phase']); $this->assertFalse($state['slide']['isVotingOpen']); } public function testFormSavesTheLimitAndStartsTheNewSlideReady(): void { [$poll, $token] = $this->createPoll(); $this->post("/admin/$token/slides/add?type=choice", [ 'type' => 'choice', 'question' => 'Mit Zeit?', 'options_text' => "Ja\nNein", 'settings' => ['time_limit' => '45'], ]); $this->assertRedirect(['_name' => 'admin', 'token' => $token]); $slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail(); $this->assertSame(45, $slide->setting('time_limit')); $this->assertSame(Slide::QUIZ_READY, $slide->phase()); // Removing the countdown from a slide nobody started opens it again. $this->post("/admin/$token/slides/{$slide->id}/edit", [ 'type' => 'choice', 'question' => 'Mit Zeit?', 'options_text' => "Ja\nNein", 'settings' => ['time_limit' => '0'], ]); $slide = $this->slidesTable()->get($slide->id); $this->assertNull($slide->phase()); $this->assertTrue($slide->isVotingOpen()); // An invalid limit falls back to "none". $this->post("/admin/$token/slides/{$slide->id}/edit", [ 'type' => 'choice', 'question' => 'Mit Zeit?', 'options_text' => "Ja\nNein", 'settings' => ['time_limit' => '7'], ]); $this->assertSame(0, $this->slidesTable()->get($slide->id)->setting('time_limit')); // Q&A has no countdown. $this->assertFalse(Slide::supportsTimer(Slide::TYPE_QA)); $this->assertArrayNotHasKey('time_limit', Slide::DEFAULT_SETTINGS[Slide::TYPE_QA]); } public function testEvaluationIsNotGatedByTheCountdown(): void { [$poll] = $this->createPoll(); $slide = $this->createTimed($poll); $results = (new ResultsService())->forSlide($slide); $this->assertArrayNotHasKey('ready', $results); $this->assertSame(Slide::QUIZ_READY, $results['timer']['phase']); } }