Results belong on the stage. Participants only get results where they interact with them (word cloud with upvotes) and, once resolved, the solution of quiz and ordering slides with their own verdict, without counts or distribution. Removes the per-poll setting show_results_to_participants (column stays unused). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
7.5 KiB
PHP
182 lines
7.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Test\TestCase\Service;
|
|
|
|
use App\Model\Entity\Poll;
|
|
use App\Model\Entity\Slide;
|
|
use App\Model\Table\SlidesTable;
|
|
use App\Service\PollStateService;
|
|
use App\Service\ResultsService;
|
|
use App\Service\VoteException;
|
|
use App\Service\VoteService;
|
|
use App\Test\TestCase\PlenarioTestTrait;
|
|
use Cake\TestSuite\TestCase;
|
|
|
|
/**
|
|
* Ordering slides: participants sort shuffled items, the solution is revealed when voting closes.
|
|
*/
|
|
class OrderTest extends TestCase
|
|
{
|
|
use PlenarioTestTrait;
|
|
|
|
/**
|
|
* @var list<string>
|
|
*/
|
|
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
|
|
|
|
/**
|
|
* @return array{0: \App\Model\Entity\Poll, 1: \App\Model\Entity\Slide}
|
|
*/
|
|
protected function createOrder(array $items = ['Steinzeit', 'Antike', 'Mittelalter', 'Neuzeit']): array
|
|
{
|
|
[$poll] = $this->createPoll();
|
|
$slide = $this->createSlide($poll, Slide::TYPE_ORDER, [], $items);
|
|
|
|
return [$this->goLive($poll, $slide), $slide];
|
|
}
|
|
|
|
/**
|
|
* @return list<int>
|
|
*/
|
|
protected function ids(Slide $slide, array $labels): array
|
|
{
|
|
$byLabel = [];
|
|
foreach ($slide->slide_options as $option) {
|
|
$byLabel[$option->label] = $option->id;
|
|
}
|
|
|
|
return array_map(fn($label) => $byLabel[$label], $labels);
|
|
}
|
|
|
|
protected function close(Slide $slide): Slide
|
|
{
|
|
$slide->setVotingOpen(false);
|
|
$this->slidesTable()->saveOrFail($slide);
|
|
|
|
return $this->slidesTable()->get($slide->id, contain: ['SlideOptions']);
|
|
}
|
|
|
|
protected function participantState(Poll $poll, string $participant): array
|
|
{
|
|
return (new PollStateService())->forParticipant($this->pollsTable()->get($poll->id), $participant);
|
|
}
|
|
|
|
public function testShuffledOptionsAreStableAndNeverTheSolution(): void
|
|
{
|
|
[, $slide] = $this->createOrder(['A', 'B']);
|
|
$correct = $this->ids($slide, ['A', 'B']);
|
|
|
|
foreach (['p1', 'p2', 'p3', 'p4', 'p5', 'slide:1'] as $seed) {
|
|
$shuffled = array_map(fn($o) => $o->id, $slide->shuffledOptions($seed));
|
|
$this->assertNotSame($correct, $shuffled);
|
|
$this->assertSame($shuffled, array_map(fn($o) => $o->id, $slide->shuffledOptions($seed)));
|
|
}
|
|
}
|
|
|
|
public function testParseOrderOptions(): void
|
|
{
|
|
$this->assertSame(
|
|
['labels' => ['Eins', 'Zwei'], 'duplicate' => false],
|
|
SlidesTable::parseOrderOptions([' Eins ', '', 'Zwei', ['x']]),
|
|
);
|
|
$this->assertTrue(SlidesTable::parseOrderOptions(['Eins', 'Eins', 'Zwei'])['duplicate']);
|
|
}
|
|
|
|
public function testRejectsIncompleteOrForeignOrders(): void
|
|
{
|
|
[$poll, $slide] = $this->createOrder();
|
|
$ids = $this->ids($slide, ['Steinzeit', 'Antike', 'Mittelalter', 'Neuzeit']);
|
|
$service = new VoteService();
|
|
|
|
$invalid = [
|
|
[],
|
|
['order' => array_slice($ids, 0, 3)],
|
|
['order' => [$ids[0], $ids[0], $ids[1], $ids[2]]],
|
|
['order' => array_merge($ids, [999999])],
|
|
['order' => [...array_slice($ids, 0, 3), 'x']],
|
|
];
|
|
foreach ($invalid as $data) {
|
|
try {
|
|
$service->submit($poll, $slide, 'p1', $data);
|
|
$this->fail('VoteException expected for ' . json_encode($data));
|
|
} catch (VoteException $e) {
|
|
$this->assertStringContainsString('Reihenfolge', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
$service->submit($poll, $slide, 'p1', ['order' => array_reverse($ids)]);
|
|
$this->expectExceptionMessage('Du hast bereits abgestimmt.');
|
|
$service->submit($poll, $slide, 'p1', ['order' => $ids]);
|
|
}
|
|
|
|
public function testOpenSlideHidesSolutionAndCounts(): void
|
|
{
|
|
[$poll, $slide] = $this->createOrder();
|
|
(new VoteService())->submit($poll, $slide, 'p1', [
|
|
'order' => $this->ids($slide, ['Steinzeit', 'Antike', 'Mittelalter', 'Neuzeit']),
|
|
]);
|
|
|
|
$results = (new ResultsService())->forSlide($slide);
|
|
$this->assertFalse($results['revealed']);
|
|
$this->assertSame(1, $results['participants']);
|
|
$this->assertArrayNotHasKey('correctCount', $results);
|
|
$this->assertNotSame($slide->slide_options[0]->id, $results['items'][0]['id'] ?? null, 'not in the correct order');
|
|
foreach ($results['items'] as $item) {
|
|
$this->assertSame(['id', 'label'], array_keys($item));
|
|
}
|
|
|
|
$state = $this->participantState($poll, 'p1');
|
|
$this->assertTrue($state['hasResponded']);
|
|
$this->assertNull($state['results']);
|
|
$this->assertSame(['revealed' => false, 'answer' => null, 'isCorrect' => null], $state['order']);
|
|
}
|
|
|
|
public function testClosingRevealsResultsToEveryone(): void
|
|
{
|
|
[$poll, $slide] = $this->createOrder();
|
|
$service = new VoteService();
|
|
$service->submit($poll, $slide, 'p1', ['order' => $this->ids($slide, ['Steinzeit', 'Antike', 'Mittelalter', 'Neuzeit'])]);
|
|
$service->submit($poll, $slide, 'p2', ['order' => $this->ids($slide, ['Antike', 'Steinzeit', 'Mittelalter', 'Neuzeit'])]);
|
|
$service->submit($poll, $slide, 'p3', ['order' => $this->ids($slide, ['Steinzeit', 'Antike', 'Mittelalter', 'Neuzeit'])]);
|
|
$slide = $this->close($slide);
|
|
|
|
$results = (new ResultsService())->forSlide($slide);
|
|
$this->assertTrue($results['revealed']);
|
|
$this->assertSame(3, $results['participants']);
|
|
$this->assertSame(2, $results['correctCount']);
|
|
$this->assertSame(['Steinzeit', 'Antike', 'Mittelalter', 'Neuzeit'], array_column($results['items'], 'label'));
|
|
$this->assertSame([1, 2, 3, 4], array_column($results['items'], 'position'));
|
|
$this->assertSame([2, 2, 3, 3], array_column($results['items'], 'placed'));
|
|
|
|
// Participants get the solution and their own verdict, but no counts.
|
|
$wrong = $this->participantState($poll, 'p2');
|
|
$this->assertTrue($wrong['order']['revealed']);
|
|
$this->assertFalse($wrong['order']['isCorrect']);
|
|
$this->assertSame($this->ids($slide, ['Antike', 'Steinzeit', 'Mittelalter', 'Neuzeit']), $wrong['order']['answer']);
|
|
$this->assertSame([1, 2, 3, 4], array_column($wrong['results']['items'], 'position'));
|
|
$this->assertArrayNotHasKey('correctCount', $wrong['results']);
|
|
$this->assertArrayNotHasKey('placed', $wrong['results']['items'][0]);
|
|
$this->assertArrayNotHasKey('percent', $wrong['results']['items'][0]);
|
|
$this->assertTrue($this->participantState($poll, 'p1')['order']['isCorrect']);
|
|
$this->assertNull($this->participantState($poll, 'nobody')['order']['isCorrect']);
|
|
|
|
$this->expectExceptionMessage('Die Abstimmung zu dieser Frage ist geschlossen.');
|
|
$service->submit($this->pollsTable()->get($poll->id), $slide, 'p4', [
|
|
'order' => $this->ids($slide, ['Steinzeit', 'Antike', 'Mittelalter', 'Neuzeit']),
|
|
]);
|
|
}
|
|
|
|
public function testReorderingOptionsChangesTheSolution(): void
|
|
{
|
|
[$poll, $slide] = $this->createOrder(['A', 'B', 'C']);
|
|
(new VoteService())->submit($poll, $slide, 'p1', ['order' => $this->ids($slide, ['B', 'A', 'C'])]);
|
|
$this->slidesTable()->syncOptions($slide, ['B', 'A', 'C']);
|
|
$slide = $this->close($slide);
|
|
|
|
$results = (new ResultsService())->forSlide($slide);
|
|
$this->assertSame(1, $results['correctCount'], 'option ids are kept, only positions change');
|
|
$this->assertSame(['B', 'A', 'C'], array_column($results['items'], 'label'));
|
|
}
|
|
}
|