Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Search/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
abstract class Index
{
protected $name;
protected $handle;
protected $locale;
protected $config;
protected static ?Closure $nameCallback = null;
Expand All @@ -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);
Expand Down Expand Up @@ -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
));
Expand Down
24 changes: 24 additions & 0 deletions tests/Search/IndexTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'
);
}
}
Loading