From b2e4867bd42893286e3b9f4c88b41788cac25de9 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Wed, 4 Sep 2024 13:35:39 -0400 Subject: [PATCH 01/11] refactor: Update migration closer to data layer --- .../MigrateFormsToCampaignForms.php | 107 ++++++++++++------ 1 file changed, 73 insertions(+), 34 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 5f8f529cd3..110c7c68ef 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -2,14 +2,14 @@ namespace Give\Campaigns\Migrations; -use DateTime; -use Give\Campaigns\Models\Campaign; use Give\Campaigns\ValueObjects\CampaignStatus; use Give\Campaigns\ValueObjects\CampaignType; -use Give\DonationForms\Models\DonationForm; use Give\DonationForms\ValueObjects\DonationFormStatus; use Give\Framework\Database\DB; +use Give\Framework\Database\Exceptions\DatabaseQueryException; use Give\Framework\Migrations\Contracts\Migration; +use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; +use Give\Framework\QueryBuilder\JoinQueryBuilder; /** * @unreleased @@ -29,7 +29,7 @@ public static function id(): string */ public static function timestamp(): int { - return strtotime('2024-08-21'); + return strtotime('2024-08-26 00:00:01'); } /** @@ -38,57 +38,96 @@ public static function timestamp(): int */ public function run() { - foreach(DonationForm::query()->getAll() ?? [] as $form) { - $this->createParentCampaignForDonationForm($form); - } + DB::transaction(function() { + try { + foreach($this->getFormData() as $formData) { + $this->createParentCampaignForDonationForm($formData); + } + } catch (DatabaseQueryException $exception) { + DB::rollback(); + throw new DatabaseMigrationException('An error occurred while creating initial campaigns', 0, $exception); + } + }); + } + + /** + * @unreleased + */ + protected function getFormData(): array + { + return DB::table('posts', 'forms') + ->select( + ['ID', 'id'], + ['post_title', 'title'], + ['post_status', 'status'], + ['meta_value', 'settings'] + ) + ->join(function (JoinQueryBuilder $builder) { + $builder + ->leftJoin('give_formmeta', 'formmeta') + ->on('formmeta.form_id', 'forms.ID'); + }) + ->where('forms.post_type', 'give_forms') + ->where('formmeta.meta_key', 'formBuilderSettings') + ->getAll(); } /** * @unreleased */ - public function createParentCampaignForDonationForm(DonationForm $form) + public function createParentCampaignForDonationForm($formData): void { - $campaign = Campaign::create([ - 'type' => CampaignType::CORE(), - 'title' => $form->title, - 'shortDescription' => $form->settings->formExcerpt, - 'longDescription' => $form->settings->description, - 'logo' => $form->settings->designSettingsLogoUrl, - 'image' => $form->settings->designSettingsImageUrl, - 'primaryColor' => $form->settings->primaryColor, - 'secondaryColor' => $form->settings->secondaryColor, - 'goal' => (int) $form->settings->goalAmount, - 'status' => $this->mapFormStatusToCampaignStatus($form->status), - 'startDate' => new DateTime($form->settings->goalStartDate), - 'endDate' => new DateTime($form->settings->goalEndDate), - ]); + $formId = $formData['id']; + $formTitle = $formData['title']; + $formStatus = $formData['status']; + $formSettings = json_decode($formData['settings']); + + $campaignId = DB::table('give_campaigns') + ->insert([ + 'type' => CampaignType::CORE(), + 'title' => $formTitle, + 'status' => $this->mapFormToCampaignStatus($formStatus), + 'shortDescription' => $formSettings->formExcerpt, + 'longDescription' => $formSettings->description, + 'logo' => $formSettings->designSettingsLogoUrl, + 'image' => $formSettings->designSettingsImageUrl, + 'primaryColor' => $formSettings->primaryColor, + 'secondaryColor' => $formSettings->secondaryColor, + 'goal' => $formSettings->goalAmount, + 'startDate' => $formSettings->goalStartDate, + 'endDate' => $formSettings->goalEndDate, + ]); DB::table('give_campaign_forms') ->insert([ - 'form_id' => $form->id, - 'campaign_id' => $campaign->id, + 'form_id' => $formId, + 'campaign_id' => $campaignId, ]); } - /** + /** * @unreleased */ - public function mapFormStatusToCampaignStatus(DonationFormStatus $status) + public function mapFormToCampaignStatus(string $status): string { - switch ($status) { - case DonationFormStatus::PUBLISHED(): - case DonationFormStatus::UPGRADED(): // TODO: How do we handle upgraded, non-upgraded forms? - case DonationFormStatus::PRIVATE(): // TODO: How do we handle Private forms? - return CampaignStatus::ACTIVE(); + switch (new DonationFormStatus($status)) { case DonationFormStatus::PENDING(): - return CampaignStatus::PENDING(); + return CampaignStatus::PENDING()->getValue(); case DonationFormStatus::DRAFT(): - return CampaignStatus::DRAFT(); + return CampaignStatus::DRAFT()->getValue(); case DonationFormStatus::TRASH(): - return CampaignStatus::INACTIVE(); + return CampaignStatus::INACTIVE()->getValue(); + + case DonationFormStatus::PUBLISHED(): + case DonationFormStatus::UPGRADED(): // TODO: How do we handle upgraded, non-upgraded forms? + case DonationFormStatus::PRIVATE(): // TODO: How do we handle Private forms? + return CampaignStatus::ACTIVE()->getValue(); + + default: // TODO: How do we handle an unknown form status? + return CampaignStatus::INACTIVE()->getValue(); } } } From 04aa44626b5972d4d6e9cd03ef2ab264f89a46b2 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Wed, 4 Sep 2024 14:15:01 -0400 Subject: [PATCH 02/11] chore: Update nameing, references --- .../MigrateFormsToCampaignForms.php | 35 ++++++++++--------- .../MigrateFormsToCampaignFormsTest.php | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 110c7c68ef..d9bd34373e 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -60,6 +60,7 @@ protected function getFormData(): array ['ID', 'id'], ['post_title', 'title'], ['post_status', 'status'], + ['post_date', 'createdAt'], ['meta_value', 'settings'] ) ->join(function (JoinQueryBuilder $builder) { @@ -77,25 +78,27 @@ protected function getFormData(): array */ public function createParentCampaignForDonationForm($formData): void { - $formId = $formData['id']; - $formTitle = $formData['title']; - $formStatus = $formData['status']; - $formSettings = json_decode($formData['settings']); + $formId = $formData->id; + $formTitle = $formData->title; + $formStatus = $formData->status; + $formCreatedAt = $formData->createdAt; + $formSettings = json_decode($formData->settings); $campaignId = DB::table('give_campaigns') ->insert([ - 'type' => CampaignType::CORE(), - 'title' => $formTitle, + 'campaign_type' => CampaignType::CORE()->getValue(), + 'campaign_title' => $formTitle, 'status' => $this->mapFormToCampaignStatus($formStatus), - 'shortDescription' => $formSettings->formExcerpt, - 'longDescription' => $formSettings->description, - 'logo' => $formSettings->designSettingsLogoUrl, - 'image' => $formSettings->designSettingsImageUrl, - 'primaryColor' => $formSettings->primaryColor, - 'secondaryColor' => $formSettings->secondaryColor, - 'goal' => $formSettings->goalAmount, - 'startDate' => $formSettings->goalStartDate, - 'endDate' => $formSettings->goalEndDate, + 'short_desc' => $formSettings->formExcerpt, + 'long_desc' => $formSettings->description, + 'campaign_logo' => $formSettings->designSettingsLogoUrl, + 'campaign_image' => $formSettings->designSettingsImageUrl, + 'primary_color' => $formSettings->primaryColor, + 'secondary_color' => $formSettings->secondaryColor, + 'campaign_goal' => $formSettings->goalAmount, + 'start_date' => $formSettings->goalStartDate, + 'end_date' => $formSettings->goalEndDate, + 'date_created' => $formCreatedAt, ]); DB::table('give_campaign_forms') @@ -110,7 +113,7 @@ public function createParentCampaignForDonationForm($formData): void */ public function mapFormToCampaignStatus(string $status): string { - switch (new DonationFormStatus($status)) { + switch ($status) { case DonationFormStatus::PENDING(): return CampaignStatus::PENDING()->getValue(); diff --git a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php index 382a955979..bfb3d6bfc3 100644 --- a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php +++ b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php @@ -24,7 +24,7 @@ public function testCreatesParentCampaignForDonationForm() $form = DonationForm::factory()->create(); $migration = new MigrateFormsToCampaignForms(); - $migration->createParentCampaignForDonationForm($form); + $migration->run(); $relationship = DB::table('give_campaign_forms')->where('form_id', $form->id)->get(); From 929d707a90507e15fbbdde2624665017b417f923 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Mon, 9 Sep 2024 15:18:47 -0400 Subject: [PATCH 03/11] feature: Exclude P2P campaigns from migration --- .../MigrateFormsToCampaignForms.php | 27 ++++++++++++------- .../MigrateFormsToCampaignFormsTest.php | 17 ++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index d9bd34373e..49dbb14720 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -55,22 +55,31 @@ public function run() */ protected function getFormData(): array { - return DB::table('posts', 'forms') + $query = DB::table('posts', 'forms') ->select( - ['ID', 'id'], - ['post_title', 'title'], - ['post_status', 'status'], - ['post_date', 'createdAt'], - ['meta_value', 'settings'] + ['forms.ID', 'id'], + ['forms.post_title', 'title'], + ['forms.post_status', 'status'], + ['forms.post_date', 'createdAt'] ) + ->where('forms.post_type', 'give_forms'); + + $query->select(['formmeta.meta_value', 'settings']) ->join(function (JoinQueryBuilder $builder) { $builder ->leftJoin('give_formmeta', 'formmeta') ->on('formmeta.form_id', 'forms.ID'); }) - ->where('forms.post_type', 'give_forms') - ->where('formmeta.meta_key', 'formBuilderSettings') - ->getAll(); + ->where('formmeta.meta_key', 'formBuilderSettings'); + + $query->join(function (JoinQueryBuilder $builder) { + $builder + ->leftJoin('give_campaigns', 'campaigns') + ->on('campaigns.form_id', 'forms.ID'); + }) + ->whereIsNull('campaigns.id'); + + return $query->getAll(); } /** diff --git a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php index bfb3d6bfc3..c8b4ab4dca 100644 --- a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php +++ b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php @@ -4,6 +4,7 @@ use Give\Campaigns\Migrations\MigrateFormsToCampaignForms; use Give\Campaigns\Models\Campaign; +use Give\Campaigns\ValueObjects\CampaignType; use Give\DonationForms\Models\DonationForm; use Give\Framework\Database\DB; use Give\Tests\TestCase; @@ -31,4 +32,20 @@ public function testCreatesParentCampaignForDonationForm() $this->assertNotNull(Campaign::find($relationship->campaign_id)); $this->assertEquals($form->id, $relationship->form_id); } + + public function testExistingPeerToPeerCampaignFormsAreNotMigrated() + { + $form = DonationForm::factory()->create(); + DB::table('give_campaigns')->insert([ + 'form_id' => $form->id, + ]); + + $migration = new MigrateFormsToCampaignForms(); + $migration->run(); + + $relationship = DB::table('give_campaign_forms')->where('form_id', $form->id)->get(); + + $this->assertNull($relationship); + $this->assertEquals(1, DB::table('give_campaigns')->count()); + } } From bde30e049132c1d485ff58253ffd30d2b2044ff8 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Mon, 9 Sep 2024 15:42:29 -0400 Subject: [PATCH 04/11] refactor: Replace enum status with raw strings --- .../MigrateFormsToCampaignForms.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 49dbb14720..b99b84346a 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -95,7 +95,7 @@ public function createParentCampaignForDonationForm($formData): void $campaignId = DB::table('give_campaigns') ->insert([ - 'campaign_type' => CampaignType::CORE()->getValue(), + 'campaign_type' => 'core', 'campaign_title' => $formTitle, 'status' => $this->mapFormToCampaignStatus($formStatus), 'short_desc' => $formSettings->formExcerpt, @@ -124,22 +124,22 @@ public function mapFormToCampaignStatus(string $status): string { switch ($status) { - case DonationFormStatus::PENDING(): - return CampaignStatus::PENDING()->getValue(); + case 'pending': + return 'pending'; - case DonationFormStatus::DRAFT(): - return CampaignStatus::DRAFT()->getValue(); + case 'draft': + return 'draft'; - case DonationFormStatus::TRASH(): - return CampaignStatus::INACTIVE()->getValue(); + case 'trash': + return 'inactive'; - case DonationFormStatus::PUBLISHED(): - case DonationFormStatus::UPGRADED(): // TODO: How do we handle upgraded, non-upgraded forms? - case DonationFormStatus::PRIVATE(): // TODO: How do we handle Private forms? - return CampaignStatus::ACTIVE()->getValue(); + case 'publish': + case 'private': + case 'upgraded': + return 'active'; default: // TODO: How do we handle an unknown form status? - return CampaignStatus::INACTIVE()->getValue(); + return 'inactive'; } } } From 7ec7b59f4808b86f05b30fb08e98f64e1ecf8cb6 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Mon, 9 Sep 2024 15:43:44 -0400 Subject: [PATCH 05/11] chore: Cleanup --- src/Campaigns/Migrations/MigrateFormsToCampaignForms.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index b99b84346a..059bc736d5 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -2,9 +2,6 @@ namespace Give\Campaigns\Migrations; -use Give\Campaigns\ValueObjects\CampaignStatus; -use Give\Campaigns\ValueObjects\CampaignType; -use Give\DonationForms\ValueObjects\DonationFormStatus; use Give\Framework\Database\DB; use Give\Framework\Database\Exceptions\DatabaseQueryException; use Give\Framework\Migrations\Contracts\Migration; @@ -35,6 +32,7 @@ public static function timestamp(): int /** * @unreleased * @inheritDoc + * @throws \Exception */ public function run() { From fe3f605c7c5424627bc9d9dec1156c5d128d47d2 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Mon, 9 Sep 2024 16:13:36 -0400 Subject: [PATCH 06/11] refactor: Exclude upgraded forms from migration --- .../Migrations/MigrateFormsToCampaignForms.php | 5 ++++- .../MigrateFormsToCampaignFormsTest.php | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 059bc736d5..13d85c064a 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -70,6 +70,7 @@ protected function getFormData(): array }) ->where('formmeta.meta_key', 'formBuilderSettings'); + // Exclude forms already associated with a campaign (ie Peer-to-peer). $query->join(function (JoinQueryBuilder $builder) { $builder ->leftJoin('give_campaigns', 'campaigns') @@ -77,6 +78,9 @@ protected function getFormData(): array }) ->whereIsNull('campaigns.id'); + // Exclude forms with an `upgraded` status, which are archived. + $query->where('forms.post_status', 'upgraded', '!='); + return $query->getAll(); } @@ -133,7 +137,6 @@ public function mapFormToCampaignStatus(string $status): string case 'publish': case 'private': - case 'upgraded': return 'active'; default: // TODO: How do we handle an unknown form status? diff --git a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php index c8b4ab4dca..4b84018f94 100644 --- a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php +++ b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php @@ -4,8 +4,8 @@ use Give\Campaigns\Migrations\MigrateFormsToCampaignForms; use Give\Campaigns\Models\Campaign; -use Give\Campaigns\ValueObjects\CampaignType; use Give\DonationForms\Models\DonationForm; +use Give\DonationForms\ValueObjects\DonationFormStatus; use Give\Framework\Database\DB; use Give\Tests\TestCase; use Give\Tests\TestTraits\RefreshDatabase; @@ -48,4 +48,19 @@ public function testExistingPeerToPeerCampaignFormsAreNotMigrated() $this->assertNull($relationship); $this->assertEquals(1, DB::table('give_campaigns')->count()); } + + public function testUpgradedFormsAreNotMigrated() + { + $form = DonationForm::factory()->create([ + 'status' => DonationFormStatus::UPGRADED(), + ]); + + $migration = new MigrateFormsToCampaignForms(); + $migration->run(); + + $relationship = DB::table('give_campaign_forms')->where('form_id', $form->id)->get(); + + $this->assertNull($relationship); + $this->assertEquals(0, DB::table('give_campaigns')->count()); + } } From 558d4ce85105f363608a757b9eb66526a98c03ea Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Tue, 10 Sep 2024 14:09:35 -0400 Subject: [PATCH 07/11] feature: Add migrated forms to campaigns --- .../MigrateFormsToCampaignForms.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 13d85c064a..4f62a48214 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -41,6 +41,9 @@ public function run() foreach($this->getFormData() as $formData) { $this->createParentCampaignForDonationForm($formData); } + foreach($this->getMigratedFormData() as $migratedFormData) { + $this->attachMigratedFormToCampaign($migratedFormData); + } } catch (DatabaseQueryException $exception) { DB::rollback(); throw new DatabaseMigrationException('An error occurred while creating initial campaigns', 0, $exception); @@ -84,6 +87,26 @@ protected function getFormData(): array return $query->getAll(); } + /** + * @unreleased + * @return array [{formId, campaignId, migratedFormId}] + */ + protected function getMigratedFormData(): array + { + return DB::table('posts', 'forms') + ->select(['forms.ID', 'formId'], ['campaign_forms.campaign_id', 'campaignId']) + ->attachMeta('give_formmeta', 'ID', 'form_id', 'migratedFormId') + ->join(function (JoinQueryBuilder $builder) { + $builder + ->rightJoin('give_campaign_forms', 'campaign_forms') + ->on('campaign_forms.form_id', 'forms.ID'); + }) + ->where('forms.post_type', 'give_forms') + ->where('forms.post_status', 'trash', '!=') + ->whereIsNotNull('give_formmeta_attach_meta_migratedFormId.meta_value') + ->getAll(); + } + /** * @unreleased */ @@ -119,6 +142,15 @@ public function createParentCampaignForDonationForm($formData): void ]); } + protected function attachMigratedFormToCampaign($data): void + { + DB::table('give_campaign_forms') + ->insert([ + 'form_id' => $data->migratedFormId, + 'campaign_id' => $data->campaignId, + ]); + } + /** * @unreleased */ From 267bb12561176cea3e1fda8605c26abae2143dcd Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Tue, 10 Sep 2024 14:21:54 -0400 Subject: [PATCH 08/11] refactor: Update Campaigns to support default form --- .../MigrateFormsToCampaignForms.php | 1 + .../Tables/CreateCampaignFormsTable.php | 1 + src/Campaigns/Models/Campaign.php | 10 +++++++ .../MigrateFormsToCampaignFormsTest.php | 28 +++++++++++++++++++ .../Campaigns/Models/CampaignModelTest.php | 13 +++++++++ 5 files changed, 53 insertions(+) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 4f62a48214..41252fd7d2 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -139,6 +139,7 @@ public function createParentCampaignForDonationForm($formData): void ->insert([ 'form_id' => $formId, 'campaign_id' => $campaignId, + 'is_default' => true, ]); } diff --git a/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php b/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php index 49c32034c2..92e58b6c57 100644 --- a/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php +++ b/src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php @@ -51,6 +51,7 @@ public function run(): void $sql = "CREATE TABLE $table ( campaign_id INT UNSIGNED NOT NULL, form_id INT UNSIGNED NOT NULL, + is_default BOOLEAN NOT NULL DEFAULT 0, KEY form_id (form_id), KEY campaign_id (campaign_id), PRIMARY KEY (campaign_id, form_id) diff --git a/src/Campaigns/Models/Campaign.php b/src/Campaigns/Models/Campaign.php index a858c6f7bf..c22f8e438e 100644 --- a/src/Campaigns/Models/Campaign.php +++ b/src/Campaigns/Models/Campaign.php @@ -59,6 +59,16 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory 'createdAt' => DateTime::class, ]; + /** + * @unreleased + */ + public function form(): DonationForm + { + return $this->forms() + ->where('campaign_forms.is_default', true) + ->get(); + } + /** * @unreleased */ diff --git a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php index 4b84018f94..01f80d3012 100644 --- a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php +++ b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php @@ -63,4 +63,32 @@ public function testUpgradedFormsAreNotMigrated() $this->assertNull($relationship); $this->assertEquals(0, DB::table('give_campaigns')->count()); } + + public function testMigratedFormsAreDefault() + { + $form = DonationForm::factory()->create(); + + $migration = new MigrateFormsToCampaignForms(); + $migration->run(); + + $relationship = DB::table('give_campaign_forms')->where('form_id', $form->id)->get(); + + $this->assertEquals(1, $relationship->is_default); + } + + public function testUpgradedFormsAreNotDefault() + { + $form1 = DonationForm::factory()->create([ + 'status' => DonationFormStatus::UPGRADED(), + ]); + $form2 = DonationForm::factory()->create(); + give_update_meta($form2->id, 'migratedFormId', $form1->id); + + $migration = new MigrateFormsToCampaignForms(); + $migration->run(); + + $relationship = DB::table('give_campaign_forms')->where('form_id', $form1->id)->get(); + + $this->assertEquals(0, $relationship->is_default); + } } diff --git a/tests/Unit/Campaigns/Models/CampaignModelTest.php b/tests/Unit/Campaigns/Models/CampaignModelTest.php index 5b3b20abda..c937e9def4 100644 --- a/tests/Unit/Campaigns/Models/CampaignModelTest.php +++ b/tests/Unit/Campaigns/Models/CampaignModelTest.php @@ -42,4 +42,17 @@ public function testCampaignHasManyForms() $this->assertEquals(2, $campaign->forms()->count()); } + + public function testCampaignHasDefaultForm() + { + $campaign = Campaign::factory()->create(); + $form1 = DonationForm::factory()->create(); + $form2 = DonationForm::factory()->create(); + + $db = DB::table('give_campaign_forms'); + $db->insert(['form_id' => $form1->id, 'campaign_id' => $campaign->id, 'is_default' => 1]); + $db->insert(['form_id' => $form2->id, 'campaign_id' => $campaign->id]); + + $this->assertEquals($form1->id, $campaign->form()->id); + } } From a7753690459601a23ef9308428c1b476fa618edb Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Tue, 10 Sep 2024 14:29:53 -0400 Subject: [PATCH 09/11] chore: Cleanup --- .../Migrations/MigrateFormsToCampaignForms.php | 17 ++++++++--------- .../MigrateFormsToCampaignFormsTest.php | 12 ++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 41252fd7d2..9e5a9f7c2a 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -38,12 +38,8 @@ public function run() { DB::transaction(function() { try { - foreach($this->getFormData() as $formData) { - $this->createParentCampaignForDonationForm($formData); - } - foreach($this->getMigratedFormData() as $migratedFormData) { - $this->attachMigratedFormToCampaign($migratedFormData); - } + array_map([$this, 'createCampaignForForm'], $this->getFormData()); + array_map([$this, 'addUpgradedFormToCampaign'], $this->getUpgradedFormData()); } catch (DatabaseQueryException $exception) { DB::rollback(); throw new DatabaseMigrationException('An error occurred while creating initial campaigns', 0, $exception); @@ -91,7 +87,7 @@ protected function getFormData(): array * @unreleased * @return array [{formId, campaignId, migratedFormId}] */ - protected function getMigratedFormData(): array + protected function getUpgradedFormData(): array { return DB::table('posts', 'forms') ->select(['forms.ID', 'formId'], ['campaign_forms.campaign_id', 'campaignId']) @@ -110,7 +106,7 @@ protected function getMigratedFormData(): array /** * @unreleased */ - public function createParentCampaignForDonationForm($formData): void + public function createCampaignForForm($formData): void { $formId = $formData->id; $formTitle = $formData->title; @@ -143,7 +139,10 @@ public function createParentCampaignForDonationForm($formData): void ]); } - protected function attachMigratedFormToCampaign($data): void + /** + * @param $data + */ + protected function addUpgradedFormToCampaign($data): void { DB::table('give_campaign_forms') ->insert([ diff --git a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php index 01f80d3012..b763b7653b 100644 --- a/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php +++ b/tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php @@ -33,6 +33,9 @@ public function testCreatesParentCampaignForDonationForm() $this->assertEquals($form->id, $relationship->form_id); } + /** + * @unreleased + */ public function testExistingPeerToPeerCampaignFormsAreNotMigrated() { $form = DonationForm::factory()->create(); @@ -49,6 +52,9 @@ public function testExistingPeerToPeerCampaignFormsAreNotMigrated() $this->assertEquals(1, DB::table('give_campaigns')->count()); } + /** + * @unreleased + */ public function testUpgradedFormsAreNotMigrated() { $form = DonationForm::factory()->create([ @@ -64,6 +70,9 @@ public function testUpgradedFormsAreNotMigrated() $this->assertEquals(0, DB::table('give_campaigns')->count()); } + /** + * @unreleased + */ public function testMigratedFormsAreDefault() { $form = DonationForm::factory()->create(); @@ -76,6 +85,9 @@ public function testMigratedFormsAreDefault() $this->assertEquals(1, $relationship->is_default); } + /** + * @unreleased + */ public function testUpgradedFormsAreNotDefault() { $form1 = DonationForm::factory()->create([ From b66fc7df91ae9e9a15ab4f3bafd26c22668f3d10 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Tue, 10 Sep 2024 14:31:03 -0400 Subject: [PATCH 10/11] chore: Add docblock --- tests/Unit/Campaigns/Models/CampaignModelTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Unit/Campaigns/Models/CampaignModelTest.php b/tests/Unit/Campaigns/Models/CampaignModelTest.php index c937e9def4..d421f736cc 100644 --- a/tests/Unit/Campaigns/Models/CampaignModelTest.php +++ b/tests/Unit/Campaigns/Models/CampaignModelTest.php @@ -43,6 +43,9 @@ public function testCampaignHasManyForms() $this->assertEquals(2, $campaign->forms()->count()); } + /** + * @unreleased + */ public function testCampaignHasDefaultForm() { $campaign = Campaign::factory()->create(); From 6146be5b853c6048987b4147e1e690e7932ff502 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Wed, 11 Sep 2024 12:13:25 -0400 Subject: [PATCH 11/11] refactor: Do not exclude trashed, upgraded forms --- src/Campaigns/Migrations/MigrateFormsToCampaignForms.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 9e5a9f7c2a..49a791709b 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -98,7 +98,6 @@ protected function getUpgradedFormData(): array ->on('campaign_forms.form_id', 'forms.ID'); }) ->where('forms.post_type', 'give_forms') - ->where('forms.post_status', 'trash', '!=') ->whereIsNotNull('give_formmeta_attach_meta_migratedFormId.meta_value') ->getAll(); }