Skip to content

Commit e926734

Browse files
committed
fix multiform when no parent entity form provided
1 parent 142e5e3 commit e926734

3 files changed

Lines changed: 77 additions & 4 deletions

File tree

src/Http/Controllers/HandlesEntityListItems.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private function addMetaToItems(array $listItems, string $entityKey, SharpEntity
3434
->all();
3535
}
3636

37-
private function getItemEntityKey(array $item, string $entityKey, SharpEntity $entity, SharpEntityList $list): string
37+
private function getItemEntityKey(array $item, string $entityKey, SharpEntity $entity, SharpEntityList $list): EntityKey
3838
{
3939
$itemEntityAttributeValue = $list->getEntityAttribute()
4040
? ($item[$list->getEntityAttribute()] ?? null)
@@ -51,10 +51,10 @@ private function getItemEntityKey(array $item, string $entityKey, SharpEntity $e
5151
);
5252
}
5353

54-
return $listEntity->getEntityKey();
54+
return new EntityKey($listEntity->getEntityKey());
5555
}
5656

57-
return $entityKey;
57+
return new EntityKey($entityKey);
5858
}
5959

6060
private function getItemUrl(array $item, string $entityKey, SharpEntity $entity, SharpEntityList $list): ?string
@@ -77,7 +77,10 @@ private function getItemUrl(array $item, string $entityKey, SharpEntity $entity,
7777
'entityKey' => $itemEntityKey,
7878
'instanceId' => $item[$list->getInstanceIdAttribute()],
7979
]);
80-
} elseif ($itemEntity->hasForm()) {
80+
}
81+
if ($itemEntity->hasForm()
82+
|| $itemEntityKey->multiformKey() && isset($entity->getMultiforms()[$itemEntityKey->multiformKey()])
83+
) {
8184
return route('code16.sharp.form.edit', [
8285
'parentUri' => $breadcrumb->getCurrentPath(),
8386
'entityKey' => $itemEntityKey,

tests/Fixtures/Entities/PersonEntity.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function setShow(?SharpShow $show): self
4747
public function setForm(?SharpForm $form): self
4848
{
4949
$this->fakeForm = $form;
50+
if ($form === null) {
51+
// Enforce show to be null
52+
$this->form = null;
53+
}
5054

5155
return $this;
5256
}

tests/Http/EntityListControllerTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,72 @@ public function buildListConfig(): void
455455
->where('key', 'nope')
456456
->where('entityKey', 'person:nope')
457457
->where('label', 'No Nobel prize')
458+
459+
->etc()
460+
)
461+
->has('entityList.data.0', fn (Assert $json) => $json
462+
->where('_meta.url', route('code16.sharp.show.show', ['parentUri' => 's-list/person', 'entityKey' => 'person:yes', 'instanceId' => 1]))
463+
->etc()
464+
)
465+
->has('entityList.data.1', fn (Assert $json) => $json
466+
->where('_meta.url', route('code16.sharp.show.show', ['parentUri' => 's-list/person', 'entityKey' => 'person:nope', 'instanceId' => 2]))
467+
->etc()
468+
)
469+
);
470+
});
471+
472+
it('gets multiform form url if configured', function () {
473+
$this->withoutExceptionHandling();
474+
fakeListFor('person', new class() extends PersonList
475+
{
476+
public function getListData(): array|Arrayable
477+
{
478+
return [
479+
['id' => 1, 'name' => 'Marie Curie', 'nobel' => 'yes'],
480+
['id' => 2, 'name' => 'Rosalind Franklin', 'nobel' => 'nope'],
481+
];
482+
}
483+
484+
public function buildListConfig(): void
485+
{
486+
$this->configureMultiformAttribute('nobel');
487+
}
488+
});
489+
490+
app(SharpEntityManager::class)
491+
->entityFor('person')
492+
->setShow(null)
493+
->setMultiforms([
494+
'yes' => [PersonForm::class, 'With Nobel prize'],
495+
'nope' => [PersonForm::class, 'No Nobel prize'],
496+
]);
497+
498+
$this->get('/sharp/s-list/person')
499+
->assertOk()
500+
->assertInertia(fn (Assert $page) => $page
501+
->has('entityList.data.0', fn (Assert $json) => $json
502+
->where('_meta.url', route('code16.sharp.form.edit', ['parentUri' => 's-list/person', 'entityKey' => 'person:yes', 'instanceId' => 1]))
503+
->etc()
504+
)
505+
->has('entityList.data.1', fn (Assert $json) => $json
506+
->where('_meta.url', route('code16.sharp.form.edit', ['parentUri' => 's-list/person', 'entityKey' => 'person:nope', 'instanceId' => 2]))
507+
->etc()
508+
)
509+
);
510+
511+
app(SharpEntityManager::class)
512+
->entityFor('person')
513+
->setForm(null);
514+
515+
$this->get('/sharp/s-list/person')
516+
->assertOk()
517+
->assertInertia(fn (Assert $page) => $page
518+
->has('entityList.data.0', fn (Assert $json) => $json
519+
->where('_meta.url', route('code16.sharp.form.edit', ['parentUri' => 's-list/person', 'entityKey' => 'person:yes', 'instanceId' => 1]))
520+
->etc()
521+
)
522+
->has('entityList.data.1', fn (Assert $json) => $json
523+
->where('_meta.url', route('code16.sharp.form.edit', ['parentUri' => 's-list/person', 'entityKey' => 'person:nope', 'instanceId' => 2]))
458524
->etc()
459525
)
460526
);

0 commit comments

Comments
 (0)