Files
Plenario/tests/TestCase/Controller/AdminControllerTest.php
T
gnilebeinandClaude Sonnet 5.5 27771e7470
CI / Deploy / PHP 8.4 (push) Successful in 1m18s
CI / Deploy / PHP 8.5 (push) Successful in 1m29s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped
Add an optional countdown to every slide that collects answers
Choice, word cloud, open question, scale and ordering slides get a time
limit like the quiz. With a limit the slide waits ("ready") showing only
the question; next starts the countdown, the next one ends it, the third
moves on. Stage results and the ordering solution are withheld until the
countdown has started. The countdown music plays during any countdown.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 12:18:33 +01:00

419 lines
18 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Model\Table\PollsTable;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\Database\Driver\Sqlite;
use Cake\TestSuite\EmailTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Admin area behind the secret token: dashboard, slides, presenter controls.
*/
class AdminControllerTest extends TestCase
{
use EmailTrait;
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();
}
public function testInvalidTokenIs404(): void
{
$this->createPoll();
$this->get('/admin/' . str_repeat('x', 43));
$this->assertResponseCode(404);
}
public function testDashboardSendsPrivacyHeaders(): void
{
[$poll, $token] = $this->createPoll('Meine <Umfrage>');
$this->get("/admin/$token");
$this->assertResponseOk();
$this->assertResponseContains('Meine &lt;Umfrage&gt;');
$this->assertResponseContains($poll->formattedJoinCode());
$this->assertHeader('Referrer-Policy', 'no-referrer');
$this->assertHeaderContains('X-Robots-Tag', 'noindex');
}
public function testSettingsFormPostsAndSaves(): void
{
[$poll, $token] = $this->createPoll('Alt');
$this->get("/admin/$token");
$this->assertResponseNotContains('name="_method" value="PUT"', 'settings form must not override method');
$this->post("/admin/$token/settings", ['title' => 'Neu', 'is_active' => '1']);
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$this->assertSame('Neu', $this->pollsTable()->get($poll->id)->title);
}
public function testCommentSpeedIsSavedAndSentToThePresenter(): void
{
[$poll, $token] = $this->createPoll();
$this->get("/admin/$token/state");
$this->assertEquals(1, json_decode((string)$this->_response->getBody(), true)['poll']['commentSpeed']);
$this->post("/admin/$token/settings", ['title' => 'Testumfrage', 'is_active' => '1', 'comment_speed' => '150']);
$this->assertSame(150, $this->pollsTable()->get($poll->id)->comment_speed);
$this->get("/admin/$token/state");
$this->assertEquals(1.5, json_decode((string)$this->_response->getBody(), true)['poll']['commentSpeed']);
}
public function testInvalidCommentSpeedIsRejected(): void
{
[$poll, $token] = $this->createPoll();
$this->post("/admin/$token/settings", ['title' => 'Testumfrage', 'is_active' => '1', 'comment_speed' => '1000']);
$this->assertSame(100, $this->pollsTable()->get($poll->id)->comment_speed);
}
public function testTokenOfOtherPollCannotAccessSlides(): void
{
[$pollA] = $this->createPoll('A');
[, $tokenB] = $this->createPoll('B');
$slide = $this->createSlide($pollA);
$this->get("/admin/$tokenB/slides/{$slide->id}/edit");
$this->assertResponseCode(404);
}
public function testAddChoiceSlide(): void
{
[$poll, $token] = $this->createPoll();
$this->post("/admin/$token/slides/add?type=choice", [
'type' => 'choice',
'question' => 'Pizza oder Pasta?',
'options_text' => "Pizza\n\nPasta\nPizza\n",
'settings' => ['multiple' => '1', 'evil' => 'x'],
]);
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$slide = $this->slidesTable()->find()->contain(['SlideOptions'])->firstOrFail();
$this->assertSame($poll->id, $slide->poll_id);
$this->assertSame(['Pizza', 'Pasta'], array_map(fn($o) => $o->label, $slide->slide_options));
$this->assertSame(['multiple' => true, 'comments' => false, 'time_limit' => 0], $slide->settings);
$this->assertSame(1, $slide->position);
}
public function testAddQuizSlide(): void
{
[, $token] = $this->createPoll();
$data = [
'type' => 'quiz',
'question' => 'Hauptstadt von Frankreich?',
'settings' => ['time_limit' => '20'],
'is_voting_open' => '1',
'quiz_options' => [
['label' => 'Lyon', 'correct' => '0'],
['label' => 'Paris', 'correct' => '1'],
['label' => ''],
],
];
$this->get("/admin/$token/slides/add?type=quiz");
$this->assertResponseOk();
$this->assertResponseContains('name="quiz_options[0][label]"');
$this->assertResponseNotContains('name="is_voting_open"');
$this->post("/admin/$token/slides/add?type=quiz", ['quiz_options' => [['label' => 'A'], ['label' => 'B']]] + $data);
$this->assertResponseOk();
$this->assertResponseContains('Bitte markiere mindestens eine Antwort als richtig.');
$this->post("/admin/$token/slides/add?type=quiz", $data);
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$slide = $this->slidesTable()->find()->contain(['SlideOptions'])->firstOrFail();
$this->assertSame(['Lyon', 'Paris'], array_map(fn($o) => $o->label, $slide->slide_options));
$this->assertSame([false, true], array_map(fn($o) => $o->is_correct, $slide->slide_options));
$this->assertSame(['time_limit' => 20], $slide->settings);
$this->assertFalse($slide->is_voting_open, 'quiz starts closed until the presenter starts it');
}
public function testPresenterStartsAndEndsQuiz(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_QUIZ, ['is_voting_open' => false], ['Richtig', 'Falsch']);
$this->goLive($poll, $slide);
$control = function (string $action) use ($token): array {
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => $action]);
$this->assertResponseOk();
return json_decode((string)$this->_response->getBody(), true);
};
$state = $control('toggle');
$this->assertSame('running', $state['results']['phase']);
$this->assertGreaterThan(25, $state['results']['remaining']);
$this->assertStringNotContainsString('correct', (string)$this->_response->getBody());
$this->assertNotNull($this->slidesTable()->get($slide->id)->voting_ends_at);
$state = $control('toggle');
$this->assertSame('finished', $state['results']['phase']);
$this->assertTrue($state['results']['options'][0]['correct']);
$this->post("/admin/$token/slides/{$slide->id}/reset");
$slide = $this->slidesTable()->get($slide->id);
$this->assertSame(Slide::QUIZ_READY, $slide->quizPhase());
}
public function testEditKeepsVotesOfUnchangedOptions(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE, [], ['A', 'B']);
$optionA = $slide->slide_options[0];
$this->post("/admin/$token/slides/{$slide->id}/edit", [
'question' => 'Neu',
'options_text' => "C\nA",
]);
$this->assertRedirect();
$slide = $this->slidesTable()->get($slide->id, contain: ['SlideOptions']);
$this->assertSame('Neu', $slide->question);
$this->assertSame(['C', 'A'], array_map(fn($o) => $o->label, $slide->slide_options));
$this->assertSame($optionA->id, $slide->slide_options[1]->id);
}
public function testDeleteSlideClosesPositionGap(): void
{
[$poll, $token] = $this->createPoll();
$first = $this->createSlide($poll, Slide::TYPE_OPEN);
$second = $this->createSlide($poll, Slide::TYPE_SCALE);
$third = $this->createSlide($poll, Slide::TYPE_CHOICE);
$this->post("/admin/$token/slides/{$second->id}/delete");
$this->assertFalse($this->slidesTable()->exists(['id' => $second->id]));
$this->assertSame(1, $this->slidesTable()->get($first->id)->position);
$this->assertSame(2, $this->slidesTable()->get($third->id)->position);
}
public function testDeletingTheLiveSlidePausesAndResumesWithTheNextOne(): void
{
[$poll, $token] = $this->createPoll();
$first = $this->createSlide($poll, Slide::TYPE_OPEN);
$second = $this->createSlide($poll, Slide::TYPE_SCALE);
$third = $this->createSlide($poll, Slide::TYPE_CHOICE);
$poll = $this->goLive($poll, $second);
$this->post("/admin/$token/slides/{$second->id}/delete");
$poll = $this->pollsTable()->get($poll->id);
$this->assertTrue($poll->isPaused(), 'Participants see the pause page.');
$this->assertSame($third->id, $poll->paused_slide_id);
// The paused slide is deleted as well: resume with the previous one (last slide).
$this->post("/admin/$token/slides/{$third->id}/delete");
$poll = $this->pollsTable()->get($poll->id);
$this->assertTrue($poll->isPaused());
$this->assertSame($first->id, $poll->paused_slide_id);
// No slides left: back to "not started".
$this->post("/admin/$token/slides/{$first->id}/delete");
$poll = $this->pollsTable()->get($poll->id);
$this->assertNull($poll->current_slide_id);
$this->assertNull($poll->paused_slide_id);
$this->assertFalse($poll->isPaused());
}
public function testDeletingAnotherSlideKeepsTheLiveOne(): void
{
[$poll, $token] = $this->createPoll();
$live = $this->createSlide($poll, Slide::TYPE_OPEN);
$other = $this->createSlide($poll, Slide::TYPE_SCALE);
$poll = $this->goLive($poll, $live);
$this->post("/admin/$token/slides/{$other->id}/delete");
$this->assertSame($live->id, $this->pollsTable()->get($poll->id)->current_slide_id);
}
public function testReorderSlidesByDragAndDrop(): void
{
[$poll, $token] = $this->createPoll();
$a = $this->createSlide($poll, Slide::TYPE_OPEN);
$b = $this->createSlide($poll, Slide::TYPE_SCALE);
$c = $this->createSlide($poll, Slide::TYPE_CHOICE);
$positions = fn(): array => array_map(
fn($s) => $s->id,
$this->slidesTable()->listForPoll($poll->id),
);
$this->get("/admin/$token");
$this->assertResponseContains('data-sortable=');
$this->assertResponseContains('data-slide-id="' . $a->id . '"');
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/slides/reorder", ['ids' => [$c->id, $a->id, $b->id]]);
$this->assertResponseOk();
$this->assertTrue(json_decode((string)$this->_response->getBody(), true)['ok']);
$this->assertSame([$c->id, $a->id, $b->id], $positions());
$this->assertSame(1, $this->slidesTable()->get($c->id)->position);
// Same order again is fine (idempotent).
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/slides/reorder", ['ids' => [$c->id, $a->id, $b->id]]);
$this->assertResponseOk();
$this->assertSame([$c->id, $a->id, $b->id], $positions());
// Incomplete, duplicated or foreign ids change nothing.
[$other] = $this->createPoll('Andere');
$foreign = $this->createSlide($other, Slide::TYPE_OPEN);
foreach ([[$a->id, $b->id], [$a->id, $a->id, $b->id], [$a->id, $b->id, $foreign->id], []] as $ids) {
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/slides/reorder", ['ids' => $ids]);
$this->assertResponseCode(409);
$this->assertSame([$c->id, $a->id, $b->id], $positions());
}
$this->assertSame(1, $this->slidesTable()->get($foreign->id)->position);
}
public function testPresentStartsWithFirstSlideAndControlNavigates(): void
{
[$poll, $token] = $this->createPoll();
$first = $this->createSlide($poll, Slide::TYPE_CHOICE);
$second = $this->createSlide($poll, Slide::TYPE_WORDCLOUD);
$this->get("/admin/$token/present");
$this->assertResponseOk();
$this->assertResponseContains('aria-keyshortcuts="V"');
$this->assertResponseContains('[H]</kbd>');
$this->assertSame($first->id, $this->pollsTable()->get($poll->id)->current_slide_id);
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => 'next']);
$this->assertResponseOk();
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertSame($second->id, $state['slide']['id']);
$this->assertSame(1, $state['index']);
$this->assertArrayNotHasKey('email', $state['poll']);
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => 'toggle']);
$this->assertFalse($this->slidesTable()->get($second->id)->is_voting_open);
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => 'next']);
$this->assertSame($second->id, $this->pollsTable()->get($poll->id)->current_slide_id, 'stays on last slide');
}
public function testPauseAndResumeAreIdempotent(): void
{
[$poll, $token] = $this->createPoll();
$this->createSlide($poll, Slide::TYPE_CHOICE);
$second = $this->createSlide($poll, Slide::TYPE_WORDCLOUD);
$this->get("/admin/$token/present");
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => 'goto', 'slide_id' => $second->id]);
foreach ([1, 2] as $attempt) {
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => 'stop']);
$this->assertResponseOk();
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertNull($state['slide'], "paused after stop #$attempt");
$this->assertTrue($state['poll']['isPaused']);
}
foreach ([1, 2] as $attempt) {
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => 'resume']);
$this->assertResponseOk();
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertSame($second->id, $state['slide']['id'], "resumes the paused slide (#$attempt)");
$this->assertFalse($state['poll']['isPaused']);
}
}
public function testControlRejectsSlideOfOtherPoll(): void
{
[, $token] = $this->createPoll('A');
[$other] = $this->createPoll('B');
$foreign = $this->createSlide($other);
$this->post("/admin/$token/control", ['action' => 'goto', 'slide_id' => $foreign->id]);
$this->assertResponseCode(400);
}
public function testRotateTokenInvalidatesOldLink(): void
{
[$poll, $token] = $this->createPoll();
$this->post("/admin/$token/rotate-token");
$this->assertRedirect();
$this->assertMailSentTo('owner@example.org');
$fresh = $this->pollsTable()->get($poll->id);
$this->assertNotSame(PollsTable::hashToken($token), $fresh->admin_token_hash);
$this->get("/admin/$token");
$this->assertResponseCode(404);
}
public function testDeleteRequiresJoinCode(): void
{
[$poll, $token] = $this->createPoll();
$this->post("/admin/$token/delete", ['confirm_code' => 'WRONG']);
$this->assertTrue($this->pollsTable()->exists(['id' => $poll->id]));
$this->post("/admin/$token/delete", ['confirm_code' => strtolower($poll->join_code)]);
$this->assertRedirect(['_name' => 'home']);
$this->assertFalse($this->pollsTable()->exists(['id' => $poll->id]));
}
public function testDeletePollRemovesOptionsAndResponses(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE);
$poll = $this->goLive($poll, $slide);
(new VoteService())->submit($poll, $slide, 'participant', ['option_id' => $slide->slide_options[0]->id]);
$options = $this->fetchTable('SlideOptions');
$responses = $this->fetchTable('Responses');
$this->assertTrue($responses->exists(['slide_id' => $slide->id]));
// A normal SQLite connection does not enforce foreign keys (the test suite turns them on),
// so deleting must not rely on ON DELETE CASCADE.
$connection = $responses->getConnection();
$sqlite = $connection->getDriver() instanceof Sqlite;
if ($sqlite) {
$connection->execute('PRAGMA foreign_keys = OFF');
}
try {
$this->post("/admin/$token/delete", ['confirm_code' => $poll->join_code]);
} finally {
if ($sqlite) {
$connection->execute('PRAGMA foreign_keys = ON');
}
}
$this->assertFalse($this->slidesTable()->exists(['id' => $slide->id]));
$this->assertFalse($options->exists(['slide_id' => $slide->id]), 'Options must not outlive their poll.');
$this->assertFalse($responses->exists(['slide_id' => $slide->id]), 'Responses must not outlive their poll.');
}
public function testQrAndExport(): void
{
[, $token] = $this->createPoll();
$this->get("/admin/$token/qr.svg");
$this->assertResponseOk();
$this->assertContentType('image/svg+xml');
$this->get("/admin/$token/export.pdf");
$this->assertResponseOk();
$this->assertContentType('application/pdf');
$this->assertHeaderContains('Content-Disposition', 'attachment');
}
}