Skip to content

Commit

Permalink
References: Fixed references count/list recycle bin interaction
Browse files Browse the repository at this point in the history
Count and reference list would get references then attempt to load
entities, which could fail to load if in the recycle bin.
This updates the queries to effectively ignore references for items we
can't see (in recycle bin).
Added test to cover.

For #4918
  • Loading branch information
ssddanbrown committed Apr 1, 2024
1 parent 58f6219 commit a33dbcb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/References/ReferenceFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ protected function queryReferencesToEntity(Entity $entity): Builder
{
$baseQuery = Reference::query()
->where('to_type', '=', $entity->getMorphClass())
->where('to_id', '=', $entity->id);
->where('to_id', '=', $entity->id)
->whereHas('from');

return $this->permissions->restrictEntityRelationQuery(
$baseQuery,
Expand Down
26 changes: 25 additions & 1 deletion tests/References/ReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,31 @@ public function test_description_links_from_book_chapter_shelf_updated_on_url_ch
}
}

protected function createReference(Model $from, Model $to)
public function test_reference_from_deleted_item_does_not_count_or_show_in_references_page()
{
$page = $this->entities->page();
$referencingPageA = $this->entities->page();
$referencingPageB = $this->entities->page();

$this->asEditor();
$this->createReference($referencingPageA, $page);
$this->createReference($referencingPageB, $page);

$resp = $this->get($page->getUrl());
$resp->assertSee('Referenced by 2 items');

$this->delete($referencingPageA->getUrl());

$resp = $this->get($page->getUrl());
$resp->assertSee('Referenced by 1 item');

$resp = $this->get($page->getUrl('/references'));
$resp->assertOk();
$resp->assertSee($referencingPageB->getUrl());
$resp->assertDontSee($referencingPageA->getUrl());
}

protected function createReference(Model $from, Model $to): void
{
(new Reference())->forceFill([
'from_type' => $from->getMorphClass(),
Expand Down

0 comments on commit a33dbcb

Please sign in to comment.