Skip to content

Commit a33dbcb

Browse files
committed
References: Fixed references count/list recycle bin interaction
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
1 parent 58f6219 commit a33dbcb

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

app/References/ReferenceFetcher.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ protected function queryReferencesToEntity(Entity $entity): Builder
4141
{
4242
$baseQuery = Reference::query()
4343
->where('to_type', '=', $entity->getMorphClass())
44-
->where('to_id', '=', $entity->id);
44+
->where('to_id', '=', $entity->id)
45+
->whereHas('from');
4546

4647
return $this->permissions->restrictEntityRelationQuery(
4748
$baseQuery,

tests/References/ReferencesTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,31 @@ public function test_description_links_from_book_chapter_shelf_updated_on_url_ch
271271
}
272272
}
273273

274-
protected function createReference(Model $from, Model $to)
274+
public function test_reference_from_deleted_item_does_not_count_or_show_in_references_page()
275+
{
276+
$page = $this->entities->page();
277+
$referencingPageA = $this->entities->page();
278+
$referencingPageB = $this->entities->page();
279+
280+
$this->asEditor();
281+
$this->createReference($referencingPageA, $page);
282+
$this->createReference($referencingPageB, $page);
283+
284+
$resp = $this->get($page->getUrl());
285+
$resp->assertSee('Referenced by 2 items');
286+
287+
$this->delete($referencingPageA->getUrl());
288+
289+
$resp = $this->get($page->getUrl());
290+
$resp->assertSee('Referenced by 1 item');
291+
292+
$resp = $this->get($page->getUrl('/references'));
293+
$resp->assertOk();
294+
$resp->assertSee($referencingPageB->getUrl());
295+
$resp->assertDontSee($referencingPageA->getUrl());
296+
}
297+
298+
protected function createReference(Model $from, Model $to): void
275299
{
276300
(new Reference())->forceFill([
277301
'from_type' => $from->getMorphClass(),

0 commit comments

Comments
 (0)