Skip to content

Commit

Permalink
Feature: Associate upgraded form to a campaign (#7592)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaca authored Nov 6, 2024
1 parent 18d4c4c commit 937c430
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/FormMigration/Controllers/MigrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

Expand Down
10 changes: 10 additions & 0 deletions src/FormMigration/Controllers/TransferController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/DonationForms/TestTraits/LegacyDonationFormAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 937c430

Please sign in to comment.