From 82b709ef1d7ee35ea167107779270bf2031aaec4 Mon Sep 17 00:00:00 2001 From: Alexander Jensen Date: Tue, 7 Jul 2026 12:04:24 +0200 Subject: [PATCH] Pass the configured handle to the search insert job --- src/Search/Index.php | 5 ++++- tests/Search/IndexTests.php | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Search/Index.php b/src/Search/Index.php index fa9ebb8779f..43e2437bbaf 100644 --- a/src/Search/Index.php +++ b/src/Search/Index.php @@ -9,6 +9,7 @@ abstract class Index { protected $name; + protected $handle; protected $locale; protected $config; protected static ?Closure $nameCallback = null; @@ -25,6 +26,8 @@ abstract protected function deleteIndex(); public function __construct($name, array $config, ?string $locale = null) { + $this->handle = $name; + $this->name = static::$nameCallback ? call_user_func(static::$nameCallback, $name, $locale) : ($locale ? $name.'_'.$locale : $name); @@ -91,7 +94,7 @@ public function insertMultiple($documents) $documents ->chunk(config('statamic.search.chunk_size')) ->each(fn ($documents) => InsertMultipleJob::dispatch( - name: $this->locale ? Str::before($this->name, "_{$this->locale}") : $this->name, + name: $this->handle, locale: $this->locale, documents: $documents )); diff --git a/tests/Search/IndexTests.php b/tests/Search/IndexTests.php index 3ac1feedec4..24e8983d94b 100644 --- a/tests/Search/IndexTests.php +++ b/tests/Search/IndexTests.php @@ -2,9 +2,11 @@ namespace Tests\Search; +use Illuminate\Support\Facades\Bus; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use Statamic\Search\Index; +use Statamic\Search\InsertMultipleJob; trait IndexTests { @@ -46,4 +48,26 @@ public static function nameProvider() 'resolver with locale' => ['test', [], 'en', fn ($name, $locale) => 'prefix_'.$name.'_'.$locale, 'prefix_test_en'], ]; } + + #[Test] + public function it_dispatches_the_insert_job_with_the_configured_handle_not_the_resolved_name() + { + Bus::fake(); + + // A custom resolver that prefixes the name. The resolved name can no longer be + // reversed back into the configured handle by string manipulation. + $this->getIndexClass()::resolveNameUsing(fn ($name, $locale) => 'prefix_'.$name.'_'.$locale); + + $index = $this->getIndex('test', [], 'en'); + $this->assertEquals('prefix_test_en', $index->name()); + + $index->insertMultiple(collect(['foo::bar'])); + + // The job re-resolves the index via Search::index($name, $locale), so it must + // receive the configured handle ('test'), not the resolved name ('prefix_test_en'). + Bus::assertDispatched( + InsertMultipleJob::class, + fn (InsertMultipleJob $job) => $job->name === 'test' && $job->locale === 'en' + ); + } }