Skip to content

Commit

Permalink
Tests: scaffold v2 to v3 donation form migration tests (#7216)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Waldstein <[email protected]>
  • Loading branch information
jonwaldstein and Jon Waldstein authored Feb 7, 2024
1 parent a785ee0 commit c11d821
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 23 deletions.
48 changes: 29 additions & 19 deletions src/FormMigration/FormMetaDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getDonationGoalType(): GoalType
return $onlyRecurringEnabled ? GoalType::DONORS_FROM_SUBSCRIPTIONS() : GoalType::DONORS();
case 'donation': // @note v2: Singular
return $onlyRecurringEnabled ? GoalType::SUBSCRIPTIONS() : GoalType::DONATIONS();
// @note v3: Plural
// @note v3: Plural
case 'amount':
case 'percentage': // @note `percentage` is not supported in v3 - defaulting to `amount`
default:
Expand Down Expand Up @@ -513,13 +513,13 @@ public function getFeeRecoverySettings(): array
*/
public function isMailchimpEnabled(): bool
{
$isFormEnabled = give_is_setting_enabled($this->getMeta('_give_mailchimp_enable'),'true');
$isFormEnabled = give_is_setting_enabled($this->getMeta('_give_mailchimp_enable'), 'true');

$isFormDisabled = give_is_setting_enabled($this->getMeta('_give_mailchimp_disable'),'true');
$isFormDisabled = give_is_setting_enabled($this->getMeta('_give_mailchimp_disable'), 'true');

$isGloballyEnabled = give_is_setting_enabled(give_get_option('give_mailchimp_show_checkout_signup'), 'on');

return !($isFormDisabled || ( !$isGloballyEnabled && !$isFormEnabled));
return !($isFormDisabled || (!$isGloballyEnabled && !$isFormEnabled));
}

/**
Expand Down Expand Up @@ -588,8 +588,8 @@ public function getMailchimpSubscriberTags(): array
*
* @since 3.0.0
*
* @param string $key
* @param mixed $default
* @param string $key
* @param mixed $default
* @return mixed
*/
private function getMeta(string $key, $default = null)
Expand Down Expand Up @@ -620,6 +620,7 @@ public function hasFundOptions(): bool
}

/**
* @unreleased added additional checks to ensure that the form has funds and fund options
* @since 3.3.0
*/
public function getFundsAndDesignationsAttributes(): array
Expand All @@ -631,23 +632,32 @@ public function getFundsAndDesignationsAttributes(): array


$options = [];
foreach ($donorOptions as $fundId) {
$options[] = [
'value' => $fundId,
'label' => $this->getFundLabel($fundId),
'checked' => $isAdminChoice ? $fundId === $adminChoice : true,
'isDefault' => $this->isDefaultFund($fundId),
];
if (!empty($donorOptions)) {
foreach ($donorOptions as $fundId) {
$options[] = [
'value' => $fundId,
'label' => $this->getFundLabel($fundId),
'checked' => $isAdminChoice ? $fundId === $adminChoice : true,
'isDefault' => $this->isDefaultFund($fundId),
];
}
}

return [
'label' => $label,
'fund' => $isAdminChoice ? [
$fund = [];
if ($isAdminChoice) {
$fund = [
'value' => $adminChoice,
'label' => $this->getFundLabel($adminChoice),
'checked' => true,
'isDefault' => $this->isDefaultFund($adminChoice),
] : $options[0],
];
} elseif (!empty($options)) {
$fund = $options[0];
}

return [
'label' => $label,
'fund' => $fund,
'options' => $options,
];
}
Expand All @@ -663,7 +673,7 @@ private function getFundLabel(int $fundId): string
$wpdb->prepare("SELECT * FROM {$wpdb->give_funds} WHERE id = %d", $fundId)
);

if ( ! $fund) {
if (!$fund) {
return '';
}

Expand All @@ -679,7 +689,7 @@ private function isDefaultFund(int $fundId): bool

$fund = $wpdb->get_row("SELECT id FROM {$wpdb->give_funds} WHERE is_default = 1");

if ( ! $fund) {
if (!$fund) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Give\Tests\Feature\FormMigration\Controllers;

use Give\FormMigration\Actions\GetMigratedFormId;
use Give\FormMigration\Controllers\MigrationController;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;


/**
* @unreleased
*
* @covers \Give\FormMigration\Controllers\MigrationController
*/
class TestMigrationController extends TestCase
{
use RefreshDatabase, LegacyDonationFormAdapter;

/**
* @unreleased
*/
public function testShouldMigrateFormV2ToV3(): void
{
$formV2 = $this->createSimpleDonationForm();

$request = $this->getMockRequest(WP_REST_Server::CREATABLE);

$controller = new MigrationController($request);

$response = $controller($formV2);

$formV3Id = (int)(new GetMigratedFormId)($formV2->id);

$this->assertInstanceOf(WP_REST_Response::class, $response);

$this->assertSame($response->data, [
'v2FormId' => $formV2->id,
'v3FormId' => $formV3Id,
'redirect' => add_query_arg([
'post_type' => 'give_forms',
'page' => 'givewp-form-builder',
'donationFormID' => $formV3Id,
], admin_url('edit.php'))
]);
}

/**
*
* @unreleased
*/
public function getMockRequest(string $method): WP_REST_Request
{
return new WP_REST_Request(
$method,
'/wp/v2/' . 'admin/forms/migrate/(?P<id>\d+)'
);
}
}
50 changes: 50 additions & 0 deletions tests/Feature/FormMigration/Steps/TestDonationGoal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Give\Tests\Feature\FormMigration\Steps;

use Give\FormMigration\DataTransferObjects\FormMigrationPayload;
use Give\FormMigration\Steps\DonationGoal;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;

/**
* @unreleased
*
* @covers \Give\FormMigration\Steps\DonationGoal
*/
class TestDonationGoal extends TestCase
{
use RefreshDatabase, LegacyDonationFormAdapter;

/**
* @unreleased
*/
public function testProcessShouldUpdateDonationFormDonationGoalSettings(): void
{
$meta = [
'_give_goal_option' => 'enabled',
'_give_goal_setting' => 'enabled',
'_give_goal_format' => 'amount',
'_give_set_goal' => 5000,
'_give_close_form_when_goal_achieved' => 'enabled',
'_give_form_goal_achieved_message' => __( 'Thank you to all our donors, we have met our fundraising goal.', 'give' ),
];

$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);

$donationGoal = new DonationGoal($payload);

$donationGoal->process();

$settings = $payload->formV3->settings;

$this->assertTrue(true, $settings->enableDonationGoal);
$this->assertTrue($settings->goalType->isAmount());
$this->assertSame((string)$meta['_give_set_goal'], $settings->goalAmount);
$this->assertTrue(true, $settings->enableAutoClose);
$this->assertSame($meta['_give_form_goal_achieved_message'], $settings->goalAchievedMessage);
}
}
75 changes: 75 additions & 0 deletions tests/Feature/FormMigration/Steps/TestDonationOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Give\Tests\Feature\FormMigration\Steps;

use Give\FormMigration\DataTransferObjects\FormMigrationPayload;
use Give\FormMigration\Steps\DonationOptions;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;

/**
* @unreleased
*
* @covers \Give\FormMigration\Steps\DonationOptions
*/
class TestDonationOptions extends TestCase {
use RefreshDatabase, LegacyDonationFormAdapter;

/**
* @unreleased
*/
public function testProcessShouldUpdateDonationAmountBlockAttributes(): void
{
$meta = [
'_give_price_option' => 'set',
'_give_set_price' => '100',
'_give_custom_amount' => 'enabled',
'_give_custom_amount_range_minimum' => '1',
'_give_custom_amount_range_maximum' => '1000',
];

$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);

$donationOptions = new DonationOptions($payload);

$donationOptions->process();

$block = $payload->formV3->blocks->findByName('givewp/donation-amount');

$this->assertSame($meta['_give_price_option'], $block->getAttribute('priceOption'));
$this->assertSame($meta['_give_set_price'], $block->getAttribute('setPrice'));
$this->assertTrue($block->getAttribute('customAmount'));
$this->assertSame((float)$meta['_give_custom_amount_range_minimum'], $block->getAttribute('customAmountMin'));
$this->assertSame((float)$meta['_give_custom_amount_range_maximum'], $block->getAttribute('customAmountMax'));
}

/**
* @unreleased
*/
public function testProcessShouldUpdateDonationAmountBlockAttributesWithDonationLevels(): void
{
$meta = [
'_give_custom_amount' => 'enabled',
'_give_custom_amount_range_minimum' => '1',
'_give_custom_amount_range_maximum' => '1000',
];

$formV2 = $this->createMultiLevelDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);

$donationOptions = new DonationOptions($payload);

$donationOptions->process();

$block = $payload->formV3->blocks->findByName('givewp/donation-amount');

$this->assertSame([10.00, 25.00, 50.00, 100.00], $block->getAttribute('levels'));
$this->assertTrue($block->getAttribute('customAmount'));
$this->assertSame((float)$meta['_give_custom_amount_range_minimum'], $block->getAttribute('customAmountMin'));
$this->assertSame((float)$meta['_give_custom_amount_range_maximum'], $block->getAttribute('customAmountMax'));
}
}
Loading

0 comments on commit c11d821

Please sign in to comment.