Skip to content

Commit 10beda2

Browse files
aernijasonvarga
andauthored
[5.x] Fix taxonomy routes on multi-site (#10398)
Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent c919a53 commit 10beda2

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/Taxonomies/Taxonomy.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,17 @@ public function toResponse($request)
368368
throw new NotFoundHttpException;
369369
}
370370

371+
if (! $this->sites()->contains($site = Site::current())) {
372+
throw new NotFoundHttpException;
373+
}
374+
375+
if ($this->collection() && ! $this->collection()->sites()->contains($site)) {
376+
throw new NotFoundHttpException;
377+
}
378+
371379
return (new \Statamic\Http\Responses\DataResponse($this))
372380
->with([
373-
'terms' => $termQuery = $this->queryTerms()->where('site', Site::current()),
381+
'terms' => $termQuery = $this->queryTerms()->where('site', $site),
374382
$this->handle() => $termQuery,
375383
])
376384
->toResponse($request);

tests/Data/Taxonomies/ViewsTest.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,47 @@ class ViewsTest extends TestCase
1818

1919
private $blogEntry;
2020
private $frenchBlogEntry;
21+
private $germanBlogEntry;
2122
private $blogCollection;
23+
private $tagsTaxonomy;
2224

2325
public function setUp(): void
2426
{
2527
parent::setUp();
2628

2729
$this->setSites([
2830
'en' => ['url' => '/', 'locale' => 'en'],
31+
'de' => ['url' => '/de/', 'locale' => 'de'],
2932
'fr' => ['url' => '/fr/', 'locale' => 'fr'],
3033
]);
3134

3235
$this->withStandardFakeViews();
3336

34-
Collection::make('pages')->routes('{slug}')->sites(['en', 'fr'])->save();
37+
Collection::make('pages')->routes('{slug}')->sites(['en', 'de', 'fr'])->save();
3538
$this->blogEntry = EntryFactory::collection('pages')->locale('en')->slug('the-blog')->create();
3639
$this->frenchBlogEntry = EntryFactory::collection('pages')->locale('fr')->slug('le-blog')->origin($this->blogEntry->id())->create();
40+
$this->germanBlogEntry = EntryFactory::collection('pages')->locale('de')->slug('der-blog')->origin($this->blogEntry->id())->create();
3741

38-
$this->blogCollection = tap(Collection::make('blog')->sites(['en', 'fr'])->taxonomies(['tags']))->save();
42+
$this->blogCollection = tap(Collection::make('blog')->sites(['en', 'de', 'fr'])->taxonomies(['tags']))->save();
3943

40-
Taxonomy::make('tags')->sites(['en', 'fr'])->title('Tags')->save();
44+
$this->tagsTaxonomy = tap(Taxonomy::make('tags')->sites(['en', 'fr'])->title('Tags'))->save();
4145

4246
tap(Term::make('test')->taxonomy('tags'), function ($term) {
4347
$term->in('en')->slug('test')->set('title', 'Test');
4448
$term->in('fr')->slug('le-test')->set('title', 'Le Test');
4549
})->save();
4650
}
4751

52+
#[Test]
53+
public function the_taxonomy_url_404s_for_unconfigured_sites()
54+
{
55+
$this->viewShouldReturnRaw('tags.index', '{{ title }} index');
56+
57+
$this->get('/tags')->assertOk()->assertSee('Tags index');
58+
$this->get('/fr/tags')->assertOk()->assertSee('Tags index');
59+
$this->get('/de/tags')->assertNotFound();
60+
}
61+
4862
#[Test]
4963
public function the_taxonomy_url_404s_if_the_view_doesnt_exist()
5064
{
@@ -81,6 +95,42 @@ public function it_loads_the_localized_term_url_if_the_view_exists()
8195
$this->get('/fr/tags/le-test')->assertOk()->assertSee('showing Le Test');
8296
}
8397

98+
#[Test]
99+
public function the_collection_specific_taxonomy_url_404s_for_unconfigured_sites()
100+
{
101+
$this->mountBlogPageToBlogCollection();
102+
103+
$this->viewShouldReturnRaw('blog.tags.index', '{{ title }} index');
104+
105+
$this->get('/the-blog/tags')->assertOk()->assertSee('Tags index');
106+
$this->get('/fr/le-blog/tags')->assertOk()->assertSee('Tags index');
107+
$this->get('/de/der-blog/tags')->assertNotFound();
108+
}
109+
110+
#[Test]
111+
public function the_collection_specific_taxonomy_url_404s_when_collection_is_not_configured_for_that_site()
112+
{
113+
$this->mountBlogPageToBlogCollection();
114+
115+
// Set all the slugs to match the taxonomy, to make sure that the
116+
// missing localized collection URL isn't the thing causing the 404.
117+
$this->blogEntry->in('en')->slug('blog')->save();
118+
$this->blogEntry->in('fr')->slug('blog')->save();
119+
$this->blogEntry->in('de')->slug('blog')->save();
120+
$this->get('/blog')->assertOk();
121+
$this->get('/fr/blog')->assertOk();
122+
$this->get('/de/blog')->assertOk();
123+
124+
$this->tagsTaxonomy->sites(['en', 'fr', 'de'])->save();
125+
$this->blogCollection->sites(['en', 'de'])->save();
126+
127+
$this->viewShouldReturnRaw('blog.tags.index', '{{ title }} index');
128+
129+
$this->get('/blog/tags')->assertOk()->assertSee('Tags index');
130+
$this->get('/fr/blog/tags')->assertNotFound();
131+
$this->get('/de/blog/tags')->assertOk()->assertSee('Tags index');
132+
}
133+
84134
#[Test]
85135
public function the_collection_specific_taxonomy_url_404s_if_the_view_doesnt_exist()
86136
{

0 commit comments

Comments
 (0)