*/ protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; protected function setUp(): void { parent::setUp(); $this->enableCsrfToken(); } /** * @param array $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); } }