- Run the checks and the insert of VoteService::submit() in one transaction with the slide row locked, so double clicks or retries cannot store a vote twice (also covers the Q&A upvote budget and word cloud duplicates). - Limit word cloud terms per request and per participant, and throttle answers per IP and minute (THROTTLE_VOTES_PER_MINUTE, 429 with message). - Treat array values in text fields as empty instead of storing "Array". - Move the throttle helper to AppController for reuse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
184 lines
7.4 KiB
PHP
184 lines
7.4 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());
|
|
}
|
|
}
|