The dashboard and per-slide reset buttons only checked for responses, so a started quiz without votes, a closed slide or a running presentation could not be reset. Check Slide::isFresh() and Poll::isStarted() as well. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
188 lines
7.8 KiB
PHP
188 lines
7.8 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\"");
|
|
}
|
|
}
|