*/ protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; public function testParticipantHashIsScopedPerPoll(): void { $this->assertNotSame(VoteService::participantHash('abc', 1), VoteService::participantHash('abc', 2)); $this->assertSame(VoteService::participantHash('abc', 1), VoteService::participantHash('abc', 1)); } public function testSingleChoiceRejectsMultipleOptions(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_CHOICE, [], ['A', 'B']); $poll = $this->goLive($poll, $slide); $ids = array_map(fn($o) => $o->id, $slide->slide_options); $this->expectException(VoteException::class); (new VoteService())->submit($poll, $slide, 'p1', ['option_ids' => $ids]); } public function testChoiceIgnoresForeignOptionIds(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_CHOICE); $poll = $this->goLive($poll, $slide); $this->expectExceptionMessage('Bitte wähle eine Antwort aus.'); (new VoteService())->submit($poll, $slide, 'p1', ['option_id' => 999999]); } public function testClosedVotingIsRejected(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_OPEN, ['is_voting_open' => false]); $poll = $this->goLive($poll, $slide); $this->expectExceptionMessage('geschlossen'); (new VoteService())->submit($poll, $slide, 'p1', ['text' => 'Hallo']); } public function testScaleRange(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['scale_min' => 1, 'scale_max' => 3]]); $poll = $this->goLive($poll, $slide); $this->expectExceptionMessage('zwischen 1 und 3'); (new VoteService())->submit($poll, $slide, 'p1', ['value' => '4']); } public function testScaleCommentOnlyCountsAsParticipantButNotInAverage(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['scale_min' => 1, 'scale_max' => 5, 'comments' => true, 'comment_only' => true]]); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['value' => '4']); $service->submit($poll, $slide, 'p2', ['comment' => 'Kann ich nicht beurteilen']); $results = (new ResultsService())->forSlide($slide); $this->assertSame(2, $results['participants']); $this->assertSame(1, $results['commentOnly']); $this->assertSame(4.0, (float)$results['average']); $this->assertSame(1, array_sum(array_column($results['buckets'], 'count'))); $this->assertSame(['Kann ich nicht beurteilen'], array_column($results['comments'], 'text')); $this->assertSame([null], array_column((new ResultsService())->comments($slide), 'value')); // Only once per person, like any answer. $this->expectExceptionMessage('bereits abgestimmt'); $service->submit($poll, $slide, 'p2', ['value' => '2']); } public function testScaleWithoutRatingNeedsCommentOnlySetting(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['comments' => true]]); $poll = $this->goLive($poll, $slide); $this->expectExceptionMessage('zwischen 1 und 5'); (new VoteService())->submit($poll, $slide, 'p1', ['comment' => 'Nur Kommentar']); } public function testScaleNeedsRatingOrComment(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['comments' => true, 'comment_only' => true]]); $poll = $this->goLive($poll, $slide); $this->expectExceptionMessage('oder schreibe einen Kommentar'); (new VoteService())->submit($poll, $slide, 'p1', ['comment' => ' ']); } public function testCommentOnlyAnswerCanBeEditedButNotEmptied(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['comments' => true, 'comment_only' => true]]); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['comment' => 'Erst']); $service->editText($poll, $slide, 'p1', ['comment' => 'Dann']); $this->assertSame('Dann', $service->ownText($slide, 'p1')); $this->expectExceptionMessage('nicht leer sein'); $service->editText($poll, $slide, 'p1', ['comment' => '']); } public function testMultipleChoiceResults(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_CHOICE, ['settings' => ['multiple' => true]], ['A', 'B', 'C']); $poll = $this->goLive($poll, $slide); [$a, $b] = array_map(fn($o) => $o->id, $slide->slide_options); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['option_ids' => [$a, $b]]); $service->submit($poll, $slide, 'p2', ['option_ids' => [$a]]); $results = (new ResultsService())->forSlide($slide); $this->assertSame(2, $results['participants']); $this->assertSame(3, $results['votes']); $this->assertSame([2, 1, 0], array_column($results['options'], 'count')); $this->assertEqualsWithDelta(66.7, $results['options'][0]['percent'], 0.01); } public function testWordcloudGroupsCaseInsensitively(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['words' => ['Kaffee', ' tee ', 'Wasser']]); $service->submit($poll, $slide, 'p2', ['words' => ['kaffee', '', 'KAFFEE']]); $results = (new ResultsService())->forSlide($slide); $this->assertSame(['Kaffee', 'tee', 'Wasser'], array_column($results['words'], 'text')); $this->assertSame(['kaffee', 'tee', 'wasser'], array_column($results['words'], 'key')); $this->assertSame([2, 1, 1], array_column($results['words'], 'count')); $this->assertEquals(1, $results['words'][0]['weight']); } public function testWordcloudAcceptsWordsOneByOneAndCountsRepeatsAsUpvote(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee']); $service->submit($poll, $slide, 'p1', ['word' => 'Tee']); $service->submit($poll, $slide, 'p1', ['word' => 'Wasser']); $service->submit($poll, $slide, 'p1', ['word' => 'Saft']); // Entering an existing word of someone else = upvote. $service->submit($poll, $slide, 'p2', ['word' => 'kaffee ']); $results = (new ResultsService())->forSlide($slide); $this->assertSame(2, $results['participants']); $this->assertSame('Kaffee', $results['words'][0]['text']); $this->assertSame(2, $results['words'][0]['count']); $this->assertCount(4, $results['words']); } public function testWordcloudRejectsOwnDuplicate(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee']); $this->expectExceptionMessage('Diesen Begriff hast du schon genannt.'); $service->submit($poll, $slide, 'p1', ['word' => 'KAFFEE']); } public function testWordcloudSettingsSanitizing(): void { $settings = SlidesTable::sanitizeSettings(Slide::TYPE_WORDCLOUD, ['max_words' => '3', 'allow_upvotes' => '0']); $this->assertSame(['allow_upvotes' => false, 'allow_downvotes' => true, 'time_limit' => 0], $settings); $settings = SlidesTable::sanitizeSettings(Slide::TYPE_WORDCLOUD, ['allow_downvotes' => '0']); $this->assertSame(['allow_upvotes' => true, 'allow_downvotes' => false, 'time_limit' => 0], $settings); } public function testWordcloudDownvotes(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee']); $service->submit($poll, $slide, 'p2', ['word' => 'kaffee']); $service->submit($poll, $slide, 'p3', ['word' => 'Kaffee', 'vote' => 'down']); $service->submit($poll, $slide, 'p3', ['word' => 'Tee']); $results = (new ResultsService())->forSlide($slide); $this->assertSame(3, $results['participants']); $this->assertSame(['Kaffee', 'Tee'], array_column($results['words'], 'text')); $this->assertSame([2, 1], array_column($results['words'], 'count')); $this->assertSame([1, 0], array_column($results['words'], 'down')); $state = (new PollStateService())->wordState($slide, 'p3'); $this->assertSame(['tee'], $state['mine']); $this->assertSame(['kaffee'], $state['down']); $this->assertTrue($state['allowDownvotes']); } public function testWordcloudDownvoteRules(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee']); $cases = [ ['p1', 'Kaffee', 'Du hast diesen Begriff selbst genannt.'], ['p2', 'Wasser', 'Diesen Begriff gibt es nicht in der Wortwolke.'], ]; foreach ($cases as [$participant, $word, $expected]) { try { $service->submit($poll, $slide, $participant, ['word' => $word, 'vote' => 'down']); $this->fail('VoteException expected for ' . $word); } catch (VoteException $e) { $this->assertSame($expected, $e->getMessage()); } } $service->submit($poll, $slide, 'p2', ['word' => 'Kaffee', 'vote' => 'down']); $this->expectExceptionMessage('Diesen Begriff hast du bereits abgelehnt.'); // Upvoting after a downvote is not possible. $service->submit($poll, $slide, 'p2', ['word' => 'Kaffee']); } public function testWordcloudDownvotesCanBeDisabled(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['allow_downvotes' => false]]); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee']); $this->expectExceptionMessage('Downvotes sind bei dieser Frage nicht möglich.'); $service->submit($poll, $slide, 'p2', ['word' => 'Kaffee', 'vote' => 'down']); } public function testWordcloudLimitsWordsPerRequest(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $slide); $words = array_map(fn(int $i) => 'Begriff ' . $i, range(1, VoteService::MAX_WORDS_PER_REQUEST + 1)); $this->expectExceptionMessage('höchstens ' . VoteService::MAX_WORDS_PER_REQUEST . ' Begriffe auf einmal'); (new VoteService())->submit($poll, $slide, 'p1', ['words' => $words]); } public function testWordcloudLimitsWordsPerParticipant(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $slide); $service = new VoteService(); foreach (array_chunk(range(1, VoteService::MAX_WORDS), VoteService::MAX_WORDS_PER_REQUEST) as $chunk) { $service->submit($poll, $slide, 'p1', ['words' => array_map(fn(int $i) => 'Begriff ' . $i, $chunk)]); } $this->assertSame(VoteService::MAX_WORDS, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count()); // Others can still add words. $service->submit($poll, $slide, 'p2', ['word' => 'Noch einer']); $this->expectExceptionMessage('höchstens ' . VoteService::MAX_WORDS . ' Begriffe'); $service->submit($poll, $slide, 'p1', ['word' => 'Einer zu viel']); } public function testArrayInputIsNeverStoredAsText(): void { [$poll] = $this->createPoll(); $open = $this->createSlide($poll, Slide::TYPE_OPEN); $poll = $this->goLive($poll, $open); $service = new VoteService(); try { $service->submit($poll, $open, 'p1', ['text' => ['injected']]); $this->fail('Array input must be rejected.'); } catch (VoteException $e) { $this->assertSame('Bitte gib eine Antwort ein.', $e->getMessage()); } $cloud = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $cloud); $service->submit($poll, $cloud, 'p1', ['words' => [['nested'], 'Kaffee']]); $texts = $this->fetchTable('Responses')->find()->where(['slide_id' => $cloud->id])->all()->extract('text_value')->toList(); $this->assertSame(['Kaffee'], $texts); } public function testRejectedVoteLeavesNoRows(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_CHOICE); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['option_id' => $slide->slide_options[0]->id]); try { // A repeated request (double click, retry) is rejected and stores nothing. $service->submit($poll, $slide, 'p1', ['option_id' => $slide->slide_options[1]->id]); $this->fail('Second vote must be rejected.'); } catch (VoteException $e) { $this->assertSame('Du hast bereits abgestimmt.', $e->getMessage()); } $this->assertSame(1, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count()); // The lock does not touch the slide. $this->assertEquals($slide->modified, $this->slidesTable()->get($slide->id)->modified); $this->assertSame($slide->position, $this->slidesTable()->get($slide->id)->position); } public function testWordcloudOwnEntryCanBeRemoved(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $poll = $this->goLive($poll, $slide); $service = new VoteService(); $service->submit($poll, $slide, 'p1', ['word' => 'Kafee']); $service->submit($poll, $slide, 'p1', ['word' => 'Tee']); $service->submit($poll, $slide, 'p2', ['word' => 'tee']); $service->submit($poll, $slide, 'p2', ['word' => 'Kafee', 'vote' => 'down']); $state = (new PollStateService())->wordState($slide, 'p1'); $this->assertSame([['text' => 'Kafee', 'down' => false], ['text' => 'Tee', 'down' => false]], $state['own']); // Only the own row goes: "Tee" stays with the other upvote, "Kafee" keeps p2's downvote. $service->submit($poll, $slide, 'p1', ['word' => 'TEE', 'vote' => 'remove']); $service->submit($poll, $slide, 'p1', ['word' => 'Kafee', 'vote' => 'remove']); $results = (new ResultsService())->forSlide($slide); $this->assertSame([['tee', 1, 0]], array_map(fn($w) => [$w['text'], $w['count'], $w['down']], $results['words'])); $this->assertSame([], (new PollStateService())->wordState($slide, 'p1')['own']); $this->assertSame(2, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count()); // A downvote can be withdrawn too; afterwards the word can be named again. $service->submit($poll, $slide, 'p2', ['word' => 'kafee', 'vote' => 'remove']); $this->assertSame([['text' => 'tee', 'down' => false]], (new PollStateService())->wordState($slide, 'p2')['own']); $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee']); $this->assertSame(['kaffee'], (new PollStateService())->wordState($slide, 'p1')['mine']); // Removing again (or someone else's word) changes nothing. foreach ([['p1', 'Tee'], ['p3', 'Kaffee'], ['p1', '']] as [$participant, $word]) { try { $service->submit($poll, $slide, $participant, ['word' => $word, 'vote' => 'remove']); $this->fail('VoteException expected for ' . $participant . '/' . $word); } catch (VoteException $e) { $this->assertSame('Diesen Begriff hast du nicht genannt.', $e->getMessage()); } } $this->assertSame(2, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count()); $this->slidesTable()->updateAll(['is_voting_open' => false], ['id' => $slide->id]); $slide = $this->slidesTable()->get($slide->id); $this->expectException(VoteException::class); $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee', 'vote' => 'remove']); } }