Skip to content
Merged
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
9 changes: 4 additions & 5 deletions src/Search/Commands/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Statamic\Console\RunsInPlease;
use Statamic\Events\SearchIndexUpdated;
use Statamic\Facades\Search;
use Statamic\Support\Str;

use function Laravel\Prompts\select;

Expand Down Expand Up @@ -35,7 +34,7 @@ public function handle()

private function getIndexes()
{
if ($requestedIndex = $this->getRequestedIndex()) {
if (! is_null($requestedIndex = $this->getRequestedIndex())) {
return $requestedIndex;
}

Expand Down Expand Up @@ -71,10 +70,10 @@ private function getRequestedIndex()
return [$this->indexes()->get($arg)];
}

// They might have entered a name as it appears in the config, but if it
// They might have entered a handle as it appears in the config, but if it
// should be localized we'll get all of the localized versions.
if (collect(config('statamic.search.indexes'))->put('cp', [])->has($arg)) {
return $this->indexes()->filter(fn ($index) => Str::startsWith($index->name(), $arg))->all();
if ($indexes = $this->indexes()->filter(fn ($index) => $index->handle() === $arg)->all()) {
return $indexes;
}

throw new \InvalidArgumentException("Index [$arg] does not exist.");
Expand Down
5 changes: 5 additions & 0 deletions src/Search/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function name()
return $this->name;
}

public function handle()
{
return $this->handle;
}

public static function resolveNameUsing(?Closure $callback)
{
static::$nameCallback = $callback;
Expand Down
73 changes: 73 additions & 0 deletions tests/Search/Commands/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Tests\Search\Commands;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Search\Commands\Update;
use Statamic\Search\Index;
use Tests\TestCase;

class UpdateTest extends TestCase
{
public function tearDown(): void
{
// Reset the static state of the Index class
Index::resolveNameUsing(null);

parent::tearDown();
}

private function setUpIndexes()
{
$this->setSites([
'en' => ['url' => '/'],
'fr' => ['url' => '/fr/'],
]);

config(['statamic.search.indexes' => [
'test' => ['driver' => 'null', 'sites' => ['en', 'fr']],
'cp' => ['driver' => 'null'],
]]);
}

#[Test]
public function it_updates_the_localized_indexes_matching_the_configured_handle()
{
$this->setUpIndexes();

$this->artisan(Update::class, ['index' => 'test'])
->expectsOutputToContain('Index test_en updated.')
->expectsOutputToContain('Index test_fr updated.')
->doesntExpectOutputToContain('Index cp updated.')
->assertExitCode(0);
}

#[Test]
public function it_updates_the_localized_indexes_when_the_names_have_been_customized()
{
// When a custom resolver prefixes the name, the resolved name can no longer be
// reversed back into the configured handle by string manipulation.
Index::resolveNameUsing(fn ($name, $locale) => 'local_'.$name.'_'.$locale);

$this->setUpIndexes();

// The argument is the handle as it appears in the config, so it should still
// match both localized versions, and it should leave the cp index alone.
$this->artisan(Update::class, ['index' => 'test'])
->expectsOutputToContain('Index local_test_en updated.')
->expectsOutputToContain('Index local_test_fr updated.')
->doesntExpectOutputToContain('Index local_cp_ updated.')
->assertExitCode(0);
}

#[Test]
public function it_errors_when_the_index_doesnt_exist()
{
$this->setUpIndexes();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Index [unknown] does not exist.');

$this->artisan(Update::class, ['index' => 'unknown'])->run();
}
}
Loading