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

Refactor: update text block conversion to use text block type #7697

Merged
merged 5 commits into from
Feb 7, 2025
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
20 changes: 11 additions & 9 deletions src/DonationForms/Actions/ConvertDonationFormBlocksToFieldsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -137,6 +140,7 @@ protected function convertInnerBlockToNode(BlockModel $block, int $blockIndex)
* @throws NameCollisionException
*
* @throws EmptyNameException
* @throws Exception
*/
protected function createNodeFromBlockWithUniqueAttributes(BlockModel $block, int $blockIndex)
{
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/FormBuilder/BlockTypes/TextBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 4 additions & 8 deletions src/Framework/Blocks/BlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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':
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
45 changes: 43 additions & 2 deletions tests/Unit/FormBuilder/BlockTypes/TestTextBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}
39 changes: 39 additions & 0 deletions tests/Unit/Framework/Blocks/TestBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Loading