Skip to content

Commit

Permalink
Feature: add mailchimp form migration unit tests (#7328)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaHungDinh authored Mar 28, 2024
1 parent 3e48195 commit 181655e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 32 deletions.
30 changes: 14 additions & 16 deletions src/FormMigration/FormMetaDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,58 +527,56 @@ public function isMailchimpEnabled(): bool
/**
* @since 3.3.0
*/
public function getMailchimpLabel()
public function getMailchimpLabel(): string
{
$value = $this->getMeta('_give_mailchimp_custom_label');
return $value === '' ? null : $value;
return $this->getMeta('_give_mailchimp_custom_label', give_get_option('give_mailchimp_label', __('Subscribe to newsletter?')));
}

/**
* @unreleased add global setting as default.
* @since 3.3.0
*/
public function getMailchimpDefaultChecked(): bool
{
return $this->getMeta('_give_mailchimp_checked_default');
return give_is_setting_enabled($this->getMeta('_give_mailchimp_checked_default',
give_get_option('give_mailchimp_checked_default', true)));
}

/**
* @since 3.3.0
*/
public function getMailchimpDoubleOptIn(): bool
{
return $this->getMeta('_give_mailchimp_double_opt_in');
}

/**
* @unreleased add global setting as default.
* @since 3.3.0
*/
public function getMailchimpSendDonationData(): bool
{
return $this->getMeta('_give_mailchimp_send_donation');
return give_is_setting_enabled($this->getMeta('_give_mailchimp_send_donation_data',
give_get_option('give_mailchimp_donation_data', true)));
}

/**
* @unreleased add global setting as default.
* @since 3.3.0
*/
public function getMailchimpSendFFMData(): bool
{
return $this->getMeta('_give_mailchimp_send_ffm');
return give_is_setting_enabled($this->getMeta('_give_mailchimp_send_ffm',
give_get_option('give_mailchimp_ffm_pass_field')));
}

/**
* @since 3.3.0
*/
public function getMailchimpDefaultAudiences(): array
{
return $this->getMeta('_give_mailchimp');
return (array)$this->getMeta('_give_mailchimp', give_get_option('give_mailchimp_list', ['']));
}

/**
* @since 3.3.0
*/
public function getMailchimpSubscriberTags(): array
public function getMailchimpSubscriberTags():? array
{
return $this->getMeta('_give_mailchimp_tags');
return (array)$this->getMeta('_give_mailchimp_tags', null);
}


Expand Down
25 changes: 9 additions & 16 deletions src/FormMigration/Steps/Mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function process(): void
{
$block = BlockModel::make([
'name' => 'givewp/mailchimp',
'attributes' => $this->getAttributes()
'attributes' => $this->getAttributes(),
]);

$this->fieldBlocks->insertAfter('givewp/email', $block);
Expand All @@ -36,21 +36,14 @@ public function process(): void
*/
private function getAttributes(): array
{
return [
'label' => $this->formV2->getMailchimpLabel() ??
give_get_option('give_mailchimp_label', __('Subscribe to newsletter?')),
'checked' => $this->formV2->getMailchimpDefaultChecked() ??
give_get_option('give_mailchimp_checked_default', true),
'doubleOptIn' => $this->formV2->getMailchimpDoubleOptIn() ??
give_get_option('give_mailchimp_double_opt_in'),
'subscriberTags' => $this->formV2->getMailchimpSubscriberTags() ??
[],
'sendDonationData' => $this->formV2->getMailchimpSendDonationData() ??
give_get_option('give_mailchimp_donation_data', true),
'sendFFMData' => $this->formV2->getMailchimpSendFFMData() ??
give_get_option('give_mailchimp_ffm_pass_field'),
'defaultAudiences' => $this->formV2->getMailchimpDefaultAudiences() ??
give_get_option('give_mailchimp_list', []),
return [
'label' => $this->formV2->getMailchimpLabel(),
'checked' => $this->formV2->getMailchimpDefaultChecked(),
'doubleOptIn' => give_get_option('give_mailchimp_double_opt_in', true),
'subscriberTags' => $this->formV2->getMailchimpSubscriberTags(),
'sendDonationData' => $this->formV2->getMailchimpSendDonationData(),
'sendFFMData' => $this->formV2->getMailchimpSendFFMData(),
'defaultAudiences' => $this->formV2->getMailchimpDefaultAudiences(),
];
}
}
Expand Down
109 changes: 109 additions & 0 deletions tests/Feature/FormMigration/Steps/TestMailchimp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Feature\FormMigration\Steps;

namespace Give\Tests\Feature\FormMigration\Steps;

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

class TestMailchimp extends TestCase
{
use RefreshDatabase, LegacyDonationFormAdapter;

/**
* @unreleased
*/
public function testProcessShouldUpdateMailchimpBlockAttributesFromV2FormMeta(): void
{
$meta = [
'_give_mailchimp_custom_label' => __('Subscribe to newsletter?'),
'_give_mailchimp_tags' => ['Animal-Rescue-Campaign', 'Housing-And-Shelter-Campaign'],
'_give_mailchimp' => ['de73f3f82f'],
'_give_mailchimp_checked_default' => true,
'_give_mailchimp_send_donation_data' => true,
'_give_mailchimp_send_ffm' => true,
];

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

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

$mailchimp = new Mailchimp($payload);

$mailchimp->process();

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

$this->assertSame($meta['_give_mailchimp_custom_label'], $block->getAttribute('label'));
$this->assertSame($meta['_give_mailchimp_tags'], $block->getAttribute('subscriberTags'));
$this->assertSame($meta['_give_mailchimp'], $block->getAttribute('defaultAudiences'));
$this->assertTrue(true, $block->getAttribute('checked'));
$this->assertTrue(true, $block->getAttribute('sendDonationData'));
$this->assertTrue(true, $block->getAttribute('sendFFMData'));
}

/**
* @unreleased
*/
public function testProcessShouldUpdateMailchimpBlockAttributesFromGlobalSettings(): void
{
$meta = [
'give_mailchimp_label' => __('Subscribe to newsletter?'),
'give_mailchimp_list' => ['de73f3f82f'],
'give_mailchimp_checked_default' => true,
'give_mailchimp_double_opt_in' => true,
'give_mailchimp_donation_data' => true,
'give_mailchimp_ffm_pass_field' => true,
];

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

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

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

$mailchimp = new Mailchimp($payload);

$mailchimp->process();

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

$this->assertSame($meta['give_mailchimp_label'], $block->getAttribute('label'));
$this->assertSame($meta['give_mailchimp_list'], $block->getAttribute('defaultAudiences'));
$this->assertNull(null, $block->getAttribute('subscriberTags'));
$this->assertTrue(true, $block->getAttribute('checked'));
$this->assertTrue(true, $block->getAttribute('doubleOptIn'));
$this->assertTrue(true, $block->getAttribute('sendDonationData'));
$this->assertTrue(true, $block->getAttribute('sendFFMData'));
}

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

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

$mailchimp = new Mailchimp($payload);

$mailchimp->process();

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

$this->assertSame(__('Subscribe to newsletter?'), $block->getAttribute('label'));
$this->assertNull(null, $block->getAttribute('subscriberTags'));
$this->assertSame([''], $block->getAttribute('defaultAudiences'));
$this->assertTrue(true, $block->getAttribute('checked'));
$this->assertTrue(true, $block->getAttribute('doubleOptIn'));
$this->assertTrue(true, $block->getAttribute('sendDonationData'));
$this->assertTrue(true, $block->getAttribute('sendFFMData'));
}
}

0 comments on commit 181655e

Please sign in to comment.