Files
Plenario/tests/TestCase/Controller/QaSlideTest.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

198 lines
8.4 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\Controller;
use App\Model\Entity\Slide;
use App\Service\QaService;
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('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' => ['upvote_share' => '25'],
]);
$this->assertRedirect();
$slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail();
$this->assertSame([Slide::TYPE_QA, 25, Slide::QA_COLLECT, true], [
$slide->type, $slide->setting('upvote_share'), $slide->stage, $slide->is_voting_open,
]);
$this->get("/admin/$token");
$this->assertResponseContains('Fragen sammeln');
$this->assertResponseContains('Stimmen für 25 % der Fragen pro Person');
}
public function testSwitchingOffUpvotesSkipsTheVotingStage(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA, ['settings' => ['upvote_share' => 50]]);
$this->goLive($poll, $slide);
(new QaService())->setStage($slide, Slide::QA_VOTE);
$this->get("/admin/$token/slides/{$slide->id}/edit");
$this->assertResponseContains('keine – ohne Abstimmung');
$this->assertResponseContains('Höchstens pro Frage');
// Switched off while voting: the round continues with the discussion.
$this->post("/admin/$token/slides/{$slide->id}/edit", [
'question' => $slide->question,
'settings' => ['upvote_share' => '0', 'max_upvotes_per_question' => '0'],
]);
$this->assertRedirect();
$slide = $this->slidesTable()->get($slide->id);
$this->assertSame([0, Slide::QA_DISCUSS, false], [$slide->setting('upvote_share'), $slide->qaStage(), $slide->is_voting_open]);
$this->get("/admin/$token");
$this->assertResponseContains('ohne Upvotes');
// Back to collecting, V goes straight to discussing; "vote" is rejected.
$state = $this->control($token, ['action' => 'stage', 'stage' => 'collect']);
$this->assertSame(['collect', 'discuss'], $state['results']['stages']);
$state = $this->control($token, ['action' => 'toggle']);
$this->assertSame('discuss', $state['results']['stage']);
$this->post("/admin/$token/control", ['action' => 'stage', 'stage' => 'vote']);
$this->assertResponseCode(400);
}
public function testFullRoundTrip(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA, ['settings' => ['upvote_share' => 50]]);
$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']);
$html = $this->exportHtml($token);
$this->assertMatchesRegularExpression('/Andere Frage.*Meine Frage/s', $html, 'ranked by upvotes');
}
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);
}
}