diff --git a/src/Taxonomies/LocalizedTerm.php b/src/Taxonomies/LocalizedTerm.php index 86e5488372..f40b394ae0 100644 --- a/src/Taxonomies/LocalizedTerm.php +++ b/src/Taxonomies/LocalizedTerm.php @@ -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); } diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index b2bfd4e76e..b7cd44e4b2 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -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), diff --git a/tests/Data/Taxonomies/ViewsTest.php b/tests/Data/Taxonomies/ViewsTest.php index b3175a7c7e..812488cb9d 100644 --- a/tests/Data/Taxonomies/ViewsTest.php +++ b/tests/Data/Taxonomies/ViewsTest.php @@ -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() { @@ -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() {