Files
Plenario/tests/TestCase/Service/VoteServiceTest.php
T
gnilebeinandClaude Opus 5.5 bd17a97487
CI / Deploy / test (push) Successful in 42s
CI / Deploy / deploy (push) Skipped
Remove per-participant word limit from word clouds
Participants can now always submit any number of words. Drops the
max_words setting, its form field, the limit checks in VoteService and
the remaining/limitReached state; existing max_words values in stored
settings are ignored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 14:05:20 +01:00

214 lines
8.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Service;
use App\Model\Entity\Slide;
use App\Model\Table\SlidesTable;
use App\Service\PollStateService;
use App\Service\ResultsService;
use App\Service\VoteException;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\TestSuite\TestCase;
/**
* Validation rules of VoteService and aggregation of ResultsService.
*/
class VoteServiceTest extends TestCase
{
use PlenarioTestTrait;
/**
* @var list<string>
*/
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 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], $settings);
$settings = SlidesTable::sanitizeSettings(Slide::TYPE_WORDCLOUD, ['allow_downvotes' => '0']);
$this->assertSame(['allow_upvotes' => true, 'allow_downvotes' => false], $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']);
}
}