Skip to content

Commit

Permalink
[5.x] Throw 404 on collection routes if taxonomy isn’t assigned to co…
Browse files Browse the repository at this point in the history
…llection (#10438)

Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
aerni and jasonvarga authored Dec 12, 2024
1 parent c028475 commit bcd4e17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
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

0 comments on commit bcd4e17

Please sign in to comment.