Skip to content

Commit

Permalink
Test: Add tests for form migration steps (#7424)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloiankoski authored Aug 22, 2024
1 parent 4fd268e commit 55b1c3b
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 268 deletions.
2 changes: 1 addition & 1 deletion src/FormMigration/FormMetaDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ public function isConvertKitEnabled(): bool
$isGloballyEnabled = $this->getMeta('_give_convertkit_override_option') === 'default' &&
give_is_setting_enabled(give_get_option('give_convertkit_show_subscribe_checkbox'));

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

/**
Expand Down
122 changes: 67 additions & 55 deletions tests/Feature/FormMigration/Steps/TestActiveCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,106 @@

namespace Give\Tests\Feature\FormMigration\Steps;

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

class TestActiveCampaign extends TestCase
{
use RefreshDatabase, LegacyDonationFormAdapter;
use FormMigrationProcessor;
use LegacyDonationFormAdapter;
use RefreshDatabase;

/**
* @since 3.10.0
* @unreleased
*/
public function testProcessShouldUpdateActiveCampaignBlockAttributesFromV2FormMeta(): void
public function testFormConfiguredToUseGlobalActiveCampaignSettingsMigratesUsingGlobalSettingsWhenGloballyEnabled()
{
$meta = [
'give_activecampaign_label' => __('Subscribe to our newsletter?'),
'give_activecampaign_lists' => ['1', '2'],
'give_activecampaign_tags' => ['tag 1', 'tag 2'],
// Arrange
$options = [
'give_activecampaign_globally_enabled' => 'on',
'give_activecampaign_label' => __('Subscribe to our newsletter?'),
'give_activecampaign_lists' => ['1', '2'],
'give_activecampaign_tags' => ['tag 1', 'tag 2'],
'give_activecampaign_checkbox_default' => true,
];
foreach ($options as $key => $value) {
give_update_option($key, $value);
}
$meta = ['activecampaign_per_form_options' => 'global'];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

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

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

$mailchimp = new ActiveCampaign($payload);

$mailchimp->process();

$block = $payload->formV3->blocks->findByName('give-activecampaign/activecampaign');
// Act
$v3Form = $this->migrateForm($v2Form, ActiveCampaign::class);

$this->assertSame($meta['give_activecampaign_label'], $block->getAttribute('label'));
$this->assertSame($meta['give_activecampaign_lists'], $block->getAttribute('selectedLists'));
$this->assertSame($meta['give_activecampaign_tags'], $block->getAttribute('selectedTags'));
// Assert
$block = $v3Form->blocks->findByName('give-activecampaign/activecampaign');
$this->assertSame($options['give_activecampaign_label'], $block->getAttribute('label'));
$this->assertSame($options['give_activecampaign_lists'], $block->getAttribute('selectedLists'));
$this->assertSame($options['give_activecampaign_tags'], $block->getAttribute('selectedTags'));
$this->assertTrue(true, $block->getAttribute('defaultChecked'));
}

/**
* @since 3.10.0
* @unreleased
*/
public function testProcessShouldUpdateActiveCampaignBlockAttributesFromGlobalSettings(): void
public function testFormConfiguredToUseGlobalActiveCampaignSettingsIsMigratedWithoutActiveCampaignBlockWhenNotGloballyEnabled()
{
$meta = [
'give_activecampaign_label' => __('Subscribe to our newsletter?'),
'give_activecampaign_lists' => ['1', '2'],
'give_activecampaign_tags' => ['tag 1', 'tag 2'],
'give_activecampaign_checkbox_default' => true,
];

foreach ($meta as $key => $value) {
give_update_option($key, $value);
}

$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);
// Arrange
give_update_option('give_activecampaign_globally_enabled', 'off');
$meta = ['activecampaign_per_form_options' => 'global'];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);
// Act
$v3Form = $this->migrateForm($v2Form, ActiveCampaign::class);

$mailchimp = new ActiveCampaign($payload);

$mailchimp->process();

$block = $payload->formV3->blocks->findByName('give-activecampaign/activecampaign');

$this->assertSame($meta['give_activecampaign_label'], $block->getAttribute('label'));
$this->assertSame($meta['give_activecampaign_lists'], $block->getAttribute('selectedLists'));
$this->assertSame($meta['give_activecampaign_tags'], $block->getAttribute('selectedTags'));
$this->assertTrue(true, $block->getAttribute('defaultChecked'));
// Assert
$block = $v3Form->blocks->findByName('give-activecampaign/activecampaign');
$this->assertNull($block);
}

/**
* @since 3.10.0
* @unreleased
*/
public function testProcessShouldUpdateActiveCampaignBlockAttributesWhenNoMeta(): void
public function testFormConfiguredToDisableActiveCampaignIsMigratedWithoutActiveCampaignBlock()
{
$formV2 = $this->createSimpleDonationForm();
// Arrange
$meta = ['activecampaign_per_form_options' => 'disabled'];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);
// Act
$v3Form = $this->migrateForm($v2Form, ActiveCampaign::class);

$mailchimp = new ActiveCampaign($payload);
// Assert
$block = $v3Form->blocks->findByName('give-activecampaign/activecampaign');
$this->assertNull($block);
}

$mailchimp->process();
/**
* @unreleased
*/
public function testFormConfiguredToUseCustomizedActiveCampaignSettingsIsMigrated()
{
// Arrange
$meta = [
'activecampaign_per_form_options' => 'customized',
'give_activecampaign_label' => __('Subscribe to our newsletter?'),
'give_activecampaign_lists' => ['1', '2'],
'give_activecampaign_tags' => ['tag 1', 'tag 2'],
'give_activecampaign_checkbox_default' => true,
];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

$block = $payload->formV3->blocks->findByName('give-activecampaign/activecampaign');
// Act
$v3Form = $this->migrateForm($v2Form, ActiveCampaign::class);

$this->assertSame(__('Subscribe to our newsletter?'), $block->getAttribute('label'));
$this->assertSame([], $block->getAttribute('selectedLists'));
$this->assertNull(null, $block->getAttribute('selectedTags'));
// Assert
$block = $v3Form->blocks->findByName('give-activecampaign/activecampaign');
$this->assertSame($meta['give_activecampaign_label'], $block->getAttribute('label'));
$this->assertSame($meta['give_activecampaign_lists'], $block->getAttribute('selectedLists'));
$this->assertSame($meta['give_activecampaign_tags'], $block->getAttribute('selectedTags'));
$this->assertTrue(true, $block->getAttribute('defaultChecked'));
}
}
105 changes: 51 additions & 54 deletions tests/Feature/FormMigration/Steps/TestConstantContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,91 @@

namespace Give\Tests\Feature\FormMigration\Steps;

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

/**
* @unreleased Update to use FormMigrationProcessor trait
* @since 3.7.0
*
* @covers \Give\FormMigration\Steps\DonationGoal
*/
class TestConstantContact extends TestCase
{
use RefreshDatabase, LegacyDonationFormAdapter;
use FormMigrationProcessor;
use LegacyDonationFormAdapter;
use RefreshDatabase;

/**
* @unreleased Update test to use FormMigrationProcessor::migrateForm method
* @since 3.7.0
*/
public function testProcessShouldUpdateConstantContactBlockAttributesWithV2FormMeta(): void
public function testFormMigratesUsingGlobalSettingsWhenGloballyEnabled(): void
{
$meta = [
'_give_constant_contact_custom_label' => 'Subscribe to our newsletter?',
'_give_constant_contact_checked_default' => 'on',
'_give_constant_contact' => ['1928414891'],
// Arrange
$options = [
'give_constant_contact_show_checkout_signup' => 'on',
'give_constant_contact_label' => 'Subscribe to our newsletter?',
'give_constant_contact_checked_default' => 'on',
'give_constant_contact_list' => ['1928414891'],
];
foreach ($options as $key => $value) {
give_update_option($key, $value);
}
$meta = ['_give_constant_contact_disabled' => 'false'];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

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

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

$constantContact = new ConstantContact($payload);

$constantContact->process();

$block = $payload->formV3->blocks->findByName('givewp/constantcontact');
// Act
$v3Form = $this->migrateForm($v2Form, ConstantContact::class);

// Assert
$block = $v3Form->blocks->findByName('givewp/constantcontact');
$this->assertTrue(true, $block->getAttribute('checked' === 'on'));
$this->assertSame($meta['_give_constant_contact_custom_label'], $block->getAttribute('label'));
$this->assertSame($meta['_give_constant_contact'], $block->getAttribute('selectedEmailLists'));
$this->assertSame($options['give_constant_contact_label'], $block->getAttribute('label'));
$this->assertSame($options['give_constant_contact_list'], $block->getAttribute('selectedEmailLists'));
}

/**
* @since 3.7.0
* @unreleased
*/
public function testProcessShouldUpdateConstantContactBlockAttributesWithGlobalSettings(): void
public function testFormConfiguredToDisableConstantContactIsMigratedWithoutConstantContactBlock()
{
$meta = [
'give_constant_contact_label' => 'Subscribe to our newsletter?',
'give_constant_contact_checked_default' => 'on',
'give_constant_contact_list' => ['1928414891'],
];

$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);
// Arrange
give_update_option('give_constant_contact_show_checkout_signup', 'on');
$meta = ['_give_constant_contact_disable' => 'true'];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);
// Act
$v3Form = $this->migrateForm($v2Form, ConstantContact::class);

foreach ($meta as $key => $value) {
give_update_option($key, $value);
}

$constantContact = new ConstantContact($payload);

$constantContact->process();

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

$this->assertTrue(true, $block->getAttribute('checked' === 'on'));
$this->assertSame($meta['give_constant_contact_label'], $block->getAttribute('label'));
$this->assertSame($meta['give_constant_contact_list'], $block->getAttribute('selectedEmailLists'));
// Assert
$block = $v3Form->blocks->findByName('givewp/constantcontact');
$this->assertNull($block);
}

/**
* @unreleased Update test to use FormMigrationProcessor::migrateForm method
* @since 3.7.0
*/
public function testProcessShouldUpdateConstantContactBlockAttributesWhenNoMeta(): void
public function testFormConfiguredToUseCustomizedConstantContactSettingsIsMigrated(): void
{
$formV2 = $this->createSimpleDonationForm();

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

$constantContact = new ConstantContact($payload);

$constantContact->process();
// Arrange
$meta = [
'_give_constant_contact_enable' => 'true',
'_give_constant_contact_custom_label' => 'Subscribe to our newsletter?',
'_give_constant_contact_checked_default' => 'on',
'_give_constant_contact' => ['1928414891'],
];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

$block = $payload->formV3->blocks->findByName('givewp/constantcontact');
// Act
$v3Form = $this->migrateForm($v2Form, ConstantContact::class);

// Assert
$block = $v3Form->blocks->findByName('givewp/constantcontact');
$this->assertTrue(true, $block->getAttribute('checked' === 'on'));
$this->assertSame('Subscribe to our newsletter?', $block->getAttribute('label'));
$this->assertNull(null, $block->getAttribute('selectedEmailLists'));
$this->assertSame($meta['_give_constant_contact_custom_label'], $block->getAttribute('label'));
$this->assertSame($meta['_give_constant_contact'], $block->getAttribute('selectedEmailLists'));
}
}
Loading

0 comments on commit 55b1c3b

Please sign in to comment.