Files
Plenario/tests/TestCase/Controller/OrderSlideTest.php
T
gnilebeinandClaude Sonnet 5.5 7fd4a0da8c
CI / Deploy / PHP 8.4 (push) Successful in 1m15s
CI / Deploy / PHP 8.5 (push) Successful in 1m28s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped
Close an open ordering slide on "next" before moving on
Forward in the presenter view now reveals the solution first (like the
quiz build steps) instead of skipping straight to the next slide.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 09:21:54 +01:00

156 lines
7.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Service\PollStateService;
use App\Service\ResultsService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Ordering slides through the HTTP layer: editor, participant page, voting, export.
*/
class OrderSlideTest 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 testAddAndEditKeepTheEnteredOrder(): void
{
[$poll, $token] = $this->createPoll();
$this->get("/admin/$token/slides/add?type=order");
$this->assertResponseOk();
$this->assertResponseContains('name="order_options[]"');
$this->assertResponseContains('data-order-options');
$data = ['type' => 'order', 'question' => 'Sortiere nach Größe', 'is_voting_open' => '0'];
$this->post("/admin/$token/slides/add?type=order", $data + ['order_options' => ['Maus', '', '']]);
$this->assertResponseOk();
$this->assertResponseContains('Bitte gib mindestens zwei Einträge an.');
$this->post("/admin/$token/slides/add?type=order", $data + ['order_options' => ['Maus', 'Maus', 'Hund']]);
$this->assertResponseContains('Jeder Eintrag darf nur einmal vorkommen');
$this->post("/admin/$token/slides/add?type=order", $data + ['order_options' => ['Maus', 'Katze', '', 'Elefant']]);
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->contain(['SlideOptions'])->firstOrFail();
$this->assertSame(['Maus', 'Katze', 'Elefant'], array_map(fn($o) => $o->label, $slide->slide_options));
$this->assertTrue($slide->is_voting_open, 'the form cannot close voting, only the presenter can');
$ids = array_map(fn($o) => $o->id, $slide->slide_options);
// Reordering in the editor keeps the options (ids), only the positions change.
$this->post("/admin/$token/slides/{$slide->id}/edit", $data + ['order_options' => ['Elefant', 'Katze', 'Maus']]);
$slide = $this->slidesTable()->get($slide->id, contain: ['SlideOptions']);
$this->assertSame(['Elefant', 'Katze', 'Maus'], array_map(fn($o) => $o->label, $slide->slide_options));
$this->assertSame(array_reverse($ids), array_map(fn($o) => $o->id, $slide->slide_options));
$this->get("/admin/$token");
$this->assertResponseContains('Elefant → Katze → Maus');
}
public function testParticipantSortsSubmitsAndSeesVerdict(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_ORDER, [], ['Eins', 'Zwei', 'Drei']);
$this->goLive($poll, $slide);
$ids = array_map(fn($o) => $o->id, $slide->slide_options);
$this->cookie('plenario_pid', str_repeat('b', 32));
$this->get('/p/' . $poll->join_code);
$this->assertResponseOk();
$this->assertResponseContains('data-order-form');
$this->assertResponseContains('Reihenfolge einreichen');
preg_match_all('/name="order\[\]" value="(\d+)"/', (string)$this->_response->getBody(), $matches);
$shown = array_map('intval', $matches[1]);
$this->assertNotSame($ids, $shown, 'items are shuffled');
$this->assertEqualsCanonicalizing($ids, $shown);
$this->post('/p/' . $poll->join_code . '/vote', ['slide_id' => $slide->id, 'order' => $ids]);
$this->assertRedirect(['_name' => 'participate', 'code' => $poll->join_code]);
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('Deine Reihenfolge ist angekommen.');
$this->post("/admin/$token/control", ['action' => 'toggle']);
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('Richtig – deine Reihenfolge stimmt!');
$html = $this->exportHtml($token);
$this->assertStringContainsString('1 von 1', $html);
$this->assertStringContainsString('komplett richtig sortiert', $html);
}
public function testNextRevealsAnOpenOrderSlideBeforeMovingOn(): void
{
[$poll, $token] = $this->createPoll();
$order = $this->createSlide($poll, Slide::TYPE_ORDER, [], ['Eins', 'Zwei', 'Drei']);
$next = $this->createSlide($poll, Slide::TYPE_CONTENT);
$this->goLive($poll, $order);
// First forward closes the voting (solution), the slide stays live.
$this->post("/admin/$token/control", ['action' => 'next']);
$poll = $this->pollsTable()->get($poll->id);
$this->assertSame($order->id, $poll->current_slide_id);
$this->assertFalse($this->slidesTable()->get($order->id)->isVotingOpen());
// A second forward (and a repeated one) really moves on.
$this->post("/admin/$token/control", ['action' => 'next']);
$this->assertSame($next->id, $this->pollsTable()->get($poll->id)->current_slide_id);
}
public function testRankingHasNoSolutionAndShowsAverageOrder(): void
{
[$poll, $token] = $this->createPoll();
$this->post("/admin/$token/slides/add?type=order", [
'type' => 'order', 'question' => 'Was ist dir wichtig?',
'settings' => ['ranking' => '1'], 'order_options' => ['Eins', 'Zwei', 'Drei'],
]);
$slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->contain(['SlideOptions'])->firstOrFail();
$this->assertTrue($slide->isRanking());
$this->goLive($poll, $slide);
[$a, $b, $c] = array_map(fn($o) => $o->id, $slide->slide_options);
// Two people rank Drei first; the stored order of the entries has no meaning.
foreach (['c' => [$c, $a, $b], 'd' => [$c, $b, $a]] as $pid => $order) {
$this->cookie('plenario_pid', str_repeat($pid, 32));
$this->post('/p/' . $poll->join_code . '/vote', ['slide_id' => $slide->id, 'order' => $order]);
}
// Participant: confirmation only, no results, no verdict - even after closing.
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('Deine Reihenfolge ist angekommen.');
$this->assertResponseNotContains('Auflösung gibt es');
$this->post("/admin/$token/control", ['action' => 'toggle']);
$this->get('/p/' . $poll->join_code);
$this->assertResponseNotContains('deine Reihenfolge stimmt');
$this->assertResponseNotContains('Leider');
$state = (new PollStateService())->forParticipant($this->pollsTable()->get($poll->id), str_repeat('c', 32));
$this->assertNull($state['results']);
$this->assertFalse($state['order']['revealed']);
// Presenter/export: items by average position, Drei first.
$results = (new ResultsService())->forSlide($this->slidesTable()->get($slide->id, contain: ['SlideOptions']));
$this->assertTrue($results['ranking']);
// Eins and Zwei tie at 2.5 and keep the order of the entries.
$this->assertSame(['Drei', 'Eins', 'Zwei'], array_column($results['items'], 'label'));
$this->assertSame(1.0, (float)$results['items'][0]['average']);
$this->assertSame(100.0, (float)$results['items'][0]['score']);
$html = $this->exportHtml($token);
$this->assertStringContainsString('Einschätzungen, sortiert nach', $html);
}
}