Skip to content

Commit

Permalink
Feature: Add migration step for Currency Switcher (#7397)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloiankoski authored Jun 21, 2024
1 parent 7da559b commit b31096b
Show file tree
Hide file tree
Showing 5 changed files with 369 additions and 30 deletions.
40 changes: 40 additions & 0 deletions src/FormMigration/FormMetaDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,4 +982,44 @@ public function getActiveCampaignTags(): array
return ! empty($this->getMeta('give_activecampaign_tags')) ?
$this->getMeta('give_activecampaign_tags') : $defaultMeta;
}

/**
* @unreleased
*/
public function getCurrencySwitcherStatus(): string
{
return $this->getMeta('cs_status', 'global');
}

/**
* @unreleased
*/
public function getCurrencySwitcherMessage(): string
{
return $this->getMeta(
'cs_message',
sprintf(
__('The current exchange rate is 1.00 %1$s equals %2$s %3$s.', 'give-currency-switcher'),
'{base_currency}',
'{new_currency_rate}',
'{new_currency}'
)
);
}

/**
* @unreleased
*/
public function getCurrencySwitcherDefaultCurrency(): string
{
return $this->getMeta('give_cs_default_currency', '');
}

/**
* @unreleased
*/
public function getCurrencySwitcherSupportedCurrencies(): array
{
return (array)$this->getMeta('cs_supported_currency', []);
}
}
1 change: 1 addition & 0 deletions src/FormMigration/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function register()
Steps\ConvertKit::class,
Steps\ActiveCampaign::class,
Steps\DoubleTheDonation::class,
Steps\CurrencySwitcher::class,
]);
});
}
Expand Down
42 changes: 42 additions & 0 deletions src/FormMigration/Steps/CurrencySwitcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Give\FormMigration\Steps;

use Give\FormMigration\Contracts\FormMigrationStep;

/**
* @unreleased
*/
class CurrencySwitcher extends FormMigrationStep
{
/**
* @unreleased
*/
public function process()
{
$status = $this->formV2->getCurrencySwitcherStatus();
if (!$status) {
return;
}

$currencySwitcherSettings = [];
$currencySwitcherSettings['enable'] = $status;

$message = $this->formV2->getCurrencySwitcherMessage();
if ($message) {
$currencySwitcherSettings['message'] = $message;
}

$defaultCurrency = $this->formV2->getCurrencySwitcherDefaultCurrency();
if ($defaultCurrency) {
$currencySwitcherSettings['defaultCurrency'] = $defaultCurrency;
}

$supportedCurrencies = $this->formV2->getCurrencySwitcherSupportedCurrencies();
if ($supportedCurrencies) {
$currencySwitcherSettings['supportedCurrencies'] = $supportedCurrencies;
}

$this->formV3->settings->currencySwitcherSettings = $currencySwitcherSettings;
}
}
141 changes: 141 additions & 0 deletions tests/Feature/FormMigration/Steps/TestCurrencySwitcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Feature\FormMigration\Steps;

use Give\DonationForms\Models\DonationForm;
use Give\DonationForms\V2\Models\DonationForm as V2DonationForm;
use Give\FormMigration\DataTransferObjects\FormMigrationPayload;
use Give\FormMigration\StepProcessor;
use Give\FormMigration\Steps\CurrencySwitcher;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;

class TestCurrencySwitcher extends TestCase
{
use RefreshDatabase;
use LegacyDonationFormAdapter;

/**
* @unreleased
*/
public function testFormWithoutCurrencySwitcherSettingsMigratesUsingGlobalSettings(): void
{
// Arrange
$v2Form = $this->setUpDonationForm();

// Act
$v3Form = $this->migrateForm($v2Form);

// Assert
$form = DonationForm::find($v3Form->id);
$this->assertIsArray($form->settings->currencySwitcherSettings);
$this->assertArrayHasKey('enable', $form->settings->currencySwitcherSettings);
$this->assertEquals('global', $form->settings->currencySwitcherSettings['enable']);
}

/**
* @unreleased
*/
public function testFormConfiguredToUseGlobalCurrencySwitcherSettingsIsMigrated(): void
{
// Arrange
$attributes = [
'cs_status' => 'global',
];
$v2Form = $this->setUpDonationForm($attributes);

// Act
$v3Form = $this->migrateForm($v2Form);

// Assert
$form = DonationForm::find($v3Form->id);
$this->assertIsArray($form->settings->currencySwitcherSettings);
$this->assertArrayHasKey('enable', $form->settings->currencySwitcherSettings);
$this->assertEquals('global', $form->settings->currencySwitcherSettings['enable']);
}

/**
* @unreleased
*/
public function testFormConfiguredToDisableCurrencySwitcherIsMigrated(): void
{
// Arrange
$attributes = [
'cs_status' => 'disabled',
];
$v2Form = $this->setUpDonationForm($attributes);

// Act
$v3Form = $this->migrateForm($v2Form);

// Assert
$form = DonationForm::find($v3Form->id);
$this->assertIsArray($form->settings->currencySwitcherSettings);
$this->assertArrayHasKey('enable', $form->settings->currencySwitcherSettings);
$this->assertEquals('disabled', $form->settings->currencySwitcherSettings['enable']);
}

/**
* @unreleased
*/
public function testFormConfiguredToUseLocalCurrencySwitcherIsMigrated(): void
{
// Arrange
$attributes = [
'cs_status' => 'enabled',
'cs_message' => 'Testing message',
'give_cs_default_currency' => 'USD',
'cs_supported_currency' => ['USD', 'EUR'],
];
$v2Form = $this->setUpDonationForm($attributes);

// Act
$v3Form = $this->migrateForm($v2Form);

// Assert
$form = DonationForm::find($v3Form->id);
$settings = $form->settings->currencySwitcherSettings;

$this->assertArrayHasKey('enable', $settings);
$this->assertEquals('enabled', $settings['enable']);
$this->assertArrayHasKey('message', $settings);
$this->assertEquals('Testing message', $settings['message']);
$this->assertArrayHasKey('defaultCurrency', $settings);
$this->assertEquals('USD', $settings['defaultCurrency']);
$this->assertArrayHasKey('supportedCurrencies', $settings);
$this->assertEquals(['USD', 'EUR'], $settings['supportedCurrencies']);
}

/**
* Sets up and returns a v2 donation form configured with the
* given attributes being set to the Currency Switcher settings.
*
* @unreleased
*
* @throws Exception
*/
private function setUpDonationForm(array $attributes = []): V2DonationForm
{
$form = $this->createSimpleDonationForm();

foreach ($attributes as $key => $value) {
give_update_meta($form->id, $key, $value);
}

return $form;
}

/**
* @unreleased
*/
private function migrateForm(V2DonationForm $form): DonationForm
{
$payload = FormMigrationPayload::fromFormV2($form);
$processor = new StepProcessor($payload);
$processor(new CurrencySwitcher($payload));
$payload->formV3->save();

return $payload->formV3;
}
}
Loading

0 comments on commit b31096b

Please sign in to comment.