Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Associate upgraded form to a campaign #7592

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
Loading