diff --git a/src/Stache/Repositories/CollectionRepository.php b/src/Stache/Repositories/CollectionRepository.php index f5df6f3a88a..1dcf9ed4004 100644 --- a/src/Stache/Repositories/CollectionRepository.php +++ b/src/Stache/Repositories/CollectionRepository.php @@ -86,6 +86,10 @@ public function save(Collection $collection) { $this->store->save($collection); + if ($collection->isDirty('sites')) { + $this->stache->store('entries')->store($collection->handle())->clear(); + } + if ($collection->orderable()) { $this->stache->store('entries')->store($collection->handle())->index('order')->update(); } diff --git a/tests/Data/Entries/CollectionTest.php b/tests/Data/Entries/CollectionTest.php index 72aec460f3c..2979f5c50bf 100644 --- a/tests/Data/Entries/CollectionTest.php +++ b/tests/Data/Entries/CollectionTest.php @@ -552,6 +552,36 @@ public function it_saves_quietly() Event::assertNotDispatched(CollectionSaving::class); } + #[Test] + public function it_only_queries_entries_in_the_sites_it_was_saved_with() + { + // Like a real cache, hand out copies. The plain array store would hand back + // the collection being saved itself, and fetching it resets its dirty state. + config([ + 'cache.stores.stache-serializing' => ['driver' => 'array', 'serialize' => true], + 'statamic.stache.cache_store' => 'stache-serializing', + ]); + + $this->setSites([ + 'en' => ['url' => '/', 'locale' => 'en_US'], + 'fr' => ['url' => '/fr/', 'locale' => 'fr_FR'], + ]); + + $collection = tap(Facades\Collection::make('test')->sites(['en', 'fr']))->save(); + Facades\Entry::make()->collection('test')->locale('en')->id('en-entry')->slug('en-entry')->save(); + Facades\Entry::make()->collection('test')->locale('fr')->id('fr-entry')->slug('fr-entry')->save(); + + $queried = []; + Event::listen(CollectionSaved::class, function ($event) use (&$queried) { + $queried[] = $event->collection->queryEntries()->get()->map->id()->sort()->values()->all(); + }); + + $collection->sites(['en'])->save(); + $collection->sites(['en', 'fr'])->save(); + + $this->assertEquals([['en-entry'], ['en-entry', 'fr-entry']], $queried); + } + #[Test] public function it_dispatches_collection_saved() { diff --git a/tests/Feature/GraphQL/EntriesTest.php b/tests/Feature/GraphQL/EntriesTest.php index e69e57368da..07b316659e3 100644 --- a/tests/Feature/GraphQL/EntriesTest.php +++ b/tests/Feature/GraphQL/EntriesTest.php @@ -79,13 +79,13 @@ public function it_queries_all_entries() #[Test] public function it_queries_all_entries_in_a_specific_site() { - $this->createEntries(); - $this->setSites([ 'en' => ['url' => 'http://localhost/', 'locale' => 'en'], 'fr' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], ]); + $this->createEntries(); + Collection::find('events')->routes('/events/{slug}')->sites(['en', 'fr'])->save(); EntryFactory::collection('events')->locale('fr')->origin('4')->id('44')->slug('event-two')->create(); diff --git a/tests/Feature/GraphQL/EntryTest.php b/tests/Feature/GraphQL/EntryTest.php index 98787b29cb5..74a5fa1dc4a 100644 --- a/tests/Feature/GraphQL/EntryTest.php +++ b/tests/Feature/GraphQL/EntryTest.php @@ -7,6 +7,7 @@ use Facades\Statamic\CP\LivePreview; use Facades\Statamic\Fields\BlueprintRepository; use Facades\Tests\Factories\EntryFactory; +use Orchestra\Testbench\Attributes\DefineEnvironment; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; @@ -269,13 +270,9 @@ public function it_queries_an_entry_by_uri() } #[Test] + #[DefineEnvironment('useMultipleSites')] public function it_queries_an_entry_in_a_specific_site() { - $this->setSites([ - 'en' => ['url' => 'http://localhost/', 'locale' => 'en'], - 'fr' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], - ]); - Collection::find('events')->routes('/events/{slug}')->sites(['en', 'fr'])->save(); EntryFactory::collection('events')->locale('fr')->origin('4')->id('44')->slug('event-two')->create(); @@ -625,6 +622,14 @@ public function adding_custom_field_to_an_implementation_does_not_add_it_to_the_ ]]]); } + protected function useMultipleSites() + { + $this->setSites([ + 'en' => ['url' => 'http://localhost/', 'locale' => 'en'], + 'fr' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], + ]); + } + private function createStructuredCollection() { $collection = Collection::find('events');