test element file routing

This commit is contained in:
Yisroel Baum 2026-06-14 23:05:45 +03:00
parent 14d2ce2284
commit 0d5783ba9c
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
14 changed files with 951 additions and 388 deletions

View file

@ -5,8 +5,10 @@ namespace Tests\Feature;
use App\Auth\CreateSessionDto;
use App\Auth\SessionRepository;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\ElementRepository;
use App\Set\CreateSetDto;
use App\Set\Set;
use App\Set\SetRepository;
use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto;
@ -28,41 +30,43 @@ class ElementsEndpointTest extends TestCase
. 'yHx-r4p6hHU&t=1s';
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
richText: '<p>A structured path for growth</p>',
pdfPath: '/assets/pdfs/baderech.pdf',
youtubeUrl: $sampleYoutubeUrl,
parentElement: null,
));
$firstChildElement = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Avodah Foundations',
description: 'Foundations for steady avodah',
iconImageUrl: null,
richText: '<p>Foundations rich text</p>',
pdfPath: '/assets/pdfs/foundations.pdf',
youtubeUrl: null,
parentElement: $element,
));
$secondChildElement = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Daily Practice',
description: 'Daily practices for growth',
iconImageUrl: null,
richText: '<p>Daily practice rich text</p>',
pdfPath: null,
youtubeUrl: null,
parentElement: $element,
));
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
'/assets/baderech-haavodah-icon.png',
'<p>A structured path for growth</p>',
'/assets/pdfs/baderech.pdf',
'/assets/pdfs/baderech-long.pdf',
$sampleYoutubeUrl,
null,
);
$firstChildElement = $this->createElement(
$elementRepository,
$set,
'Avodah Foundations',
'Foundations for steady avodah',
null,
'<p>Foundations rich text</p>',
'/assets/pdfs/foundations.pdf',
null,
null,
$element,
);
$secondChildElement = $this->createElement(
$elementRepository,
$set,
'Daily Practice',
'Daily practices for growth',
null,
'<p>Daily practice rich text</p>',
null,
null,
null,
$element,
);
$response = $this->getJson("/api/elements/{$element->getId()}");
@ -86,6 +90,8 @@ class ElementsEndpointTest extends TestCase
'description' => 'A structured path for growth',
'iconImageUrl' => '/assets/baderech-haavodah-icon.png',
'richText' => '<p>A structured path for growth</p>',
'shortPdfPath' => '/assets/pdfs/baderech.pdf',
'longPdfPath' => '/assets/pdfs/baderech-long.pdf',
'pdfPath' => '/assets/pdfs/baderech.pdf',
'youtubeUrl' => $sampleYoutubeUrl,
],
@ -106,21 +112,19 @@ class ElementsEndpointTest extends TestCase
{
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
richText: '<p>A structured path for growth</p>',
pdfPath: '/assets/pdfs/baderech.pdf',
youtubeUrl: null,
parentElement: null,
));
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
'/assets/baderech-haavodah-icon.png',
'<p>A structured path for growth</p>',
'/assets/pdfs/baderech.pdf',
null,
null,
null,
);
$response = $this->postJson('/api/element/update', [
'elementId' => $element->getId(),
@ -128,7 +132,8 @@ class ElementsEndpointTest extends TestCase
'description' => 'Updated description',
'iconImageUrl' => '/assets/updated-icon.png',
'richText' => '<p>Updated rich text</p>',
'pdfPath' => '/assets/pdfs/updated.pdf',
'shortPdfPath' => '/assets/pdfs/updated.pdf',
'longPdfPath' => '/assets/pdfs/updated-long.pdf',
'youtubeUrl' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
]);
@ -144,21 +149,19 @@ class ElementsEndpointTest extends TestCase
. 'dQw4w9WgXcQ';
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
richText: '<p>A structured path for growth</p>',
pdfPath: '/assets/pdfs/baderech.pdf',
youtubeUrl: null,
parentElement: null,
));
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
'/assets/baderech-haavodah-icon.png',
'<p>A structured path for growth</p>',
'/assets/pdfs/baderech.pdf',
null,
null,
null,
);
$this->createSession('valid-token');
$response = $this->withCredentials()
@ -169,7 +172,8 @@ class ElementsEndpointTest extends TestCase
'description' => 'Updated description',
'iconImageUrl' => '/assets/updated-icon.png',
'richText' => '<p>Updated rich text</p>',
'pdfPath' => '/assets/pdfs/updated.pdf',
'shortPdfPath' => '/assets/pdfs/updated.pdf',
'longPdfPath' => '/assets/pdfs/updated-long.pdf',
'youtubeUrl' => $sampleYoutubeUrl,
]);
@ -181,6 +185,8 @@ class ElementsEndpointTest extends TestCase
'description' => 'Updated description',
'iconImageUrl' => '/assets/updated-icon.png',
'richText' => '<p>Updated rich text</p>',
'shortPdfPath' => '/assets/pdfs/updated.pdf',
'longPdfPath' => '/assets/pdfs/updated-long.pdf',
'pdfPath' => '/assets/pdfs/updated.pdf',
'youtubeUrl' => $sampleYoutubeUrl,
],
@ -191,6 +197,52 @@ class ElementsEndpointTest extends TestCase
'<p>Updated rich text</p>',
$updatedElement->getRichText(),
);
$this->assertSame(
'/assets/pdfs/updated-long.pdf',
$updatedElement->getLongPdfPath(),
);
}
public function testAuthenticatedUpdateAcceptsLegacyPdfPath(): void
{
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
null,
'<p>A structured path for growth</p>',
'/assets/pdfs/baderech.pdf',
'/assets/pdfs/baderech-long.pdf',
null,
null,
);
$this->createSession('valid-token');
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->postJson('/api/element/update', [
'elementId' => $element->getId(),
'pdfPath' => '/assets/pdfs/legacy-updated.pdf',
]);
$response->assertOk();
$body = $response->json();
$this->assertSame(
'/assets/pdfs/legacy-updated.pdf',
$body['element']['shortPdfPath'],
);
$this->assertSame(
'/assets/pdfs/legacy-updated.pdf',
$body['element']['pdfPath'],
);
$this->assertSame(
'/assets/pdfs/baderech-long.pdf',
$body['element']['longPdfPath'],
);
}
public function testAuthenticatedUpdateClearsUploadedMedia(): void
@ -201,26 +253,28 @@ class ElementsEndpointTest extends TestCase
'icon bytes',
);
Storage::disk('public')->put(
'element-pdfs/original.pdf',
'pdf bytes',
'element-pdfs/short/original.pdf',
'short pdf bytes',
);
Storage::disk('public')->put(
'element-pdfs/long/original-long.pdf',
'long pdf bytes',
);
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: 'element-icons/original-icon.png',
richText: '<p>A structured path for growth</p>',
pdfPath: 'element-pdfs/original.pdf',
youtubeUrl: null,
parentElement: null,
));
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
'element-icons/original-icon.png',
'<p>A structured path for growth</p>',
'element-pdfs/short/original.pdf',
'element-pdfs/long/original-long.pdf',
null,
null,
);
$this->createSession('valid-token');
$response = $this->withCredentials()
@ -229,21 +283,28 @@ class ElementsEndpointTest extends TestCase
'elementId' => $element->getId(),
'iconImageUrl' => '',
'pdfPath' => '',
'longPdfPath' => '',
]);
$response->assertOk();
$body = $response->json();
$this->assertNull($body['element']['iconImageUrl']);
$this->assertNull($body['element']['shortPdfPath']);
$this->assertNull($body['element']['longPdfPath']);
$this->assertNull($body['element']['pdfPath']);
$updatedElement = $elementRepository->find($element->getId());
$this->assertNotNull($updatedElement);
$this->assertNull($updatedElement->getIconImageUrl());
$this->assertNull($updatedElement->getPdfPath());
$this->assertNull($updatedElement->getShortPdfPath());
$this->assertNull($updatedElement->getLongPdfPath());
Storage::disk('public')->assertMissing(
'element-icons/original-icon.png',
);
Storage::disk('public')->assertMissing(
'element-pdfs/original.pdf',
'element-pdfs/short/original.pdf',
);
Storage::disk('public')->assertMissing(
'element-pdfs/long/original-long.pdf',
);
}
@ -251,27 +312,26 @@ class ElementsEndpointTest extends TestCase
{
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: null,
richText: '<p>A structured path for growth</p>',
pdfPath: null,
youtubeUrl: null,
parentElement: null,
));
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
null,
'<p>A structured path for growth</p>',
null,
null,
null,
null,
);
$response = $this->post(
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'iconImage' => $this->iconImageUpload(),
'fileType' => 'iconImage',
'file' => $this->iconImageUpload(),
],
);
@ -286,21 +346,19 @@ class ElementsEndpointTest extends TestCase
Storage::fake('public');
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: null,
richText: '<p>A structured path for growth</p>',
pdfPath: null,
youtubeUrl: null,
parentElement: null,
));
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
null,
'<p>A structured path for growth</p>',
null,
null,
null,
null,
);
$this->createSession('valid-token');
$response = $this->withCredentials()
@ -309,7 +367,8 @@ class ElementsEndpointTest extends TestCase
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'iconImage' => $this->iconImageUpload(),
'fileType' => 'iconImage',
'file' => $this->iconImageUpload(),
],
);
@ -331,31 +390,30 @@ class ElementsEndpointTest extends TestCase
Storage::disk('public')->assertExists($updatedIconImageUrl);
}
public function testPdfUploadRequiresAuthentication(): void
public function testShortPdfUploadRequiresAuthentication(): void
{
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: null,
richText: '<p>A structured path for growth</p>',
pdfPath: null,
youtubeUrl: null,
parentElement: null,
));
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
null,
'<p>A structured path for growth</p>',
null,
null,
null,
null,
);
$response = $this->post(
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'pdf' => $this->pdfUpload(),
'fileType' => 'shortPdf',
'file' => $this->pdfUpload(),
],
);
@ -365,26 +423,24 @@ class ElementsEndpointTest extends TestCase
]);
}
public function testAuthenticatedPdfUploadReturnsElementPayload(): void
public function testAuthenticatedShortPdfUploadReturnsElementPayload(): void
{
Storage::fake('public');
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
$element = $elementRepository->create(new CreateElementDto(
set: $set,
title: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: null,
richText: '<p>A structured path for growth</p>',
pdfPath: null,
youtubeUrl: null,
parentElement: null,
));
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
null,
'<p>A structured path for growth</p>',
null,
null,
null,
null,
);
$this->createSession('valid-token');
$response = $this->withCredentials()
@ -393,7 +449,8 @@ class ElementsEndpointTest extends TestCase
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'pdf' => $this->pdfUpload(),
'fileType' => 'shortPdf',
'file' => $this->pdfUpload(),
],
);
@ -401,17 +458,107 @@ class ElementsEndpointTest extends TestCase
$body = $response->json();
$this->assertSame($element->getId(), $body['element']['id']);
$this->assertStringContainsString(
'/storage/element-pdfs/',
'/storage/element-pdfs/short/',
$body['element']['shortPdfPath'],
);
$this->assertSame(
$body['element']['shortPdfPath'],
$body['element']['pdfPath'],
);
$updatedElement = $elementRepository->find($element->getId());
$this->assertNotNull($updatedElement);
$updatedPdfPath = $updatedElement->getPdfPath();
$updatedPdfPath = $updatedElement->getShortPdfPath();
$this->assertIsString($updatedPdfPath);
$this->assertStringStartsWith('element-pdfs/', $updatedPdfPath);
$this->assertStringStartsWith(
'element-pdfs/short/',
$updatedPdfPath,
);
Storage::disk('public')->assertExists($updatedPdfPath);
}
public function testAuthenticatedLongPdfUploadReturnsElementPayload(): void
{
Storage::fake('public');
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $this->createSet($setRepository);
$element = $this->createElement(
$elementRepository,
$set,
'Baderech HaAvodah',
'A structured path for growth',
null,
'<p>A structured path for growth</p>',
null,
null,
null,
null,
);
$this->createSession('valid-token');
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->post(
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'fileType' => 'longPdf',
'file' => $this->pdfUpload(),
],
);
$response->assertOk();
$body = $response->json();
$this->assertSame($element->getId(), $body['element']['id']);
$this->assertStringContainsString(
'/storage/element-pdfs/long/',
$body['element']['longPdfPath'],
);
$updatedElement = $elementRepository->find($element->getId());
$this->assertNotNull($updatedElement);
$updatedPdfPath = $updatedElement->getLongPdfPath();
$this->assertIsString($updatedPdfPath);
$this->assertStringStartsWith(
'element-pdfs/long/',
$updatedPdfPath,
);
Storage::disk('public')->assertExists($updatedPdfPath);
}
private function createSet(SetRepository $setRepository): Set
{
return $setRepository->create(new CreateSetDto(
name: 'Baderech HaAvodah',
description: 'A structured path for growth',
iconImageUrl: '/assets/baderech-haavodah-icon.png',
));
}
private function createElement(
ElementRepository $elementRepository,
Set $set,
string $title,
string $description,
?string $iconImageUrl,
string $richText,
?string $shortPdfPath,
?string $longPdfPath,
?string $youtubeUrl,
?Element $parentElement,
): Element {
return $elementRepository->create(new CreateElementDto(
set: $set,
title: $title,
description: $description,
iconImageUrl: $iconImageUrl,
richText: $richText,
shortPdfPath: $shortPdfPath,
longPdfPath: $longPdfPath,
youtubeUrl: $youtubeUrl,
parentElement: $parentElement,
));
}
private function createSession(string $token): void
{
$userRepository = app(UserRepository::class);