Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Commit

Permalink
Fix: Store backwards compatible form meta (#109)
Browse files Browse the repository at this point in the history
* fix: Store backwards compatible form meta

Co-authored-by: Jon Waldstein
  • Loading branch information
kjohnson authored Jan 9, 2023
1 parent 8c1e258 commit c4cbe83
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
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);
}
}
}
4 changes: 4 additions & 0 deletions src/NextGen/DonationForm/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Give\NextGen\DonationForm;

use Give\Helpers\Hooks;
use Give\NextGen\DonationForm\Actions\StoreBackwardsCompatibleFormMeta;
use Give\NextGen\DonationForm\Blocks\DonationFormBlock\Block as DonationFormBlock;
use Give\NextGen\DonationForm\Routes\DonateRoute;
use Give\NextGen\DonationForm\Routes\DonationFormPreviewRoute;
Expand Down Expand Up @@ -31,5 +32,8 @@ public function boot()
Hooks::addAction('template_redirect', DonateRoute::class);
Hooks::addAction('template_redirect', DonationFormViewRoute::class);
Hooks::addAction('template_redirect', DonationFormPreviewRoute::class);

Hooks::addAction('givewp_donation_form_created', StoreBackwardsCompatibleFormMeta::class);
Hooks::addAction('givewp_donation_form_updated', StoreBackwardsCompatibleFormMeta::class);
}
}
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);
}
}

0 comments on commit c4cbe83

Please sign in to comment.