Store answers atomically and limit word cloud input
CI / Deploy / test (push) Successful in 51s
CI / Deploy / deploy (push) Skipped

- Run the checks and the insert of VoteService::submit() in one transaction
  with the slide row locked, so double clicks or retries cannot store a vote
  twice (also covers the Q&A upvote budget and word cloud duplicates).
- Limit word cloud terms per request and per participant, and throttle
  answers per IP and minute (THROTTLE_VOTES_PER_MINUTE, 429 with message).
- Treat array values in text fields as empty instead of storing "Array".
- Move the throttle helper to AppController for reuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-08 22:01:19 +01:00
1 parent 8b0e82d806
commit 9c532d164f
11 files changed
+224 -50

No files matched your search

@@ -210,4 +210,73 @@ class VoteServiceTest extends TestCase
$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);
}
}