diff --git a/src/Stache/Stores/TaxonomyTermsStore.php b/src/Stache/Stores/TaxonomyTermsStore.php index 1d3081d0afd..5e3a0291378 100644 --- a/src/Stache/Stores/TaxonomyTermsStore.php +++ b/src/Stache/Stores/TaxonomyTermsStore.php @@ -73,6 +73,8 @@ public function getItem($key) $this->handleFileChanges(); if ($item = $this->getCachedItem($key)) { + $item->term()->syncOriginal(); + return $item; } @@ -87,6 +89,8 @@ public function getItem($key) ->in($site); } + $item->term()->syncOriginal(); + $this->cacheItem($item); return $item; diff --git a/tests/Listeners/UpdateTermReferencesTest.php b/tests/Listeners/UpdateTermReferencesTest.php index 11f98d0a9ce..b9f4f30dee5 100644 --- a/tests/Listeners/UpdateTermReferencesTest.php +++ b/tests/Listeners/UpdateTermReferencesTest.php @@ -314,6 +314,68 @@ public function it_updates_scoped_term_fields_regardless_of_max_items_setting() $this->assertEquals('topics::norris', $entry->fresh()->get('non_favourites')); } + #[Test] + public function it_nullifies_references_when_deleting_a_term_loaded_from_its_file() + { + $collection = tap(Facades\Collection::make('articles'))->save(); + + $this->setInBlueprints('collections/articles', [ + 'fields' => [ + [ + 'handle' => 'favourites', + 'field' => [ + 'type' => 'terms', + 'taxonomies' => ['topics'], + 'mode' => 'select', + ], + ], + ], + ]); + + $entry = tap(Facades\Entry::make()->collection($collection)->data([ + 'favourites' => ['hoff', 'norris'], + ]))->save(); + + $this->assertEquals(['hoff', 'norris'], $entry->get('favourites')); + + Facades\Stache::store('terms')->store('topics')->forgetItem('en::hoff'); + + Facades\Term::find('topics::hoff')->delete(); + + $this->assertEquals(['norris'], $entry->fresh()->get('favourites')); + } + + /** @see https://github.com/statamic/cms/issues/11264 */ + #[Test] + public function it_nullifies_references_when_deleting_a_term_that_only_exists_in_entry_data() + { + $collection = tap(Facades\Collection::make('articles')->taxonomies(['topics']))->save(); + + $this->setInBlueprints('collections/articles', [ + 'fields' => [ + [ + 'handle' => 'topics', + 'field' => [ + 'type' => 'terms', + 'taxonomies' => ['topics'], + 'mode' => 'select', + ], + ], + ], + ]); + + $entry = tap(Facades\Entry::make()->collection($collection)->data([ + 'topics' => ['hoff', 'ghost'], + ]))->save(); + + $this->assertEquals(['hoff', 'ghost'], $entry->get('topics')); + + Facades\Term::find('topics::ghost')->delete(); + + $this->assertEquals(['hoff'], $entry->fresh()->get('topics')); + $this->assertNull(Facades\Term::find('topics::ghost')); + } + #[Test] public function it_nullifies_references_when_deleting_a_scoped_term() {