Files
Plenario/tests/TestCase/Controller/ParticipateControllerTest.php
T
gnilebeinandClaude Opus 5.5 cfb445123e
CI / Deploy / test (push) Successful in 1m9s
CI / Deploy / deploy (push) Skipped
Keep results off participant devices
Results belong on the stage. Participants only get results where they
interact with them (word cloud with upvotes) and, once resolved, the
solution of quiz and ordering slides with their own verdict, without
counts or distribution. Removes the per-poll setting
show_results_to_participants (column stays unused).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:02:36 +01:00

161 lines
6.2 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 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');
}
}