*/ protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; protected function setUp(): void { parent::setUp(); $this->enableCsrfToken(); } protected function tearDown(): void { $this->clearImages(); parent::tearDown(); } /** * Creates a content slide with an uploaded image through the form. * * @return array{0: \App\Model\Entity\Poll, 1: string, 2: \App\Model\Entity\Slide} */ protected function slideWithImage(array $data = []): array { [$poll, $token] = $this->createPoll(); $this->postWithImage("/admin/$token/slides/add?type=content", $data + [ 'type' => 'content', 'question' => 'Mit Bild', ], $this->makeImage(3000, 2000)); $slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->first(); return [$poll, $token, $slide]; } public function testUploadStoresScaledImageWithAltAndLayout(): void { [$poll, $token, $slide] = $this->slideWithImage(['image_layout' => 'full']); $this->assertRedirect(['_name' => 'admin', 'token' => $token]); $this->assertNotNull($slide); $this->assertTrue($slide->hasImage()); $this->assertSame([1600, 1067], [$slide->image_width, $slide->image_height]); $this->assertSame(Slide::IMAGE_FULL, $slide->imageLayout()); $this->assertSame([$slide->image], $this->storedImages($poll->id)); } public function testPlacementIsOnlyOfferedWithAnImage(): void { [$poll, $token, $slide] = $this->slideWithImage(); $empty = $this->createSlide($poll, Slide::TYPE_CONTENT, ['question' => 'Ohne Bild']); // The placement fieldset is always rendered (usable without JavaScript); the script shows it only // for an existing image or a chosen file, marked by data-has-image. $this->get("/admin/$token/slides/{$slide->id}/edit"); $this->assertResponseContains('data-image-layout'); $this->assertResponseContains('data-has-image'); $this->get("/admin/$token/slides/{$empty->id}/edit"); $this->assertResponseContains('data-image-layout'); $this->assertResponseNotContains('data-has-image'); } public function testLayoutDefaultsByType(): void { [$poll, , $content] = $this->slideWithImage(); $this->assertSame(Slide::IMAGE_SIDE, $content->imageLayout()); $choice = $this->createSlide($poll, Slide::TYPE_CHOICE, ['image' => 'a', 'image_layout' => 'full']); $this->assertSame(Slide::IMAGE_TOP, $choice->imageLayout(), 'full exists for content slides only'); } public function testNoDescriptionIsNeeded(): void { [$poll, $token] = $this->createPoll(); $this->postWithImage("/admin/$token/slides/add?type=content", [ 'type' => 'content', 'question' => 'Ohne Beschreibung', ], $this->makeImage()); $this->assertRedirect(['_name' => 'admin', 'token' => $token]); $slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail(); $this->assertTrue($slide->hasImage()); $this->assertSame('', $slide->image_alt); $this->assertSame([$slide->image], $this->storedImages($poll->id)); } public function testRejectsFilesThatAreNoImages(): void { [$poll, $token] = $this->createPoll(); $path = tempnam(sys_get_temp_dir(), 'plen'); $this->imageFiles[] = $path; file_put_contents($path, 'just text'); $this->postWithImage("/admin/$token/slides/add?type=content", [ 'type' => 'content', 'question' => 'Kein Bild', ], $path, UPLOAD_ERR_OK, 'text.jpg'); $this->assertResponseContains('JPEG, PNG, WebP oder GIF'); $this->assertSame(0, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count()); } public function testFailedSaveKeepsTheOldImage(): void { [$poll, $token, $slide] = $this->slideWithImage(); $old = $slide->image; $this->postWithImage("/admin/$token/slides/{$slide->id}/edit", [ 'question' => '', ], $this->makeImage(100, 100)); $this->assertResponseContains('Bitte prüfe deine Eingaben'); $this->assertSame([$old], $this->storedImages($poll->id)); $this->assertSame($old, $this->slidesTable()->get($slide->id)->image); } public function testQuotaPerPoll(): void { Configure::write('Plenario.images.maxPerPoll', 1); [$poll, $token] = $this->slideWithImage(); $this->postWithImage("/admin/$token/slides/add?type=content", [ 'type' => 'content', 'question' => 'Zweites Bild', ], $this->makeImage()); $this->assertResponseContains('höchstens 1 Folien mit Bild'); $this->assertCount(1, $this->storedImages($poll->id)); $this->assertSame(1, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count()); } public function testReplacingAnImageDeletesTheOldFile(): void { [$poll, $token, $slide] = $this->slideWithImage(); $old = $slide->image; $this->postWithImage("/admin/$token/slides/{$slide->id}/edit", [ 'question' => 'Mit Bild', ], $this->makeImage(300, 200)); $slide = $this->slidesTable()->get($slide->id); $this->assertNotSame($old, $slide->image); $this->assertSame([$slide->image], $this->storedImages($poll->id)); $this->assertSame([300, 200], [$slide->image_width, $slide->image_height]); } public function testEditingWithoutUploadKeepsImageAndAllowsAltChange(): void { [$poll, $token, $slide] = $this->slideWithImage(); $this->postWithImage("/admin/$token/slides/{$slide->id}/edit", [ 'question' => 'Mit Bild', 'image_layout' => 'top', ], $this->makeImage(), UPLOAD_ERR_NO_FILE); $after = $this->slidesTable()->get($slide->id); $this->assertSame($slide->image, $after->image); $this->assertSame(Slide::IMAGE_TOP, $after->imageLayout()); $this->assertSame([$slide->image], $this->storedImages($poll->id)); } public function testRemoveImage(): void { [$poll, $token, $slide] = $this->slideWithImage(); $this->post("/admin/$token/slides/{$slide->id}/edit", ['question' => 'Mit Bild', 'remove_image' => '1']); $this->post("/admin/$token/slides/{$slide->id}/edit", ['question' => 'Mit Bild', 'remove_image' => '1']); $after = $this->slidesTable()->get($slide->id); $this->assertFalse($after->hasImage()); $this->assertSame([], $this->storedImages($poll->id)); } public function testDeletingTheSlideDeletesTheFile(): void { [$poll, $token, $slide] = $this->slideWithImage(); $other = $this->createSlide($poll, Slide::TYPE_OPEN); $this->post("/admin/$token/slides/{$other->id}/delete"); $this->assertCount(1, $this->storedImages($poll->id), 'another slide does not touch the image'); $this->post("/admin/$token/slides/{$slide->id}/delete"); $this->assertSame([], $this->storedImages($poll->id)); } public function testDeletingThePollDeletesAllImages(): void { [$poll, $token] = $this->slideWithImage(); $this->postWithImage("/admin/$token/slides/add?type=content", [ 'type' => 'content', 'question' => 'Zweites', ], $this->makeImage()); $this->assertCount(2, $this->storedImages($poll->id)); $this->post("/admin/$token/delete", ['confirm_code' => $poll->join_code]); $this->assertSame([], $this->storedImages($poll->id)); $this->assertDirectoryDoesNotExist($this->uploadRoot() . DS . $poll->id); } public function testDuplicateGetsItsOwnFile(): void { [$poll, $token, $slide] = $this->slideWithImage(['image_layout' => 'top']); $this->post("/admin/$token/slides/{$slide->id}/duplicate"); $slides = $this->slidesTable()->listForPoll($poll->id); $copy = $slides[1]; $this->assertTrue($copy->hasImage()); $this->assertNotSame($slide->image, $copy->image); $this->assertSame(Slide::IMAGE_TOP, $copy->imageLayout()); $this->assertCount(2, $this->storedImages($poll->id)); $this->post("/admin/$token/slides/{$copy->id}/delete"); $this->assertSame([$slide->image], $this->storedImages($poll->id), 'the original keeps its image'); } public function testResetKeepsImages(): void { [$poll, $token, $slide] = $this->slideWithImage(); $this->post("/admin/$token/reset"); $this->assertTrue($this->slidesTable()->get($slide->id)->hasImage()); $this->assertCount(1, $this->storedImages($poll->id)); } public function testYouTubeSlidesIgnoreImages(): void { $this->assertFalse(Slide::supportsImage(Slide::TYPE_YOUTUBE)); $this->assertTrue(Slide::supportsImage(Slide::TYPE_QUIZ)); } public function testImageIsServedForTheOwnPollOnly(): void { [$poll, , $slide] = $this->slideWithImage(); [$otherPoll] = $this->createPoll('Andere'); $this->get(['_name' => 'participate:image', 'code' => $poll->join_code, 'file' => $slide->image]); $this->assertResponseOk(); $this->assertContentType('image/webp'); $this->assertHeaderContains('Cache-Control', 'immutable'); $this->assertHeaderContains('Cache-Control', 'private'); $this->assertSame(IMAGETYPE_WEBP, getimagesizefromstring((string)$this->_response->getBody())[2]); $this->assertCookieNotSet('plenario_pid'); $this->get(['_name' => 'participate:image', 'code' => $otherPoll->join_code, 'file' => $slide->image]); $this->assertResponseCode(404); $this->get('/p/' . $poll->join_code . '/img/' . str_repeat('a', 24) . '.webp'); $this->assertResponseCode(404); $this->get('/p/' . $poll->join_code . '/img/..%2F..%2Fapp.php'); $this->assertResponseCode(404); } public function testImageIsGoneWhenReplaced(): void { [$poll, $token, $slide] = $this->slideWithImage(); $old = $slide->image; $this->postWithImage("/admin/$token/slides/{$slide->id}/edit", [ 'question' => 'Mit Bild', ], $this->makeImage(300, 200)); $this->get(['_name' => 'participate:image', 'code' => $poll->join_code, 'file' => $old]); $this->assertResponseCode(404); } public function testPresenterStateAndParticipantPageShowTheImage(): void { [$poll, $token, $slide] = $this->slideWithImage(); $poll = $this->goLive($poll, $slide); $this->get("/admin/$token/state"); $payload = json_decode((string)$this->_response->getBody(), true)['slide']; $this->assertSame('', $payload['image']['alt']); $this->assertSame('side', $payload['image']['layout']); $this->assertSame("/p/{$poll->join_code}/img/{$slide->image}", $payload['image']['url']); $this->get('/p/' . $poll->join_code); $this->assertResponseOk(); $this->assertResponseContains('/p/' . $poll->join_code . '/img/' . $slide->image); $this->assertResponseContains('alt=""'); $this->assertResponseContains('width="1600" height="1067"'); } public function testSlideWithoutImageHasNoImagePayload(): void { [$poll, $token] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_OPEN); $this->goLive($poll, $slide); $this->get("/admin/$token/state"); $this->assertNull(json_decode((string)$this->_response->getBody(), true)['slide']['image']); } public function testEditFormShowsImageFields(): void { [, $token, $slide] = $this->slideWithImage(); $this->get("/admin/$token/slides/{$slide->id}/edit"); $this->assertResponseOk(); $this->assertResponseContains('enctype="multipart/form-data"'); $this->assertResponseContains('name="remove_image"'); $this->assertResponseContains('value="full"'); } }