diff --git a/src/Search/Searchables/Entries.php b/src/Search/Searchables/Entries.php index 8b1c1f15d7d..1861a441e44 100644 --- a/src/Search/Searchables/Entries.php +++ b/src/Search/Searchables/Entries.php @@ -41,7 +41,9 @@ public function provide(): Collection|LazyCollection ->map->reference(); } - $query->whereStatus('published'); + if ($this->index->handle() !== 'cp') { + $query->whereStatus('published'); + } return $query->pluck('reference'); } @@ -66,7 +68,7 @@ public function contains($searchable): bool $query = Entry::query()->where('id', $searchable->id()); - if (! $filter) { + if (! $filter && $this->index->handle() !== 'cp') { $query->whereStatus('published'); } diff --git a/tests/Search/Searchables/EntriesTest.php b/tests/Search/Searchables/EntriesTest.php index 90b7c397560..86fa90ab04d 100644 --- a/tests/Search/Searchables/EntriesTest.php +++ b/tests/Search/Searchables/EntriesTest.php @@ -217,19 +217,35 @@ public function it_can_use_a_query_scope_and_a_custom_filter() $this->assertFalse($provider->contains($c)); } - private function makeProvider($locale, $config) + #[Test] + public function it_includes_unpublished_entries_in_the_cp_index() + { + Collection::make('blog')->save(); + $a = EntryFactory::collection('blog')->id('a')->create(); + $b = EntryFactory::collection('blog')->id('b')->published(false)->create(); + + $provider = $this->makeProvider(null, ['searchables' => 'content'], 'cp'); + + $this->assertEquals(['entry::a', 'entry::b'], $provider->provide()->all()); + + $this->assertTrue($provider->contains($a)); + $this->assertTrue($provider->contains($b)); + } + + private function makeProvider($locale, $config, $handle = 'default') { - $index = $this->makeIndex($locale, $config); + $index = $this->makeIndex($locale, $config, $handle); $keys = $this->normalizeSearchableKeys($config['searchables'] ?? null); return (new Entries)->setIndex($index)->setKeys($keys); } - private function makeIndex($locale, $config) + private function makeIndex($locale, $config, $handle) { $index = $this->mock(\Statamic\Search\Index::class); + $index->shouldReceive('handle')->andReturn($handle); $index->shouldReceive('config')->andReturn($config); $index->shouldReceive('locale')->andReturn($locale);