Files
gnilebeinandClaude Sonnet 5.5 4b5f9d92a1 Add prepared questions to Q&A slides
The slide form takes questions entered beforehand (e.g. received by mail),
one per line, while collecting. They are stored as regular questions with a
reserved participant hash, synced idempotently, and not counted as participants.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-12 22:34:12 +01:00

124 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Service\PollStateService;
use App\Service\QaService;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Questions the admin enters beforehand on a Q&A slide (textarea `prepared_text`).
*/
class QaPreparedTest extends TestCase
{
use IntegrationTestTrait;
use PlenarioTestTrait;
/**
* @var list<string>
*/
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
}
public function testParsePreparedCleansLines(): void
{
$texts = QaService::parsePrepared(" Wann geht's los? \n\nwann geht's LOS?\r\nZweite Frage");
$this->assertSame(["Wann geht's los?", 'Zweite Frage'], $texts);
}
public function testCreatingSlideStoresPreparedQuestions(): void
{
[$poll, $token] = $this->createPoll();
$this->post("/admin/$token/slides/add", [
'type' => Slide::TYPE_QA,
'question' => 'Fragen',
'prepared_text' => "Frage A\nFrage B",
]);
$this->assertRedirect();
$slide = $this->slidesTable()->listForPoll($poll->id)[0];
$this->assertSame(['Frage A', 'Frage B'], (new QaService())->preparedQuestions($slide));
}
public function testEditSyncsAndIsIdempotent(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA);
$url = "/admin/$token/slides/{$slide->id}/edit";
$data = ['question' => 'Fragen', 'prepared_text' => "Frage A\nFrage B"];
$this->post($url, $data);
$responses = TableRegistry::getTableLocator()->get('Responses');
$ids = $responses->find()->where(['slide_id' => $slide->id])->all()->extract('id')->toList();
$this->assertCount(2, $ids);
$this->post($url, $data);
$this->assertSame($ids, $responses->find()->where(['slide_id' => $slide->id])->all()->extract('id')->toList());
$this->post($url, ['prepared_text' => "Frage B\nFrage C"] + $data);
$this->assertSame(['Frage B', 'Frage C'], (new QaService())->preparedQuestions($slide));
}
public function testParticipantQuestionsAreKeptAndNotDuplicated(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA);
$poll = $this->goLive($poll, $slide);
$hash = VoteService::participantHash(str_repeat('a', 32), $poll->id);
(new VoteService())->submit($poll, $slide, $hash, ['question' => 'Eigene Frage']);
$this->post("/admin/$token/slides/{$slide->id}/edit", [
'question' => 'Fragen',
'prepared_text' => "eigene frage\nVorab",
]);
$this->post("/admin/$token/slides/{$slide->id}/edit", ['question' => 'Fragen', 'prepared_text' => '']);
$responses = TableRegistry::getTableLocator()->get('Responses');
$this->assertSame(
['Eigene Frage'],
$responses->find()->where(['slide_id' => $slide->id])->all()->extract('text_value')->toList(),
);
}
public function testPreparedQuestionsAppearForParticipantsButNotAsOwn(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA);
(new QaService())->syncPrepared($slide, ['Vorab']);
$poll = $this->goLive($poll, $slide);
$slide = $this->slidesTable()->get($slide->id);
$hash = VoteService::participantHash(str_repeat('b', 32), $poll->id);
$qa = (new PollStateService())->qaState($slide, $hash);
$this->assertCount(1, $qa['questions']);
$this->assertSame([], $qa['mine']);
$this->assertSame(0, TableRegistry::getTableLocator()->get('Responses')->countParticipants($slide->id));
}
public function testNotChangedOutsideCollectStage(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QA);
(new QaService())->syncPrepared($slide, ['Vorab']);
(new QaService())->setStage($slide, Slide::QA_VOTE);
$this->post("/admin/$token/slides/{$slide->id}/edit", ['question' => 'Fragen', 'prepared_text' => '']);
$this->assertSame(['Vorab'], (new QaService())->preparedQuestions($slide));
}
}