-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add migration step for Currency Switcher (#7397)
- Loading branch information
1 parent
7da559b
commit b31096b
Showing
5 changed files
with
369 additions
and
30 deletions.
There are no files selected for viewing
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
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
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,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
141
tests/Feature/FormMigration/Steps/TestCurrencySwitcher.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,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; | ||
} | ||
} |
Oops, something went wrong.