diff --git a/src/Exceptions/StatusFilterNotSupportedException.php b/src/Exceptions/StatusFilterNotSupportedException.php new file mode 100644 index 00000000000..9b8e5f5b950 --- /dev/null +++ b/src/Exceptions/StatusFilterNotSupportedException.php @@ -0,0 +1,7 @@ + $collection->handle(), 'title' => $collection->title(), 'entries_count' => $collection->queryEntries()->where('site', Site::selected())->count(), - 'published_entries_count' => $collection->queryEntries()->where('site', Site::selected())->where('status', 'published')->count(), - 'draft_entries_count' => $collection->queryEntries()->where('site', Site::selected())->where('status', 'draft')->count(), - 'scheduled_entries_count' => $collection->queryEntries()->where('site', Site::selected())->where('status', 'scheduled')->count(), + 'published_entries_count' => $collection->queryEntries()->where('site', Site::selected())->whereStatus('published')->count(), + 'draft_entries_count' => $collection->queryEntries()->where('site', Site::selected())->whereStatus('draft')->count(), + 'scheduled_entries_count' => $collection->queryEntries()->where('site', Site::selected())->whereStatus('scheduled')->count(), 'blueprints' => $collection->entryBlueprints()->reject->hidden() ->map(fn ($blueprint) => [ ...$blueprint->toArray(), diff --git a/src/Stache/Query/EntryQueryBuilder.php b/src/Stache/Query/EntryQueryBuilder.php index 701b050c46f..0ae11b6d259 100644 --- a/src/Stache/Query/EntryQueryBuilder.php +++ b/src/Stache/Query/EntryQueryBuilder.php @@ -4,6 +4,7 @@ use Statamic\Contracts\Entries\QueryBuilder; use Statamic\Entries\EntryCollection; +use Statamic\Exceptions\StatusFilterNotSupportedException; use Statamic\Facades; use Statamic\Facades\Blink; use Statamic\Facades\Collection; @@ -26,7 +27,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' } if ($column === 'status') { - trigger_error('Filtering by status is deprecated. Use whereStatus() instead.', E_USER_DEPRECATED); + throw new StatusFilterNotSupportedException('Filtering by status is not supported. Use whereStatus() instead.'); } return parent::where($column, $operator, $value, $boolean); @@ -43,7 +44,7 @@ public function whereIn($column, $values, $boolean = 'and') } if ($column === 'status') { - trigger_error('Filtering by status is deprecated. Use whereStatus() instead.', E_USER_DEPRECATED); + throw new StatusFilterNotSupportedException('Filtering by status is not supported. Use whereStatus() instead.'); } return parent::whereIn($column, $values, $boolean); diff --git a/src/Tags/Collection/Entries.php b/src/Tags/Collection/Entries.php index e11ef1c762e..df3b3dd3a59 100644 --- a/src/Tags/Collection/Entries.php +++ b/src/Tags/Collection/Entries.php @@ -276,7 +276,7 @@ protected function queryPublished($query) return; } - return $query->where('published', true); + return $query->whereStatus('published'); } protected function queryPastFuture($query) diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index 70a67fc8f33..2a038018cbd 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Carbon; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use Statamic\Exceptions\StatusFilterNotSupportedException; use Statamic\Facades\Blueprint; use Statamic\Facades\Collection; use Statamic\Facades\Entry; @@ -1097,11 +1098,11 @@ public function entries_can_be_reordered() } #[Test] - public function filtering_using_where_status_column_writes_deprecation_log() + public function filtering_using_where_status_column_throws_exception() { $this->withoutDeprecationHandling(); - $this->expectException(\ErrorException::class); - $this->expectExceptionMessage('Filtering by status is deprecated. Use whereStatus() instead.'); + $this->expectException(StatusFilterNotSupportedException::class); + $this->expectExceptionMessage('Filtering by status is not supported. Use whereStatus() instead.'); $this->createDummyCollectionAndEntries(); @@ -1109,11 +1110,11 @@ public function filtering_using_where_status_column_writes_deprecation_log() } #[Test] - public function filtering_using_whereIn_status_column_writes_deprecation_log() + public function filtering_using_whereIn_status_column_throws_exception() { $this->withoutDeprecationHandling(); - $this->expectException(\ErrorException::class); - $this->expectExceptionMessage('Filtering by status is deprecated. Use whereStatus() instead.'); + $this->expectException(StatusFilterNotSupportedException::class); + $this->expectExceptionMessage('Filtering by status is not supported. Use whereStatus() instead.'); $this->createDummyCollectionAndEntries(); diff --git a/tests/Tags/Collection/EntriesTest.php b/tests/Tags/Collection/EntriesTest.php index 603f6bae49c..d99325bc783 100644 --- a/tests/Tags/Collection/EntriesTest.php +++ b/tests/Tags/Collection/EntriesTest.php @@ -10,6 +10,7 @@ use Mockery; use PHPUnit\Framework\Attributes\Test; use Statamic\Contracts\Query\Builder; +use Statamic\Exceptions\StatusFilterNotSupportedException; use Statamic\Facades; use Statamic\Facades\Blueprint; use Statamic\Facades\Site; @@ -291,7 +292,7 @@ public function it_filters_by_future_and_past() $this->assertCount(3, $this->getEntries()); $this->assertCount(0, $this->getEntries(['show_future' => false])); $this->assertCount(3, $this->getEntries(['show_future' => true])); - $this->assertCount(8, $this->getEntries(['show_past' => true])); + $this->assertCount(3, $this->getEntries(['show_past' => true])); $this->assertCount(3, $this->getEntries(['show_past' => false])); $this->assertCount(3, $this->getEntries(['show_past' => false, 'show_future' => true])); @@ -307,10 +308,10 @@ public function it_filters_by_future_and_past() $this->collection->dated(true)->futureDateBehavior('private')->pastDateBehavior('public')->save(); $this->assertCount(4, $this->getEntries()); $this->assertCount(4, $this->getEntries(['show_future' => false])); - $this->assertCount(8, $this->getEntries(['show_future' => true])); + $this->assertCount(4, $this->getEntries(['show_future' => true])); $this->assertCount(4, $this->getEntries(['show_past' => true])); $this->assertCount(0, $this->getEntries(['show_past' => false])); - $this->assertCount(3, $this->getEntries(['show_past' => false, 'show_future' => true])); + $this->assertCount(0, $this->getEntries(['show_past' => false, 'show_future' => true])); } #[Test] @@ -353,11 +354,27 @@ public function it_filters_by_status() $this->assertCount(1, $this->getEntries()); // defaults to 'published' $this->assertCount(1, $this->getEntries(['status:is' => 'published'])); - $this->assertCount(3, $this->getEntries(['status:not' => 'published'])); - $this->assertCount(3, $this->getEntries(['status:in' => 'published|draft'])); $this->assertCount(4, $this->getEntries(['status:is' => 'any'])); } + #[Test] + public function it_throws_an_exception_when_filtering_by_status_with_not_operator() + { + $this->expectException(StatusFilterNotSupportedException::class); + $this->expectExceptionMessage('Filtering by status is not supported. Use whereStatus() instead.'); + + $this->getEntries(['status:not' => 'published']); + } + + #[Test] + public function it_throws_an_exception_when_filtering_by_status_with_in_operator() + { + $this->expectException(StatusFilterNotSupportedException::class); + $this->expectExceptionMessage('Filtering by status is not supported. Use whereStatus() instead.'); + + $this->getEntries(['status:in' => 'published|draft']); + } + #[Test] public function it_filters_by_published_boolean() {