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: Change v2 to v3 migration to include level descriptions #7362

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
17 changes: 15 additions & 2 deletions src/FormMigration/Steps/DonationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Give\FormMigration\Steps;

use Give\FormMigration\Contracts\FormMigrationStep;
use Give\Framework\Blocks\BlockModel;

class DonationOptions extends FormMigrationStep
{
public function process()
{
/** @var BlockModel $amountField */
$amountField = $this->fieldBlocks->findByName('givewp/donation-amount');

$priceOption = $this->getMetaV2('_give_price_option');
Expand All @@ -20,9 +22,20 @@ public function process()
if('multi' === $priceOption) {
// @note $formV2->levels only returns a single level
$donationLevels = $this->getMetaV2('_give_donation_levels');
// @note No corresponding setting in v3 for `_give_text` for Donation Levels.

$isDescriptionEnabled = false;
$amountField->setAttribute('levels',
array_map([$this, 'roundAmount'], wp_list_pluck($donationLevels, '_give_amount')));
array_map(function ($donationLevel) use (&$isDescriptionEnabled) {
$isDescriptionEnabled = $isDescriptionEnabled || ! empty($donationLevel['_give_text']);

return [
'value' => $this->roundAmount($donationLevel['_give_amount']),
'label' => $donationLevel['_give_text'],
'checked' => isset($donationLevel['_give_default']) && $donationLevel['_give_default'] === 'default',
];
}, $donationLevels)
);
$amountField->setAttribute('descriptionsEnabled', $isDescriptionEnabled);
}

$isCustomAmountEnabled = $this->formV2->isCustomAmountOptionEnabled();
Expand Down
25 changes: 24 additions & 1 deletion tests/Feature/FormMigration/Steps/TestDonationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testProcessShouldUpdateDonationAmountBlockAttributes(): void
}

/**
* @unreleased Updated test to include donation levels with descriptions
* @since 3.4.0
*/
public function testProcessShouldUpdateDonationAmountBlockAttributesWithDonationLevels(): void
Expand All @@ -67,7 +68,29 @@ public function testProcessShouldUpdateDonationAmountBlockAttributesWithDonation

$block = $payload->formV3->blocks->findByName('givewp/donation-amount');

$this->assertSame([10.00, 25.00, 50.00, 100.00], $block->getAttribute('levels'));
$expectedLevels = [
[
'value' => 10.00,
'label' => 'Small Gift',
'checked' => false,
],
[
'value' => 25.00,
'label' => 'Mid-size Gift',
'checked' => true,
],
[
'value' => 50.00,
'label' => 'Large Gift',
'checked' => false,
],
[
'value' => 100.00,
'label' => 'Big Gift',
'checked' => false,
],
];
$this->assertSame($expectedLevels, $block->getAttribute('levels'));
$this->assertTrue($block->getAttribute('customAmount'));
$this->assertSame((float)$meta['_give_custom_amount_range_minimum'], $block->getAttribute('customAmountMin'));
$this->assertSame((float)$meta['_give_custom_amount_range_maximum'], $block->getAttribute('customAmountMax'));
Expand Down
Loading