Files
Toiminnanohjaus/tests/Unit/Modules/Jobs/Application/TaskImageUseCasesTest.php
petriandClaude Sonnet 5.5 ae8172898d Add images to tasks
Everyone who may comment on a task sends images to it, e.g. photos
taken with a phone. The server stores an upright, at most 2560 px JPEG
without EXIF data (no location) and a 400 px thumbnail on the private
disk, served only to those who see the job. Uploaders change captions
(also the alt text) and remove their own images; admin and management
may remove anyone's (jobs.remove-any-task-image). Removing a task also
removes its image files.

Livewire's temporary upload limit is raised to PHP's 40 MB so that the
page's own 12 MB limit gives a translated message.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 07:46:53 +02:00

104 lines
5.5 KiB
PHP

<?php
use App\Modules\Jobs\Application\Services\TaskImageFinder;
use App\Modules\Jobs\Application\UseCases\AddTaskImage;
use App\Modules\Jobs\Application\UseCases\ChangeTaskImageCaption;
use App\Modules\Jobs\Application\UseCases\RemoveTask;
use App\Modules\Jobs\Application\UseCases\RemoveTaskImage;
use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImage;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImageCaption;
use App\Modules\Jobs\Domain\Exceptions\NotTaskImageUploader;
use App\Modules\Jobs\Domain\Exceptions\TaskImageNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\ValueObjects\TaskImageCaption;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeTaskImageFiles;
use Tests\Fakes\FakeTaskUsage;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryTaskImageRepository;
use Tests\Fakes\InMemoryTaskRepository;
beforeEach(function () {
$this->tasks = new InMemoryTaskRepository;
$this->images = new InMemoryTaskImageRepository;
$this->files = new FakeTaskImageFiles;
$transactions = new FakeTransactionManager;
$finder = new TaskImageFinder($this->tasks, $this->images);
$this->jobId = 5;
$this->taskId = (int) $this->tasks->save(Task::reconstitute(1, $this->jobId, 11, new TaskTitle('Kuvaus'), null, TaskStatus::Waiting, null, null, null, [], 10))->id();
$this->add = new AddTaskImage($finder, $this->images, $this->files, $transactions, new FakeClock(new DateTimeImmutable('2026-11-25')));
$this->changeCaption = new ChangeTaskImageCaption($finder, $this->images, $transactions);
$this->remove = new RemoveTaskImage($finder, $this->images, $this->files, $transactions);
$this->removeTask = new RemoveTask($this->tasks, new FakeTaskUsage, $transactions, $this->images, $this->files);
});
it('treats an empty caption as none and rejects too long captions', function () {
expect(TaskImageCaption::fromNullable(' '))->toBeNull()
->and(TaskImageCaption::fromNullable(null))->toBeNull()
->and(TaskImageCaption::fromNullable(" Valot \n paikallaan ")?->value)->toBe('Valot paikallaan')
->and(fn () => TaskImageCaption::fromNullable(str_repeat('a', 256)))->toThrow(InvalidTaskImageCaption::class);
});
it('adds an image with its files, uploader, caption and time', function () {
$imageId = $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.jpg', 'IMG_1234.HEIC.jpg', 'Valot');
$image = $this->images->images[$imageId];
expect($image->uploaderId())->toBe(10)
->and($image->caption()?->value)->toBe('Valot')
->and($image->originalName())->toBe('IMG_1234.HEIC.jpg')
->and($image->uploadedAt()->format('Y-m-d H:i'))->toBe('2026-11-25 12:00')
->and(array_keys($this->files->files))->toBe($image->file()->paths());
});
it('stores no files for an unreadable image, an invalid caption or a task of another job', function () {
expect(fn () => $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.txt', 'muistio.txt', null))->toThrow(InvalidTaskImage::class)
->and(fn () => $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.jpg', 'kuva.jpg', str_repeat('a', 256)))->toThrow(InvalidTaskImageCaption::class)
->and(fn () => $this->add->execute(99, $this->taskId, 10, '/tmp/upload.jpg', 'kuva.jpg', null))->toThrow(TaskNotFound::class)
->and($this->files->files)->toBe([]);
});
it('removes the stored files when the image cannot be saved', function () {
$this->images->failOnSave = true;
expect(fn () => $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.jpg', 'kuva.jpg', null))->toThrow(RuntimeException::class)
->and($this->files->files)->toBe([]);
});
it('lets only the uploader change or clear the caption', function () {
$imageId = $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.jpg', 'kuva.jpg', null);
$this->changeCaption->execute($this->jobId, $this->taskId, $imageId, 10, 'Lavasteet');
expect($this->images->images[$imageId]->caption()?->value)->toBe('Lavasteet');
$this->changeCaption->execute($this->jobId, $this->taskId, $imageId, 10, '');
expect($this->images->images[$imageId]->caption())->toBeNull()
->and(fn () => $this->changeCaption->execute($this->jobId, $this->taskId, $imageId, 12, 'Toisen'))->toThrow(NotTaskImageUploader::class);
});
it('lets the uploader remove an image with its files, and others only when they may remove anyone\'s', function () {
$own = $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/a.jpg', 'a.jpg', null);
$other = $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/b.jpg', 'b.jpg', null);
expect(fn () => $this->remove->execute($this->jobId, $this->taskId, $other, 12, false))->toThrow(NotTaskImageUploader::class);
$this->remove->execute($this->jobId, $this->taskId, $own, 10, false);
$this->remove->execute($this->jobId, $this->taskId, $other, 12, true);
expect($this->images->images)->toBe([])
->and($this->files->files)->toBe([])
->and(fn () => $this->remove->execute($this->jobId, $this->taskId, $own, 10, true))->toThrow(TaskImageNotFound::class);
});
it('removes the files of the images of a removed task', function () {
$this->add->execute($this->jobId, $this->taskId, 10, '/tmp/a.jpg', 'a.jpg', null);
$this->removeTask->execute($this->jobId, $this->taskId);
expect($this->files->files)->toBe([]);
});