Files
Plenario/tests/TestCase/Controller/ParticipateControllerTest.php
gnilebeinandClaude Sonnet 5.5 b496032604
CI / Deploy / PHP 8.5 (push) Successful in 1m13s
CI / Deploy / PHP 8.4 (push) Successful in 1m17s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped
Make "comment without rating" a choice in a comment dropdown
The scale form replaces the comment checkbox with a dropdown: no
comment field, comment with mandatory rating, or comment alone is
enough. Existing slides keep their behaviour (rating mandatory).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 13:07:20 +01:00

267 lines
12 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Participant view, state endpoint and voting.
*/
class ParticipateControllerTest 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();
Cache::clear('throttle');
}
public function testUnknownCodeIs404(): void
{
$this->get('/p/ZZZZZZ');
$this->assertResponseCode(404);
}
public function testWaitingScreenAndCookie(): void
{
[$poll] = $this->createPoll();
$this->get('/p/' . $poll->join_code);
$this->assertResponseOk();
$this->assertResponseContains('Gleich geht');
$this->assertCookieIsSet('plenario_pid');
}
public function testVoteOnceOnly(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE);
$this->goLive($poll, $slide);
$optionId = $slide->slide_options[0]->id;
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'option_id' => $optionId]);
$this->assertRedirect(['_name' => 'participate', 'code' => $poll->join_code]);
$this->assertFlashMessage('Danke für deine Antwort!');
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'option_id' => $optionId]);
$this->assertFlashMessage('Du hast bereits abgestimmt.');
$this->assertSame(1, TableRegistry::getTableLocator()->get('Responses')->find()->count());
}
public function testVoteForInactiveSlideIsRejected(): void
{
[$poll] = $this->createPoll();
$live = $this->createSlide($poll, Slide::TYPE_OPEN);
$other = $this->createSlide($poll, Slide::TYPE_OPEN);
$this->goLive($poll, $live);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $other->id, 'text' => 'Hallo']);
$this->assertFlashMessage('Diese Frage ist nicht mehr aktiv.');
$this->assertSame(0, TableRegistry::getTableLocator()->get('Responses')->find()->count());
}
public function testStateNeverContainsResults(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_SCALE);
$this->goLive($poll, $slide);
$this->cookie('plenario_pid', self::PID);
$this->get("/p/{$poll->join_code}/state");
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertSame($slide->id, $state['slideId']);
$this->assertFalse($state['hasResponded']);
$this->assertNull($state['results']);
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'value' => 4]);
$this->cookie('plenario_pid', self::PID);
$this->get("/p/{$poll->join_code}/state");
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertTrue($state['hasResponded']);
// Results are for the stage only, not even after answering or closing.
$this->assertNull($state['results']);
$this->slidesTable()->updateAll(['is_voting_open' => false], ['id' => $slide->id]);
$this->cookie('plenario_pid', self::PID);
$this->get("/p/{$poll->join_code}/state");
$this->assertNull(json_decode((string)$this->_response->getBody(), true)['results']);
}
public function testWordcloudAjaxVoteReturnsStateWithUpvoteCloud(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['allow_upvotes' => true]]);
$this->goLive($poll, $slide);
$this->cookie('plenario_pid', self::PID);
$this->configRequest(['headers' => ['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json']]);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'word' => 'Kaffee']);
$this->assertResponseOk();
$body = json_decode((string)$this->_response->getBody(), true);
$this->assertTrue($body['ok']);
$this->assertSame(['kaffee'], $body['state']['words']['mine']);
// The cloud reaches the device because participants upvote in it.
$this->assertSame('Kaffee', $body['state']['results']['words'][0]['text']);
$this->cookie('plenario_pid', self::PID);
$this->configRequest(['headers' => ['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json']]);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'word' => 'kaffee']);
$body = json_decode((string)$this->_response->getBody(), true);
$this->assertFalse($body['ok']);
$this->assertSame('Diesen Begriff hast du schon genannt.', $body['message']);
}
public function testWordcloudWithoutUpvotesHidesCloudBeforeAnswer(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['allow_upvotes' => false]]);
$this->goLive($poll, $slide);
$this->cookie('plenario_pid', self::PID);
$this->get('/p/' . $poll->join_code);
$this->assertResponseOk();
$this->assertResponseContains('data-word-input');
$this->assertResponseNotContains('data-upvote-hint');
$this->cookie('plenario_pid', self::PID);
$this->get("/p/{$poll->join_code}/state");
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertNull($state['results']);
$this->assertFalse($state['words']['allowUpvotes']);
}
public function testClosedPoll(): void
{
[$poll] = $this->createPoll();
$poll->is_active = false;
$this->pollsTable()->saveOrFail($poll);
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('Diese Umfrage ist geschlossen');
}
public function testVotesAreThrottledPerIp(): void
{
Configure::write('Plenario.throttle.votesPerMinute', 2);
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD);
$this->goLive($poll, $slide);
foreach (['Kaffee', 'Tee', 'Wasser'] as $i => $word) {
$this->cookie('plenario_pid', self::PID);
$this->configRequest(['headers' => ['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json']]);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'word' => $word]);
$body = json_decode((string)$this->_response->getBody(), true);
$this->assertSame($i < 2, $body['ok'], $word);
}
$this->assertResponseCode(429);
$this->assertStringContainsString('Zu viele Antworten', $body['message']);
$this->assertArrayHasKey('state', $body);
$this->assertSame(2, TableRegistry::getTableLocator()->get('Responses')->find()->where(['slide_id' => $slide->id])->count());
}
public function testOpenAnswerCanBeEditedWhileVotingIsOpen(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_OPEN);
$this->goLive($poll, $slide);
$responses = TableRegistry::getTableLocator()->get('Responses');
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'text' => 'Tipfehler']);
$this->cookie('plenario_pid', self::PID);
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('Antwort bearbeiten');
$this->assertResponseContains('Tipfehler');
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => ' Tippfehler ']);
$this->assertRedirect(['_name' => 'participate', 'code' => $poll->join_code]);
$this->assertFlashMessage('Deine Antwort wurde geändert.');
$this->assertSame(1, $responses->find()->count());
$this->assertSame('Tippfehler', $responses->find()->firstOrFail()->text_value);
// An open answer cannot be emptied.
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => ' ']);
$this->assertFlashMessage('Bitte gib eine Antwort ein.');
$this->assertSame('Tippfehler', $responses->find()->firstOrFail()->text_value);
// Without an own answer there is nothing to edit (and nothing is created).
$this->cookie('plenario_pid', str_repeat('f', 32));
$this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => 'Neu']);
$this->assertFlashMessage('Du hast hier noch nicht geantwortet.');
$this->assertSame(1, $responses->find()->count());
// Closed: no form, no change.
TableRegistry::getTableLocator()->get('Slides')->updateAll(['is_voting_open' => false], ['id' => $slide->id]);
$this->cookie('plenario_pid', self::PID);
$this->get('/p/' . $poll->join_code);
$this->assertResponseNotContains('Antwort bearbeiten');
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => 'Zu spät']);
$this->assertFlashMessage('Die Abstimmung zu dieser Frage ist geschlossen.');
$this->assertSame('Tippfehler', $responses->find()->firstOrFail()->text_value);
}
public function testChoiceAnswersCannotBeEdited(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE);
$this->goLive($poll, $slide);
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'option_id' => $slide->slide_options[0]->id]);
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => 'Hack']);
$this->assertFlashMessage('Diese Antwort kann nicht mehr geändert werden.');
$this->assertFalse(TableRegistry::getTableLocator()->get('Responses')->exists(['text_value' => 'Hack']));
}
public function testScaleCommentOnlyWithoutRating(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['comments' => true]]);
$this->goLive($poll, $slide);
$this->cookie('plenario_pid', self::PID);
$this->get('/p/' . $poll->join_code);
$this->assertResponseNotContains('auch nur einen Kommentar');
$slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['comments' => true, 'comment_only' => true]]);
$this->goLive($poll, $slide);
$this->cookie('plenario_pid', self::PID);
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('auch nur einen Kommentar');
$this->assertResponseNotContains('required>');
$this->cookie('plenario_pid', self::PID);
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'comment' => 'Keine Meinung dazu']);
$this->assertFlashMessage('Danke für deine Antwort!');
$this->cookie('plenario_pid', self::PID);
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('Keine Meinung dazu');
}
}