From d474d6723e7b3a3c19e7df0d1e2cfb8651a97f2d Mon Sep 17 00:00:00 2001 From: John Koster Date: Fri, 3 May 2024 08:38:10 -0500 Subject: [PATCH] [5.x] Prevent falsey values from returning blueprint defaults (#9990) --- src/Fields/Value.php | 4 ++-- tests/Fields/ValueTest.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Fields/Value.php b/src/Fields/Value.php index ca4f86c8fc..6dd929dfde 100644 --- a/src/Fields/Value.php +++ b/src/Fields/Value.php @@ -76,8 +76,8 @@ public function value() return $raw; } - if (! $raw && ($default = $this->fieldtype->field()?->defaultValue())) { - $raw = $default; + if ($raw === null) { + $raw = $this->fieldtype->field()?->defaultValue() ?? null; } $value = $this->shallow diff --git a/tests/Fields/ValueTest.php b/tests/Fields/ValueTest.php index a0f0bc0e8b..6abc4bf999 100644 --- a/tests/Fields/ValueTest.php +++ b/tests/Fields/ValueTest.php @@ -178,6 +178,42 @@ public function augment($value) $this->assertEquals('foo!', $value->value()); }); } + + /** @test */ + public function it_does_not_use_the_default_when_returning_falsey_values() + { + $fieldtype = new class extends Fieldtype + { + public function augment($value) + { + return $value; + } + }; + + $fieldtype->setField(new Field('the_handle', ['default' => true])); + + tap(new Value(false, null, $fieldtype), function ($value) { + $this->assertSame(false, $value->value()); + }); + } + + /** @test */ + public function falsey_values_can_be_used_as_the_default() + { + $fieldtype = new class extends Fieldtype + { + public function augment($value) + { + return $value; + } + }; + + $fieldtype->setField(new Field('the_handle', ['default' => false])); + + tap(new Value(null, null, $fieldtype), function ($value) { + $this->assertSame(false, $value->value()); + }); + } } class DummyAugmentable implements \Statamic\Contracts\Data\Augmentable