*/ 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 scaleWithComments(array $settings = []): array { [$poll, $token] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, [ 'settings' => $settings + ['scale_min' => 1, 'scale_max' => 5, 'comments' => true], ]); $poll = $this->goLive($poll, $slide); $votes = new VoteService(); $votes->submit($poll, $slide, $this->hash('a', $poll), ['value' => 4, 'comment' => ' Sehr ']); $votes->submit($poll, $slide, $this->hash('b', $poll), ['value' => 2, 'comment' => '']); $votes->submit($poll, $slide, $this->hash('c', $poll), ['value' => 3, 'comment' => 'Zu lang']); return [$poll, $token, $slide]; } protected function hash(string $char, Poll $poll): string { return VoteService::participantHash(str_repeat($char, 32), $poll->id); } /** * @return array */ protected function presenterResults(string $token): array { $this->get("/admin/$token/state"); return json_decode((string)$this->_response->getBody(), true)['results']; } public function testSettingIsSanitized(): void { $this->assertTrue(SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['comments' => '1'])['comments']); $this->assertFalse(SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, [])['comments']); [$poll] = $this->createPoll(); $this->assertFalse($this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => []])->hasComments()); } public function testFormSavesCommentSetting(): void { [$poll, $token] = $this->createPoll(); $this->post("/admin/$token/slides/add?type=scale", [ 'type' => 'scale', 'question' => 'Wie fandest du den Abend?', 'settings' => ['scale_min' => '1', 'scale_max' => '5', 'comments' => '1'], ]); $slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail(); $this->assertTrue($slide->hasComments()); } public function testParticipantPageShowsCommentField(): void { [$poll, , $slide] = $this->scaleWithComments(); $this->slidesTable()->Responses->deleteAll(['slide_id' => $slide->id]); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('name="comment"'); $this->assertResponseContains('Kommentar (optional)'); } public function testCommentIsStoredWithTheRating(): void { [$poll, , $slide] = $this->scaleWithComments(); $this->post('/p/' . $poll->join_code . '/vote', ['slide_id' => $slide->id, 'value' => '5', 'comment' => 'Per Formular']); $row = $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->orderBy(['id' => 'DESC'])->firstOrFail(); $this->assertSame(5, $row->numeric_value); $this->assertSame('Per Formular', $row->text_value); } public function testCommentIsIgnoredWithoutCommentField(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['scale_min' => 1, 'scale_max' => 5]]); $poll = $this->goLive($poll, $slide); (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), ['value' => 3, 'comment' => 'Eingeschmuggelt']); $row = $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->firstOrFail(); $this->assertNull($row->text_value); } public function testTooLongCommentIsRejected(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['comments' => true]]); $poll = $this->goLive($poll, $slide); $this->expectException(VoteException::class); (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [ 'value' => 3, 'comment' => str_repeat('x', VoteService::MAX_TEXT_LENGTH + 1), ]); } public function testPresenterGetsCommentsOldestFirst(): void { [, $token] = $this->scaleWithComments(); $results = $this->presenterResults($token); $this->assertSame(3, $results['participants']); $this->assertEqualsWithDelta(3.0, $results['average'], 0.001); $this->assertSame(['Sehr ', 'Zu lang'], array_column($results['comments'], 'text')); $this->assertSame([null, null], array_column($results['comments'], 'value')); } public function testRatingsNextToCommentsAreOptional(): void { $this->assertTrue(SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['comment_ratings' => '1'])['comment_ratings']); $this->assertFalse(SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, [])['comment_ratings']); [, $token] = $this->scaleWithComments(['comment_ratings' => true]); $this->assertSame([4, 3], array_column($this->presenterResults($token)['comments'], 'value')); } public function testCommentIntervalIsNotConfigurable(): void { // The presentation times each comment by its length; there is no setting for it. $settings = SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['comment_interval' => '15']); $this->assertArrayNotHasKey('comment_interval', $settings); [, $token] = $this->scaleWithComments(); $this->assertArrayNotHasKey('commentInterval', $this->presenterResults($token)); $this->get("/admin/$token/slides/add?type=scale"); $this->assertResponseOk(); $this->assertResponseNotContains('comment_interval'); } public function testParticipantsNeverSeeComments(): void { [$poll] = $this->scaleWithComments(); // Participant "a" has already rated – results stay on the stage. $this->cookie('plenario_pid', str_repeat('a', 32)); $this->get('/p/' . $poll->join_code . '/state'); $state = json_decode((string)$this->_response->getBody(), true); $this->assertTrue($state['hasResponded']); $this->assertNull($state['results']); $this->assertResponseNotContains('Zu lang'); $this->get('/p/' . $poll->join_code); $this->assertResponseNotContains('Zu lang'); } public function testEmbedShowsComments(): void { [$poll, , $slide] = $this->scaleWithComments(); $key = $this->pollsTable()->ensureEmbedKey($poll); $this->get("/e/$key/{$slide->id}/state"); $state = json_decode((string)$this->_response->getBody(), true); $this->assertCount(2, $state['results']['comments']); } public function testPrivateScaleCommentsOnlyForTheStage(): void { [$poll, $token, $slide] = $this->scaleWithComments(['private' => true]); // The presenter starts hidden but may show them, so the data is there. $results = $this->presenterResults($token); $this->assertTrue($results['private']); $this->assertCount(2, $results['comments']); $key = $this->pollsTable()->ensureEmbedKey($poll); $this->get("/e/$key/{$slide->id}/state"); $this->assertResponseContains('Sehr'); $this->get('/p/' . $poll->join_code . '/state'); $this->assertResponseNotContains('Sehr'); } public function testAdminEvaluationListsComments(): void { [, $token, $slide] = $this->scaleWithComments(['private' => true]); $this->get("/admin/$token"); $this->assertResponseContains("/slides/{$slide->id}/answers"); $this->get("/admin/$token/slides/{$slide->id}/answers"); $this->assertResponseOk(); $this->assertResponseContains('2 Kommentare'); $this->assertResponseContains('Sehr <gut>'); $this->assertResponseContains('Bewertung 4'); } public function testExportContainsComments(): void { [, $token] = $this->scaleWithComments(); $html = $this->exportHtml($token); $this->assertStringContainsString('2 Kommentare', $html); $this->assertStringContainsString('Sehr <gut>', $html); $this->assertStringNotContainsString('Bewertung 4', $html, 'ratings only with comment_ratings'); } public function testInternalFlagDoesNotRevealRatingsWithoutPrivateSlides(): void { [, $token] = $this->scaleWithComments(); $html = $this->exportHtml($token, true); $this->assertStringContainsString('Sehr <gut>', $html); $this->assertStringNotContainsString('Bewertung 4', $html); $this->assertStringNotContainsString('-intern', (string)$this->_response->getHeaderLine('Content-Disposition')); } public function testCompleteExportShowsRatingsOfAllScales(): void { [$poll, $token] = $this->scaleWithComments(); $this->createSlide($poll, Slide::TYPE_OPEN, ['settings' => ['private' => true]]); $html = $this->exportHtml($token, true); $this->assertStringContainsString('Bewertung 4', $html); } public function testParticipantCanEditOwnCommentWhileOpen(): void { [$poll, , $slide] = $this->scaleWithComments(); $this->cookie('plenario_pid', str_repeat('a', 32)); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('Kommentar bearbeiten'); $this->assertResponseContains('Sehr <gut>'); $this->post('/p/' . $poll->join_code . '/edit', ['slide_id' => $slide->id, 'comment' => ' Sehr gut! ', 'value' => '1']); $this->assertRedirect('/p/' . $poll->join_code); $this->assertFlashMessage('Dein Kommentar wurde gespeichert.'); $rows = $this->slidesTable()->Responses->find() ->where(['slide_id' => $slide->id, 'participant_hash' => $this->hash('a', $poll)]) ->all(); $this->assertCount(1, $rows); $this->assertSame('Sehr gut!', $rows->first()->text_value); $this->assertSame(4, $rows->first()->numeric_value, 'the rating stays unchanged'); // Repeating the request is harmless, an empty comment removes it. $this->post('/p/' . $poll->join_code . '/edit', ['slide_id' => $slide->id, 'comment' => ' Sehr gut! ']); $this->post('/p/' . $poll->join_code . '/edit', ['slide_id' => $slide->id, 'comment' => '']); $this->assertFlashMessage('Dein Kommentar wurde entfernt.'); $this->assertNull($this->slidesTable()->Responses->get($rows->first()->id)->text_value); } public function testParticipantWithoutCommentCanAddOne(): void { [$poll, , $slide] = $this->scaleWithComments(); $this->cookie('plenario_pid', str_repeat('b', 32)); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('Kommentar hinzufügen'); $this->post('/p/' . $poll->join_code . '/edit', ['slide_id' => $slide->id, 'comment' => 'Nachgereicht']); $row = $this->slidesTable()->Responses->find() ->where(['slide_id' => $slide->id, 'participant_hash' => $this->hash('b', $poll)]) ->firstOrFail(); $this->assertSame('Nachgereicht', $row->text_value); } public function testCommentCannotBeEditedAfterVotingClosed(): void { [$poll, , $slide] = $this->scaleWithComments(); $this->slidesTable()->updateAll(['is_voting_open' => false], ['id' => $slide->id]); $this->cookie('plenario_pid', str_repeat('a', 32)); $this->get('/p/' . $poll->join_code); $this->assertResponseNotContains('Kommentar bearbeiten'); $this->post('/p/' . $poll->join_code . '/edit', ['slide_id' => $slide->id, 'comment' => 'Zu spät']); $this->assertFlashMessage('Die Abstimmung zu dieser Frage ist geschlossen.'); $this->assertSame('Sehr ', $this->slidesTable()->Responses->find() ->where(['slide_id' => $slide->id, 'participant_hash' => $this->hash('a', $poll)]) ->firstOrFail()->text_value); } public function testCommentEditNeedsARatingAndACommentField(): void { [$poll, , $slide] = $this->scaleWithComments(); $votes = new VoteService(); try { $votes->editText($poll, $slide, $this->hash('z', $poll), ['comment' => 'Ohne Bewertung']); $this->fail('Expected VoteException'); } catch (VoteException $e) { $this->assertSame('Bitte gib zuerst deine Bewertung ab.', $e->getMessage()); } $this->assertFalse($this->slidesTable()->Responses->exists(['text_value' => 'Ohne Bewertung'])); $plain = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['scale_min' => 1, 'scale_max' => 5]]); $poll = $this->goLive($poll, $plain); $votes->submit($poll, $plain, $this->hash('a', $poll), ['value' => 3]); $this->expectException(VoteException::class); $votes->editText($poll, $plain, $this->hash('a', $poll), ['comment' => 'Eingeschmuggelt']); } }