diff --git a/src/DonationForms/Actions/ConvertDonationFormBlocksToFieldsApi.php b/src/DonationForms/Actions/ConvertDonationFormBlocksToFieldsApi.php index 7b85700e21..69c2df88c0 100644 --- a/src/DonationForms/Actions/ConvertDonationFormBlocksToFieldsApi.php +++ b/src/DonationForms/Actions/ConvertDonationFormBlocksToFieldsApi.php @@ -2,6 +2,7 @@ namespace Give\DonationForms\Actions; +use Exception; use Give\DonationForms\Repositories\DonationFormRepository; use Give\DonationForms\Rules\AuthenticationRule; use Give\DonationForms\Rules\BillingAddressCityRule; @@ -10,6 +11,7 @@ use Give\DonationForms\Rules\GatewayRule; use Give\DonationForms\Rules\PhoneIntlInputRule; use Give\FormBuilder\BlockModels\DonationAmountBlockModel; +use Give\FormBuilder\BlockTypes\TextBlockType; use Give\Framework\Blocks\BlockCollection; use Give\Framework\Blocks\BlockModel; use Give\Framework\FieldsAPI\Authentication; @@ -129,6 +131,7 @@ protected function convertInnerBlockToNode(BlockModel $block, int $blockIndex) } /** + * @unreleased updated to use TextBlockType * @since 3.19.4 add max rule to company field * @since 3.9.0 Add "givewp/donor-phone" block * @since 3.0.0 @@ -137,6 +140,7 @@ protected function convertInnerBlockToNode(BlockModel $block, int $blockIndex) * @throws NameCollisionException * * @throws EmptyNameException + * @throws Exception */ protected function createNodeFromBlockWithUniqueAttributes(BlockModel $block, int $blockIndex) { @@ -195,15 +199,13 @@ protected function createNodeFromBlockWithUniqueAttributes(BlockModel $block, in case "givewp/company": return Text::make('company')->rules('max:255'); - case "givewp/text": - return Text::make( - $block->hasAttribute('fieldName') ? - $block->getAttribute('fieldName') : - $block->getShortName() . '-' . $blockIndex - )->storeAsDonorMeta( - $block->hasAttribute('storeAsDonorMeta') ? $block->getAttribute('storeAsDonorMeta') : false - )->description($block->getAttribute('description')) - ->defaultValue($block->getAttribute('defaultValue')); + case TextBlockType::name(): + $textBlockType = new TextBlockType($block); + + return Text::make($textBlockType->getFieldName($blockIndex)) + ->storeAsDonorMeta($textBlockType->storeAsDonorMeta) + ->description($textBlockType->description) + ->defaultValue($textBlockType->defaultValue); case "givewp/terms-and-conditions": return $this->createNodeFromConsentBlock($block, $blockIndex) diff --git a/src/FormBuilder/BlockTypes/TextBlockType.php b/src/FormBuilder/BlockTypes/TextBlockType.php index b78b348f38..df41ea534e 100644 --- a/src/FormBuilder/BlockTypes/TextBlockType.php +++ b/src/FormBuilder/BlockTypes/TextBlockType.php @@ -19,4 +19,12 @@ public static function name(): string { return 'givewp/text'; } + + /** + * @unreleased + */ + public function getFieldName(int $blockIndex): string + { + return !empty($this->fieldName) ? $this->fieldName : $this->block->getShortName() . '-' . $blockIndex; + } } diff --git a/src/Framework/Blocks/BlockType.php b/src/Framework/Blocks/BlockType.php index b47122e25c..b95d063400 100644 --- a/src/Framework/Blocks/BlockType.php +++ b/src/Framework/Blocks/BlockType.php @@ -162,14 +162,11 @@ public function isAttributeTypeValid(string $key, $value): bool } /** + * @unreleased updated to always cast value even if null * @since 3.8.0 */ public function castAttributeType(string $key, $value) { - if (is_null($value)) { - return null; - } - $type = $this->getPropertyType($key); switch ($type) { @@ -178,7 +175,7 @@ public function castAttributeType(string $key, $value) case 'string': return (string)($value); case 'bool': - return filter_var($value, FILTER_VALIDATE_BOOLEAN); + return (bool)filter_var($value, FILTER_VALIDATE_BOOLEAN); case 'array': return (array)($value); case 'float': @@ -224,14 +221,13 @@ protected function setDefaultProperties(): array } /** + * @unreleased updated to always set default properties * @since 3.8.0 */ private function fillDefaultProperties(): void { foreach ($this->setDefaultProperties() as $key => $type) { - if ($this->hasAttribute($key)) { - $this->properties[$key] = $type; - } + $this->properties[$key] = $type; } } } diff --git a/tests/Unit/FormBuilder/BlockTypes/TestDonationAmountBlockType.php b/tests/Unit/FormBuilder/BlockTypes/TestDonationAmountBlockType.php index 387db3c01f..2bea4ee19f 100644 --- a/tests/Unit/FormBuilder/BlockTypes/TestDonationAmountBlockType.php +++ b/tests/Unit/FormBuilder/BlockTypes/TestDonationAmountBlockType.php @@ -25,6 +25,7 @@ public function testGetName(): void } /** + * @unreleased updated customAmountMax to expect int value * @since 3.12.0 Update test to use the new levels schema * @since 3.8.0 * @throws Exception @@ -47,7 +48,7 @@ public function testDefaultBlockModelAttributesMatchBlockTypeProperties(): void $this->assertSame(25, $block->setPrice); $this->assertTrue($block->customAmount); $this->assertSame(1, $block->customAmountMin); - $this->assertNull($block->customAmountMax); + $this->assertSame(0, $block->customAmountMax); $this->assertFalse($block->recurringEnabled); $this->assertSame(1, $block->recurringBillingInterval); $this->assertSame(["month"], $block->recurringBillingPeriodOptions); diff --git a/tests/Unit/FormBuilder/BlockTypes/TestTextBlockType.php b/tests/Unit/FormBuilder/BlockTypes/TestTextBlockType.php index 580a5ee75b..65c2290512 100644 --- a/tests/Unit/FormBuilder/BlockTypes/TestTextBlockType.php +++ b/tests/Unit/FormBuilder/BlockTypes/TestTextBlockType.php @@ -3,6 +3,7 @@ namespace Give\Tests\Unit\FormBuilder\BlockTypes; use Exception; +use Give\FormBuilder\BlockTypes\TextBlockType; use Give\Framework\Blocks\BlockModel; use Give\Tests\TestCase; use Give\Tests\TestTraits\RefreshDatabase; @@ -26,7 +27,7 @@ public function testNameShouldMatch(): void ] ); - $blockType = new \Give\FormBuilder\BlockTypes\TextBlockType($blockModel); + $blockType = new TextBlockType($blockModel); $this->assertSame('givewp/text', $blockType::name()); } @@ -67,7 +68,7 @@ public function testAttributesShouldMatchProperties(): void ] ); - $blockType = new \Give\FormBuilder\BlockTypes\TextBlockType($blockModel); + $blockType = new TextBlockType($blockModel); $this->assertSame('Test Label', $blockType->label); $this->assertSame('Test Description', $blockType->description); @@ -94,4 +95,44 @@ public function testAttributesShouldMatchProperties(): void $blockType->conditionalLogic ); } + + /** + * @unreleased + * @throws Exception + */ + public function testGetFieldNameShouldReturnCustomName(): void + { + $blockModel = BlockModel::make( + [ + 'name' => 'givewp/text', + 'attributes' => [ + 'fieldName' => 'custom_field_name', + ] + ] + ); + + $blockType = new TextBlockType($blockModel); + + $this->assertSame('custom_field_name', $blockType->getFieldName(1)); + } + + /** + * @unreleased + * @throws Exception + */ + public function testGetFieldNameShouldReturnNameWithIndex() + { + $blockModel = BlockModel::make( + [ + 'name' => 'givewp/text', + 'attributes' => [ + 'fieldName' => '', + ] + ] + ); + + $blockType = new TextBlockType($blockModel); + + $this->assertSame('text-1', $blockType->getFieldName(1)); + } } diff --git a/tests/Unit/Framework/Blocks/TestBlockType.php b/tests/Unit/Framework/Blocks/TestBlockType.php index f9339eac11..1d88655158 100644 --- a/tests/Unit/Framework/Blocks/TestBlockType.php +++ b/tests/Unit/Framework/Blocks/TestBlockType.php @@ -263,6 +263,45 @@ public static function name(): string $this->assertIsFloat($blockType->intFloatAttribute); } + /** + * @unreleased + */ + public function testShouldCastNullValuesAsIntendedType(): void + { + $blockModel = BlockModel::make([ + 'name' => 'givewp/donation-amount', + 'attributes' => [ + 'floatAttribute' => null, + 'stringAttribute' => null, + 'intAttribute' => null, + 'boolAttribute' => null, + ] + ]); + + $blockType = new class ($blockModel) extends BlockType { + protected $properties = [ + 'floatAttribute' => 'float', + 'stringAttribute' => 'string', + 'intAttribute' => 'int', + 'boolAttribute' => 'bool', + 'extraStringAttribute' => 'string', + 'extraBoolAttribute' => 'bool', + ]; + + public static function name(): string + { + return 'givewp/donation-amount'; + } + }; + + $this->assertIsFloat($blockType->floatAttribute); + $this->assertIsString($blockType->stringAttribute); + $this->assertIsInt($blockType->intAttribute); + $this->assertIsBool($blockType->boolAttribute); + $this->assertIsBool($blockType->extraBoolAttribute); + $this->assertIsString($blockType->extraStringAttribute); + } + /** * @since 3.8.0 *