Files
Plenario/tests/TestCase/Controller/PollResetTest.php
T
gnilebeinandClaude Sonnet 5.5 8c7818ee40
CI / Deploy / PHP 8.4 (push) Successful in 1m12s
CI / Deploy / PHP 8.5 (push) Successful in 1m13s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped
Let the dashboard follow the presenter
The live marker in the slide list stayed on the last known slide when the
presenter moved on in another window or with a clicker. The dashboard now
polls a cheap version fingerprint (admin:live) and reloads the lists when it
changes, but never under an open dialog, a pending request or a drag.

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

214 lines
8.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Resetting a whole poll from the dashboard (admin:reset).
*/
class PollResetTest extends TestCase
{
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 testResetOnlyDeletesResponses(): void
{
[$poll, $token] = $this->createPoll();
[$other] = $this->createPoll('Andere');
$choice = $this->createSlide($poll, Slide::TYPE_CHOICE);
$quiz = $this->createSlide($poll, Slide::TYPE_QUIZ, ['is_voting_open' => false], ['Richtig', 'Falsch']);
$order = $this->createSlide($poll, Slide::TYPE_ORDER, [], ['A', 'B', 'C']);
$qa = $this->createSlide($poll, Slide::TYPE_QA);
$foreign = $this->createSlide($other, Slide::TYPE_CHOICE);
$votes = new VoteService();
$hash = VoteService::participantHash(str_repeat('a', 32), $poll->id);
$poll = $this->goLive($poll, $choice);
$votes->submit($poll, $choice, $hash, ['option_id' => $choice->slide_options[0]->id]);
$other = $this->goLive($other, $foreign);
$votes->submit($other, $foreign, VoteService::participantHash(str_repeat('a', 32), $other->id), [
'option_id' => $foreign->slide_options[0]->id,
]);
// Quiz played and finished, ordering revealed, Q&A in discussion.
$quiz->setVotingOpen(true);
$quiz->is_voting_open = false;
$order->is_voting_open = false;
$choice->is_voting_open = false;
$qa->stage = Slide::QA_DISCUSS;
$qa->is_voting_open = false;
$this->slidesTable()->saveManyOrFail([$quiz, $order, $qa, $choice]);
$poll->showSlide($qa->id);
$poll->pause();
$this->pollsTable()->saveOrFail($poll);
$this->post("/admin/$token/reset");
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$this->assertFlashMessage('Alle Antworten wurden gelöscht.');
$responses = TableRegistry::getTableLocator()->get('Responses');
$this->assertSame(0, $responses->find()->where(['slide_id IN' => [$choice->id, $quiz->id, $order->id, $qa->id]])->count());
$this->assertSame(1, $responses->find()->where(['slide_id' => $foreign->id])->count(), 'other polls stay untouched');
// The presentation is back to "not started".
$poll = $this->pollsTable()->get($poll->id);
$this->assertNull($poll->current_slide_id);
$this->assertNull($poll->paused_slide_id);
$slides = $this->slidesTable();
$this->assertSame(Slide::QUIZ_READY, $slides->get($quiz->id)->quizPhase());
$this->assertNull($slides->get($quiz->id)->voting_started_at);
$this->assertTrue($slides->get($order->id)->is_voting_open, 'ordering is open again, solution hidden');
$this->assertSame(Slide::QA_COLLECT, $slides->get($qa->id)->qaStage());
$this->assertTrue($slides->get($qa->id)->is_voting_open);
$this->assertTrue($slides->get($choice->id)->is_voting_open, 'closed slides are open again');
// Idempotent: a second reset changes nothing and does not fail.
$this->post("/admin/$token/reset");
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$this->assertNull($this->pollsTable()->get($poll->id)->paused_slide_id);
}
public function testResetStopsLivePresentation(): void
{
[$poll, $token] = $this->createPoll();
$this->createSlide($poll, Slide::TYPE_CHOICE);
$second = $this->createSlide($poll, Slide::TYPE_CHOICE);
$poll = $this->goLive($poll, $second);
(new VoteService())->submit($poll, $second, VoteService::participantHash(str_repeat('a', 32), $poll->id), [
'option_id' => $second->slide_options[0]->id,
]);
$this->post("/admin/$token/reset");
$reloaded = $this->pollsTable()->get($poll->id);
$this->assertNull($reloaded->current_slide_id);
$this->assertNull($reloaded->paused_slide_id);
}
public function testResetKeepsPreparedQuestionsUnlessAsked(): void
{
[$poll, $token] = $this->createPoll();
$qa = $this->createSlide($poll, Slide::TYPE_QA);
$responses = TableRegistry::getTableLocator()->get('Responses');
$question = $responses->saveOrFail($responses->newEntity([
'slide_id' => $qa->id,
'participant_hash' => VoteService::PREPARED_HASH,
'text_value' => 'Vorbereitet?',
], ['accessibleFields' => ['*' => true]]));
$responses->saveOrFail($responses->newEntity([
'slide_id' => $qa->id,
'participant_hash' => VoteService::participantHash(str_repeat('a', 32), $poll->id),
'parent_id' => $question->id,
], ['accessibleFields' => ['*' => true]]));
$this->post("/admin/$token/reset");
$this->assertSame(1, $responses->find()->where(['slide_id' => $qa->id])->count());
$this->assertSame(1, $responses->find()->where(['id' => $question->id])->count());
$this->post("/admin/$token/reset", ['keep_prepared' => '0']);
$this->assertSame(0, $responses->find()->where(['slide_id' => $qa->id])->count());
}
public function testResetRequiresPost(): void
{
[, $token] = $this->createPoll();
$this->get("/admin/$token/reset");
$this->assertResponseCode(404);
}
public function testDashboardOffersResetOnlyWithSomethingToReset(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE);
$this->get("/admin/$token");
$this->assertResponseNotContains("/admin/$token/reset\"");
$this->assertResponseContains('Noch nichts zurückzusetzen');
$poll = $this->goLive($poll, $slide);
(new VoteService())->submit($poll, $slide, VoteService::participantHash(str_repeat('a', 32), $poll->id), [
'option_id' => $slide->slide_options[0]->id,
]);
$this->get("/admin/$token");
$this->assertResponseContains("/admin/$token/reset\"");
}
public function testDashboardOffersResetWithoutAnswersWhenSlidesWereTouched(): void
{
[$poll, $token] = $this->createPoll();
$quiz = $this->createSlide($poll, Slide::TYPE_QUIZ);
$quiz->startFresh();
$this->slidesTable()->saveOrFail($quiz);
$choice = $this->createSlide($poll, Slide::TYPE_CHOICE);
$this->get("/admin/$token");
$this->assertResponseContains('Noch nichts zurückzusetzen');
// Quiz started, nobody answered yet.
$quiz->setVotingOpen(true);
$this->slidesTable()->saveOrFail($quiz);
$this->get("/admin/$token");
$this->assertResponseContains("/admin/$token/reset\"");
$this->assertResponseContains("/admin/$token/slides/{$quiz->id}/reset");
$this->assertResponseNotContains("/admin/$token/slides/{$choice->id}/reset");
// Closed slide.
$quiz->startFresh();
$this->slidesTable()->saveOrFail($quiz);
$choice->is_voting_open = false;
$this->slidesTable()->saveOrFail($choice);
$this->get("/admin/$token");
$this->assertResponseContains("/admin/$token/reset\"");
$this->assertResponseContains("/admin/$token/slides/{$choice->id}/reset");
// Presentation running.
$choice->startFresh();
$this->slidesTable()->saveOrFail($choice);
$this->goLive($poll, $choice);
$this->get("/admin/$token");
$this->assertResponseContains("/admin/$token/reset\"");
}
public function testDashboardLiveVersionFollowsThePresenter(): void
{
[$poll, $token] = $this->createPoll();
$first = $this->createSlide($poll, Slide::TYPE_CHOICE);
$second = $this->createSlide($poll, Slide::TYPE_CHOICE);
$version = function () use ($token): string {
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->get("/admin/$token/live");
$this->assertResponseOk();
return json_decode((string)$this->_response->getBody(), true)['version'];
};
$start = $version();
$this->assertSame($start, $version(), 'stable while nothing happens');
$poll = $this->goLive($poll, $first);
$live = $version();
$this->assertNotSame($start, $live, 'live slide changed');
$this->goLive($poll, $second);
$this->assertNotSame($live, $version(), 'live marker moved');
$this->assertStringNotContainsString($token, (string)$this->_response->getBody());
}
}