CakePHP 5.4 app for live polls: create via email without login, manage via secret admin link, join via code. Includes slide types (choice, wordcloud, open, scale, content), presenter mode, CSV export, captcha, throttling, tests and Gitea CI/deploy workflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
234 lines
9.9 KiB
PHP
234 lines
9.9 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 testWordcloudGroupsCaseInsensitivelyAndLimitsWords(): void
|
|
{
|
|
[$poll] = $this->createPoll();
|
|
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['max_words' => 2]]);
|
|
$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'], array_column($results['words'], 'text'));
|
|
$this->assertSame(['kaffee', 'tee'], array_column($results['words'], 'key'));
|
|
$this->assertSame([2, 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, ['settings' => ['max_words' => 0]]);
|
|
$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 testWordcloudLimitCountsAcrossSubmits(): void
|
|
{
|
|
[$poll] = $this->createPoll();
|
|
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['max_words' => 2]]);
|
|
$poll = $this->goLive($poll, $slide);
|
|
|
|
$service = new VoteService();
|
|
$service->submit($poll, $slide, 'p1', ['word' => 'Kaffee']);
|
|
$service->submit($poll, $slide, 'p1', ['word' => 'Tee']);
|
|
|
|
$state = (new PollStateService())->wordState($slide, 'p1');
|
|
$this->assertSame(['kaffee', 'tee'], $state['mine']);
|
|
$this->assertSame(0, $state['remaining']);
|
|
$this->assertTrue($state['limitReached']);
|
|
|
|
$this->expectExceptionMessage('Du hast bereits alle 2 Begriffe abgegeben.');
|
|
$service->submit($poll, $slide, 'p1', ['word' => 'Wasser']);
|
|
}
|
|
|
|
public function testWordcloudSettingsSanitizing(): void
|
|
{
|
|
$settings = SlidesTable::sanitizeSettings(Slide::TYPE_WORDCLOUD, ['max_words' => '-3', 'allow_upvotes' => '0']);
|
|
$this->assertSame(['max_words' => 0, 'allow_upvotes' => false, 'allow_downvotes' => true], $settings);
|
|
$settings = SlidesTable::sanitizeSettings(Slide::TYPE_WORDCLOUD, ['max_words' => '999', 'allow_downvotes' => '0']);
|
|
$this->assertSame(['max_words' => 50, 'allow_upvotes' => true, 'allow_downvotes' => false], $settings);
|
|
}
|
|
|
|
public function testWordcloudDownvotes(): void
|
|
{
|
|
[$poll] = $this->createPoll();
|
|
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['max_words' => 1]]);
|
|
$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']);
|
|
// Downvotes do not count towards max_words.
|
|
$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']);
|
|
}
|
|
}
|