Files
Plenario/tests/TestCase/Service/QaTest.php
gnilebeinandClaude Sonnet 5.5 575fb8649d
CI / Deploy / test (push) Successful in 58s
CI / Deploy / deploy (push) Skipped
Make Q&A upvote budget a share of the questions
The fixed number of votes per person does not fit varying question
counts. The budget is now `upvote_share` percent of the questions
(rounded up, at least one), so it grows with the number of questions.
A migration converts existing Q&A slides (no voting stays off, others
get 25 %).

On the stage the voting step no longer lists the questions but points
participants to their devices.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 23:53:32 +01:00

414 lines
20 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Service;
use App\Model\Entity\Poll;
use App\Model\Entity\Slide;
use App\Service\PollStateService;
use App\Service\QaService;
use App\Service\ResultsService;
use App\Service\VoteException;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
/**
* Q&A slides: collect questions, upvote, discuss one by one.
*/
class QaTest extends TestCase
{
use PlenarioTestTrait;
/**
* @var list<string>
*/
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
/**
* @return array{0: \App\Model\Entity\Poll, 1: \App\Model\Entity\Slide}
*/
protected function createQa(int $share = 50): array
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA, ['settings' => ['upvote_share' => $share]]);
return [$this->goLive($poll, $slide), $slide];
}
protected function ask(Poll $poll, Slide $slide, string $participant, string $question): int
{
(new VoteService())->submit($poll, $slide, $participant, ['question' => $question]);
return (int)$this->fetchTable('Responses')->find()
->where(['slide_id' => $slide->id, 'participant_hash' => $participant])
->orderBy(['id' => 'DESC'])
->firstOrFail()
->get('id');
}
protected function vote(Poll $poll, Slide $slide, string $participant, int $questionId, string $vote = 'up'): void
{
(new VoteService())->submit($poll, $slide, $participant, ['vote' => $vote, 'question_id' => $questionId]);
}
protected function stage(Slide $slide, string $stage): Slide
{
(new QaService())->setStage($slide, $stage);
return $this->slidesTable()->get($slide->id, contain: ['SlideOptions']);
}
protected function assertVoteFails(string $message, callable $fn): void
{
try {
$fn();
$this->fail('VoteException expected');
} catch (VoteException $e) {
$this->assertSame($message, $e->getMessage());
}
}
public function testSanitizesUpvoteShare(): void
{
[, $slide] = $this->createQa();
$this->assertSame(50, $slide->setting('upvote_share'));
$this->assertSame(Slide::QA_COLLECT, $slide->qaStage());
$table = $this->slidesTable();
$share = fn(string $value) => $table->patchEntity($slide, ['type' => 'qa', 'settings' => ['upvote_share' => $value]])->setting('upvote_share');
$this->assertSame(0, $share('0'));
$this->assertSame(33, $share('33'));
$this->assertSame(25, $share('-3'), 'invalid values fall back to the default');
$this->assertSame(25, $share('99'));
}
public function testBudgetGrowsWithTheNumberOfQuestions(): void
{
[, $slide] = $this->createQa(25);
$this->assertSame([0, 1, 1, 1, 2, 3, 25], array_map($slide->qaBudget(...), [0, 1, 4, 3, 5, 12, 100]));
[, $off] = $this->createQa(0);
$this->assertSame(0, $off->qaBudget(10));
$table = $this->slidesTable();
$this->assertSame(0, $slide->setting('max_upvotes_per_question'));
$patched = $table->patchEntity($slide, ['type' => 'qa', 'settings' => ['upvote_share' => '25', 'max_upvotes_per_question' => '2']]);
$this->assertSame([2, 2, 1], [$patched->setting('max_upvotes_per_question'), $patched->qaUpvotesPerQuestion(5), $patched->qaUpvotesPerQuestion(1)]);
$patched = $table->patchEntity($slide, ['type' => 'qa', 'settings' => ['max_upvotes_per_question' => '99']]);
// Never more per question than in total.
$this->assertSame([10, 3], [$patched->setting('max_upvotes_per_question'), $patched->qaUpvotesPerQuestion(3)]);
$this->assertSame(4, $slide->qaUpvotesPerQuestion(4), '0 = no limit per question');
}
public function testWithoutUpvotesThereIsNoVotingStage(): void
{
[$poll, $slide] = $this->createQa(0);
$this->assertFalse($slide->hasQaUpvotes());
$this->assertSame([Slide::QA_COLLECT, Slide::QA_DISCUSS], $slide->qaStages());
$first = $this->ask($poll, $slide, 'p1', 'Frage A');
$this->ask($poll, $slide, 'p2', 'Frage B');
$this->assertVoteFails('Bei dieser Fragerunde wird nicht abgestimmt.', fn() => $this->vote($poll, $slide, 'p2', $first));
$qa = new QaService();
try {
$qa->setStage($slide, Slide::QA_VOTE);
$this->fail('No voting stage expected');
} catch (InvalidArgumentException) {
}
// collect → discuss, in submission order.
$this->assertTrue($qa->advance($slide));
$slide = $this->slidesTable()->get($slide->id);
$this->assertSame([Slide::QA_DISCUSS, $first, false], [$slide->qaStage(), $slide->focus_response_id, $slide->is_voting_open]);
$this->assertFalse($qa->advance($slide));
$results = (new ResultsService())->forSlide($slide);
$this->assertSame([['collect', 'discuss'], 0], [$results['stages'], $results['maxUpvotes']]);
$this->assertSame(['Frage A', 'Frage B'], array_column($results['questions'], 'text'));
$this->assertSame(0, (new PollStateService())->forParticipant($poll, 'p1')['qa']['maxUpvotes']);
}
public function testUpvotesPerQuestionAreLimited(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA, ['settings' => ['upvote_share' => 50, 'max_upvotes_per_question' => 1]]);
$poll = $this->goLive($poll, $slide);
$a = $this->ask($poll, $slide, 'p1', 'Frage A');
$b = $this->ask($poll, $slide, 'p1', 'Frage B');
// Six questions at 50 % make a budget of three votes.
foreach (['C', 'D', 'E', 'F'] as $letter) {
$this->ask($poll, $slide, 'p1', 'Frage ' . $letter);
}
$slide = $this->stage($slide, Slide::QA_VOTE);
$this->vote($poll, $slide, 'p2', $a);
$this->assertVoteFails('Du kannst jeder Frage nur eine Stimme geben.', fn() => $this->vote($poll, $slide, 'p2', $a));
$this->vote($poll, $slide, 'p2', $b);
// Withdrawing frees the question again.
$this->vote($poll, $slide, 'p2', $a, 'remove');
$this->vote($poll, $slide, 'p2', $a);
$state = (new PollStateService())->forParticipant($poll, 'p2')['qa'];
$this->assertEquals([1, 1, [$a => 1, $b => 1]], [$state['maxPerQuestion'], $state['votesLeft'], $state['upvoted']]);
$this->slidesTable()->patchEntity($slide, ['type' => 'qa', 'settings' => ['upvote_share' => 50, 'max_upvotes_per_question' => 2]]);
$this->slidesTable()->saveOrFail($slide);
$this->vote($poll, $slide, 'p2', $a);
$this->vote($poll, $slide, 'p3', $b);
$this->vote($poll, $slide, 'p3', $b);
$this->assertVoteFails('Du kannst jeder Frage höchstens 2 Stimmen geben.', fn() => $this->vote($poll, $slide, 'p3', $b));
}
public function testCollectQuestionsSeveralPerParticipantWithoutDuplicates(): void
{
[$poll, $slide] = $this->createQa();
$this->ask($poll, $slide, 'p1', 'Wann kommt das neue Fahrzeug?');
$this->ask($poll, $slide, 'p1', 'Wer organisiert das Sommerfest?');
$this->assertVoteFails('Diese Frage wurde schon gestellt. Du kannst sie gleich beim Abstimmen unterstützen.', fn() => $this->ask($poll, $slide, 'p2', 'wann kommt das neue fahrzeug?'));
$this->assertVoteFails('Bitte gib eine Frage ein.', fn() => $this->ask($poll, $slide, 'p2', ' '));
$this->assertVoteFails('Die Abstimmung über die Fragen hat noch nicht begonnen.', fn() => $this->vote($poll, $slide, 'p2', 1));
$results = (new ResultsService())->forSlide($slide);
$this->assertSame('collect', $results['stage']);
$this->assertSame(['Wann kommt das neue Fahrzeug?', 'Wer organisiert das Sommerfest?'], array_column($results['questions'], 'text'));
// Participants see all questions while collecting (to avoid duplicates), even before asking.
$state = (new PollStateService())->forParticipant($poll, 'p2');
$this->assertSame(['Wann kommt das neue Fahrzeug?', 'Wer organisiert das Sommerfest?'], array_column($state['qa']['questions'], 'text'));
$this->assertSame([], $state['qa']['mine']);
$this->assertArrayNotHasKey('votes', $state['qa']['questions'][0]);
$this->assertNull($state['results']);
$this->assertCount(2, (new PollStateService())->forParticipant($poll, 'p1')['qa']['questions']);
}
public function testQuestionLimitPerParticipant(): void
{
[$poll, $slide] = $this->createQa();
for ($i = 1; $i <= VoteService::MAX_QA_QUESTIONS; $i++) {
$this->ask($poll, $slide, 'p1', 'Frage ' . $i);
}
$this->assertVoteFails('Du kannst höchstens 10 Fragen stellen.', fn() => $this->ask($poll, $slide, 'p1', 'Noch eine'));
}
public function testUpvotesAreABudgetThatCanBeStackedAndWithdrawn(): void
{
[$poll, $slide] = $this->createQa(50);
$a = $this->ask($poll, $slide, 'p1', 'Frage A');
$b = $this->ask($poll, $slide, 'p2', 'Frage B');
$c = $this->ask($poll, $slide, 'p2', 'Frage C');
// Six questions at 50 % make a budget of three votes.
$rest = array_map(fn(string $x) => $this->ask($poll, $slide, 'p9', 'Frage ' . $x), ['D', 'E', 'F']);
$slide = $this->stage($slide, Slide::QA_VOTE);
$this->assertTrue($slide->isVotingOpen());
$this->assertVoteFails('Es können keine neuen Fragen mehr eingereicht werden.', fn() => $this->ask($poll, $slide, 'p3', 'Zu spät'));
$this->assertVoteFails('Deine eigene Frage kannst du nicht unterstützen.', fn() => $this->vote($poll, $slide, 'p1', $a));
$this->assertVoteFails('Diese Frage gibt es nicht.', fn() => $this->vote($poll, $slide, 'p1', 999999));
$this->vote($poll, $slide, 'p1', $b);
// Several upvotes on one question push it.
$this->vote($poll, $slide, 'p3', $c);
$this->vote($poll, $slide, 'p3', $b);
$this->vote($poll, $slide, 'p3', $b);
$this->assertVoteFails('Du hast alle 3 Stimmen vergeben. Nimm eine zurück, um eine andere Frage zu unterstützen.', fn() => $this->vote($poll, $slide, 'p3', $a));
// Withdraw one vote at a time and use it elsewhere.
$this->vote($poll, $slide, 'p3', $c, 'remove');
$this->assertVoteFails('Du hast diese Frage nicht unterstützt.', fn() => $this->vote($poll, $slide, 'p3', $c, 'remove'));
$this->vote($poll, $slide, 'p3', $a);
// While voting is open, the stage only gets the total: no ranking, no counts per question.
$results = (new ResultsService())->forSlide($slide);
$this->assertSame([$a, $b, $c, ...$rest], array_column($results['questions'], 'id'));
$this->assertArrayNotHasKey('votes', $results['questions'][0]);
$this->assertSame(4, $results['upvotes']);
$ranked = (new QaService())->rankedQuestions($slide);
$this->assertSame([[$b, 3], [$a, 1], [$c, 0], [$rest[0], 0], [$rest[1], 0], [$rest[2], 0]], array_map(fn($q) => [$q['id'], $q['votes']], $ranked));
$this->vote($poll, $slide, 'p3', $b, 'remove');
$this->assertSame(2, (new QaService())->rankedQuestions($slide)[0]['votes'], 'remove withdraws only one upvote');
$this->vote($poll, $slide, 'p3', $b);
// Voting view of participants: all questions, submission order, no counts.
$state = (new PollStateService())->forParticipant($poll, 'p3');
$this->assertSame([$a, $b, $c, ...$rest], array_column($state['qa']['questions'], 'id'));
$this->assertArrayNotHasKey('votes', $state['qa']['questions'][0]);
$this->assertSame([$b => 2, $a => 1], $state['qa']['upvoted']);
$this->assertSame(0, $state['qa']['votesLeft']);
$this->assertFalse($state['qa']['allowSelfUpvote']);
$this->assertTrue($state['hasResponded']);
}
public function testSelfUpvoteOnlyWithSetting(): void
{
[, $slide] = $this->createQa();
$this->assertFalse($slide->setting('allow_self_upvote'));
$table = $this->slidesTable();
$this->assertTrue($table->patchEntity($slide, ['type' => 'qa', 'settings' => ['allow_self_upvote' => '1']])->setting('allow_self_upvote'));
$this->assertFalse($table->patchEntity($slide, ['type' => 'qa', 'settings' => ['upvote_share' => '20']])->setting('allow_self_upvote'));
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA, ['settings' => ['upvote_share' => 25, 'allow_self_upvote' => true]]);
$poll = $this->goLive($poll, $slide);
$a = $this->ask($poll, $slide, 'p1', 'Thema A');
$b = $this->ask($poll, $slide, 'p2', 'Thema B');
// Twelve questions at 25 % make a budget of three votes.
for ($i = 1; $i <= 10; $i++) {
$this->ask($poll, $slide, 'p3', 'Thema ' . $i);
}
$slide = $this->stage($slide, Slide::QA_VOTE);
// Own questions take votes from the same budget, stacked and withdrawn like any other.
$this->vote($poll, $slide, 'p1', $a);
$this->vote($poll, $slide, 'p1', $a);
$this->vote($poll, $slide, 'p1', $b);
$this->assertVoteFails('Du hast alle 3 Stimmen vergeben. Nimm eine zurück, um eine andere Frage zu unterstützen.', fn() => $this->vote($poll, $slide, 'p1', $a));
$this->vote($poll, $slide, 'p1', $a, 'remove');
$state = (new PollStateService())->forParticipant($poll, 'p1')['qa'];
$this->assertTrue($state['allowSelfUpvote']);
$this->assertSame([$a => 1, $b => 1], $state['upvoted']);
$this->assertSame(1, $state['votesLeft']);
}
public function testDiscussStepsThroughQuestionsByRank(): void
{
[$poll, $slide] = $this->createQa(50);
$a = $this->ask($poll, $slide, 'p1', 'Frage A');
$b = $this->ask($poll, $slide, 'p1', 'Frage B');
$slide = $this->stage($slide, Slide::QA_VOTE);
$this->vote($poll, $slide, 'p2', $b);
$slide = $this->stage($slide, Slide::QA_DISCUSS);
$this->assertFalse($slide->isVotingOpen());
$this->assertSame($b, $slide->focus_response_id, 'most upvoted question first');
$this->assertVoteFails('Die Fragerunde läuft – Fragen und Stimmen sind nicht mehr möglich.', fn() => $this->vote($poll, $slide, 'p2', $a));
$qa = new QaService();
$this->assertFalse($qa->moveFocus($slide, -1));
$modified = $slide->modified;
$this->assertTrue($qa->moveFocus($slide, 1));
$this->assertFalse($qa->moveFocus($slide, 1), 'no question after the last one');
$slide = $this->slidesTable()->get($slide->id, contain: ['SlideOptions']);
$this->assertSame($a, $slide->focus_response_id);
$this->assertEquals($modified, $slide->modified, 'focus changes do not reload participant pages');
$results = (new ResultsService())->forSlide($slide);
$this->assertSame([$a, 1], [$results['focusId'], $results['focusIndex']]);
$state = (new PollStateService())->forParticipant($poll, 'p2');
$this->assertSame($a, $state['qa']['focusId']);
$this->assertSame([1, 0], array_column($state['qa']['questions'], 'votes'));
// Idempotent: setting the same stage again keeps the focus.
$slide = $this->stage($slide, Slide::QA_DISCUSS);
$this->assertSame($a, $slide->focus_response_id);
}
public function testAdvanceAndReset(): void
{
[$poll, $slide] = $this->createQa();
$this->ask($poll, $slide, 'p1', 'Frage A');
$qa = new QaService();
$this->assertTrue($qa->advance($slide));
$this->assertTrue($qa->advance($slide));
$this->assertFalse($qa->advance($slide));
$this->assertSame(Slide::QA_DISCUSS, $slide->qaStage());
$this->fetchTable('Responses')->deleteAll(['slide_id' => $slide->id]);
$qa->reset($slide);
$slide = $this->slidesTable()->get($slide->id);
$this->assertSame([Slide::QA_COLLECT, true, null], [$slide->qaStage(), $slide->is_voting_open, $slide->focus_response_id]);
}
public function testOwnQuestionCanBeEditedWhileCollecting(): void
{
[$poll, $slide] = $this->createQa();
$votes = new VoteService();
$mine = $this->ask($poll, $slide, 'p1', 'Wan gehts los?');
$other = $this->ask($poll, $slide, 'p2', 'Gibt es Kaffee?');
$votes->submit($poll, $slide, 'p1', ['vote' => 'edit', 'question_id' => $mine, 'question' => ' Wann geht es los? ']);
$responses = $this->fetchTable('Responses');
$this->assertSame('Wann geht es los?', $responses->get($mine)->get('text_value'));
// Repeating the same edit is harmless, case-only changes of the own question are fine.
$votes->submit($poll, $slide, 'p1', ['vote' => 'edit', 'question_id' => $mine, 'question' => 'WANN geht es los?']);
$this->assertSame(2, $responses->find()->where(['slide_id' => $slide->id])->count());
$this->assertVoteFails('Diese Frage wurde schon gestellt. Du kannst sie gleich beim Abstimmen unterstützen.', fn() => $votes->submit(
$poll,
$slide,
'p1',
['vote' => 'edit', 'question_id' => $mine, 'question' => 'gibt es kaffee?'],
));
$this->assertVoteFails('Bitte gib eine Frage ein.', fn() => $votes->submit(
$poll,
$slide,
'p1',
['vote' => 'edit', 'question_id' => $mine, 'question' => ' '],
));
$this->assertVoteFails('Diese Frage gibt es nicht mehr oder sie stammt nicht von dir.', fn() => $votes->submit(
$poll,
$slide,
'p1',
['vote' => 'edit', 'question_id' => $other, 'question' => 'Gekapert'],
));
$this->assertSame('Gibt es Kaffee?', $responses->get($other)->get('text_value'));
}
public function testOwnQuestionCanBeDeletedWhileCollecting(): void
{
[$poll, $slide] = $this->createQa();
$votes = new VoteService();
$mine = $this->ask($poll, $slide, 'p1', 'Weg damit');
$other = $this->ask($poll, $slide, 'p2', 'Bleibt');
$this->assertVoteFails('Diese Frage gibt es nicht mehr oder sie stammt nicht von dir.', fn() => $votes->submit(
$poll,
$slide,
'p1',
['vote' => 'delete', 'question_id' => $other],
));
// Upvotes left over from a voting stage the presenter stepped back from go with it.
$slide = $this->stage($slide, Slide::QA_VOTE);
$this->vote($poll, $slide, 'p2', $mine);
$slide = $this->stage($slide, Slide::QA_COLLECT);
$votes->submit($poll, $slide, 'p1', ['vote' => 'delete', 'question_id' => $mine]);
$responses = $this->fetchTable('Responses');
$this->assertSame([$other], $responses->find()->where(['slide_id' => $slide->id])->all()->extract('id')->toList());
$state = (new PollStateService())->qaState($slide, 'p1');
$this->assertSame([], $state['mine']);
// Deleting again is answered with a message, nothing else happens.
$this->assertVoteFails('Diese Frage gibt es nicht mehr oder sie stammt nicht von dir.', fn() => $votes->submit(
$poll,
$slide,
'p1',
['vote' => 'delete', 'question_id' => $mine],
));
}
public function testQuestionsAreFixedOnceVotingStarts(): void
{
[$poll, $slide] = $this->createQa();
$votes = new VoteService();
$mine = $this->ask($poll, $slide, 'p1', 'Feste Frage');
foreach ([Slide::QA_VOTE, Slide::QA_DISCUSS] as $stage) {
$slide = $this->stage($slide, $stage);
$poll = $this->fetchTable('Polls')->get($poll->id);
foreach (['edit', 'delete'] as $action) {
try {
$votes->submit($poll, $slide, 'p1', ['vote' => $action, 'question_id' => $mine, 'question' => 'Geändert']);
$this->fail('VoteException expected');
} catch (VoteException) {
}
}
}
$this->assertSame('Feste Frage', $this->fetchTable('Responses')->get($mine)->get('text_value'));
}
}