Add integration tests for WC checkout opt-in [MAILPOET-1483]
This commit is contained in:
@ -29,6 +29,9 @@ class SetupTest extends \MailPoetTest {
|
|||||||
$signup_confirmation = $settings->fetch('signup_confirmation.enabled');
|
$signup_confirmation = $settings->fetch('signup_confirmation.enabled');
|
||||||
expect($signup_confirmation)->true();
|
expect($signup_confirmation)->true();
|
||||||
|
|
||||||
|
$woocommerce_optin_on_checkout = $settings->fetch('woocommerce.optin_on_checkout');
|
||||||
|
expect($woocommerce_optin_on_checkout['enabled'])->true();
|
||||||
|
|
||||||
$hook_name = 'mailpoet_setup_reset';
|
$hook_name = 'mailpoet_setup_reset';
|
||||||
expect(WPHooksHelper::isActionDone($hook_name))->true();
|
expect(WPHooksHelper::isActionDone($hook_name))->true();
|
||||||
}
|
}
|
||||||
|
@ -146,7 +146,7 @@ class WooCommerceTest extends \MailPoetTest {
|
|||||||
$random_number = 12345;
|
$random_number = 12345;
|
||||||
$subscriber = Subscriber::createOrUpdate(array(
|
$subscriber = Subscriber::createOrUpdate(array(
|
||||||
'email' => 'user-sync-test' . $random_number . '@example.com',
|
'email' => 'user-sync-test' . $random_number . '@example.com',
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED
|
'status' => Subscriber::STATUS_UNSUBSCRIBED
|
||||||
));
|
));
|
||||||
$user = $this->insertRegisteredCustomer($random_number);
|
$user = $this->insertRegisteredCustomer($random_number);
|
||||||
$this->woocommerce_segment->synchronizeCustomers();
|
$this->woocommerce_segment->synchronizeCustomers();
|
||||||
@ -156,13 +156,14 @@ class WooCommerceTest extends \MailPoetTest {
|
|||||||
->findOne();
|
->findOne();
|
||||||
expect($wp_subscriber)->notEmpty();
|
expect($wp_subscriber)->notEmpty();
|
||||||
expect($wp_subscriber->id)->equals($subscriber->id);
|
expect($wp_subscriber->id)->equals($subscriber->id);
|
||||||
|
expect($wp_subscriber->status)->equals(Subscriber::STATUS_UNSUBSCRIBED);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItSynchronizesPresubscribedGuestCustomers() {
|
function testItSynchronizesPresubscribedGuestCustomers() {
|
||||||
$random_number = 12345;
|
$random_number = 12345;
|
||||||
$subscriber = Subscriber::createOrUpdate(array(
|
$subscriber = Subscriber::createOrUpdate(array(
|
||||||
'email' => 'user-sync-test' . $random_number . '@example.com',
|
'email' => 'user-sync-test' . $random_number . '@example.com',
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED
|
'status' => Subscriber::STATUS_UNSUBSCRIBED
|
||||||
));
|
));
|
||||||
$guest = $this->insertGuestCustomer($random_number);
|
$guest = $this->insertGuestCustomer($random_number);
|
||||||
$this->woocommerce_segment->synchronizeCustomers();
|
$this->woocommerce_segment->synchronizeCustomers();
|
||||||
@ -172,6 +173,7 @@ class WooCommerceTest extends \MailPoetTest {
|
|||||||
->findOne();
|
->findOne();
|
||||||
expect($wp_subscriber)->notEmpty();
|
expect($wp_subscriber)->notEmpty();
|
||||||
expect($wp_subscriber->email)->equals($subscriber->email);
|
expect($wp_subscriber->email)->equals($subscriber->email);
|
||||||
|
expect($wp_subscriber->status)->equals(Subscriber::STATUS_UNSUBSCRIBED);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItDoesNotSynchronizeEmptyEmailsForNewUsers() {
|
function testItDoesNotSynchronizeEmptyEmailsForNewUsers() {
|
||||||
|
170
tests/integration/WooCommerce/SubscriptionTest.php
Normal file
170
tests/integration/WooCommerce/SubscriptionTest.php
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\WooCommerce;
|
||||||
|
|
||||||
|
use Codeception\Util\Fixtures;
|
||||||
|
use MailPoet\DI\ContainerWrapper;
|
||||||
|
use MailPoet\Models\Segment;
|
||||||
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Models\SubscriberSegment;
|
||||||
|
use MailPoet\Segments\WP;
|
||||||
|
use MailPoet\Settings\SettingsController;
|
||||||
|
use MailPoet\Subscribers\Source;
|
||||||
|
|
||||||
|
class SubscriptionTest extends \MailPoetTest {
|
||||||
|
function _before() {
|
||||||
|
$this->order_id = 123; // dummy
|
||||||
|
$this->subscription = ContainerWrapper::getInstance()->get(Subscription::class);
|
||||||
|
$this->settings = new SettingsController();
|
||||||
|
$this->wc_segment = Segment::getWooCommerceSegment();
|
||||||
|
|
||||||
|
$subscriber = Subscriber::create();
|
||||||
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
||||||
|
$subscriber->is_woocommerce_user = 1;
|
||||||
|
$this->subscriber = $subscriber->save();
|
||||||
|
|
||||||
|
// back up settings
|
||||||
|
$this->original_settings = $this->settings->get('woocommerce');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDisplaysACheckedCheckboxIfCurrentUserIsSubscribed() {
|
||||||
|
WP::synchronizeUsers();
|
||||||
|
$wp_users = get_users();
|
||||||
|
wp_set_current_user($wp_users[0]->ID);
|
||||||
|
$subscriber = Subscriber::getCurrentWPUser();
|
||||||
|
SubscriberSegment::subscribeToSegments(
|
||||||
|
$subscriber,
|
||||||
|
array($this->wc_segment->id)
|
||||||
|
);
|
||||||
|
expect($this->getRenderedOptinField())->contains('checked');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDisplaysAnUncheckedCheckboxIfCurrentUserIsNotSubscribed() {
|
||||||
|
WP::synchronizeUsers();
|
||||||
|
$wp_users = get_users();
|
||||||
|
wp_set_current_user($wp_users[0]->ID);
|
||||||
|
$subscriber = Subscriber::getCurrentWPUser();
|
||||||
|
SubscriberSegment::unsubscribeFromSegments(
|
||||||
|
$subscriber,
|
||||||
|
array($this->wc_segment->id)
|
||||||
|
);
|
||||||
|
expect($this->getRenderedOptinField())->notContains('checked');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDisplaysAnUncheckedCheckboxIfCurrentUserIsNotLoggedIn() {
|
||||||
|
wp_set_current_user(0);
|
||||||
|
expect($this->getRenderedOptinField())->notContains('checked');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDisplaysCheckboxOptinMessageFromSettings() {
|
||||||
|
$new_message = 'This is a test message.';
|
||||||
|
$this->settings->set(Subscription::OPTIN_MESSAGE_SETTING_NAME, $new_message);
|
||||||
|
expect($this->getRenderedOptinField())->contains($new_message);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItsTemplateCanBeOverriddenByAHook() {
|
||||||
|
$new_template = 'This is a new template';
|
||||||
|
add_filter(
|
||||||
|
'mailpoet_woocommerce_checkout_optin_template',
|
||||||
|
function ($template, $input_name, $checked, $label_string) use ($new_template) {
|
||||||
|
return $new_template . $input_name . $checked . $label_string;
|
||||||
|
},
|
||||||
|
10,
|
||||||
|
4
|
||||||
|
);
|
||||||
|
$result = $this->getRenderedOptinField();
|
||||||
|
expect($result)->contains($new_template);
|
||||||
|
expect($result)->contains(Subscription::CHECKOUT_OPTIN_INPUT_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotTryToSubscribeIfThereIsNoEmailInOrderData() {
|
||||||
|
$data = [];
|
||||||
|
$subscribed = $this->subscription->subscribeOnCheckout($this->order_id, $data);
|
||||||
|
expect($subscribed)->equals(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotTryToSubscribeIfSubscriberWithTheEmailWasNotSynced() {
|
||||||
|
// non-existent
|
||||||
|
$data['billing_email'] = 'non-existent-subscriber@example.com';
|
||||||
|
$subscribed = $this->subscription->subscribeOnCheckout($this->order_id, $data);
|
||||||
|
expect($subscribed)->equals(null);
|
||||||
|
// not a WooCommerce user
|
||||||
|
$this->subscriber->is_woocommerce_user = 0;
|
||||||
|
$this->subscriber->save();
|
||||||
|
$data['billing_email'] = $this->subscriber->email;
|
||||||
|
$subscribed = $this->subscription->subscribeOnCheckout($this->order_id, $data);
|
||||||
|
expect($subscribed)->equals(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItUnsubscribesIfCheckoutOptinIsDisabled() {
|
||||||
|
SubscriberSegment::subscribeToSegments(
|
||||||
|
$this->subscriber,
|
||||||
|
array($this->wc_segment->id)
|
||||||
|
);
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(1);
|
||||||
|
|
||||||
|
$this->settings->set(Subscription::OPTIN_ENABLED_SETTING_NAME, false);
|
||||||
|
$data['billing_email'] = $this->subscriber->email;
|
||||||
|
$subscribed = $this->subscription->subscribeOnCheckout($this->order_id, $data);
|
||||||
|
expect($subscribed)->equals(false);
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItUnsubscribesIfCheckboxIsNotChecked() {
|
||||||
|
SubscriberSegment::subscribeToSegments(
|
||||||
|
$this->subscriber,
|
||||||
|
array($this->wc_segment->id)
|
||||||
|
);
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(1);
|
||||||
|
|
||||||
|
$this->settings->set(Subscription::OPTIN_ENABLED_SETTING_NAME, true);
|
||||||
|
$data['billing_email'] = $this->subscriber->email;
|
||||||
|
$subscribed = $this->subscription->subscribeOnCheckout($this->order_id, $data);
|
||||||
|
expect($subscribed)->equals(false);
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItSubscribesIfCheckboxIsChecked() {
|
||||||
|
$this->subscriber->status = Subscriber::STATUS_UNSUBSCRIBED;
|
||||||
|
$this->subscriber->save();
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(0);
|
||||||
|
|
||||||
|
$this->settings->set(Subscription::OPTIN_ENABLED_SETTING_NAME, true);
|
||||||
|
$_POST[Subscription::CHECKOUT_OPTIN_INPUT_NAME] = 'on';
|
||||||
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||||
|
$data['billing_email'] = $this->subscriber->email;
|
||||||
|
$subscribed = $this->subscription->subscribeOnCheckout($this->order_id, $data);
|
||||||
|
expect($subscribed)->equals(true);
|
||||||
|
unset($_POST[Subscription::CHECKOUT_OPTIN_INPUT_NAME]);
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(1);
|
||||||
|
|
||||||
|
$subscriber = Subscriber::findOne($this->subscriber->id);
|
||||||
|
expect($subscriber->source)->equals(Source::WOOCOMMERCE_CHECKOUT);
|
||||||
|
expect($subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
|
expect($subscriber->confirmed_ip)->notEmpty();
|
||||||
|
expect($subscriber->confirmed_at)->notEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getRenderedOptinField() {
|
||||||
|
ob_start();
|
||||||
|
$this->subscription->extendWooCommerceCheckoutForm();
|
||||||
|
$result = ob_get_clean();
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _after() {
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
||||||
|
// restore settings
|
||||||
|
$this->settings->set('woocommerce', $this->original_settings);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user