CakePHP 5.4 app for live polls: create via email without login, manage via secret admin link, join via code. Includes slide types (choice, wordcloud, open, scale, content), presenter mode, CSV export, captcha, throttling, tests and Gitea CI/deploy workflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
6.1 KiB
PHP
158 lines
6.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Test\TestCase\Controller;
|
|
|
|
use App\Model\Entity\Slide;
|
|
use App\Test\TestCase\PlenarioTestTrait;
|
|
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();
|
|
}
|
|
|
|
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 testStateHidesResultsUntilAnswered(): 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']);
|
|
$this->assertEqualsWithDelta(4.0, $state['results']['average'], 0.001);
|
|
}
|
|
|
|
public function testWordcloudAjaxVoteReturnsStateWithUpvoteCloud(): void
|
|
{
|
|
[$poll] = $this->createPoll();
|
|
$poll->show_results_to_participants = false;
|
|
$this->pollsTable()->saveOrFail($poll);
|
|
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['max_words' => 0, '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']);
|
|
$this->assertNull($body['state']['words']['remaining']);
|
|
// Cloud is visible despite show_results_to_participants=false, because upvotes are on.
|
|
$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');
|
|
}
|
|
}
|