diff --git a/backend/tests/Feature/ElementsEndpointTest.php b/backend/tests/Feature/ElementsEndpointTest.php index 157f944..a914dd9 100644 --- a/backend/tests/Feature/ElementsEndpointTest.php +++ b/backend/tests/Feature/ElementsEndpointTest.php @@ -94,6 +94,74 @@ class ElementsEndpointTest extends TestCase 'longPdfPath' => '/assets/pdfs/baderech-long.pdf', 'youtubeUrl' => $sampleYoutubeUrl, ], + 'siblingElements' => [ + [ + 'id' => $element->getId(), + 'title' => 'Baderech HaAvodah', + 'description' => 'A structured path for growth', + ], + ], + ]); + } + + public function testReturnsSameParentSiblingElements(): 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, + '', + null, + null, + null, + null, + ); + $firstChildElement = $this->createElement( + $elementRepository, + $set, + 'Avodah Foundations', + 'Foundations for steady avodah', + null, + '

Foundations rich text

', + null, + null, + null, + $parentElement, + ); + $secondChildElement = $this->createElement( + $elementRepository, + $set, + 'Daily Practice', + 'Daily practices for growth', + null, + '', + null, + null, + null, + $parentElement, + ); + + $response = $this->getJson( + "/api/elements/{$firstChildElement->getId()}" + ); + + $response->assertOk(); + $response->assertJsonPath('siblingElements', [ + [ + 'id' => $firstChildElement->getId(), + 'title' => 'Avodah Foundations', + 'description' => 'Foundations for steady avodah', + ], + [ + 'id' => $secondChildElement->getId(), + 'title' => 'Daily Practice', + 'description' => 'Daily practices for growth', + ], ]); } diff --git a/backend/tests/Unit/Controllers/ElementControllerTest.php b/backend/tests/Unit/Controllers/ElementControllerTest.php index c0ce0a6..c32ac21 100644 --- a/backend/tests/Unit/Controllers/ElementControllerTest.php +++ b/backend/tests/Unit/Controllers/ElementControllerTest.php @@ -150,6 +150,68 @@ class ElementControllerTest extends TestCase 'description' => 'Daily practices for growth', ], ], $body['childElements']); + $this->assertSame([ + [ + 'id' => $element->getId(), + 'title' => 'Baderech HaAvodah', + 'description' => 'A structured path for growth', + ], + ], $body['siblingElements']); + } + + public function testShowReturnsSameParentSiblingPayload(): void + { + $set = $this->createSet(1, 'Baderech'); + $parentElement = $this->createElement( + $set, + 'Baderech HaAvodah', + 'A structured path for growth', + null, + '', + null, + null, + null, + null, + ); + $firstChildElement = $this->createElement( + $set, + 'Avodah Foundations', + 'Foundations for steady avodah', + null, + '

Foundations rich text

', + null, + null, + null, + $parentElement, + ); + $secondChildElement = $this->createElement( + $set, + 'Daily Practice', + 'Daily practices for growth', + null, + '', + null, + null, + null, + $parentElement, + ); + + $response = $this->controller->show($firstChildElement->getId()); + + $this->assertEquals(200, $response->getStatusCode()); + $body = json_decode($response->getContent(), true); + $this->assertSame([ + [ + 'id' => $firstChildElement->getId(), + 'title' => 'Avodah Foundations', + 'description' => 'Foundations for steady avodah', + ], + [ + 'id' => $secondChildElement->getId(), + 'title' => 'Daily Practice', + 'description' => 'Daily practices for growth', + ], + ], $body['siblingElements']); } public function testShowReturns400WhenIdMissing(): void diff --git a/backend/tests/Unit/Element/UseCases/GetElementTest.php b/backend/tests/Unit/Element/UseCases/GetElementTest.php index d4053f5..69917bd 100644 --- a/backend/tests/Unit/Element/UseCases/GetElementTest.php +++ b/backend/tests/Unit/Element/UseCases/GetElementTest.php @@ -159,6 +159,134 @@ class GetElementTest extends TestCase ); } + public function testReturnsSiblingElementsWithSameParent(): void + { + $set = $this->createSet(1, 'Baderech'); + $parentElement = $this->createElement( + $set, + 'Baderech HaAvodah', + 'A structured path for growth', + null, + '

A structured path for growth

', + '/assets/pdfs/baderech.pdf', + null, + null, + ); + $firstChildElement = $this->createElement( + $set, + 'Avodah Foundations', + 'Foundations for steady avodah', + null, + '

Foundations rich text

', + '/assets/pdfs/foundations.pdf', + null, + $parentElement, + ); + $secondChildElement = $this->createElement( + $set, + 'Daily Practice', + 'Daily practices for growth', + null, + '

Daily practice rich text

', + null, + null, + $parentElement, + ); + $this->createElement( + $set, + 'Nested Practice', + 'Nested description', + null, + '

Nested rich text

', + null, + null, + $firstChildElement, + ); + $otherSet = $this->createSet(2, 'Daily Learning'); + $otherParentElement = $this->createElement( + $otherSet, + 'Other Parent', + 'Other parent description', + null, + '

Other parent rich text

', + null, + null, + null, + ); + $this->createElement( + $otherSet, + 'Other Child', + 'Other child description', + null, + '

Other child rich text

', + null, + null, + $otherParentElement, + ); + + $result = $this->getElement->execute(new GetElementRequest( + id: $firstChildElement->getId(), + )); + $siblingElements = $result->getSiblingElements(); + + $this->assertCount(2, $siblingElements); + $this->assertSame( + $firstChildElement->getId(), + $siblingElements[0]->getId(), + ); + $this->assertSame( + 'Avodah Foundations', + $siblingElements[0]->getTitle(), + ); + $this->assertSame( + 'Foundations for steady avodah', + $siblingElements[0]->getDescription(), + ); + $this->assertSame( + $secondChildElement->getId(), + $siblingElements[1]->getId(), + ); + $this->assertSame('Daily Practice', $siblingElements[1]->getTitle()); + $this->assertSame( + 'Daily practices for growth', + $siblingElements[1]->getDescription(), + ); + } + + public function testReturnsRootElementAsOnlySibling(): void + { + $set = $this->createSet(1, 'Baderech'); + $rootElement = $this->createElement( + $set, + 'Baderech HaAvodah', + 'A structured path for growth', + null, + '

A structured path for growth

', + '/assets/pdfs/baderech.pdf', + null, + null, + ); + $this->createElement( + $set, + 'Avodah Foundations', + 'Foundations for steady avodah', + null, + '

Foundations rich text

', + '/assets/pdfs/foundations.pdf', + null, + $rootElement, + ); + + $result = $this->getElement->execute(new GetElementRequest( + id: $rootElement->getId(), + )); + $siblingElements = $result->getSiblingElements(); + + $this->assertCount(1, $siblingElements); + $this->assertSame($rootElement->getId(), $siblingElements[0]->getId()); + $this->assertSame('Baderech HaAvodah', $siblingElements[0]->getTitle()); + } + public function testThrowsWhenIdMissing(): void { $this->expectException(BadRequestException::class);