Skip to content

Commit

Permalink
Test: Add new tests for form migration steps (#7505)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloiankoski authored Aug 24, 2024
1 parent 947525f commit e1b19af
Show file tree
Hide file tree
Showing 12 changed files with 783 additions and 0 deletions.
91 changes: 91 additions & 0 deletions tests/Feature/FormMigration/Steps/TestEmailSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Give\Tests\Feature\FormMigration\Steps;

use Give\FormMigration\Steps\EmailSettings;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;
use Give\Tests\Unit\FormMigration\TestTraits\FormMigrationProcessor;
use Give_Email_Notification_Util;
use Give_Email_Notifications;
use Give_Email_Setting_Field;

/**
* @unreleased
*
* @covers \Give\FormMigration\Steps\EmailSettings
*/
class TestEmailSettings extends TestCase
{
use FormMigrationProcessor;
use LegacyDonationFormAdapter;
use RefreshDatabase;

public function testProcessShouldUpdateEmailSettings(): void
{
// Arrange
$meta = [
'_give_email_options' => 'enabled',
'_give_email_template' => 'default',
'_give_email_logo' => 'logo.png',
'_give_from_name' => 'Charity Org',
'_give_from_email' => '[email protected]',
];

$notifications = Give_Email_Notifications::get_instance()->get_email_notifications();
foreach ($notifications as $notification) {
add_filter("give_{$notification->config['id']}_get_recipients", [$this, 'getNotificationRecipients'], 1, 3);

$prefix = '_give_' . $notification->config['id'];
$notificationMeta = [
$prefix . '_notification' => 'enabled',
$prefix . '_email_subject' => $notification->config['label'],
$prefix . '_email_header' => 'Header for: ' . $notification->config['label'],
$prefix . '_email_message' => 'Message for: ' . $notification->config['label'],
$prefix . '_email_content_type' => 'text/html',
];

if ($notification->config['has_recipient_field']) {
$notificationMeta[$prefix . '_recipient'] = [['email' => '[email protected]']];
}
$meta = array_merge($meta, $notificationMeta);
}
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

// Act
$v3Form = $this->migrateForm($v2Form, EmailSettings::class);

// Assert
$this->assertSame($meta['_give_email_options'], $v3Form->settings->emailOptionsStatus);
$this->assertSame($meta['_give_email_template'], $v3Form->settings->emailTemplate);
$this->assertSame($meta['_give_email_logo'], $v3Form->settings->emailLogo);
$this->assertSame($meta['_give_from_name'], $v3Form->settings->emailFromName);
$this->assertSame($meta['_give_from_email'], $v3Form->settings->emailFromEmail);

foreach ($notifications as $notification) {
$configId = $notification->config['id'];
$this->assertSame('enabled', $v3Form->settings->emailTemplateOptions[$configId]['status']);
$this->assertSame($notification->config['label'], $v3Form->settings->emailTemplateOptions[$configId]['email_subject']);
$this->assertSame('Header for: ' . $notification->config['label'], $v3Form->settings->emailTemplateOptions[$configId]['email_header']);
$this->assertSame('Message for: ' . $notification->config['label'], $v3Form->settings->emailTemplateOptions[$configId]['email_message']);
$this->assertSame('text/html', $v3Form->settings->emailTemplateOptions[$configId]['email_content_type']);

if ($notification->config['has_recipient_field']) {
$this->assertSame(['[email protected]'],
$v3Form->settings->emailTemplateOptions[$configId]['recipient']);
}

remove_filter("give_{$notification->config['id']}_get_recipients", [$this, 'getNotificationRecipients'], 1);
}
}

public function getNotificationRecipients($recipientEmail, $instance, $formId)
{
return Give_Email_Notification_Util::get_value(
$instance,
Give_Email_Setting_Field::get_prefix( $instance, $formId ) . 'recipient',
$formId
);
}
}
114 changes: 114 additions & 0 deletions tests/Feature/FormMigration/Steps/TestFeeRecovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Give\Tests\Feature\FormMigration\Steps;

use Give\FormMigration\Steps\FeeRecovery;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;
use Give\Tests\Unit\FormMigration\TestTraits\FormMigrationProcessor;

/**
* @unreleased
*
* @covers \Give\FormMigration\Steps\FeeRecovery
*/
class TestFeeRecovery extends TestCase
{
use FormMigrationProcessor;
use LegacyDonationFormAdapter;
use RefreshDatabase;

/**
* @unreleased
*/
public function testFeeRecoveryProcessWithGlobalSettings(): void
{
// Arrange
$options = [
'give_fee_recovery' => 'enabled',
'give_fee_configuration' => 'all_gateways',
'give_fee_percentage' => 5,
'give_fee_base_amount' => 0.50,
'give_fee_maximum_fee_amount' => 20.00,
'give_fee_breakdown' => 'enabled',
'give_fee_mode' => 'donor_opt_in',
'give_fee_checkbox_label' => 'Fee Recovery checkbox label',
'give_fee_explanation' => 'Message for fee recovery',
];
foreach ($options as $key => $value) {
give_update_option($key, $value);
}
$meta = ['_form_give_fee_recovery' => 'global'];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

// Act
$v3Form = $this->migrateForm($v2Form, FeeRecovery::class);

// Assert
$block = $v3Form->blocks->findByName('givewp-fee-recovery/fee-recovery');
$this->assertSame(true, $block->getAttribute('useGlobalSettings'));
$this->assertSame(true, $block->getAttribute('feeSupportForAllGateways'));
$this->assertSame([], $block->getAttribute('perGatewaySettings'));
$this->assertSame(5.0, $block->getAttribute('feePercentage'));
$this->assertSame(0.5, $block->getAttribute('feeBaseAmount'));
$this->assertSame(20.0, $block->getAttribute('maxFeeAmount'));
$this->assertSame(true, $block->getAttribute('includeInDonationSummary'));
$this->assertSame(true, $block->getAttribute('donorOptIn'));
$this->assertSame('Fee Recovery checkbox label', $block->getAttribute('feeCheckboxLabel'));
$this->assertSame('Message for fee recovery', $block->getAttribute('feeMessage'));
}

/**
* @unreleased
*/
public function testFeeRecoveryProcessWithPerFormSettings(): void
{
// Arrange
$meta = [
'_form_give_fee_recovery' => 'enabled',
'_form_give_fee_configuration' => 'all_gateways',
'_form_give_fee_percentage' => 5,
'_form_give_fee_base_amount' => 0.50,
'_form_give_fee_maximum_fee_amount' => 20.00,
'_form_breakdown' => 'enabled',
'_form_give_fee_mode' => 'donor_opt_in',
'_form_give_fee_checkbox_label' => 'Fee Recovery checkbox label',
'_form_give_fee_explanation' => 'Message for fee recovery',
];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

// Act
$v3Form = $this->migrateForm($v2Form, FeeRecovery::class);

// Assert
$block = $v3Form->blocks->findByName('givewp-fee-recovery/fee-recovery');
$this->assertSame(false, $block->getAttribute('useGlobalSettings'));
$this->assertSame(true, $block->getAttribute('feeSupportForAllGateways'));
$this->assertSame(5.0, $block->getAttribute('feePercentage'));
$this->assertSame(0.5, $block->getAttribute('feeBaseAmount'));
$this->assertSame(20.0, $block->getAttribute('maxFeeAmount'));
$this->assertSame(true, $block->getAttribute('includeInDonationSummary'));
$this->assertSame(true, $block->getAttribute('donorOptIn'));
$this->assertSame('Fee Recovery checkbox label', $block->getAttribute('feeCheckboxLabel'));
$this->assertSame('Message for fee recovery', $block->getAttribute('feeMessage'));
}

/**
* @unreleased
*/
public function testFeeRecoveryProcessWithGlobalSettingsDisabled(): void
{
// Arrange
give_update_option('give_fee_recovery', 'disabled');
$meta = ['_form_give_fee_recovery' => 'global'];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

// Act
$v3Form = $this->migrateForm($v2Form, FeeRecovery::class);

// Assert
$block = $v3Form->blocks->findByName('givewp-fee-recovery/fee-recovery');
$this->assertNull($block);
}
}
39 changes: 39 additions & 0 deletions tests/Feature/FormMigration/Steps/TestFormExcerpt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Give\Tests\Feature\FormMigration\Steps;

use Give\FormMigration\Steps\FormExcerpt;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;
use Give\Tests\Unit\FormMigration\TestTraits\FormMigrationProcessor;

/**
* @unreleased
*
* @covers \Give\FormMigration\Steps\FormExcerpt
*/
class TestFormExcerpt extends TestCase
{
use FormMigrationProcessor;
use LegacyDonationFormAdapter;
use RefreshDatabase;

/**
* @unreleased
*/
public function testFormExcerptProcess(): void
{
// Arrange
$form = [
'post_excerpt' => 'This is a test excerpt',
];
$v2Form = $this->createSimpleDonationForm(['form' => $form]);

// Act
$v3Form = $this->migrateForm($v2Form, FormExcerpt::class);

// Assert
$this->assertSame($form['post_excerpt'], $v3Form->settings->formExcerpt);
}
}
106 changes: 106 additions & 0 deletions tests/Feature/FormMigration/Steps/TestFormFeaturedImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Give\Tests\Feature\FormMigration\Steps;

use Give\FormMigration\Steps\FormFeaturedImage;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;
use Give\Tests\Unit\FormMigration\TestTraits\FormMigrationProcessor;

/**
* @unreleased
*
* @covers \Give\FormMigration\Steps\FormFeaturedImage
*/
class TestFormFeaturedImage extends TestCase
{
use FormMigrationProcessor;
use LegacyDonationFormAdapter;
use RefreshDatabase;

/**
* @unreleased
*/
public function testSequoiaTemplateFeaturedImageIsMigratedCorrectly(): void
{
// Arrange
$meta = [
'_give_form_template' => 'sequoia',
'_give_sequoia_form_template_settings' => [
'introduction' => [
'image' => 'https://example.com/image.jpg',
],
],
];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

// Act
$v3Form = $this->migrateForm($v2Form, FormFeaturedImage::class);

// Assert
$this->assertSame('https://example.com/image.jpg', $v3Form->settings->designSettingsImageUrl);
$this->assertSame('center', $v3Form->settings->designSettingsImageStyle);
}

/**
* @unreleased
*/
public function testClassicTemplateHeaderBackgroundImageIsMigratedCorrectly(): void
{
// Arrange
$meta = [
'_give_form_template' => 'classic',
'_give_classic_form_template_settings' => [
'visual_appearance' => [
'header_background_image' => 'https://example.com/image.jpg',
],
],
];
$v2Form = $this->createSimpleDonationForm(['meta' => $meta]);

// Act
$v3Form = $this->migrateForm($v2Form, FormFeaturedImage::class);

// Assert
$this->assertSame('https://example.com/image.jpg', $v3Form->settings->designSettingsImageUrl);
}

/**
* @unreleased
*/
public function testFallbackImageIsMigratedWhenFeaturedImageIsMissing(): void
{
// Arrange
$v2Form = $this->createSimpleDonationForm();
update_post_meta($v2Form->id, '_thumbnail_id', '1');
add_filter('wp_get_attachment_image_src', function ($image, $attachmentId) {
if ($attachmentId === 1) {
return ['https://example.com/image.jpg', 100, 100, false];
}

return $image;
}, 10, 2);

// Act
$v3Form = $this->migrateForm($v2Form, FormFeaturedImage::class);

// Assert
$this->assertSame('https://example.com/image.jpg', $v3Form->settings->designSettingsImageUrl);
}

/**
* @unreleased
*/
public function testNoImageIsMigratedWhenNoImageExists ()
{
// Arrange
$v2Form = $this->createSimpleDonationForm();

// Act
$v3Form = $this->migrateForm($v2Form, FormFeaturedImage::class);

// Assert
$this->assertEmpty($v3Form->settings->designSettingsImageUrl);
}
}
Loading

0 comments on commit e1b19af

Please sign in to comment.