*/ protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; protected function setUp(): void { parent::setUp(); $this->enableCsrfToken(); } /** * @param array $settings Extra settings * @return array{0: \App\Model\Entity\Poll, 1: string, 2: \App\Model\Entity\Slide} */ protected function choiceWithComments(array $settings = ['comments' => true, 'multiple' => true]): array { [$poll, $token] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_CHOICE, ['settings' => $settings], ['Ja', 'Nein', 'Vielleicht']); $poll = $this->goLive($poll, $slide); return [$poll, $token, $slide]; } protected function hash(string $char, Poll $poll): string { return VoteService::participantHash(str_repeat($char, 32), $poll->id); } public function testSettingIsSanitized(): void { $this->assertTrue(SlidesTable::sanitizeSettings(Slide::TYPE_CHOICE, ['comments' => '1'])['comments']); $this->assertFalse(SlidesTable::sanitizeSettings(Slide::TYPE_CHOICE, [])['comments']); } public function testFormShowsAndSavesCommentSetting(): void { [, $token] = $this->createPoll(); $this->get("/admin/$token/slides/add?type=choice"); $this->assertResponseContains('name="settings[comments]"'); $this->post("/admin/$token/slides/add?type=choice", [ 'type' => 'choice', 'question' => 'Was nehmen wir?', 'options_text' => "Ja\nNein", 'settings' => ['comments' => '1'], ]); $slide = $this->slidesTable()->find()->firstOrFail(); $this->assertTrue($slide->hasComments()); } public function testParticipantPageShowsCommentFieldOnlyWhenEnabled(): void { [$poll] = $this->choiceWithComments(); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('name="comment"'); [$other] = $this->choiceWithComments(['multiple' => true]); $this->get('/p/' . $other->join_code); $this->assertResponseNotContains('name="comment"'); } public function testCommentIsStoredOnceInFirstRowAndCountsStayCorrect(): void { [$poll, , $slide] = $this->choiceWithComments(); $options = array_map(fn($o) => $o->id, $slide->slide_options); (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [ 'option_ids' => [$options[0], $options[2]], 'comment' => ' Weil ', ]); $rows = $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->orderBy(['id' => 'ASC'])->all()->toList(); $this->assertCount(2, $rows); $this->assertSame('Weil ', $rows[0]->text_value); $this->assertNull($rows[1]->text_value); $results = (new ResultsService())->forSlide($slide); $this->assertSame([1, 0, 1], array_column($results['options'], 'count')); $this->assertSame(2, $results['votes']); $this->assertSame(1, $results['participants']); $this->assertSame(['Weil '], array_column($results['comments'], 'text')); $this->assertSame([null], array_column($results['comments'], 'value')); } public function testCommentIsIgnoredWithoutCommentField(): void { [$poll, , $slide] = $this->choiceWithComments(['multiple' => false]); (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [ 'option_id' => $slide->slide_options[0]->id, 'comment' => 'Eingeschmuggelt', ]); $row = $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->firstOrFail(); $this->assertNull($row->text_value); $this->assertSame([], (new ResultsService())->forSlide($slide)['comments']); } public function testTooLongCommentIsRejected(): void { [$poll, , $slide] = $this->choiceWithComments(); $this->expectException(VoteException::class); (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [ 'option_ids' => [$slide->slide_options[0]->id], 'comment' => str_repeat('x', VoteService::MAX_TEXT_LENGTH + 1), ]); } public function testCommentCanBeEditedAddedAndRemoved(): void { [$poll, , $slide] = $this->choiceWithComments(); $hash = $this->hash('a', $poll); $votes = new VoteService(); $votes->submit($poll, $slide, $hash, [ 'option_ids' => [$slide->slide_options[0]->id, $slide->slide_options[1]->id], ]); $this->assertNull($votes->ownText($slide, $hash)); $votes->editText($poll, $slide, $hash, ['comment' => 'Nachgereicht']); $this->assertSame('Nachgereicht', $votes->ownText($slide, $hash)); $votes->editText($poll, $slide, $hash, ['comment' => 'Korrigiert']); $this->assertSame(['Korrigiert'], array_column((new ResultsService())->comments($slide), 'text')); $this->assertSame(2, $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->count()); $votes->editText($poll, $slide, $hash, ['comment' => '']); $this->assertNull($votes->ownText($slide, $hash)); $this->assertSame([], (new ResultsService())->comments($slide)); } public function testEditRequiresAnAnswer(): void { [$poll, , $slide] = $this->choiceWithComments(); $this->expectException(VoteException::class); (new VoteService())->editText($poll, $slide, $this->hash('z', $poll), ['comment' => 'Ohne Auswahl']); } public function testPresenterGetsCommentsButParticipantsNever(): void { [$poll, $token, $slide] = $this->choiceWithComments(); (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [ 'option_ids' => [$slide->slide_options[0]->id], 'comment' => 'Sichtbar auf der Bühne', ]); $this->get("/admin/$token/state"); $results = json_decode((string)$this->_response->getBody(), true)['results']; $this->assertSame(['Sichtbar auf der Bühne'], array_column($results['comments'], 'text')); $this->get('/p/' . $poll->join_code . '/state'); $state = json_decode((string)$this->_response->getBody(), true); $this->assertNull($state['results']); $this->assertStringNotContainsString('Sichtbar auf der Bühne', (string)$this->_response->getBody()); } public function testAnswersPageListsCommentsOfChoiceSlides(): void { [$poll, $token, $slide] = $this->choiceWithComments(); (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [ 'option_ids' => [$slide->slide_options[0]->id], 'comment' => 'Erster ', ]); $this->get("/admin/$token/slides/{$slide->id}/answers"); $this->assertResponseOk(); $this->assertResponseContains('Erster <Kommentar>'); } public function testAnswersPageIsNotFoundForChoiceWithoutComments(): void { [, $token, $slide] = $this->choiceWithComments(['multiple' => false]); $this->get("/admin/$token/slides/{$slide->id}/answers"); $this->assertResponseCode(404); } public function testExportContainsComments(): void { [$poll, $token, $slide] = $this->choiceWithComments(); (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [ 'option_ids' => [$slide->slide_options[0]->id], 'comment' => 'Im PDF enthalten', ]); $this->assertStringContainsString('Im PDF enthalten', $this->exportHtml($token)); } }