Files
Plenario/tests/TestCase/Controller/DuplicateSlideTest.php
T
gnilebeinandClaude Opus 5.5 e2f24877dd Add duplicating slides
Copies question, description, settings and options (incl. correct quiz
answers) directly behind the original, without responses. The copy starts
like a new slide (Slide::startFresh()) and opens in the edit form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:18:10 +01:00

130 lines
5.3 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;
/**
* Duplicating a slide from the dashboard (slides:duplicate).
*/
class DuplicateSlideTest 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 testCopyIsPlacedBehindOriginalWithoutResponses(): void
{
[$poll, $token] = $this->createPoll();
$first = $this->createSlide($poll, Slide::TYPE_CHOICE, [
'description' => '*Wichtig*',
'settings' => ['multiple' => true] + Slide::DEFAULT_SETTINGS[Slide::TYPE_CHOICE],
], ['Rot', 'Grün', 'Blau']);
$last = $this->createSlide($poll, Slide::TYPE_OPEN);
$poll = $this->goLive($poll, $first);
(new VoteService())->submit($poll, $first, VoteService::participantHash(str_repeat('a', 32), $poll->id), [
'option_id' => $first->slide_options[0]->id,
]);
$this->post("/admin/$token/slides/{$first->id}/duplicate");
$slides = $this->slidesTable()->listForPoll($poll->id);
$this->assertSame([$first->id, $last->id], [$slides[0]->id, $slides[2]->id]);
$copy = $slides[1];
$this->assertRedirect(['_name' => 'slides:edit', 'token' => $token, 'id' => $copy->id]);
$this->assertSame([1, 2, 3], array_map(fn($s) => $s->position, $slides));
$this->assertSame('Frage choice (Kopie)', $copy->question);
$this->assertSame('*Wichtig*', $copy->description);
$this->assertSame($first->getSettings(), $copy->getSettings());
$this->assertSame(['Rot', 'Grün', 'Blau'], array_map(fn($o) => $o->label, $copy->slide_options));
$this->assertNotContains($copy->slide_options[0]->id, array_map(fn($o) => $o->id, $first->slide_options));
$responses = TableRegistry::getTableLocator()->get('Responses');
$this->assertSame(0, $responses->find()->where(['slide_id' => $copy->id])->count());
$this->assertSame(1, $responses->find()->where(['slide_id' => $first->id])->count(), 'original keeps its votes');
$this->assertSame($first->id, $this->pollsTable()->get($poll->id)->current_slide_id, 'live slide stays');
}
public function testCopyStartsFresh(): void
{
[$poll, $token] = $this->createPoll();
$quiz = $this->createSlide($poll, Slide::TYPE_QUIZ, [], ['Richtig', 'Falsch', 'Auch falsch']);
$quiz->setVotingOpen(true);
$quiz->is_voting_open = false;
$qa = $this->createSlide($poll, Slide::TYPE_QA);
$qa->stage = Slide::QA_DISCUSS;
$qa->is_voting_open = false;
$closed = $this->createSlide($poll, Slide::TYPE_SCALE, ['is_voting_open' => false]);
$this->slidesTable()->saveManyOrFail([$quiz, $qa]);
foreach ([$quiz, $qa, $closed] as $slide) {
$this->post("/admin/$token/slides/{$slide->id}/duplicate");
$this->assertResponseCode(302);
}
$slides = $this->slidesTable()->listForPoll($poll->id);
$this->assertCount(6, $slides);
[, $quizCopy, , $qaCopy, , $scaleCopy] = $slides;
$this->assertSame(Slide::QUIZ_READY, $quizCopy->quizPhase());
$this->assertNull($quizCopy->voting_started_at);
$this->assertSame([true, false, false], array_map(fn($o) => $o->is_correct, $quizCopy->slide_options));
$this->assertSame(Slide::QA_COLLECT, $qaCopy->qaStage());
$this->assertTrue($qaCopy->is_voting_open);
$this->assertSame(Slide::TYPE_SCALE, $scaleCopy->type);
$this->assertTrue($scaleCopy->is_voting_open);
}
public function testOnlySlidesOfOwnPollAndOnlyPost(): void
{
[$poll, $token] = $this->createPoll();
[$other] = $this->createPoll('Andere');
$own = $this->createSlide($poll);
$foreign = $this->createSlide($other);
$this->post("/admin/$token/slides/{$foreign->id}/duplicate");
$this->assertResponseCode(404);
$this->assertSame(1, $this->slidesTable()->find()->where(['poll_id' => $other->id])->count());
$this->get("/admin/$token/slides/{$own->id}/duplicate");
$this->assertResponseCode(404);
$this->assertSame(1, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count());
}
public function testLongQuestionIsShortened(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CONTENT, ['question' => str_repeat('x', 500)], []);
$this->post("/admin/$token/slides/{$slide->id}/duplicate");
$copy = $this->slidesTable()->listForPoll($poll->id)[1];
$this->assertSame(500, mb_strlen($copy->question));
}
public function testDashboardOffersDuplicate(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll);
$this->get("/admin/$token");
$this->assertResponseContains("/admin/$token/slides/{$slide->id}/duplicate");
$this->assertResponseContains('Duplizieren');
}
}