Skip to content

Commit

Permalink
Allow tag conditions to query on sub fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Nov 24, 2023
1 parent 791d157 commit a41abae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Tags/Concerns/QueriesConditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ protected function queryCondition($query, $field, $condition, $value)
{
$regexOperator = $value ? 'regexp' : 'not regexp';

$field = str_replace('.', '->', $field);

switch ($condition) {
case 'is':
case 'equals':
Expand Down
33 changes: 33 additions & 0 deletions tests/Tags/Concerns/QueriesConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,39 @@ public function it_filters_by_is_after_or_before_date_conditions()
$this->assertCount(1, $this->getEntries(['show_future' => true, 'date:is_after' => $time]));
}

/** @test */
public function it_filters_by_is_after_or_before_date_range_conditions()
{
$this->collection->save();
$blueprint = Blueprint::makeFromFields(['date' => ['type' => 'date', 'mode' => 'range', 'time_enabled' => true, 'time_seconds_enabled' => true]])->setHandle('test');
Blueprint::shouldReceive('in')->with('collections/test')->once()->andReturn(collect([$blueprint]));

Carbon::setTestNow(Carbon::parse('2019-03-10 13:00'));

$this->makeEntry('a')->data(['date_field' => ['start' => '2019-03-09', 'end' => '2019-03-10']])->save(); // definitely in past
$this->makeEntry('b')->data(['date_field' => ['start' => '2019-03-10', 'end' => '2019-03-11']])->save(); // today
$this->makeEntry('c')->data(['date_field' => ['start' => '2019-03-11', 'end' => '2019-03-18']])->save(); // today, but before "now"
$this->makeEntry('e')->data(['date_field' => ['start' => '2019-03-11', 'end' => '2019-03-16']])->save(); // today, and also "now"
$this->makeEntry('f')->data(['date_field' => ['start' => '2019-03-12', 'end' => '2019-03-14']])->save(); // today, but after "now"
$this->makeEntry('g')->data(['date_field' => ['start' => '2019-03-11', 'end' => '2019-03-12']])->save(); // definitely in future

$this->assertCount(6, $this->getEntries([]));

$this->assertCount(2, $this->getEntries(['date_field.start:is_before' => true]));
$this->assertCount(2, $this->getEntries(['date_field.start:is_past' => true]));
$this->assertCount(2, $this->getEntries(['date_field.start:is_before' => 'today']));
$this->assertCount(2, $this->getEntries(['date_field.start:is_past' => 'today']));
$this->assertCount(4, $this->getEntries(['date_field.start:is_before' => false]));
$this->assertCount(4, $this->getEntries(['date_field.start:is_past' => false]));

$this->assertCount(4, $this->getEntries(['date_field.start:is_after' => true]));
$this->assertCount(4, $this->getEntries(['date_field.start:is_future' => true]));
$this->assertCount(4, $this->getEntries(['date_field.start:is_after' => 'today']));
$this->assertCount(4, $this->getEntries(['date_field.start:is_future' => 'today']));
$this->assertCount(2, $this->getEntries(['date_field.start:is_after' => false]));
$this->assertCount(2, $this->getEntries(['date_field.start:is_future' => false]));
}

/** @test */
public function it_filters_by_is_alpha_condition()
{
Expand Down

0 comments on commit a41abae

Please sign in to comment.