Skip to content
Open
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 src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ public function getQueryableValue(string $field)
return $value;
}

return $field->fieldtype()->toQueryableValue($value);
return $field->toQueryableValue($value);
}

private function queryableMethods(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public function getQueryableValue(string $field)
return $value;
}

return $field->fieldtype()->toQueryableValue($value);
return $field->toQueryableValue($value);
}

private function queryableMethods(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ public function getQueryableValue(string $field)
return $value;
}

return $field->fieldtype()->toQueryableValue($value);
return $field->toQueryableValue($value);
}

private function queryableMethods(): array
Expand Down
9 changes: 9 additions & 0 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,15 @@ public function defaultValue()
return $this->config['default'] ?? $this->fieldtype()->defaultValue();
}

public function toQueryableValue($value)
{
if ($value === null && ! $this->hasComputedDefault()) {
$value = $this->defaultValue();
}

return $this->fieldtype()->toQueryableValue($value);
}

public function hasComputedDefault(): bool
{
if (! isset($this->config['default'])) {
Expand Down
4 changes: 4 additions & 0 deletions src/Fieldtypes/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ public function toGqlType()

public function toQueryableValue($value)
{
if ($value === 'now') {
return null;
}

return $this->augment($value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public function getQueryableValue(string $field)
return $value;
}

return $field->fieldtype()->toQueryableValue($value);
return $field->toQueryableValue($value);
}

private function queryableMethods(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public function getQueryableValue(string $field)
return $value;
}

return $field->fieldtype()->toQueryableValue($value);
return $field->toQueryableValue($value);
}

private function queryableMethods(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Globals/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function getQueryableValue(string $field)
return $value;
}

return $field->fieldtype()->toQueryableValue($value);
return $field->toQueryableValue($value);
}

private function queryableMethods(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Taxonomies/LocalizedTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public function getQueryableValue(string $field)
return $value;
}

return $field->fieldtype()->toQueryableValue($value);
return $field->toQueryableValue($value);
}

private function queryableMethods(): array
Expand Down
15 changes: 15 additions & 0 deletions tests/Assets/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3075,4 +3075,19 @@ public function it_clones_internal_collections()
$this->assertEquals('A', $asset->getSupplement('bar'));
$this->assertEquals('B', $clone->getSupplement('bar'));
}

#[Test]
public function it_uses_a_fields_default_as_the_queryable_value()
{
$blueprint = Facades\Blueprint::makeFromFields(['foo' => ['type' => 'toggle', 'default' => true]]);
BlueprintRepository::shouldReceive('find')->with('assets/test_container')->andReturn($blueprint);

$asset = (new Asset)->path('test.txt')->container($this->container);

$this->assertTrue($asset->getQueryableValue('foo'));

$asset->set('foo', false);

$this->assertFalse($asset->getQueryableValue('foo'));
}
}
15 changes: 15 additions & 0 deletions tests/Auth/UserContractTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,4 +821,19 @@ public function __construct($handle)

return $class;
}

#[Test]
public function it_uses_a_fields_default_as_the_queryable_value()
{
$blueprint = Blueprint::makeFromFields(['my_toggle' => ['type' => 'toggle', 'default' => true]]);
BlueprintRepository::shouldReceive('find')->with('user')->andReturn($blueprint);

$user = $this->user();

$this->assertTrue($user->getQueryableValue('my_toggle'));

$user->set('my_toggle', false);

$this->assertFalse($user->getQueryableValue('my_toggle'));
}
}
15 changes: 15 additions & 0 deletions tests/Data/Globals/VariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,19 @@ public function it_can_be_found_via_the_data_repository_using_its_reference()
$this->assertEquals($variables->id(), $found->id());
$this->assertEquals('bar', $found->get('foo'));
}

#[Test]
public function it_uses_a_fields_default_as_the_queryable_value()
{
$blueprint = Blueprint::makeFromFields(['foo' => ['type' => 'toggle', 'default' => true]]);
BlueprintRepository::shouldReceive('find')->with('globals.settings')->andReturn($blueprint);

$variables = GlobalSet::make('settings')->in('en');

$this->assertTrue($variables->getQueryableValue('foo'));

$variables->set('foo', false);

$this->assertFalse($variables->getQueryableValue('foo'));
}
}
16 changes: 16 additions & 0 deletions tests/Data/Taxonomies/LocalizedTermTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,20 @@ public function it_gets_the_site_handle_as_the_queryable_site_value()
$this->assertSame('en', (new LocalizedTerm(new Term, 'en'))->getQueryableValue('site'));
$this->assertSame('removed', (new LocalizedTerm(new Term, 'removed'))->getQueryableValue('site'));
}

#[Test]
public function it_uses_a_fields_default_as_the_queryable_value()
{
$blueprint = Blueprint::makeFromFields(['foo' => ['type' => 'toggle', 'default' => true]]);
BlueprintRepository::shouldReceive('in')->with('taxonomies/tags')->andReturn(collect(['tags' => $blueprint]));
Taxonomy::make('tags')->save();

$localized = (new Term)->taxonomy('tags')->in('en');

$this->assertTrue($localized->getQueryableValue('foo'));

$localized->set('foo', false);

$this->assertFalse($localized->getQueryableValue('foo'));
}
}
28 changes: 28 additions & 0 deletions tests/Fields/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,4 +748,32 @@ public function it_gets_and_sets_the_form()
$this->assertEquals($field, $return);
$this->assertEquals($form, $field->form());
}

#[Test]
public function it_uses_the_default_when_converting_a_null_value_to_a_queryable_value()
{
$field = new Field('test', ['type' => 'toggle', 'default' => true]);

$this->assertTrue($field->toQueryableValue(null));
$this->assertTrue($field->toQueryableValue(true));
$this->assertFalse($field->toQueryableValue(false));
}

#[Test]
public function it_converts_a_null_value_to_a_queryable_value_when_there_is_no_default()
{
$this->assertFalse((new Field('test', ['type' => 'toggle']))->toQueryableValue(null));
$this->assertNull((new Field('test', ['type' => 'text']))->toQueryableValue(null));
$this->assertEquals('foo', (new Field('test', ['type' => 'text', 'default' => 'bar']))->toQueryableValue('foo'));
}

#[Test]
public function it_does_not_use_a_computed_default_when_converting_to_a_queryable_value()
{
Fields::computedDefault('computed-value', fn () => 'computed defined default');

$field = new Field('test', ['type' => 'text', 'default' => 'computed:computed-value']);

$this->assertNull($field->toQueryableValue(null));
}
}
9 changes: 9 additions & 0 deletions tests/Fieldtypes/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,15 @@ public function it_includes_timezone_in_config_field_items()
$this->assertEquals('timezones', $dateTimeSection['fields']['timezone']['dictionary']);
}

#[Test]
public function it_does_not_use_a_default_of_now_as_the_queryable_value()
{
$field = new Field('test', ['type' => 'date', 'default' => 'now']);

$this->assertNull($field->toQueryableValue(null));
$this->assertEquals('2012-01-04', $field->toQueryableValue('2012-01-04')->format('Y-m-d'));
}

public function fieldtype($config = [])
{
$field = new Field('test', array_replace([
Expand Down
16 changes: 16 additions & 0 deletions tests/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Statamic\Events\FormDeleting;
use Statamic\Events\FormSaved;
use Statamic\Events\FormSaving;
use Statamic\Facades;
use Statamic\Facades\File;
use Statamic\Facades\Form;
use Statamic\Fields\Blueprint;
Expand Down Expand Up @@ -291,4 +292,19 @@ public function it_clones_internal_collections()
$this->assertEquals('A', $form->getSupplement('bar'));
$this->assertEquals('B', $clone->getSupplement('bar'));
}

#[Test]
public function it_uses_a_fields_default_as_the_queryable_value()
{
$blueprint = Facades\Blueprint::makeFromFields(['foo' => ['type' => 'toggle', 'default' => true]]);
Facades\Blueprint::shouldReceive('find')->with('forms.test')->andReturn($blueprint);

$form = Form::make('test');

$this->assertTrue($form->getQueryableValue('foo'));

$form->set('foo', false);

$this->assertFalse($form->getQueryableValue('foo'));
}
}
15 changes: 15 additions & 0 deletions tests/Forms/SubmissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,19 @@ public function it_clones_internal_collections()
$this->assertEquals('A', $submission->getSupplement('bar'));
$this->assertEquals('B', $clone->getSupplement('bar'));
}

#[Test]
public function it_uses_a_fields_default_as_the_queryable_value()
{
$blueprint = Blueprint::makeFromFields(['foo' => ['type' => 'toggle', 'default' => true]]);
Blueprint::shouldReceive('find')->with('forms.test')->andReturn($blueprint);

$submission = Form::make('test')->makeSubmission();

$this->assertTrue($submission->getQueryableValue('foo'));

$submission->set('foo', false);

$this->assertFalse($submission->getQueryableValue('foo'));
}
}
14 changes: 14 additions & 0 deletions tests/Tags/Collection/EntriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ public function it_limits_entries_with_offset_using_value_objects()
);
}

#[Test]
public function it_filters_by_a_fields_default_value()
{
$blueprint = Blueprint::makeFromFields(['my_toggle' => ['type' => 'toggle', 'default' => true]])->setHandle('test');
Blueprint::shouldReceive('in')->with('collections/test')->andReturn(collect([$blueprint]));

$this->makeEntry('unsaved')->save();
$this->makeEntry('explicit-true')->set('my_toggle', true)->save();
$this->makeEntry('explicit-false')->set('my_toggle', false)->save();

$this->assertEqualsCanonicalizing(['unsaved', 'explicit-true'], $this->getEntryIds(['my_toggle:is' => 'true']));
$this->assertEquals(['explicit-false'], $this->getEntryIds(['my_toggle:is' => 'false']));
}

#[Test]
public function it_filters_by_future_and_past()
{
Expand Down
Loading