test media removal

This commit is contained in:
Yisroel Baum 2026-06-02 16:58:00 +03:00
parent d0810ba4b3
commit 4b432d9b99
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
4 changed files with 177 additions and 4 deletions

View file

@ -19,15 +19,19 @@ use App\Element\UseCases\UpdateElement\UpdateYoutubeUrlRequest;
use App\Exceptions\BadRequestException;
use App\Set\Set as DomainSet;
use Tests\Fakes\FakeElementRepository;
use Tests\Fakes\FakeFileUploader;
use Tests\TestCase;
class UpdateElementFieldsTest extends TestCase
{
private FakeElementRepository $elementRepo;
private FakeFileUploader $fileUploader;
protected function setUp(): void
{
$this->elementRepo = new FakeElementRepository();
$this->fileUploader = new FakeFileUploader();
}
public function testUpdateTitleUpdatesOnlyTitle(): void
@ -88,7 +92,10 @@ class UpdateElementFieldsTest extends TestCase
public function testUpdateIconImageUrlClearsEmptyString(): void
{
$element = $this->createFilledElement();
$updateIconImageUrl = new UpdateIconImageUrl($this->elementRepo);
$updateIconImageUrl = new UpdateIconImageUrl(
$this->elementRepo,
$this->fileUploader,
);
$updatedElement = $updateIconImageUrl->execute(
new UpdateIconImageUrlRequest(
@ -99,6 +106,32 @@ class UpdateElementFieldsTest extends TestCase
$this->assertNull($updatedElement->getIconImageUrl());
$this->assertSame($element->getTitle(), $updatedElement->getTitle());
$this->assertSame([], $this->fileUploader->deletedPaths);
}
public function testUpdateIconImageUrlDeletesManagedFileWhenCleared(): void
{
$element = $this->createElementWithMedia(
'element-icons/original-icon.png',
'/assets/pdfs/original.pdf',
);
$updateIconImageUrl = new UpdateIconImageUrl(
$this->elementRepo,
$this->fileUploader,
);
$updatedElement = $updateIconImageUrl->execute(
new UpdateIconImageUrlRequest(
id: $element->getId(),
iconImageUrl: '',
)
);
$this->assertNull($updatedElement->getIconImageUrl());
$this->assertSame(
['element-icons/original-icon.png'],
$this->fileUploader->deletedPaths,
);
}
public function testUpdateRichTextUpdatesOnlyRichText(): void
@ -126,7 +159,10 @@ class UpdateElementFieldsTest extends TestCase
public function testUpdatePdfPathClearsEmptyString(): void
{
$element = $this->createFilledElement();
$updatePdfPath = new UpdatePdfPath($this->elementRepo);
$updatePdfPath = new UpdatePdfPath(
$this->elementRepo,
$this->fileUploader,
);
$updatedElement = $updatePdfPath->execute(
new UpdatePdfPathRequest(id: $element->getId(), pdfPath: '')
@ -134,6 +170,29 @@ class UpdateElementFieldsTest extends TestCase
$this->assertNull($updatedElement->getPdfPath());
$this->assertSame($element->getTitle(), $updatedElement->getTitle());
$this->assertSame([], $this->fileUploader->deletedPaths);
}
public function testUpdatePdfPathDeletesManagedFileWhenCleared(): void
{
$element = $this->createElementWithMedia(
'/assets/original-icon.png',
'element-pdfs/original.pdf',
);
$updatePdfPath = new UpdatePdfPath(
$this->elementRepo,
$this->fileUploader,
);
$updatedElement = $updatePdfPath->execute(
new UpdatePdfPathRequest(id: $element->getId(), pdfPath: '')
);
$this->assertNull($updatedElement->getPdfPath());
$this->assertSame(
['element-pdfs/original.pdf'],
$this->fileUploader->deletedPaths,
);
}
public function testUpdateYoutubeUrlClearsEmptyString(): void
@ -154,6 +213,16 @@ class UpdateElementFieldsTest extends TestCase
private function createFilledElement(): Element
{
return $this->createElementWithMedia(
'/assets/original-icon.png',
'/assets/pdfs/original.pdf',
);
}
private function createElementWithMedia(
?string $iconImageUrl,
?string $pdfPath,
): Element {
$set = new DomainSet(
id: 1,
name: 'Baderech',
@ -165,9 +234,9 @@ class UpdateElementFieldsTest extends TestCase
set: $set,
title: 'Original title',
description: 'Original description',
iconImageUrl: '/assets/original-icon.png',
iconImageUrl: $iconImageUrl,
richText: '<p>Original rich text</p>',
pdfPath: '/assets/pdfs/original.pdf',
pdfPath: $pdfPath,
youtubeUrl: 'https://www.youtube.com/watch?v=yHx-r4p6hHU',
parentElement: null,
));