test child creation

This commit is contained in:
Yisroel Baum 2026-06-21 21:44:06 +03:00
parent 416866a22c
commit 7c84eefe78
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
3 changed files with 213 additions and 0 deletions

View file

@ -142,6 +142,93 @@ class ElementsEndpointTest extends TestCase
]);
}
public function testCreateChildRequiresAuthentication(): void
{
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $this->createSet($setRepository);
$parentElement = $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->postJson(
"/api/elements/{$parentElement->getId()}/children",
[
'title' => 'New child',
],
);
$response->assertUnauthorized();
$response->assertExactJson([
'error' => 'unauthenticated',
]);
}
public function testAuthenticatedCreateChildReturnsElementPayload(): void
{
$setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class);
$set = $this->createSet($setRepository);
$parentElement = $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')
->postJson(
"/api/elements/{$parentElement->getId()}/children",
[
'title' => 'New admin child',
],
);
$response->assertCreated();
$body = $response->json();
$childElementId = $body['element']['id'];
$response->assertExactJson([
'element' => [
'id' => $childElementId,
'title' => 'New admin child',
'description' => '',
'iconImageUrl' => null,
'richText' => '',
'shortPdfPath' => null,
'longPdfPath' => null,
'youtubeUrl' => null,
],
]);
$createdElement = $elementRepository->find($childElementId);
$this->assertNotNull($createdElement);
$this->assertSame(
$parentElement->getId(),
$createdElement->getParentElement()->getId(),
);
$this->assertSame(
$parentElement->getSet()->getId(),
$createdElement->getSet()->getId(),
);
}
public function testAuthenticatedUpdateReturnsElementPayload(): void
{
$sampleYoutubeUrl = 'https://www.youtube.com/watch?v='

View file

@ -0,0 +1,102 @@
<?php
namespace Tests\Unit\Element\UseCases;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\UseCases\CreateChildElement\CreateChildElement;
use App\Element\UseCases\CreateChildElement\CreateChildElementRequest;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
use App\Set\Set as DomainSet;
use Tests\Fakes\FakeElementRepository;
use Tests\TestCase;
class CreateChildElementTest extends TestCase
{
private FakeElementRepository $elementRepository;
private CreateChildElement $createChildElement;
protected function setUp(): void
{
$this->elementRepository = new FakeElementRepository();
$this->createChildElement = new CreateChildElement(
$this->elementRepository,
);
}
public function testCreatesChildElementFromParentSet(): void
{
$parentElement = $this->createParentElement();
$childElement = $this->createChildElement->execute(
new CreateChildElementRequest(
parentElementId: $parentElement->getId(),
title: 'Admin child',
)
);
$this->assertSame('Admin child', $childElement->getTitle());
$this->assertSame('', $childElement->getDescription());
$this->assertNull($childElement->getIconImageUrl());
$this->assertSame('', $childElement->getRichText());
$this->assertNull($childElement->getShortPdfPath());
$this->assertNull($childElement->getLongPdfPath());
$this->assertNull($childElement->getYoutubeUrl());
$this->assertSame(
$parentElement->getSet()->getId(),
$childElement->getSet()->getId(),
);
$this->assertSame(
$parentElement->getId(),
$childElement->getParentElement()->getId(),
);
}
public function testThrowsWhenParentElementIsMissing(): void
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('Parent element not found');
$this->createChildElement->execute(new CreateChildElementRequest(
parentElementId: 999,
title: 'Missing parent child',
));
}
public function testThrowsWhenTitleIsMissing(): void
{
$parentElement = $this->createParentElement();
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('title is required');
$this->createChildElement->execute(new CreateChildElementRequest(
parentElementId: $parentElement->getId(),
title: '',
));
}
private function createParentElement(): Element
{
$set = new DomainSet(
id: 1,
name: 'Baderech',
description: 'Baderech description',
iconImageUrl: '/assets/baderech-icon.png',
);
return $this->elementRepository->create(new CreateElementDto(
set: $set,
title: 'Parent',
description: 'Parent description',
iconImageUrl: null,
richText: '',
shortPdfPath: null,
longPdfPath: null,
youtubeUrl: null,
parentElement: null,
));
}
}

View file

@ -139,6 +139,30 @@ describe('admin element editing', () => {
cy.get('[data-cy="element-rich-text"]').should('not.exist')
})
it('adds a child element and opens it for editing', () => {
cy.resetDb()
loginAsAdmin()
cy.visit('/admin/element/1')
cy.intercept('POST', /\/api\/elements\/1\/children$/)
.as('createChildElement')
cy.get('[data-cy="admin-child-title"]').type('Admin added child')
cy.get('[data-cy="admin-add-child"]').click()
cy.wait('@createChildElement')
cy.location('pathname').should('match', /^\/admin\/element\/\d+$/)
cy.get('[data-cy="admin-element-title"]')
.should('have.value', 'Admin added child')
cy.contains('header.site-header a', 'View Element').click()
cy.contains('h1', 'Admin added child').should('be.visible')
cy.visit('/element/1')
cy.get('[data-cy="child-element-list"]')
.should('contain.text', 'Admin added child')
cy.resetDb()
})
it('uploads icon and pdf files immediately through the UI', () => {
cy.resetDb()
loginAsAdmin()