test element updates

This commit is contained in:
Yisroel Baum 2026-05-28 20:05:19 +03:00
parent c7659b45ee
commit 6d0acaae56
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 309 additions and 0 deletions

View file

@ -2,10 +2,17 @@
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 Tests\TestCase;
@ -92,4 +99,115 @@ class ElementsEndpointTest extends TestCase
'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->patchJson("/api/elements/{$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->withCookie('auth_token', 'valid-token')
->patchJson("/api/elements/{$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(),
);
}
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'),
),
));
}
}