Files
Plenario/tests/TestCase/Controller/SlideImageTest.php
T
gnilebeinandClaude Sonnet 5.5 a395b50894
CI / Deploy / PHP 8.4 (push) Successful in 1m10s
CI / Deploy / PHP 8.5 (push) Successful in 1m16s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped
Add optional images to slides
Slides (except YouTube) can carry one image. Uploads are validated, rotated
by EXIF orientation, scaled to 1600 px and stored as WebP outside webroot,
which also strips camera metadata. Images have an alt text (or are marked
decorative) and a layout: top, side or full screen (content slides).

Files are removed after the commit when a slide is deleted or its image is
replaced, with the poll, and `bin/cake images_cleanup` sweeps leftovers.
Duplicated slides get their own file. Docker image gains gd with WebP.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 23:17:37 +01:00

340 lines
13 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Test\TestCase\ImageTestTrait;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\Core\Configure;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Images on slides: upload and scaling, alternative text, quota, serving, and what happens to the
* file when the image is replaced, removed or the slide (or poll) is deleted or duplicated.
*/
class SlideImageTest extends TestCase
{
use ImageTestTrait;
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();
}
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',
'image_alt' => 'Ein roter Kreis',
], $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('Ein roter Kreis', $slide->image_alt);
$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 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 testImageNeedsAltTextOrDecorativeFlag(): void
{
[$poll, $token] = $this->createPoll();
$this->postWithImage("/admin/$token/slides/add?type=content", [
'type' => 'content',
'question' => 'Ohne Text',
], $this->makeImage());
$this->assertResponseOk();
$this->assertResponseContains('Bitte beschreibe das Bild');
$this->assertSame(0, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count());
$this->assertSame([], $this->storedImages($poll->id), 'no file left behind');
$this->postWithImage("/admin/$token/slides/add?type=content", [
'type' => 'content',
'question' => 'Dekoration',
'image_decorative' => '1',
], $this->makeImage());
$slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail();
$this->assertSame('', $slide->image_alt);
$this->assertTrue($slide->hasImage());
}
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',
'image_alt' => 'x',
], $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' => '',
'image_alt' => 'neu',
], $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',
'image_alt' => 'x',
], $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',
'image_alt' => 'Neu',
], $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_alt' => 'Anderer Text',
'image_layout' => 'top',
], $this->makeImage(), UPLOAD_ERR_NO_FILE);
$after = $this->slidesTable()->get($slide->id);
$this->assertSame($slide->image, $after->image);
$this->assertSame('Anderer Text', $after->image_alt);
$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->assertNull($after->image_alt);
$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',
'image_alt' => 'x',
], $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('Ein roter Kreis', $copy->image_alt);
$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',
'image_alt' => 'Neu',
], $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('Ein roter Kreis', $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="Ein roter Kreis"');
$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="Ein roter Kreis"');
$this->assertResponseContains('Bildschirm füllen');
}
}