This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Store backwards compatible form meta (#109)
* fix: Store backwards compatible form meta Co-authored-by: Jon Waldstein
- Loading branch information
Showing
3 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/NextGen/DonationForm/Actions/StoreBackwardsCompatibleFormMeta.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Give\NextGen\DonationForm\Actions; | ||
|
||
use Give\DonationForms\ValueObjects\DonationFormMetaKeys; | ||
use Give\NextGen\DonationForm\Models\DonationForm; | ||
|
||
class StoreBackwardsCompatibleFormMeta | ||
{ | ||
/** | ||
* @unreleased | ||
*/ | ||
public function __invoke(DonationForm $donationForm) | ||
{ | ||
$this->storeDonationLevels($donationForm); | ||
$this->storeDonationGoal($donationForm); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function storeDonationLevels(DonationForm $donationForm) | ||
{ | ||
$amountField = $donationForm->schema()->getNodeByName('amount'); | ||
|
||
if (!$amountField) { | ||
return; | ||
} | ||
|
||
$donationLevels = $amountField->getLevels(); | ||
$donationLevels = array_map(function ($donationLevel, $index) { | ||
return [ | ||
'_give_id' => [ | ||
'level_id' => $index, | ||
], | ||
'_give_amount' => $donationLevel | ||
]; | ||
}, $donationLevels, array_keys($donationLevels)); | ||
|
||
// @todo: Replace with DonationFormMetaKeys::PRICE_OPTION when available. | ||
$this->saveSingleFormMeta($donationForm->id, '_give_price_option', 'multi'); | ||
$this->saveSingleFormMeta($donationForm->id, DonationFormMetaKeys::DONATION_LEVELS, $donationLevels); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function storeDonationGoal(DonationForm $donationForm) | ||
{ | ||
$this->saveSingleFormMeta( | ||
$donationForm->id, | ||
DonationFormMetaKeys::GOAL_OPTION, | ||
$donationForm->settings->enableDonationGoal ? 'enabled' : 'disabled' | ||
); | ||
|
||
$goalType = $donationForm->settings->goalType->getValue(); | ||
$goalType = ($goalType === 'donations') ? 'donation' : $goalType; // @todo Mismatch. Legacy uses "donation" instead of "donations". | ||
$this->saveSingleFormMeta($donationForm->id, '_give_goal_format', $goalType); | ||
|
||
$metaLookup = [ | ||
'donation' => '_give_number_of_donation_goal', | ||
'donors' => '_give_number_of_donor_goal', | ||
'amount' => '_give_set_goal', | ||
]; | ||
|
||
$goalAmount = ('amount' === $goalType) ? give_sanitize_amount_for_db( | ||
$donationForm->settings->goalAmount | ||
) : $donationForm->settings->goalAmount; | ||
$this->saveSingleFormMeta($donationForm->id, $metaLookup[$goalType], $goalAmount); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
protected function saveSingleFormMeta($formId, $metaKey, $metaValue) | ||
{ | ||
if (give()->form_meta->get_meta($formId, $metaKey, true)) { | ||
give()->form_meta->update_meta($formId, $metaKey, $metaValue); | ||
} else { | ||
give()->form_meta->add_meta($formId, $metaKey, $metaValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
tests/Unit/DonationForm/Actions/StoreBackwardsCompatibleFormMetaTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace Give\Tests\Unit\DonationForm\Actions; | ||
|
||
use Give\DonationForms\ValueObjects\DonationFormMetaKeys; | ||
use Give\NextGen\DonationForm\Models\DonationForm; | ||
use Give\NextGen\DonationForm\ValueObjects\GoalType; | ||
use Give\Tests\TestCase; | ||
use Give\Tests\TestTraits\RefreshDatabase; | ||
|
||
class StoreBackwardsCompatibleFormMetaTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function testDonationLevelMetaIsStoredOnInsert() | ||
{ | ||
/** @var DonationForm $donationForm */ | ||
$donationForm = DonationForm::factory()->create(); | ||
|
||
$this->assertEquals('multi', $this->getSingleFormMeta($donationForm->id, '_give_price_option' )); | ||
$this->assertIsArray($this->getSingleFormMeta($donationForm->id, DonationFormMetaKeys::DONATION_LEVELS)); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function testDonationLevelMetaIsStoredOnUpdate() | ||
{ | ||
/** @var DonationForm $donationForm */ | ||
$donationForm = DonationForm::factory()->create(); | ||
give()->form_meta->delete_meta($donationForm->id, '_give_price_option'); | ||
give()->form_meta->delete_meta($donationForm->id, DonationFormMetaKeys::DONATION_LEVELS); | ||
|
||
$donationForm->save(); // Trigger update. | ||
|
||
$this->assertEquals('multi', $this->getSingleFormMeta($donationForm->id, '_give_price_option' )); | ||
$this->assertIsArray($this->getSingleFormMeta($donationForm->id, DonationFormMetaKeys::DONATION_LEVELS)); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function testDonationGoalMetaIsStoredOnInsert() | ||
{ | ||
/** @var DonationForm $donationForm */ | ||
$donationForm = DonationForm::factory()->make(); | ||
$donationForm->settings->enableDonationGoal = true; | ||
$donationForm->settings->goalType = GoalType::AMOUNT(); | ||
$donationForm->settings->goalAmount = 500; | ||
$donationForm->save(); // Trigger created hook. | ||
|
||
$this->assertEquals('enabled', $this->getSingleFormMeta($donationForm->id, DonationFormMetaKeys::GOAL_OPTION )); | ||
$this->assertEquals('amount', $this->getSingleFormMeta($donationForm->id, '_give_goal_format' )); | ||
$this->assertSame('500.000000', $this->getSingleFormMeta($donationForm->id, '_give_set_goal' )); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function testDonationGoalMetaIsStoredOnUpdate() | ||
{ | ||
/** @var DonationForm $donationForm */ | ||
$donationForm = DonationForm::factory()->create(); | ||
give()->form_meta->delete_meta($donationForm->id, DonationFormMetaKeys::GOAL_OPTION); | ||
give()->form_meta->delete_meta($donationForm->id, '_give_goal_format'); | ||
give()->form_meta->delete_meta($donationForm->id, '_give_set_goal'); | ||
|
||
$donationForm->settings->enableDonationGoal = true; | ||
$donationForm->settings->goalType = GoalType::AMOUNT(); | ||
$donationForm->settings->goalAmount = 500; | ||
$donationForm->save(); // Trigger update hook. | ||
|
||
$this->assertEquals('enabled', $this->getSingleFormMeta($donationForm->id, DonationFormMetaKeys::GOAL_OPTION )); | ||
$this->assertEquals('amount', $this->getSingleFormMeta($donationForm->id, '_give_goal_format' )); | ||
$this->assertSame('500.000000', $this->getSingleFormMeta($donationForm->id, '_give_set_goal' )); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function testDonationGoalMetaUsesGoalTypeMetaKeyAmount() | ||
{ | ||
/** @var DonationForm $donationForm */ | ||
$donationForm = DonationForm::factory()->make(); | ||
$donationForm->settings->goalType = GoalType::AMOUNT(); | ||
$donationForm->save(); | ||
|
||
$this->assertNotNull($this->getSingleFormMeta($donationForm->id, '_give_set_goal' )); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function testDonationGoalMetaUsesGoalTypeMetaKeyDonations() | ||
{ | ||
/** @var DonationForm $donationForm */ | ||
$donationForm = DonationForm::factory()->make(); | ||
$donationForm->settings->goalType = GoalType::DONATIONS(); | ||
$donationForm->save(); | ||
|
||
$this->assertNotNull($this->getSingleFormMeta($donationForm->id, '_give_number_of_donation_goal' )); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function testDonationGoalMetaUsesGoalTypeMetaKeyDonors() | ||
{ | ||
/** @var DonationForm $donationForm */ | ||
$donationForm = DonationForm::factory()->make(); | ||
$donationForm->settings->goalType = GoalType::DONORS(); | ||
$donationForm->save(); | ||
|
||
$this->assertNotNull($this->getSingleFormMeta($donationForm->id, '_give_number_of_donor_goal' )); | ||
} | ||
|
||
protected function getSingleFormMeta($formId, $metaKey) | ||
{ | ||
return give()->form_meta->get_meta($formId, $metaKey, $single = true); | ||
} | ||
} |