Files
gnilebeinandClaude Opus 5.5 c1f9be2eef Add a star rating style to scale slides
Scales can now be shown as stars (1 to n) instead of numbered steps:
participants tap stars, the stage shows the average as partly filled
stars and the distribution as bars from most to fewest stars, and the
evaluation page and PDF export print ratings as stars.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:46:33 +01:00

157 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Poll;
use App\Model\Entity\Slide;
use App\Model\Table\SlidesTable;
use App\Service\VoteException;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Scale slides shown as a star rating (style "stars"): always from 1 star to scale_max stars.
*/
class StarRatingTest 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();
}
/**
* @param array<string, mixed> $settings Extra settings
* @return array{0: \App\Model\Entity\Poll, 1: string, 2: \App\Model\Entity\Slide}
*/
protected function starSlide(array $settings = []): array
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_SCALE, [
'settings' => $settings + ['style' => Slide::SCALE_STARS, 'scale_max' => 5],
]);
return [$this->goLive($poll, $slide), $token, $slide];
}
protected function hash(string $char, Poll $poll): string
{
return VoteService::participantHash(str_repeat($char, 32), $poll->id);
}
public function testSettingIsSanitized(): void
{
$stars = SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['style' => 'stars', 'scale_min' => '0', 'scale_max' => '5']);
$this->assertSame(Slide::SCALE_STARS, $stars['style']);
$this->assertSame(1, $stars['scale_min'], 'Stars always start at one star');
$this->assertSame(Slide::SCALE_NUMBERS, SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, [])['style']);
$this->assertSame(Slide::SCALE_NUMBERS, SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['style' => 'hearts'])['style']);
$this->assertSame(0, SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['scale_min' => '0'])['scale_min']);
}
public function testExistingScalesStayNumbers(): void
{
[$poll] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['scale_min' => 1, 'scale_max' => 5]]);
$this->assertFalse($slide->isStarRating());
$this->assertSame('4', $slide->ratingText(4));
}
public function testRatingText(): void
{
[, , $slide] = $this->starSlide();
$this->assertTrue($slide->isStarRating());
$this->assertSame('★★★★☆', $slide->ratingText(4));
$this->assertSame('★☆☆☆☆', $slide->ratingText(1));
}
public function testFormSavesStarStyle(): void
{
[$poll, $token] = $this->createPoll();
$this->post("/admin/$token/slides/add?type=scale", [
'type' => 'scale',
'question' => 'Wie gefällt dir das Produkt?',
'settings' => ['style' => 'stars', 'scale_min' => '0', 'scale_max' => '5'],
]);
$slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail();
$this->assertTrue($slide->isStarRating());
$this->assertSame(1, $slide->setting('scale_min'));
}
public function testEditFormOffersStyle(): void
{
[, $token, $slide] = $this->starSlide();
$this->get("/admin/$token/slides/{$slide->id}/edit");
$this->assertResponseOk();
$this->assertResponseContains('data-scale-style');
$this->assertResponseContains('Anzahl Sterne');
}
public function testParticipantSeesStars(): void
{
[$poll] = $this->starSlide();
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('class="stars"');
$this->assertResponseContains('1 von 5 Sternen');
$this->assertResponseContains('5 von 5 Sternen');
$this->assertResponseNotContains('value="0"');
}
public function testZeroStarsAreRejected(): void
{
[$poll, , $slide] = $this->starSlide();
$this->expectException(VoteException::class);
$this->expectExceptionMessage('Sternen');
(new VoteService())->submit($poll, $slide, $this->hash('a', $poll), ['value' => 0]);
}
public function testPresenterGetsStarFlag(): void
{
[$poll, $token, $slide] = $this->starSlide();
$votes = new VoteService();
$votes->submit($poll, $slide, $this->hash('a', $poll), ['value' => 5]);
$votes->submit($poll, $slide, $this->hash('b', $poll), ['value' => 4]);
$this->get("/admin/$token/state");
$results = json_decode((string)$this->_response->getBody(), true)['results'];
$this->assertTrue($results['stars']);
$this->assertSame(1, $results['min']);
$this->assertEquals(4.5, $results['average']);
$this->assertSame([1, 2, 3, 4, 5], array_column($results['buckets'], 'value'));
}
public function testEvaluationAndExportShowStars(): void
{
[$poll, $token, $slide] = $this->starSlide(['comments' => true, 'comment_ratings' => true]);
(new VoteService())->submit($poll, $slide, $this->hash('a', $poll), ['value' => 4, 'comment' => 'Gut']);
$this->get("/admin/$token/slides/{$slide->id}/answers");
$this->assertResponseOk();
$this->assertResponseContains('★★★★☆');
$this->assertResponseContains('4 von 5 Sternen');
$html = $this->exportHtml($token);
$this->assertStringContainsString('von 5 Sternen', $html);
$this->assertStringContainsString('Bewertung ★★★★☆', $html);
}
}