Add Q&A slide type
CI / Deploy / test (push) Successful in 48s
CI / Deploy / deploy (push) Skipped

Participants submit questions, upvote them and the presenter discusses
them one by one, most upvoted first.

- Stages collect -> vote -> discuss, switched manually by the presenter
  (V key or stepper, going back is possible)
- Configurable upvote budget per participant, freely distributable
  (several upvotes on one question are allowed), votes can be withdrawn
- Participants see all questions while collecting to avoid duplicates;
  no counts before the discussion
- While discussing, next/prev step through the questions before
  moving to the next slide
- Migration: slides.stage, slides.focus_response_id, responses.parent_id

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 16:09:46 +01:00
1 parent ea944b080f
commit f0c6687100
21 files changed
+1327 -27

No files matched your search

+166
View File
@@ -0,0 +1,166 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Q&A slides end to end: slide form, presenter controls, participant page, export.
*/
class QaSlideTest extends TestCase
{
use IntegrationTestTrait;
use PlenarioTestTrait;
/**
* @var list<string>
*/
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
protected const PID = '0123456789abcdef0123456789abcdef';
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
}
/**
* @return array<string, mixed>
*/
protected function control(string $token, array $data): array
{
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", $data);
$this->assertResponseOk();
return json_decode((string)$this->_response->getBody(), true);
}
/**
* @return array<string, mixed>
*/
protected function participantPost(string $code, array $data): array
{
$this->cookie('plenario_pid', self::PID);
$this->configRequest(['headers' => ['Accept' => 'application/json', 'X-Requested-With' => 'XMLHttpRequest']]);
$this->post("/p/$code/vote", $data);
$this->assertResponseOk();
return json_decode((string)$this->_response->getBody(), true);
}
public function testAddQaSlide(): void
{
[$poll, $token] = $this->createPoll();
$this->get("/admin/$token/slides/add?type=qa");
$this->assertResponseOk();
$this->assertResponseContains('Upvotes (Stimmen) pro Person');
$this->assertResponseNotContains('Abstimmung geöffnet');
$this->post("/admin/$token/slides/add?type=qa", [
'type' => 'qa',
'question' => 'Eure Fragen an den Vorstand',
'settings' => ['max_upvotes' => '5'],
]);
$this->assertRedirect();
$slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail();
$this->assertSame([Slide::TYPE_QA, 5, Slide::QA_COLLECT, true], [
$slide->type, $slide->setting('max_upvotes'), $slide->stage, $slide->is_voting_open,
]);
$this->get("/admin/$token");
$this->assertResponseContains('Fragen sammeln');
$this->assertResponseContains('max. 5 Upvotes pro Person');
}
public function testFullRoundTrip(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA, ['settings' => ['max_upvotes' => 1]]);
$next = $this->createSlide($poll, Slide::TYPE_CONTENT);
$this->goLive($poll, $slide);
$code = $poll->join_code;
$this->cookie('plenario_pid', self::PID);
$this->get("/p/$code");
$this->assertResponseContains('Frage einreichen');
$answer = $this->participantPost($code, ['slide_id' => $slide->id, 'question' => 'Meine Frage']);
$this->assertTrue($answer['ok']);
$this->assertSame('Danke, deine Frage ist angekommen!', $answer['message']);
$this->assertSame(['Meine Frage'], array_column($answer['state']['qa']['questions'], 'text'));
// Others ask too (directly via the service).
$hash = VoteService::participantHash('ffffffffffffffffffffffffffffffff', $poll->id);
(new VoteService())->submit($this->pollsTable()->get($poll->id), $this->slidesTable()->get($slide->id), $hash, ['question' => 'Andere Frage']);
// V: collect → vote
$state = $this->control($token, ['action' => 'toggle']);
$this->assertSame('vote', $state['results']['stage']);
$this->assertTrue($state['slide']['isVotingOpen']);
$this->cookie('plenario_pid', self::PID);
$this->get("/p/$code");
$this->assertResponseContains('Du hast eine Stimme');
$other = $state['results']['questions'][1]['id'];
$answer = $this->participantPost($code, ['slide_id' => $slide->id, 'vote' => 'up', 'question_id' => $other]);
$this->assertTrue($answer['ok'], $answer['message']);
$this->assertSame(0, $answer['state']['qa']['votesLeft']);
// Presenter clicks "Besprechen" in the stepper.
$state = $this->control($token, ['action' => 'stage', 'stage' => 'discuss']);
$this->assertSame(['discuss', 0], [$state['results']['stage'], $state['results']['focusIndex']]);
$this->assertSame('Andere Frage', $state['results']['questions'][0]['text']);
$this->assertStringNotContainsString('participant_hash', (string)json_encode($state));
$this->cookie('plenario_pid', self::PID);
$this->get("/p/$code");
$this->assertResponseContains('Die Fragen werden jetzt besprochen');
$this->get("/admin/$token/present");
$this->assertResponseOk();
// → steps through the questions first, then to the next slide.
$state = $this->control($token, ['action' => 'next']);
$this->assertSame([$slide->id, 1], [$state['slide']['id'], $state['results']['focusIndex']]);
$state = $this->control($token, ['action' => 'question', 'step' => '-1']);
$this->assertSame(0, $state['results']['focusIndex']);
$this->control($token, ['action' => 'next']);
$state = $this->control($token, ['action' => 'next']);
$this->assertSame($next->id, $state['slide']['id']);
$this->get("/admin/$token/export.csv");
$this->assertResponseOk();
$this->assertResponseContains('Upvote: Andere Frage');
$this->assertResponseContains('Meine Frage');
}
public function testResetStartsOver(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA);
$this->goLive($poll, $slide);
(new VoteService())->submit($this->pollsTable()->get($poll->id), $slide, 'p1', ['question' => 'Frage']);
$this->control($token, ['action' => 'stage', 'stage' => 'discuss']);
$this->post("/admin/$token/slides/{$slide->id}/reset");
$this->assertRedirect();
$slide = $this->slidesTable()->get($slide->id);
$this->assertSame([Slide::QA_COLLECT, true, null], [$slide->stage, $slide->is_voting_open, $slide->focus_response_id]);
$this->assertSame(0, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count());
}
public function testStageActionRequiresQaSlide(): void
{
[$poll, $token] = $this->createPoll();
$this->goLive($poll, $this->createSlide($poll, Slide::TYPE_CHOICE));
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => 'stage', 'stage' => 'vote']);
$this->assertResponseCode(400);
}
}
+205
View File
@@ -0,0 +1,205 @@
<?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;
/**
* 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 $maxUpvotes = 2): array
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA, ['settings' => ['max_upvotes' => $maxUpvotes]]);
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 testSanitizesMaxUpvotes(): void
{
[, $slide] = $this->createQa();
$this->assertSame(2, $slide->setting('max_upvotes'));
$this->assertSame(Slide::QA_COLLECT, $slide->qaStage());
$table = $this->slidesTable();
$this->assertSame(1, $table->patchEntity($slide, ['type' => 'qa', 'settings' => ['max_upvotes' => '0']])->setting('max_upvotes'));
$this->assertSame(10, $table->patchEntity($slide, ['type' => 'qa', 'settings' => ['max_upvotes' => '99']])->setting('max_upvotes'));
}
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(3);
$a = $this->ask($poll, $slide, 'p1', 'Frage A');
$b = $this->ask($poll, $slide, 'p2', 'Frage B');
$c = $this->ask($poll, $slide, 'p2', 'Frage C');
$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);
$results = (new ResultsService())->forSlide($slide);
$this->assertSame([[$b, 3], [$a, 1], [$c, 0]], array_map(fn($q) => [$q['id'], $q['votes']], $results['questions']));
$this->assertSame(4, $results['upvotes']);
$this->vote($poll, $slide, 'p3', $b, 'remove');
$this->assertSame(2, (new ResultsService())->forSlide($slide)['questions'][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], 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->assertTrue($state['hasResponded']);
}
public function testDiscussStepsThroughQuestionsByRank(): void
{
[$poll, $slide] = $this->createQa(3);
$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]);
}
}