Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Throw 404 on collection routes if taxonomy isn’t assigned to collection #10438

Merged
merged 10 commits into from
Dec 12, 2024
Merged
4 changes: 4 additions & 0 deletions src/Taxonomies/LocalizedTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ public function toResponse($request)
throw new NotFoundHttpException;
}

if ($this->collection() && ! $this->taxonomy()->collections()->contains($this->collection())) {
throw new NotFoundHttpException;
}

return (new DataResponse($this))->toResponse($request);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Taxonomies/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ public function toResponse($request)
throw new NotFoundHttpException;
}

if ($this->collection() && ! $this->collections()->contains($this->collection())) {
throw new NotFoundHttpException;
}

return (new \Statamic\Http\Responses\DataResponse($this))
->with([
'terms' => $termQuery = $this->queryTerms()->where('site', $site),
Expand Down
24 changes: 24 additions & 0 deletions tests/Data/Taxonomies/ViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ public function the_collection_specific_taxonomy_url_404s_if_the_view_doesnt_exi
$this->get('/the-blog/tags/test')->assertNotFound();
}

#[Test]
public function the_collection_specific_taxonomy_url_404s_if_the_collection_is_not_configured()
{
$this->mountBlogPageToBlogCollection();

$this->viewShouldReturnRaw('blog.tags.index', '{{ title }} index');

$this->blogCollection->taxonomies([])->save();

$this->get('/the-blog/tags')->assertNotFound();
}

#[Test]
public function it_loads_the_collection_specific_taxonomy_url_if_the_view_exists()
{
Expand All @@ -157,6 +169,18 @@ public function the_collection_specific_term_url_404s_if_the_view_doesnt_exist()
$this->get('/the-blog/tags/test')->assertNotFound();
}

#[Test]
public function the_collection_specific_term_url_404s_if_the_collection_is_not_assigned_to_the_taxonomy()
{
$this->mountBlogPageToBlogCollection();

$this->viewShouldReturnRaw('blog.tags.show', 'showing {{ title }}');

$this->blogCollection->taxonomies([])->save();

$this->get('/the-blog/tags/test')->assertNotFound();
}

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