Add optional images to slides
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

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>
This commit is contained in:
gnilebeinandClaude Sonnet 5.5 committed 2026-11-14 23:17:37 +01:00
1 parent 8c7818ee40
commit a395b50894
30 files changed
+1705 -11

No files matched your search

+188
View File
@@ -0,0 +1,188 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Service;
use App\Service\ImageException;
use App\Service\ImageService;
use App\Test\TestCase\ImageTestTrait;
use Cake\TestSuite\TestCase;
use Laminas\Diactoros\UploadedFile;
/**
* Scaling, validation and file handling of slide images.
*/
class ImageServiceTest extends TestCase
{
use ImageTestTrait;
protected ImageService $images;
protected function setUp(): void
{
parent::setUp();
$this->images = new ImageService();
}
protected function tearDown(): void
{
$this->clearImages();
parent::tearDown();
}
protected function file(string $path, int $error = UPLOAD_ERR_OK): UploadedFile
{
return new UploadedFile($path, (int)filesize($path), $error, 'bild.jpg', 'image/jpeg');
}
public function testLargeImageIsScaledDownAndStoredAsWebp(): void
{
$stored = $this->images->store($this->file($this->makeImage(3200, 2000)), 7);
$this->assertSame(1600, $stored['width']);
$this->assertSame(1000, $stored['height']);
$this->assertTrue(ImageService::isValidName($stored['name']));
$path = $this->images->path(7, $stored['name']);
$this->assertNotNull($path);
$info = getimagesize($path);
$this->assertSame(IMAGETYPE_WEBP, $info[2]);
$this->assertSame([1600, 1000], [$info[0], $info[1]]);
}
public function testSmallImageIsNotEnlarged(): void
{
$stored = $this->images->store($this->file($this->makeImage(300, 200, 'png')), 7);
$this->assertSame([300, 200], [$stored['width'], $stored['height']]);
}
public function testAcceptsGifAndWebp(): void
{
foreach (['gif', 'webp'] as $format) {
$stored = $this->images->store($this->file($this->makeImage(40, 30, $format)), 7);
$this->assertSame([40, 30], [$stored['width'], $stored['height']], $format);
}
}
public function testExifOrientationIsApplied(): void
{
// A landscape JPEG that says "rotate by 90°" ends up as a portrait picture.
$jpeg = (string)file_get_contents($this->makeImage(200, 100));
$path = $this->makeImage(1, 1);
file_put_contents($path, substr($jpeg, 0, 2) . $this->exifOrientation(6) . substr($jpeg, 2));
$this->assertSame(6, ImageService::jpegOrientation((string)file_get_contents($path)));
$stored = $this->images->store($this->file($path), 7);
$this->assertSame([100, 200], [$stored['width'], $stored['height']]);
}
public function testJpegWithoutExifIsUpright(): void
{
$this->assertSame(1, ImageService::jpegOrientation((string)file_get_contents($this->makeImage(20, 10))));
$this->assertSame(1, ImageService::jpegOrientation('not a jpeg'));
}
public function testRejectsNonImages(): void
{
$path = tempnam(sys_get_temp_dir(), 'plen');
$this->imageFiles[] = $path;
file_put_contents($path, '<?php echo 1;');
$this->expectException(ImageException::class);
$this->images->store($this->file($path), 7);
}
public function testRejectsSvg(): void
{
$path = tempnam(sys_get_temp_dir(), 'plen');
$this->imageFiles[] = $path;
file_put_contents($path, '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><script>alert(1)</script></svg>');
$this->expectException(ImageException::class);
$this->images->store($this->file($path), 7);
}
public function testRejectsAnimatedGif(): void
{
$header = 'GIF89a' . "\x01\x00\x01\x00" . "\x80\x00\x00" . "\x00\x00\x00\xFF\xFF\xFF";
$frame = "\x21\xF9\x04\x00\x0A\x00\x00\x00" . "\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00" . "\x02\x02\x44\x01\x00";
$path = tempnam(sys_get_temp_dir(), 'plen');
$this->imageFiles[] = $path;
file_put_contents($path, $header . $frame . $frame . "\x3B");
$this->expectException(ImageException::class);
$this->expectExceptionMessage('Animierte GIFs');
$this->images->store($this->file($path), 7);
}
public function testRejectsUploadErrors(): void
{
$path = $this->makeImage();
$this->expectException(ImageException::class);
$this->images->store($this->file($path, UPLOAD_ERR_PARTIAL), 7);
}
public function testDeleteCopyAndPollCleanup(): void
{
$first = $this->images->store($this->file($this->makeImage()), 7)['name'];
$copy = $this->images->copy(7, $first);
$this->assertNotNull($copy);
$this->assertNotSame($first, $copy);
$this->assertCount(2, $this->storedImages(7));
$this->images->delete(7, $first);
$this->images->delete(7, $first);
$this->images->delete(7, null);
$this->images->delete(7, '../../evil.php');
$this->assertSame([$copy], $this->storedImages(7));
$this->assertNull($this->images->copy(7, $first), 'copy of a missing file');
$this->images->deletePoll(7);
$this->images->deletePoll(7);
$this->assertSame([], $this->storedImages(7));
$this->assertDirectoryDoesNotExist($this->uploadRoot() . DS . '7');
}
public function testPathRejectsForeignNames(): void
{
$name = $this->images->store($this->file($this->makeImage()), 7)['name'];
$this->assertNotNull($this->images->path(7, $name));
$this->assertNull($this->images->path(8, $name), 'other poll');
$this->assertNull($this->images->path(7, '../7/' . $name));
$this->assertNull($this->images->path(7, 'image.php'));
}
public function testCleanupRemovesOrphansOnly(): void
{
$kept = $this->images->store($this->file($this->makeImage()), 7)['name'];
$orphan = $this->images->store($this->file($this->makeImage()), 7)['name'];
$gone = $this->images->store($this->file($this->makeImage()), 8)['name'];
$this->assertEqualsCanonicalizing(['7/' . $orphan, '8/' . $gone], $this->images->cleanup([7 => [$kept]], true));
$this->assertCount(2, $this->storedImages(7), 'dry run deletes nothing');
$removed = $this->images->cleanup([7 => [$kept]]);
$this->assertEqualsCanonicalizing(['7/' . $orphan, '8/' . $gone], $removed);
$this->assertSame([$kept], $this->storedImages(7));
$this->assertSame([], $this->storedImages(8));
$this->assertSame([], $this->images->cleanup([7 => [$kept]]), 'second run changes nothing');
}
/**
* An Exif segment (big endian) that only contains the orientation.
*
* @param int $orientation 1-8
* @return string
*/
protected function exifOrientation(int $orientation): string
{
$tiff = "MM\x00\x2A\x00\x00\x00\x08" . "\x00\x01" . "\x01\x12\x00\x03\x00\x00\x00\x01" . pack('n', $orientation) . "\x00\x00" . "\x00\x00\x00\x00";
return "\xFF\xE1" . pack('n', 2 + 6 + strlen($tiff)) . "Exif\x00\x00" . $tiff;
}
}