Files
Plenario/tests/TestCase/Controller/PollResetTest.php
T
gnilebeinandClaude Opus 5.5 e1b327899f
CI / Deploy / test (push) Successful in 51s
CI / Deploy / deploy (push) Skipped
Allow resetting the whole poll from the dashboard
Deletes all responses of all slides, puts every slide back to its
initial state and restarts the presentation. Slides, settings, links
and the join code are kept. The per-slide reset shares the logic via
ResetService and now also reopens closed slides.

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

109 lines
4.4 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 testResetDeletesAllResponsesAndStartsOver(): 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('Die Umfrage wurde zurückgesetzt: Alle Antworten sind gelöscht, die Präsentation beginnt wieder bei der ersten Folie.');
$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');
$poll = $this->pollsTable()->get($poll->id);
$this->assertNull($poll->current_slide_id);
$this->assertNull($poll->paused_slide_id);
$this->assertSame($foreign->id, $this->pollsTable()->get($other->id)->current_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);
$this->assertFalse($slides->get($order->id)->isOrderRevealed());
$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->assertNull($this->pollsTable()->get($poll->id)->current_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');
$this->goLive($poll, $slide);
$this->get("/admin/$token");
$this->assertResponseContains("/admin/$token/reset\"");
}
}