From d2c5271a5ed5cf030111ca8c11d19e9f262fe637 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Wed, 10 Jul 2024 19:09:56 -0400 Subject: [PATCH 1/9] =?UTF-8?q?Throw=20404=20if=20taxonomy=20isn=E2=80=99t?= =?UTF-8?q?=20assigned=20to=20collection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Taxonomies/LocalizedTerm.php | 4 ++++ src/Taxonomies/Taxonomy.php | 9 +++++++++ src/Taxonomies/Term.php | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Taxonomies/LocalizedTerm.php b/src/Taxonomies/LocalizedTerm.php index 15808c4083..126ad69f87 100644 --- a/src/Taxonomies/LocalizedTerm.php +++ b/src/Taxonomies/LocalizedTerm.php @@ -373,6 +373,10 @@ public function toResponse($request) throw new NotFoundHttpException; } + if (! $this->taxonomy()->isAssignedToCollection()) { + throw new NotFoundHttpException; + } + return (new DataResponse($this))->toResponse($request); } diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index a118c0f45d..a7b1e83655 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -362,12 +362,21 @@ public function collections() })->values(); } + public function isAssignedToCollection() + { + return $this->collection() && $this->collections()->contains($this->collection()); + } + public function toResponse($request) { if (! view()->exists($this->template())) { throw new NotFoundHttpException; } + if (! $this->isAssignedToCollection()) { + throw new NotFoundHttpException; + } + return (new \Statamic\Http\Responses\DataResponse($this)) ->with([ 'terms' => $termQuery = $this->queryTerms(), diff --git a/src/Taxonomies/Term.php b/src/Taxonomies/Term.php index 667b9f466e..b826aec4ed 100644 --- a/src/Taxonomies/Term.php +++ b/src/Taxonomies/Term.php @@ -58,7 +58,7 @@ public function taxonomy($taxonomy = null) }) ->getter(function ($taxonomy) { return $taxonomy ? Blink::once("taxonomy-{$taxonomy}", function () use ($taxonomy) { - return Taxonomy::findByHandle($taxonomy); + return Taxonomy::findByHandle($taxonomy)?->collection($this->collection()); }) : null; }) ->args(func_get_args()); From ecb295b17a6f00b46646fce9d98a1af3db97cdb2 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 11 Jul 2024 14:47:52 -0400 Subject: [PATCH 2/9] Fix condition --- src/Taxonomies/Taxonomy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index a7b1e83655..987cb705fe 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -364,7 +364,7 @@ public function collections() public function isAssignedToCollection() { - return $this->collection() && $this->collections()->contains($this->collection()); + return $this->collections()->contains($this->collection()); } public function toResponse($request) @@ -373,7 +373,7 @@ public function toResponse($request) throw new NotFoundHttpException; } - if (! $this->isAssignedToCollection()) { + if ($this->collection() && ! $this->isAssignedToCollection()) { throw new NotFoundHttpException; } From a01946b12496e0a9857ea4308aa5ddfc29ad0dc0 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 11 Jul 2024 14:47:58 -0400 Subject: [PATCH 3/9] Fix test --- tests/Data/Taxonomies/TermTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Data/Taxonomies/TermTest.php b/tests/Data/Taxonomies/TermTest.php index b2352c3bb7..fef59d01b3 100644 --- a/tests/Data/Taxonomies/TermTest.php +++ b/tests/Data/Taxonomies/TermTest.php @@ -74,6 +74,7 @@ public function the_blueprint_is_blinked_when_getting_and_flushed_when_setting() $taxonomy = Mockery::mock(Taxonomy::make('tags')); $taxonomy->shouldReceive('termBlueprint')->with(null, $term)->once()->andReturn('the old blueprint'); $taxonomy->shouldReceive('termBlueprint')->with('new', $term)->once()->andReturn('the new blueprint'); + $taxonomy->shouldReceive('collection')->with(null)->andReturn($taxonomy); Taxonomy::shouldReceive('findByHandle')->with('tags')->andReturn($taxonomy); $this->assertEquals('the old blueprint', $term->blueprint()); From bd9451156ec45048d0653e3c55dcd77638d4f2fd Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 11 Jul 2024 15:00:44 -0400 Subject: [PATCH 4/9] Fix condition --- src/Taxonomies/LocalizedTerm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Taxonomies/LocalizedTerm.php b/src/Taxonomies/LocalizedTerm.php index 126ad69f87..5217700e4b 100644 --- a/src/Taxonomies/LocalizedTerm.php +++ b/src/Taxonomies/LocalizedTerm.php @@ -373,7 +373,7 @@ public function toResponse($request) throw new NotFoundHttpException; } - if (! $this->taxonomy()->isAssignedToCollection()) { + if ($this->collection() && ! $this->taxonomy()->isAssignedToCollection()) { throw new NotFoundHttpException; } From a82e881c71ad9b4fc7db92e11b40e937a99770fd Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 11 Jul 2024 15:11:44 -0400 Subject: [PATCH 5/9] Fix tests --- tests/Data/Taxonomies/ViewsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Data/Taxonomies/ViewsTest.php b/tests/Data/Taxonomies/ViewsTest.php index 4ef8f448a4..147fec4665 100644 --- a/tests/Data/Taxonomies/ViewsTest.php +++ b/tests/Data/Taxonomies/ViewsTest.php @@ -37,9 +37,9 @@ public function setUp(): void $this->blogCollection = tap(Collection::make('blog')->sites(['en', 'fr'])->taxonomies(['tags']))->save(); - Taxonomy::make('tags')->sites(['en', 'fr'])->title('Tags')->save(); + Taxonomy::make('tags')->collection($this->blogCollection)->sites(['en', 'fr'])->title('Tags')->save(); - tap(Term::make('test')->taxonomy('tags'), function ($term) { + tap(Term::make('test')->taxonomy('tags')->collection($this->blogCollection), function ($term) { $term->in('en')->slug('test')->set('title', 'Test'); $term->in('fr')->slug('le-test')->set('title', 'Le Test'); })->save(); From 118c0140d313dbfb5afef956b92e866b02143899 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 12 Dec 2024 13:53:43 -0500 Subject: [PATCH 6/9] Setting a collection shouldn't be done in setup ... It gets assigned during the request. --- tests/Data/Taxonomies/ViewsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Data/Taxonomies/ViewsTest.php b/tests/Data/Taxonomies/ViewsTest.php index 9187e75adc..b3175a7c7e 100644 --- a/tests/Data/Taxonomies/ViewsTest.php +++ b/tests/Data/Taxonomies/ViewsTest.php @@ -41,9 +41,9 @@ public function setUp(): void $this->blogCollection = tap(Collection::make('blog')->sites(['en', 'de', 'fr'])->taxonomies(['tags']))->save(); - $this->tagsTaxonomy = tap(Taxonomy::make('tags')->collection($this->blogCollection)->sites(['en', 'fr'])->title('Tags'))->save(); + $this->tagsTaxonomy = tap(Taxonomy::make('tags')->sites(['en', 'fr'])->title('Tags'))->save(); - tap(Term::make('test')->taxonomy('tags')->collection($this->blogCollection), function ($term) { + tap(Term::make('test')->taxonomy('tags'), function ($term) { $term->in('en')->slug('test')->set('title', 'Test'); $term->in('fr')->slug('le-test')->set('title', 'Le Test'); })->save(); From eeb7911eb238fdfb204ede23c437a900d4c3ff43 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 12 Dec 2024 14:32:30 -0500 Subject: [PATCH 7/9] add tests, fix --- src/Taxonomies/LocalizedTerm.php | 2 +- src/Taxonomies/Taxonomy.php | 7 +------ tests/Data/Taxonomies/ViewsTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Taxonomies/LocalizedTerm.php b/src/Taxonomies/LocalizedTerm.php index 1eccc9010a..f40b394ae0 100644 --- a/src/Taxonomies/LocalizedTerm.php +++ b/src/Taxonomies/LocalizedTerm.php @@ -375,7 +375,7 @@ public function toResponse($request) throw new NotFoundHttpException; } - if ($this->collection() && ! $this->taxonomy()->isAssignedToCollection()) { + if ($this->collection() && ! $this->taxonomy()->collections()->contains($this->collection())) { throw new NotFoundHttpException; } diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index 54eaf5e0f0..b7cd44e4b2 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -367,11 +367,6 @@ public function collections() })->values(); } - public function isAssignedToCollection() - { - return $this->collections()->contains($this->collection()); - } - public function toResponse($request) { if (! view()->exists($this->template())) { @@ -386,7 +381,7 @@ public function toResponse($request) throw new NotFoundHttpException; } - if ($this->collection() && ! $this->isAssignedToCollection()) { + if ($this->collection() && ! $this->collections()->contains($this->collection())) { throw new NotFoundHttpException; } 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() { From 3d55414a9d1c674fa3a5ed6477af496ee87c7c5e Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 12 Dec 2024 14:33:41 -0500 Subject: [PATCH 8/9] Not needed --- src/Taxonomies/Term.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Taxonomies/Term.php b/src/Taxonomies/Term.php index b826aec4ed..667b9f466e 100644 --- a/src/Taxonomies/Term.php +++ b/src/Taxonomies/Term.php @@ -58,7 +58,7 @@ public function taxonomy($taxonomy = null) }) ->getter(function ($taxonomy) { return $taxonomy ? Blink::once("taxonomy-{$taxonomy}", function () use ($taxonomy) { - return Taxonomy::findByHandle($taxonomy)?->collection($this->collection()); + return Taxonomy::findByHandle($taxonomy); }) : null; }) ->args(func_get_args()); From 35833e253d9cc81e834eba332c90b2e23de999b7 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 12 Dec 2024 15:08:47 -0500 Subject: [PATCH 9/9] Not needed either --- tests/Data/Taxonomies/TermTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Data/Taxonomies/TermTest.php b/tests/Data/Taxonomies/TermTest.php index fef59d01b3..b2352c3bb7 100644 --- a/tests/Data/Taxonomies/TermTest.php +++ b/tests/Data/Taxonomies/TermTest.php @@ -74,7 +74,6 @@ public function the_blueprint_is_blinked_when_getting_and_flushed_when_setting() $taxonomy = Mockery::mock(Taxonomy::make('tags')); $taxonomy->shouldReceive('termBlueprint')->with(null, $term)->once()->andReturn('the old blueprint'); $taxonomy->shouldReceive('termBlueprint')->with('new', $term)->once()->andReturn('the new blueprint'); - $taxonomy->shouldReceive('collection')->with(null)->andReturn($taxonomy); Taxonomy::shouldReceive('findByHandle')->with('tags')->andReturn($taxonomy); $this->assertEquals('the old blueprint', $term->blueprint());