Files
Plenario/tests/TestCase/Controller/ChoiceCommentsTest.php
T
gnilebeinandClaude Sonnet 5.5 61718ed7c6
CI / Deploy / PHP 8.4 (push) Successful in 1m11s
CI / Deploy / PHP 8.5 (push) Successful in 1m11s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped
Add optional comment field to multiple choice slides
Participants can justify their choice; the comment is stored once in the
first response row, rotates on the stage like scale comments and is listed
in the admin area and the PDF export.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-13 09:29:25 +01:00

214 lines
8.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Poll;
use App\Model\Entity\Slide;
use App\Model\Table\SlidesTable;
use App\Service\ResultsService;
use App\Service\VoteException;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Multiple choice slides with an optional comment field (stored once per participant in the first row).
*/
class ChoiceCommentsTest 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();
}
/**
* @param array<string, mixed> $settings Extra settings
* @return array{0: \App\Model\Entity\Poll, 1: string, 2: \App\Model\Entity\Slide}
*/
protected function choiceWithComments(array $settings = ['comments' => true, 'multiple' => true]): array
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE, ['settings' => $settings], ['Ja', 'Nein', 'Vielleicht']);
$poll = $this->goLive($poll, $slide);
return [$poll, $token, $slide];
}
protected function hash(string $char, Poll $poll): string
{
return VoteService::participantHash(str_repeat($char, 32), $poll->id);
}
public function testSettingIsSanitized(): void
{
$this->assertTrue(SlidesTable::sanitizeSettings(Slide::TYPE_CHOICE, ['comments' => '1'])['comments']);
$this->assertFalse(SlidesTable::sanitizeSettings(Slide::TYPE_CHOICE, [])['comments']);
}
public function testFormShowsAndSavesCommentSetting(): void
{
[, $token] = $this->createPoll();
$this->get("/admin/$token/slides/add?type=choice");
$this->assertResponseContains('name="settings[comments]"');
$this->post("/admin/$token/slides/add?type=choice", [
'type' => 'choice',
'question' => 'Was nehmen wir?',
'options_text' => "Ja\nNein",
'settings' => ['comments' => '1'],
]);
$slide = $this->slidesTable()->find()->firstOrFail();
$this->assertTrue($slide->hasComments());
}
public function testParticipantPageShowsCommentFieldOnlyWhenEnabled(): void
{
[$poll] = $this->choiceWithComments();
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('name="comment"');
[$other] = $this->choiceWithComments(['multiple' => true]);
$this->get('/p/' . $other->join_code);
$this->assertResponseNotContains('name="comment"');
}
public function testCommentIsStoredOnceInFirstRowAndCountsStayCorrect(): void
{
[$poll, , $slide] = $this->choiceWithComments();
$options = array_map(fn($o) => $o->id, $slide->slide_options);
(new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [
'option_ids' => [$options[0], $options[2]],
'comment' => ' Weil <deshalb> ',
]);
$rows = $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->orderBy(['id' => 'ASC'])->all()->toList();
$this->assertCount(2, $rows);
$this->assertSame('Weil <deshalb>', $rows[0]->text_value);
$this->assertNull($rows[1]->text_value);
$results = (new ResultsService())->forSlide($slide);
$this->assertSame([1, 0, 1], array_column($results['options'], 'count'));
$this->assertSame(2, $results['votes']);
$this->assertSame(1, $results['participants']);
$this->assertSame(['Weil <deshalb>'], array_column($results['comments'], 'text'));
$this->assertSame([null], array_column($results['comments'], 'value'));
}
public function testCommentIsIgnoredWithoutCommentField(): void
{
[$poll, , $slide] = $this->choiceWithComments(['multiple' => false]);
(new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [
'option_id' => $slide->slide_options[0]->id,
'comment' => 'Eingeschmuggelt',
]);
$row = $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->firstOrFail();
$this->assertNull($row->text_value);
$this->assertSame([], (new ResultsService())->forSlide($slide)['comments']);
}
public function testTooLongCommentIsRejected(): void
{
[$poll, , $slide] = $this->choiceWithComments();
$this->expectException(VoteException::class);
(new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [
'option_ids' => [$slide->slide_options[0]->id],
'comment' => str_repeat('x', VoteService::MAX_TEXT_LENGTH + 1),
]);
}
public function testCommentCanBeEditedAddedAndRemoved(): void
{
[$poll, , $slide] = $this->choiceWithComments();
$hash = $this->hash('a', $poll);
$votes = new VoteService();
$votes->submit($poll, $slide, $hash, [
'option_ids' => [$slide->slide_options[0]->id, $slide->slide_options[1]->id],
]);
$this->assertNull($votes->ownText($slide, $hash));
$votes->editText($poll, $slide, $hash, ['comment' => 'Nachgereicht']);
$this->assertSame('Nachgereicht', $votes->ownText($slide, $hash));
$votes->editText($poll, $slide, $hash, ['comment' => 'Korrigiert']);
$this->assertSame(['Korrigiert'], array_column((new ResultsService())->comments($slide), 'text'));
$this->assertSame(2, $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->count());
$votes->editText($poll, $slide, $hash, ['comment' => '']);
$this->assertNull($votes->ownText($slide, $hash));
$this->assertSame([], (new ResultsService())->comments($slide));
}
public function testEditRequiresAnAnswer(): void
{
[$poll, , $slide] = $this->choiceWithComments();
$this->expectException(VoteException::class);
(new VoteService())->editText($poll, $slide, $this->hash('z', $poll), ['comment' => 'Ohne Auswahl']);
}
public function testPresenterGetsCommentsButParticipantsNever(): void
{
[$poll, $token, $slide] = $this->choiceWithComments();
(new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [
'option_ids' => [$slide->slide_options[0]->id],
'comment' => 'Sichtbar auf der Bühne',
]);
$this->get("/admin/$token/state");
$results = json_decode((string)$this->_response->getBody(), true)['results'];
$this->assertSame(['Sichtbar auf der Bühne'], array_column($results['comments'], 'text'));
$this->get('/p/' . $poll->join_code . '/state');
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertNull($state['results']);
$this->assertStringNotContainsString('Sichtbar auf der Bühne', (string)$this->_response->getBody());
}
public function testAnswersPageListsCommentsOfChoiceSlides(): void
{
[$poll, $token, $slide] = $this->choiceWithComments();
(new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [
'option_ids' => [$slide->slide_options[0]->id],
'comment' => 'Erster <Kommentar>',
]);
$this->get("/admin/$token/slides/{$slide->id}/answers");
$this->assertResponseOk();
$this->assertResponseContains('Erster &lt;Kommentar&gt;');
}
public function testAnswersPageIsNotFoundForChoiceWithoutComments(): void
{
[, $token, $slide] = $this->choiceWithComments(['multiple' => false]);
$this->get("/admin/$token/slides/{$slide->id}/answers");
$this->assertResponseCode(404);
}
public function testExportContainsComments(): void
{
[$poll, $token, $slide] = $this->choiceWithComments();
(new VoteService())->submit($poll, $slide, $this->hash('a', $poll), [
'option_ids' => [$slide->slide_options[0]->id],
'comment' => 'Im PDF enthalten',
]);
$this->assertStringContainsString('Im PDF enthalten', $this->exportHtml($token));
}
}