Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix querying statuses #279

Merged
merged 11 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"require": {
"php": "^8.1",
"statamic/cms": "v5.0.0-alpha.6"
"statamic/cms": "dev-master"
},
"require-dev": {
"doctrine/dbal": "^3.3",
Expand Down
54 changes: 53 additions & 1 deletion src/Entries/EntryQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Query\EloquentQueryBuilder;
use Statamic\Stache\Query\QueriesEntryStatus;
use Statamic\Stache\Query\QueriesTaxonomizedEntries;

class EntryQueryBuilder extends EloquentQueryBuilder implements QueryBuilder
{
use QueriesTaxonomizedEntries;
use QueriesEntryStatus,
QueriesTaxonomizedEntries;

private $selectedQueryColumns;

private const STATUSES = ['published', 'draft', 'scheduled', 'expired'];

const COLUMNS = [
'id', 'site', 'origin_id', 'published', 'slug', 'uri',
'date', 'collection', 'created_at', 'updated_at', 'order', 'blueprint',
Expand Down Expand Up @@ -161,4 +165,52 @@ public function with($relations, $callback = null)
{
return $this;
}

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);
}

return parent::where($column, $operator, $value, $boolean);
}

public function whereIn($column, $values, $boolean = 'and')
{
if ($column === 'status') {
trigger_error('Filtering by status is deprecated. Use whereStatus() instead.', E_USER_DEPRECATED);
}

return parent::whereIn($column, $values, $boolean);
}

private function ensureCollectionsAreQueriedForStatusQuery(): void
{
$wheres = collect($this->builder->getQuery()->wheres);

// If there are where clauses on the collection column, it means the user has explicitly
// queried for them. In that case, we'll use them and skip the auto-detection.
if ($wheres->where('column', 'collection')->isNotEmpty()) {
return;
}

// Otherwise, we'll detect them by looking at where clauses targeting the "id" column.
$ids = $wheres->where('column', 'id')->flatMap(fn ($where) => $where['values'] ?? [$where['value']]);

// If no IDs were queried, fall back to all collections.
$ids->isEmpty()
? $this->whereIn('collection', Collection::handles())
: $this->whereIn('collection', app(static::class)->whereIn('id', $ids)->pluck('collection')->unique()->values());
}

private function getCollectionsForStatusQuery(): \Illuminate\Support\Collection
{
// Since we have to add nested queries for each collection, we only want to add clauses for the
// applicable collections. By this point, there should be where clauses on the collection column.

return collect($this->builder->getQuery()->wheres)
->where('column', 'collection')
->flatMap(fn ($where) => $where['values'] ?? [$where['value']])
->map(fn ($handle) => Collection::find($handle));
}
}
92 changes: 92 additions & 0 deletions tests/Data/Entries/EntryQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,4 +794,96 @@ public function entries_can_be_ordered_by_an_date_json_field()
$this->assertCount(3, $entries);
$this->assertEquals(['Post 2', 'Post 1', 'Post 3'], $entries->map->title->all());
}

/** @test */
public function filtering_using_where_status_column_writes_deprecation_log()
{
$this->withoutDeprecationHandling();
$this->expectException(\ErrorException::class);
$this->expectExceptionMessage('Filtering by status is deprecated. Use whereStatus() instead.');

$this->createDummyCollectionAndEntries();

Entry::query()->where('collection', 'posts')->where('status', 'published')->get();
}

/** @test */
public function filtering_using_whereIn_status_column_writes_deprecation_log()
{
$this->withoutDeprecationHandling();
$this->expectException(\ErrorException::class);
$this->expectExceptionMessage('Filtering by status is deprecated. Use whereStatus() instead.');

$this->createDummyCollectionAndEntries();

Entry::query()->where('collection', 'posts')->whereIn('status', ['published'])->get();
}

/** @test */
public function filtering_by_unexpected_status_throws_exception()
{
$this->expectExceptionMessage('Invalid status [foo]');

Entry::query()->whereStatus('foo')->get();
}

/**
* @test
*
* @dataProvider filterByStatusProvider
*/
public function it_filters_by_status($status, $expected)
{
Collection::make('pages')->dated(false)->save();
EntryFactory::collection('pages')->slug('page')->published(true)->create();
EntryFactory::collection('pages')->slug('page-draft')->published(false)->create();

Collection::make('blog')->dated(true)->futureDateBehavior('private')->pastDateBehavior('public')->save();
EntryFactory::collection('blog')->slug('blog-future')->published(true)->date(now()->addDay())->create();
EntryFactory::collection('blog')->slug('blog-future-draft')->published(false)->date(now()->addDay())->create();
EntryFactory::collection('blog')->slug('blog-past')->published(true)->date(now()->subDay())->create();
EntryFactory::collection('blog')->slug('blog-past-draft')->published(false)->date(now()->subDay())->create();

Collection::make('events')->dated(true)->futureDateBehavior('public')->pastDateBehavior('private')->save();
EntryFactory::collection('events')->slug('event-future')->published(true)->date(now()->addDay())->create();
EntryFactory::collection('events')->slug('event-future-draft')->published(false)->date(now()->addDay())->create();
EntryFactory::collection('events')->slug('event-past')->published(true)->date(now()->subDay())->create();
EntryFactory::collection('events')->slug('event-past-draft')->published(false)->date(now()->subDay())->create();

Collection::make('calendar')->dated(true)->futureDateBehavior('public')->pastDateBehavior('public')->save();
EntryFactory::collection('calendar')->slug('calendar-future')->published(true)->date(now()->addDay())->create();
EntryFactory::collection('calendar')->slug('calendar-future-draft')->published(false)->date(now()->addDay())->create();
EntryFactory::collection('calendar')->slug('calendar-past')->published(true)->date(now()->subDay())->create();
EntryFactory::collection('calendar')->slug('calendar-past-draft')->published(false)->date(now()->subDay())->create();

$this->assertEquals($expected, Entry::query()->whereStatus($status)->get()->map->slug()->sort()->all());
}

public static function filterByStatusProvider()
{
return [
'draft' => ['draft', [
'blog-future-draft',
'blog-past-draft',
'calendar-future-draft',
'calendar-past-draft',
'event-future-draft',
'event-past-draft',
'page-draft',
]],
'published' => ['published', [
'blog-past',
'calendar-future',
'calendar-past',
'event-future',
'page',
]],
'scheduled' => ['scheduled', [
'blog-future',
]],
'expired' => ['expired', [
'event-past',
]],
];
}
}
5 changes: 4 additions & 1 deletion tests/Factories/EntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ public function origin($origin)

public function make()
{
$collection = $this->createCollection();

$entry = Entry::make()
->locale($this->locale)
->collection($this->createCollection())
->collection($collection)
->blueprint($collection->entryBlueprint())
->slug($this->slug)
->data($this->data)
->origin($this->origin)
Expand Down
Loading