Rabbi_Gerzi/backend/tests/Feature/ElementsEndpointTest.php

409 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Auth\CreateSessionDto;
use App\Auth\SessionRepository;
use App\Element\CreateElementDto;
use App\Element\ElementRepository;
use App\Set\CreateSetDto;
use App\Set\SetRepository;
use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto;
use App\User\UserRepository;
use DateTimeImmutable;
use DateTimeZone;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class ElementsEndpointTest extends TestCase
{
use RefreshDatabase;
public function testReturnsElementTitle(): void
{
$sampleYoutubeUrl = 'https://www.youtube.com/watch?v='
. '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,
));
$response = $this->getJson("/api/elements/{$element->getId()}");
$response->assertOk();
$response->assertExactJson([
'childElements' => [
[
'id' => $firstChildElement->getId(),
'title' => 'Avodah Foundations',
'description' => 'Foundations for steady avodah',
],
[
'id' => $secondChildElement->getId(),
'title' => 'Daily Practice',
'description' => 'Daily practices for growth',
],
],
'element' => [
'id' => $element->getId(),
'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,
],
]);
}
public function testReturns404WhenElementDoesNotExist(): void
{
$response = $this->getJson('/api/elements/999');
$response->assertNotFound();
$response->assertExactJson([
'error' => 'Element not found',
]);
}
public function testUpdateRequiresAuthentication(): 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: '/assets/baderech-haavodah-icon.png',
richText: '<p>A structured path for growth</p>',
pdfPath: '/assets/pdfs/baderech.pdf',
youtubeUrl: null,
parentElement: null,
));
$response = $this->postJson('/api/element/update', [
'elementId' => $element->getId(),
'title' => 'Updated title',
'description' => 'Updated description',
'iconImageUrl' => '/assets/updated-icon.png',
'richText' => '<p>Updated rich text</p>',
'pdfPath' => '/assets/pdfs/updated.pdf',
'youtubeUrl' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
]);
$response->assertUnauthorized();
$response->assertExactJson([
'error' => 'unauthenticated',
]);
}
public function testAuthenticatedUpdateReturnsElementPayload(): void
{
$sampleYoutubeUrl = 'https://www.youtube.com/watch?v='
. '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,
));
$this->createSession('valid-token');
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->postJson('/api/element/update', [
'elementId' => $element->getId(),
'title' => 'Updated title',
'description' => 'Updated description',
'iconImageUrl' => '/assets/updated-icon.png',
'richText' => '<p>Updated rich text</p>',
'pdfPath' => '/assets/pdfs/updated.pdf',
'youtubeUrl' => $sampleYoutubeUrl,
]);
$response->assertOk();
$response->assertExactJson([
'element' => [
'id' => $element->getId(),
'title' => 'Updated title',
'description' => 'Updated description',
'iconImageUrl' => '/assets/updated-icon.png',
'richText' => '<p>Updated rich text</p>',
'pdfPath' => '/assets/pdfs/updated.pdf',
'youtubeUrl' => $sampleYoutubeUrl,
],
]);
$updatedElement = $elementRepository->find($element->getId());
$this->assertSame('Updated title', $updatedElement->getTitle());
$this->assertSame(
'<p>Updated rich text</p>',
$updatedElement->getRichText(),
);
}
public function testIconImageUploadRequiresAuthentication(): 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,
));
$response = $this->post(
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'iconImage' => $this->iconImageUpload(),
],
);
$response->assertUnauthorized();
$response->assertExactJson([
'error' => 'unauthenticated',
]);
}
public function testAuthenticatedIconImageUploadReturnsElementPayload(): 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,
));
$this->createSession('valid-token');
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->post(
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'iconImage' => $this->iconImageUpload(),
],
);
$response->assertOk();
$body = $response->json();
$this->assertSame($element->getId(), $body['element']['id']);
$this->assertStringContainsString(
'/storage/element-icons/',
$body['element']['iconImageUrl'],
);
$updatedElement = $elementRepository->find($element->getId());
$this->assertNotNull($updatedElement);
$updatedIconImageUrl = $updatedElement->getIconImageUrl();
$this->assertIsString($updatedIconImageUrl);
$this->assertStringStartsWith(
'element-icons/',
$updatedIconImageUrl,
);
Storage::disk('public')->assertExists($updatedIconImageUrl);
}
public function testPdfUploadRequiresAuthentication(): 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,
));
$response = $this->post(
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'pdf' => $this->pdfUpload(),
],
);
$response->assertUnauthorized();
$response->assertExactJson([
'error' => 'unauthenticated',
]);
}
public function testAuthenticatedPdfUploadReturnsElementPayload(): 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,
));
$this->createSession('valid-token');
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->post(
'/api/element/update',
[
'elementId' => (string) $element->getId(),
'pdf' => $this->pdfUpload(),
],
);
$response->assertOk();
$body = $response->json();
$this->assertSame($element->getId(), $body['element']['id']);
$this->assertStringContainsString(
'/storage/element-pdfs/',
$body['element']['pdfPath'],
);
$updatedElement = $elementRepository->find($element->getId());
$this->assertNotNull($updatedElement);
$updatedPdfPath = $updatedElement->getPdfPath();
$this->assertIsString($updatedPdfPath);
$this->assertStringStartsWith('element-pdfs/', $updatedPdfPath);
Storage::disk('public')->assertExists($updatedPdfPath);
}
private function createSession(string $token): void
{
$userRepository = app(UserRepository::class);
$sessionRepository = app(SessionRepository::class);
$user = $userRepository->create(new CreateUserDto(
email: new EmailAddress('admin@example.com'),
passwordHash: 'password-hash',
));
$now = new DateTimeImmutable(
'2026-05-01T00:00:00',
new DateTimeZone('UTC'),
);
$sessionRepository->create(new CreateSessionDto(
token: $token,
user: $user,
createdAt: $now,
expiresAt: new DateTimeImmutable(
'2099-01-01T00:00:00',
new DateTimeZone('UTC'),
),
));
}
private function iconImageUpload(): UploadedFile
{
$fixturePath = __DIR__ . '/../fixtures/icon.png';
return new UploadedFile(
$fixturePath,
'icon.png',
'image/png',
null,
true,
);
}
private function pdfUpload(): UploadedFile
{
$fixturePath = __DIR__ . '/../fixtures/baderech.pdf';
return new UploadedFile(
$fixturePath,
'baderech.pdf',
'application/pdf',
null,
true,
);
}
}