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
2 changes: 1 addition & 1 deletion src/Http/Controllers/CP/Taxonomies/TermsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function indexQuery($taxonomy)
{
$query = $taxonomy->queryTerms();

$query->where('site', Site::selected());
$query->where('site', Site::selected()->handle());

if ($search = request('search')) {
if ($taxonomy->hasSearchIndex()) {
Expand Down
3 changes: 2 additions & 1 deletion src/Taxonomies/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Facades\URL;
use Statamic\Fields\Value;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Support\Traits\FluentlyGetsAndSets;
Expand Down Expand Up @@ -405,7 +406,7 @@ public function toResponse($request)

return (new \Statamic\Http\Responses\DataResponse($this))
->with([
'terms' => $termQuery = $this->queryTerms()->where('site', $site),
'terms' => $termQuery = new Value($this->queryTerms()->where('site', $site->handle())),
$this->handle() => $termQuery,
])
->toResponse($request);
Expand Down
2 changes: 1 addition & 1 deletion src/Taxonomies/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function in($site)
{
return app()->makeWith(LocalizedTerm::class, [
'term' => $this,
'locale' => $site,
'locale' => $site instanceof \Statamic\Sites\Site ? $site->handle() : $site,
]);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Data/Taxonomies/TermTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ public function it_gets_file_contents_for_saving()
], $term->fileData());
}

#[Test]
public function it_gets_a_localization_using_a_site_instance()
{
$this->setSites([
'en' => ['url' => '/'],
'fr' => ['url' => '/fr/'],
]);

Taxonomy::make('tags')->sites(['en', 'fr'])->save();

$term = (new Term)->taxonomy('tags');
$term->in('fr')->set('title', 'Le Test');

$localized = $term->in(Facades\Site::get('fr'));

$this->assertSame('fr', $localized->locale());
$this->assertEquals('Le Test', $localized->get('title'));
}

#[Test]
public function it_gets_preview_targets()
{
Expand Down
35 changes: 35 additions & 0 deletions tests/Data/Taxonomies/ViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Tests\Data\Taxonomies;

use Facades\Tests\Factories\EntryFactory;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Collection;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\Term;
use Statamic\Stache\Query\TermQueryBuilder;
use Tests\FakesViews;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
Expand Down Expand Up @@ -73,6 +75,39 @@ public function it_loads_the_taxonomy_url_if_the_view_exists()
$this->get('/tags')->assertOk()->assertSee('Tags index');
}

#[Test]
public function it_queries_the_taxonomy_terms_using_the_current_site_handle()
{
$this->viewShouldReturnRaw('tags.index', '{{ title }} index');

// Query builders like the Eloquent driver's use the site as a localization key, so it must be a handle.
$builder = Mockery::mock(TermQueryBuilder::class);
$builder->shouldReceive('where')->with('taxonomy', 'tags')->andReturnSelf();
$builder->shouldReceive('where')->with('site', Mockery::on(fn ($site) => $site === 'fr'))->once()->andReturnSelf();

$repository = Mockery::mock(Term::getFacadeRoot())->makePartial();
$repository->shouldReceive('query')->andReturn($builder);
Term::swap($repository);

$this->get('/fr/tags')->assertOk()->assertSee('Tags index');
}

#[Test]
public function it_loops_over_the_taxonomy_terms_in_antlers()
{
$this->viewShouldReturnRaw('tags.index', '{{ terms }}{{ title }} {{ url }}{{ /terms }} | {{ tags }}{{ title }}{{ /tags }}');

$this->get('/fr/tags')->assertOk()->assertSee('Le Test /fr/tags/le-test | Le Test');
}

#[Test]
public function it_loops_over_the_taxonomy_terms_in_blade()
{
$this->viewShouldReturnRaw('tags.index', '@foreach ($terms as $term){{ $term->title }} {{ $term->url }}@endforeach | @foreach ($tags as $tag){{ $tag->title }}@endforeach', 'blade.php');

$this->get('/fr/tags')->assertOk()->assertSee('Le Test /fr/tags/le-test | Le Test');
}

#[Test]
public function the_term_url_404s_if_the_view_doesnt_exist()
{
Expand Down
Loading