diff --git a/src/FormMigration/Controllers/MigrationController.php b/src/FormMigration/Controllers/MigrationController.php index 1fc45cf3ac..5fa685ddd9 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,12 @@ public function __invoke(DonationForm $formV2) ->process($payload) ->finally(function(FormMigrationPayload $payload) { $payload->formV3->save(); + + // Associate upgraded form to a campaign + $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); } 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); + } + }