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, '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, ''); $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; } }