From 5f87590631efb8ae44c27538b20f4c887760fb19 Mon Sep 17 00:00:00 2001 From: alaca Date: Tue, 29 Oct 2024 09:18:02 +0100 Subject: [PATCH 1/3] initial commit --- .../Controllers/MigrationController.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/FormMigration/Controllers/MigrationController.php b/src/FormMigration/Controllers/MigrationController.php index 1fc45cf3ac..a51e20b5bb 100644 --- a/src/FormMigration/Controllers/MigrationController.php +++ b/src/FormMigration/Controllers/MigrationController.php @@ -2,6 +2,7 @@ namespace Give\FormMigration\Controllers; +use Give\Campaigns\Repositories\CampaignRepository; use Give\DonationForms\V2\Models\DonationForm; use Give\FormMigration\Concerns\Blocks\BlockDifference; use Give\FormMigration\DataTransferObjects\FormMigrationPayload; @@ -50,6 +51,17 @@ public function __invoke(DonationForm $formV2) ->process($payload) ->finally(function(FormMigrationPayload $payload) { $payload->formV3->save(); + + $campaignRepository = give(CampaignRepository::class); + + // Associate upgraded form to a campaign + if ($campaign = $campaignRepository->getByFormId($payload->formV2->id)) { + $defaultForm = $campaign->defaultForm(); + $isDefaultForm = $defaultForm && $defaultForm->id == $payload->formV2->id; + + $campaignRepository->addCampaignForm($campaign, $payload->formV3->id, $isDefaultForm); + } + Log::info(esc_html__('Form migrated from v2 to v3.', 'give'), $this->debugContext); }); From 70c5d826e5aac917eccd395b22c238629d8cee66 Mon Sep 17 00:00:00 2001 From: alaca Date: Mon, 4 Nov 2024 07:28:35 +0100 Subject: [PATCH 2/3] refactor: promote upgraded form as default on data transfer --- src/FormMigration/Controllers/MigrationController.php | 11 +++-------- src/FormMigration/Controllers/TransferController.php | 10 ++++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/FormMigration/Controllers/MigrationController.php b/src/FormMigration/Controllers/MigrationController.php index a51e20b5bb..5fa685ddd9 100644 --- a/src/FormMigration/Controllers/MigrationController.php +++ b/src/FormMigration/Controllers/MigrationController.php @@ -52,15 +52,10 @@ public function __invoke(DonationForm $formV2) ->finally(function(FormMigrationPayload $payload) { $payload->formV3->save(); - $campaignRepository = give(CampaignRepository::class); - // Associate upgraded form to a campaign - if ($campaign = $campaignRepository->getByFormId($payload->formV2->id)) { - $defaultForm = $campaign->defaultForm(); - $isDefaultForm = $defaultForm && $defaultForm->id == $payload->formV2->id; - - $campaignRepository->addCampaignForm($campaign, $payload->formV3->id, $isDefaultForm); - } + $campaignRepository = give(CampaignRepository::class); + $campaign = $campaignRepository->getByFormId($payload->formV2->id); + $campaignRepository->addCampaignForm($campaign, $payload->formV3->id); Log::info(esc_html__('Form migrated from v2 to v3.', 'give'), $this->debugContext); }); diff --git a/src/FormMigration/Controllers/TransferController.php b/src/FormMigration/Controllers/TransferController.php index cccc2f32df..4810eb050d 100644 --- a/src/FormMigration/Controllers/TransferController.php +++ b/src/FormMigration/Controllers/TransferController.php @@ -2,6 +2,7 @@ namespace Give\FormMigration\Controllers; +use Give\Campaigns\Repositories\CampaignRepository; use Give\DonationForms\V2\Models\DonationForm; use Give\DonationForms\ValueObjects\DonationFormStatus; use Give\FormMigration\Actions\GetMigratedFormId; @@ -35,6 +36,15 @@ public function __invoke(DonationForm $formV2, TransferOptions $options) TransferFormUrl::from($formV2->id)->to($v3FormId); TransferDonations::from($formV2->id)->to($v3FormId); + // Promote upgraded form to default form + $campaignRepository = give(CampaignRepository::class); + $campaign = $campaignRepository->getByFormId($formV2->id); + $defaultForm = $campaign->defaultForm(); + + if ($defaultForm->id === $formV2->id) { + $campaignRepository->updateDefaultCampaignForm($campaign, $v3FormId); + } + if($options->shouldDelete()) { wp_trash_post($formV2->id); } From 73b0268edb300f8ea657887dcb0a8baea5b4cb27 Mon Sep 17 00:00:00 2001 From: alaca Date: Mon, 4 Nov 2024 07:42:35 +0100 Subject: [PATCH 3/3] feature: create campaign for form --- .../Controllers/TestMigrationController.php | 2 ++ .../TestTraits/LegacyDonationFormAdapter.php | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/tests/Feature/FormMigration/Controllers/TestMigrationController.php b/tests/Feature/FormMigration/Controllers/TestMigrationController.php index a2f628632e..74d10d575c 100644 --- a/tests/Feature/FormMigration/Controllers/TestMigrationController.php +++ b/tests/Feature/FormMigration/Controllers/TestMigrationController.php @@ -28,6 +28,8 @@ public function testShouldMigrateFormV2ToV3(): void { $formV2 = $this->createSimpleDonationForm(); + $this->createCampaignForDonationForm($formV2->id); + $request = $this->getMockRequest(WP_REST_Server::CREATABLE); $controller = new MigrationController($request); diff --git a/tests/Unit/DonationForms/TestTraits/LegacyDonationFormAdapter.php b/tests/Unit/DonationForms/TestTraits/LegacyDonationFormAdapter.php index 16d95bc6af..4a1cd0aacb 100644 --- a/tests/Unit/DonationForms/TestTraits/LegacyDonationFormAdapter.php +++ b/tests/Unit/DonationForms/TestTraits/LegacyDonationFormAdapter.php @@ -2,6 +2,9 @@ namespace Give\Tests\Unit\DonationForms\TestTraits; +use Exception; +use Give\Campaigns\Models\Campaign; +use Give\Campaigns\Repositories\CampaignRepository; use Give\DonationForms\V2\Models\DonationForm; use Give\DonationForms\V2\Properties\DonationFormLevel; use Give\DonationForms\V2\ValueObjects\DonationFormStatus; @@ -58,4 +61,13 @@ public function getDonationFormModelFromLegacyGiveDonateForm(Give_Donate_Form $g ]); } + /** + * @unreleased + */ + public function createCampaignForDonationForm($formId) + { + $campaign = Campaign::factory()->create(); + give(CampaignRepository::class)->addCampaignForm($campaign, $formId); + } + }