From 158bb14529bb72bd42f324fc49938a272f91320c Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 12 Nov 2024 10:58:45 +0000 Subject: [PATCH 1/2] Match how whereJsonContains to how Laravel handles it --- src/Query/IteratorBuilder.php | 4 ++-- src/Stache/Query/Builder.php | 4 ++-- tests/Data/Assets/AssetQueryBuilderTest.php | 6 +++--- tests/Data/Entries/EntryQueryBuilderTest.php | 6 +++--- tests/Data/Taxonomies/TermQueryBuilderTest.php | 6 +++--- tests/Forms/SubmissionQueryBuilderTest.php | 6 +++--- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Query/IteratorBuilder.php b/src/Query/IteratorBuilder.php index a917ba7b3d..ab214b9a5a 100644 --- a/src/Query/IteratorBuilder.php +++ b/src/Query/IteratorBuilder.php @@ -180,7 +180,7 @@ protected function filterWhereJsonContains($entries, $where) return false; } - return ! empty(array_intersect($value, $where['values'])); + return count(array_intersect($value, $where['values'])) == count($where['values']); }); } @@ -193,7 +193,7 @@ protected function filterWhereJsonDoesntContain($entries, $where) return true; } - return empty(array_intersect($value, $where['values'])); + return count(array_intersect($value, $where['values'])) != count($where['values']); }); } diff --git a/src/Stache/Query/Builder.php b/src/Stache/Query/Builder.php index 3444923648..850d1399ec 100644 --- a/src/Stache/Query/Builder.php +++ b/src/Stache/Query/Builder.php @@ -301,7 +301,7 @@ protected function filterWhereJsonContains($values, $where) return false; } - return ! empty(array_intersect($value, $where['values'])); + return count(array_intersect($value, $where['values'])) == count($where['values']); }); } @@ -312,7 +312,7 @@ protected function filterWhereJsonDoesntContain($values, $where) return true; } - return empty(array_intersect($value, $where['values'])); + return count(array_intersect($value, $where['values'])) != count($where['values']); }); } diff --git a/tests/Data/Assets/AssetQueryBuilderTest.php b/tests/Data/Assets/AssetQueryBuilderTest.php index f3cdc8297e..d916a6692c 100644 --- a/tests/Data/Assets/AssetQueryBuilderTest.php +++ b/tests/Data/Assets/AssetQueryBuilderTest.php @@ -344,10 +344,10 @@ public function assets_are_found_using_where_json_contains() Asset::find('test::d.jpg')->data(['test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save(); Asset::find('test::e.jpg')->data(['test_taxonomy' => ['taxonomy-5']])->save(); - $assets = $this->container->queryAssets()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get(); + $assets = $this->container->queryAssets()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get(); - $this->assertCount(3, $assets); - $this->assertEquals(['a', 'c', 'e'], $assets->map->filename()->all()); + $this->assertCount(1, $assets); + $this->assertEquals(['c'], $assets->map->filename()->all()); $assets = $this->container->queryAssets()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get(); diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index 75c6706dbe..0312df6015 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -399,10 +399,10 @@ public function entries_are_found_using_where_json_contains() EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->create(); EntryFactory::id('5')->slug('post-5')->collection('posts')->data(['title' => 'Post 5', 'test_taxonomy' => ['taxonomy-5']])->create(); - $entries = Entry::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get(); + $entries = Entry::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get(); - $this->assertCount(3, $entries); - $this->assertEquals(['Post 1', 'Post 3', 'Post 5'], $entries->map->title->all()); + $this->assertCount(1, $entries); + $this->assertEquals(['Post 3'], $entries->map->title->all()); $entries = Entry::query()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get(); diff --git a/tests/Data/Taxonomies/TermQueryBuilderTest.php b/tests/Data/Taxonomies/TermQueryBuilderTest.php index b7136ede31..4d3cf34e4f 100644 --- a/tests/Data/Taxonomies/TermQueryBuilderTest.php +++ b/tests/Data/Taxonomies/TermQueryBuilderTest.php @@ -509,10 +509,10 @@ public function terms_are_found_using_where_json_contains() Term::make('4')->taxonomy('tags')->data(['test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save(); Term::make('5')->taxonomy('tags')->data(['test_taxonomy' => ['taxonomy-5']])->save(); - $entries = Term::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get(); + $entries = Term::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get(); - $this->assertCount(3, $entries); - $this->assertEquals(['1', '3', '5'], $entries->map->slug()->all()); + $this->assertCount(1, $entries); + $this->assertEquals(['3'], $entries->map->slug()->all()); $entries = Term::query()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get(); diff --git a/tests/Forms/SubmissionQueryBuilderTest.php b/tests/Forms/SubmissionQueryBuilderTest.php index 51ab55718d..5b228d6b28 100644 --- a/tests/Forms/SubmissionQueryBuilderTest.php +++ b/tests/Forms/SubmissionQueryBuilderTest.php @@ -293,10 +293,10 @@ public function submissions_are_found_using_where_json_contains() FormSubmission::make()->form($form)->data(['id' => '4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save(); FormSubmission::make()->form($form)->data(['id' => '5', 'test_taxonomy' => ['taxonomy-5']])->save(); - $entries = FormSubmission::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get(); + $entries = FormSubmission::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get(); - $this->assertCount(3, $entries); - $this->assertEquals(['1', '3', '5'], $entries->map->get('id')->all()); + $this->assertCount(1, $entries); + $this->assertEquals(['3'], $entries->map->get('id')->all()); $entries = FormSubmission::query()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get(); From f2f233865860aa4accaa877d6c31beffeb4298f3 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 12 Nov 2024 11:06:45 +0000 Subject: [PATCH 2/2] Fix failing test --- tests/Search/QueryBuilderTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Search/QueryBuilderTest.php b/tests/Search/QueryBuilderTest.php index 0578cedb7b..0ddbfda0dd 100644 --- a/tests/Search/QueryBuilderTest.php +++ b/tests/Search/QueryBuilderTest.php @@ -365,10 +365,10 @@ public function results_are_found_using_where_json_contains() ['reference' => 'e', 'test_taxonomy' => ['taxonomy-5']], ]); - $results = (new FakeQueryBuilder($items))->withoutData()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get(); + $results = (new FakeQueryBuilder($items))->withoutData()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get(); - $this->assertCount(3, $results); - $this->assertEquals(['a', 'c', 'e'], $results->map->reference->all()); + $this->assertCount(1, $results); + $this->assertEquals(['c'], $results->map->reference->all()); $results = (new FakeQueryBuilder($items))->withoutData()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get();