Files
Plenario/tests/TestCase/Controller/PollResetTest.php
T
gnilebeinandClaude Opus 5.5 8644d4bc4a
CI / Deploy / test (push) Successful in 52s
CI / Deploy / deploy (push) Skipped
Make the poll reset only delete responses
The presentation (current slide, pause) and the open/closed state of
slides are kept. Only quiz and Q&A slides go back to their start, as
the per-slide reset already did before.

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

111 lines
4.5 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;
$qa->stage = Slide::QA_DISCUSS;
$qa->is_voting_open = false;
$this->slidesTable()->saveManyOrFail([$quiz, $order, $qa]);
$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 stays where it is (paused on the choice slide).
$poll = $this->pollsTable()->get($poll->id);
$this->assertNull($poll->current_slide_id);
$this->assertSame($choice->id, $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->assertFalse($slides->get($order->id)->is_voting_open, 'open/closed is kept');
$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);
// Idempotent: a second reset changes nothing and does not fail.
$this->post("/admin/$token/reset");
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$this->assertSame($choice->id, $this->pollsTable()->get($poll->id)->paused_slide_id);
}
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\"");
}
}