diff --git a/assets/js/src/segments/list.jsx b/assets/js/src/segments/list.jsx index b2d9a6d423..612584949f 100644 --- a/assets/js/src/segments/list.jsx +++ b/assets/js/src/segments/list.jsx @@ -6,6 +6,10 @@ import PropTypes from 'prop-types'; import Listing from 'listing/listing.jsx'; +const isWPUsersSegment = segment => segment.type === 'wp_users'; +const isWooCommerceCustomersSegment = segment => segment.type === 'woocommerce_users'; +const isSpecialSegment = segmt => isWPUsersSegment(segmt) || isWooCommerceCustomersSegment(segmt); + const columns = [ { name: 'name', @@ -104,7 +108,7 @@ const itemActions = [ ); }, display: function display(segment) { - return (segment.type !== 'wp_users'); + return isWPUsersSegment(segment); }, }, { @@ -129,7 +133,7 @@ const itemActions = [ ); }), display: function display(segment) { - return (segment.type !== 'wp_users'); + return !isSpecialSegment(segment); }, }, { @@ -144,7 +148,7 @@ const itemActions = [ ); }, display: function display(segment) { - return (segment.type === 'wp_users'); + return isWPUsersSegment(segment); }, }, { @@ -173,7 +177,7 @@ const itemActions = [ }); }, display: function display(segment) { - return (segment.type === 'wp_users'); + return isWPUsersSegment(segment); }, }, { @@ -187,7 +191,7 @@ const itemActions = [ { name: 'trash', display: function display(segment) { - return (segment.type !== 'wp_users'); + return !isSpecialSegment(segment); }, }, ]; @@ -207,8 +211,9 @@ class SegmentList extends React.Component { let segmentName; - if (segment.type === 'wp_users') { - // the WP users segment is not editable so just display its name + if (isSpecialSegment(segment)) { + // the WP users and WooCommerce customers segments + // are not editable so just display their names segmentName = ( { segment.name } ); diff --git a/assets/js/src/subscribers/form.jsx b/assets/js/src/subscribers/form.jsx index ebdc1461ea..a26268afe8 100644 --- a/assets/js/src/subscribers/form.jsx +++ b/assets/js/src/subscribers/form.jsx @@ -11,7 +11,7 @@ const fields = [ label: MailPoet.I18n.t('email'), type: 'text', disabled: function disabled(subscriber) { - return Number(subscriber.wp_user_id > 0); + return Number(subscriber.wp_user_id > 0) || Number(subscriber.is_woocommerce_user) === 1; }, }, { @@ -19,7 +19,7 @@ const fields = [ label: MailPoet.I18n.t('firstname'), type: 'text', disabled: function disabled(subscriber) { - return Number(subscriber.wp_user_id > 0); + return Number(subscriber.wp_user_id > 0) || Number(subscriber.is_woocommerce_user) === 1; }, }, { @@ -27,7 +27,7 @@ const fields = [ label: MailPoet.I18n.t('lastname'), type: 'text', disabled: function disabled(subscriber) { - return Number(subscriber.wp_user_id > 0); + return Number(subscriber.wp_user_id > 0) || Number(subscriber.is_woocommerce_user) === 1; }, }, { @@ -41,7 +41,7 @@ const fields = [ bounced: MailPoet.I18n.t('bounced'), }, filter: function filter(subscriber, value) { - if (Number(subscriber.wp_user_id) > 0 && value === 'unconfirmed') { + if ((Number(subscriber.wp_user_id) > 0 || Number(subscriber.is_woocommerce_user) === 1) && value === 'unconfirmed') { return false; } return true; diff --git a/assets/js/src/subscribers/list.jsx b/assets/js/src/subscribers/list.jsx index 3528e8d24c..f7671282c7 100644 --- a/assets/js/src/subscribers/list.jsx +++ b/assets/js/src/subscribers/list.jsx @@ -238,7 +238,7 @@ const itemActions = [ { name: 'trash', display: function display(subscriber) { - return Number(subscriber.wp_user_id) === 0; + return Number(subscriber.wp_user_id) === 0 && Number(subscriber.is_woocommerce_user) === 0; }, }, ]; diff --git a/lib/API/MP/v1/API.php b/lib/API/MP/v1/API.php index c69e81a5c2..f8c99b4d1e 100644 --- a/lib/API/MP/v1/API.php +++ b/lib/API/MP/v1/API.php @@ -86,12 +86,15 @@ class API { throw new \Exception($exception); } - // throw exception when trying to subscribe to a WP Users segment + // throw exception when trying to subscribe to WP Users or WooCommerce Customers segments $found_segments_ids = array(); foreach($found_segments as $found_segment) { if($found_segment->type === Segment::TYPE_WP_USERS) { throw new \Exception(__(sprintf("Can't subscribe to a WordPress Users list with ID %d.", $found_segment->id), 'mailpoet')); } + if($found_segment->type === Segment::TYPE_WC_USERS) { + throw new \Exception(__(sprintf("Can't subscribe to a WooCommerce Customers list with ID %d.", $found_segment->id), 'mailpoet')); + } $found_segments_ids[] = $found_segment->id; } @@ -137,12 +140,15 @@ class API { throw new \Exception($exception); } - // throw exception when trying to subscribe to a WP Users segment + // throw exception when trying to subscribe to WP Users or WooCommerce Customers segments $found_segments_ids = array(); foreach($found_segments as $segment) { if($segment->type === Segment::TYPE_WP_USERS) { throw new \Exception(__(sprintf("Can't subscribe to a WordPress Users list with ID %d.", $segment->id), 'mailpoet')); } + if($segment->type === Segment::TYPE_WC_USERS) { + throw new \Exception(__(sprintf("Can't subscribe to a WooCommerce Customers list with ID %d.", $segment->id), 'mailpoet')); + } $found_segments_ids[] = $segment->id; } @@ -161,7 +167,8 @@ class API { } function getLists() { - return Segment::whereNotEqual('type', Segment::TYPE_WP_USERS)->findArray(); + return Segment::whereNotIn('type', [Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS]) + ->findArray(); } function addSubscriber(array $subscriber, $segments = array(), $options = array()) { diff --git a/lib/Config/MP2Migrator.php b/lib/Config/MP2Migrator.php index 3701978a5e..53db4b8fce 100644 --- a/lib/Config/MP2Migrator.php +++ b/lib/Config/MP2Migrator.php @@ -221,14 +221,14 @@ class MP2Migrator { } /** - * Delete the existing segments except the wp_users segment + * Delete the existing segments except the wp_users and woocommerce_users segments * */ private function deleteSegments() { global $wpdb; $table = MP_SEGMENTS_TABLE; - $wpdb->query("DELETE FROM {$table} WHERE type != '" . Segment::TYPE_WP_USERS . "'"); + $wpdb->query("DELETE FROM {$table} WHERE type != '" . Segment::TYPE_WP_USERS . "' AND type != '" . Segment::TYPE_WC_USERS ."'"); } /** diff --git a/lib/Config/Migrator.php b/lib/Config/Migrator.php index 3d7f2e0e6a..2f195f0ec8 100644 --- a/lib/Config/Migrator.php +++ b/lib/Config/Migrator.php @@ -166,6 +166,7 @@ class Migrator { $attributes = array( 'id int(11) unsigned NOT NULL AUTO_INCREMENT,', 'wp_user_id bigint(20) NULL,', + 'is_woocommerce_user int(1) NOT NULL DEFAULT 0,', 'first_name varchar(255) NOT NULL DEFAULT "",', 'last_name varchar(255) NOT NULL DEFAULT "",', 'email varchar(150) NOT NULL,', diff --git a/lib/Config/Populator.php b/lib/Config/Populator.php index 37b3f1c357..2488a380b8 100644 --- a/lib/Config/Populator.php +++ b/lib/Config/Populator.php @@ -191,6 +191,8 @@ class Populator { private function createDefaultSegments() { // WP Users segment Segment::getWPSegment(); + // WooCommerce customers segment + Segment::getWooCommerceSegment(); // Synchronize WP Users WP::synchronizeUsers(); @@ -399,6 +401,12 @@ class Populator { ' WHERE `source` = "' . Source::UNKNOWN . '"' . ' AND `wp_user_id` IS NOT NULL' ); + Subscriber::rawExecute( + 'UPDATE LOW_PRIORITY `' . Subscriber::$_table . '`' . + ' SET `source` = "' . Source::WOOCOMMERCE_USER . '"' . + ' WHERE `source` = "' . Source::UNKNOWN . '"' . + ' AND `is_woocommerce_user` = 1' + ); } private function updateNewsletterCategories() { diff --git a/lib/Models/Segment.php b/lib/Models/Segment.php index fc20251420..bc8a7feb8c 100644 --- a/lib/Models/Segment.php +++ b/lib/Models/Segment.php @@ -6,6 +6,7 @@ if(!defined('ABSPATH')) exit; class Segment extends Model { static $_table = MP_SEGMENTS_TABLE; const TYPE_WP_USERS = 'wp_users'; + const TYPE_WC_USERS = 'woocommerce_users'; const TYPE_DEFAULT = 'default'; function __construct() { @@ -114,7 +115,7 @@ class Segment extends Model { 'name' => __('WordPress Users', 'mailpoet'), 'description' => __('This list contains all of your WordPress users.', 'mailpoet'), - 'type' => 'wp_users' + 'type' => self::TYPE_WP_USERS )); $wp_segment->save(); } @@ -122,6 +123,24 @@ class Segment extends Model { return $wp_segment; } + static function getWooCommerceSegment() { + $wc_segment = self::where('type', self::TYPE_WC_USERS)->findOne(); + + if($wc_segment === false) { + // create the WooCommerce customers segment + $wc_segment = Segment::create(); + $wc_segment->hydrate(array( + 'name' => __('WooCommerce Customers', 'mailpoet'), + 'description' => + __('This list contains all of your WooCommerce customers.', 'mailpoet'), + 'type' => self::TYPE_WC_USERS + )); + $wc_segment->save(); + } + + return $wc_segment; + } + static function search($orm, $search = '') { return $orm->whereLike('name', '%'.$search.'%'); } @@ -152,7 +171,7 @@ class Segment extends Model { static function getSegmentsWithSubscriberCount($type = self::TYPE_DEFAULT) { $query = self::selectMany(array(self::$_table.'.id', self::$_table.'.name')) - ->whereIn('type', array(Segment::TYPE_WP_USERS, Segment::TYPE_DEFAULT)) + ->whereIn('type', array(Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS)) ->selectExpr( self::$_table.'.*, ' . 'COUNT(IF('. @@ -182,7 +201,10 @@ class Segment extends Model { } static function getSegmentsForImport() { - return self::getSegmentsWithSubscriberCount($type = false); + $segments = self::getSegmentsWithSubscriberCount($type = false); + return array_values(array_filter($segments, function($segment) { + return $segment['type'] !== Segment::TYPE_WC_USERS; + })); } static function getSegmentsForExport() { @@ -207,7 +229,7 @@ class Segment extends Model { static function listingQuery(array $data = array()) { $query = self::select('*'); - $query->whereIn('type', array(Segment::TYPE_WP_USERS, Segment::TYPE_DEFAULT)); + $query->whereIn('type', array(Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS)); if(isset($data['group'])) { $query->filter('groupBy', $data['group']); } diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index 0f1c088329..4f0b4e595b 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -55,7 +55,7 @@ class Subscriber extends Model { function delete() { // WP Users cannot be deleted - if($this->isWPUser()) { + if($this->isWPUser() || $this->isWooCommerceUser()) { return false; } else { // delete all relations to segments @@ -68,7 +68,7 @@ class Subscriber extends Model { function trash() { // WP Users cannot be trashed - if($this->isWPUser()) { + if($this->isWPUser() || $this->isWooCommerceUser()) { return false; } else { return parent::trash(); @@ -79,6 +79,10 @@ class Subscriber extends Model { return ($this->wp_user_id !== null); } + function isWooCommerceUser() { + return $this->is_woocommerce_user == true; + } + static function getCurrentWPUser() { $wp_user = wp_get_current_user(); return self::where('wp_user_id', $wp_user->ID)->findOne(); @@ -173,6 +177,7 @@ class Subscriber extends Model { $reserved_columns = array( 'id', 'wp_user_id', + 'is_woocommerce_user', 'status', 'subscribed_ip', 'confirmed_ip', @@ -205,7 +210,7 @@ class Subscriber extends Model { $segments = Segment::orderByAsc('name') ->whereNull('deleted_at') - ->whereIn('type', array(Segment::TYPE_WP_USERS, Segment::TYPE_DEFAULT)) + ->whereIn('type', array(Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS)) ->findMany(); $segment_list = array(); $segment_list[] = array( @@ -659,7 +664,8 @@ class Subscriber extends Model { 'WHERE `id` IN ('. rtrim(str_repeat('?,', count($subscriber_ids)), ',') .')', - 'AND `wp_user_id` IS NULL' + 'AND `wp_user_id` IS NULL', + 'AND `is_woocommerce_user` = 0' )), $subscriber_ids ); @@ -677,6 +683,7 @@ class Subscriber extends Model { // delete subscribers (except WP Users) Subscriber::whereIn('id', $subscriber_ids) ->whereNull('wp_user_id') + ->whereEqual('is_woocommerce_user', 0) ->deleteMany(); }); @@ -738,6 +745,7 @@ class Subscriber extends Model { static function updateMultiple($columns, $subscribers, $updated_at = false) { $ignore_columns_on_update = array( 'wp_user_id', + 'is_woocommerce_user', 'email', 'created_at', 'status' diff --git a/lib/Models/SubscriberSegment.php b/lib/Models/SubscriberSegment.php index 395b21db22..2584b0af69 100644 --- a/lib/Models/SubscriberSegment.php +++ b/lib/Models/SubscriberSegment.php @@ -15,13 +15,16 @@ class SubscriberSegment extends Model { if($subscriber === false) return false; $wp_segment = Segment::getWPSegment(); + $wc_segment = Segment::getWooCommerceSegment(); if(!empty($segment_ids)) { // unsubscribe from segments foreach($segment_ids as $segment_id) { - // do not remove subscriptions to the WP Users segment - if($wp_segment !== false && (int)$wp_segment->id === (int)$segment_id) { + // do not remove subscriptions to the WP Users or WooCommerce Customers segments + if(($wp_segment !== false && (int)$wp_segment->id === (int)$segment_id) + || ($wc_segment !== false && (int)$wc_segment->id === (int)$segment_id) + ) { continue; } @@ -34,7 +37,7 @@ class SubscriberSegment extends Model { } } } else { - // unsubscribe from all segments (except the WP users segment) + // unsubscribe from all segments (except the WP users and WooCommerce customers segments) $subscriptions = self::where('subscriber_id', $subscriber->id); if($wp_segment !== false) { @@ -42,6 +45,11 @@ class SubscriberSegment extends Model { 'segment_id', $wp_segment->id ); } + if($wc_segment !== false) { + $subscriptions = $subscriptions->whereNotEqual( + 'segment_id', $wc_segment->id + ); + } $subscriptions->findResultSet() ->set('status', Subscriber::STATUS_UNSUBSCRIBED) @@ -113,17 +121,23 @@ class SubscriberSegment extends Model { static function deleteManySubscriptions($subscriber_ids = array(), $segment_ids = array()) { if(empty($subscriber_ids)) return false; - // delete subscribers' relations to segments (except WP segment) + // delete subscribers' relations to segments (except WP and WooCommerce segments) $subscriptions = self::whereIn( 'subscriber_id', $subscriber_ids ); $wp_segment = Segment::getWPSegment(); + $wc_segment = Segment::getWooCommerceSegment(); if($wp_segment !== false) { $subscriptions = $subscriptions->whereNotEqual( 'segment_id', $wp_segment->id ); } + if($wc_segment !== false) { + $subscriptions = $subscriptions->whereNotEqual( + 'segment_id', $wc_segment->id + ); + } if(!empty($segment_ids)) { $subscriptions = $subscriptions->whereIn('segment_id', $segment_ids); @@ -136,9 +150,10 @@ class SubscriberSegment extends Model { if($subscriber === false) return false; $wp_segment = Segment::getWPSegment(); + $wc_segment = Segment::getWooCommerceSegment(); $subscriptions = self::where('subscriber_id', $subscriber->id) - ->whereNotEqual('segment_id', $wp_segment->id); + ->whereNotIn('segment_id', [$wp_segment->id, $wc_segment->id]); if(!empty($segment_ids)) { $subscriptions = $subscriptions->whereIn('segment_id', $segment_ids); diff --git a/lib/Segments/BulkAction.php b/lib/Segments/BulkAction.php index d45dacface..7576a1b256 100644 --- a/lib/Segments/BulkAction.php +++ b/lib/Segments/BulkAction.php @@ -36,7 +36,9 @@ class BulkAction { * @throws \Exception */ private function applySegment($segment) { - if(!$segment || $segment['type'] === Segment::TYPE_DEFAULT || $segment['type'] === Segment::TYPE_WP_USERS) { + if(!$segment + || in_array($segment['type'], [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS], true) + ) { $bulk_action = new \MailPoet\Listing\BulkActionController(new Handler()); return $bulk_action->apply('\MailPoet\Models\Subscriber', $this->data); } else { diff --git a/lib/Segments/SubscribersFinder.php b/lib/Segments/SubscribersFinder.php index 881be1ee64..2ffce4bd0d 100644 --- a/lib/Segments/SubscribersFinder.php +++ b/lib/Segments/SubscribersFinder.php @@ -36,7 +36,7 @@ class SubscribersFinder { } private function isStaticSegment(Segment $segment) { - return $segment->type === Segment::TYPE_DEFAULT || $segment->type === Segment::TYPE_WP_USERS; + return in_array($segment->type, [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS], true); } function addSubscribersToTaskFromSegments(ScheduledTask $task, array $segments) { diff --git a/lib/Segments/SubscribersListings.php b/lib/Segments/SubscribersListings.php index 38dbfd4424..899aa3f4b7 100644 --- a/lib/Segments/SubscribersListings.php +++ b/lib/Segments/SubscribersListings.php @@ -25,8 +25,9 @@ class SubscribersListings { } private function getListings($data, Segment $segment = null) { - if(!$segment || $segment->type === Segment::TYPE_DEFAULT || $segment->type === Segment::TYPE_WP_USERS) { - + if(!$segment + || in_array($segment->type, [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS], true) + ) { return $listing_data = $this->handler->get('\MailPoet\Models\Subscriber', $data); } $handlers = Hooks::applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', array()); diff --git a/lib/Segments/WP.php b/lib/Segments/WP.php index 457270ebac..0ccb4bd4d8 100644 --- a/lib/Segments/WP.php +++ b/lib/Segments/WP.php @@ -170,7 +170,7 @@ class WP { SET %1$s.last_name = wpum.meta_value WHERE %1$s.last_name = "" AND %1$s.wp_user_id IS NOT NULL - AND wpum.meta_value IS NOT NULL + AND wpum.meta_value IS NOT NULL ', $subscribers_table, $wpdb->usermeta)); } diff --git a/lib/Subscribers/Source.php b/lib/Subscribers/Source.php index b19b606486..7c2973dcf2 100644 --- a/lib/Subscribers/Source.php +++ b/lib/Subscribers/Source.php @@ -11,6 +11,7 @@ class Source { const ADMINISTRATOR = 'administrator'; const API = 'api'; const WORDPRESS_USER = 'wordpress_user'; + const WOOCOMMERCE_USER = 'woocommerce_user'; const UNKNOWN = 'unknown'; private static $allowed_sources = array( @@ -19,6 +20,7 @@ class Source { Source::ADMINISTRATOR, Source::API, Source::WORDPRESS_USER, + Source::WOOCOMMERCE_USER, Source::UNKNOWN, ); diff --git a/lib/Subscription/Pages.php b/lib/Subscription/Pages.php index 2e54ac2d55..8186ab5202 100644 --- a/lib/Subscription/Pages.php +++ b/lib/Subscription/Pages.php @@ -294,7 +294,7 @@ class Pages { 'params' => array( 'label' => __('First name', 'mailpoet'), 'value' => $subscriber->first_name, - 'disabled' => ($subscriber->isWPUser()) + 'disabled' => ($subscriber->isWPUser() || $subscriber->isWooCommerceUser()) ) ), array( @@ -303,7 +303,7 @@ class Pages { 'params' => array( 'label' => __('Last name', 'mailpoet'), 'value' => $subscriber->last_name, - 'disabled' => ($subscriber->isWPUser()) + 'disabled' => ($subscriber->isWPUser() || $subscriber->isWooCommerceUser()) ) ), array( @@ -387,7 +387,7 @@ class Pages { $form_html .= ''; $form_html .= '
'; // special case for WP users as they cannot edit their subscriber's email - if($subscriber->isWPUser()) { + if($subscriber->isWPUser() || $subscriber->isWooCommerceUser()) { // check if subscriber's associated WP user is the currently logged in WP user $wp_current_user = wp_get_current_user(); if($wp_current_user->user_email === $subscriber->email) { diff --git a/tests/_data/acceptanceDump.sql b/tests/_data/acceptanceDump.sql index aa416233f6..2309867ed3 100644 --- a/tests/_data/acceptanceDump.sql +++ b/tests/_data/acceptanceDump.sql @@ -323,7 +323,8 @@ CREATE TABLE `mp_mailpoet_segments` ( INSERT INTO `mp_mailpoet_segments` (`id`, `name`, `type`, `description`, `created_at`, `updated_at`, `deleted_at`) VALUES (1, 'WordPress Users', 'wp_users', 'This list contains all of your WordPress users.', '2017-10-30 00:57:39', '2017-10-30 00:57:39', NULL), -(2, 'My First List', 'default', 'This list is automatically created when you install MailPoet.', '2017-10-30 00:57:39', '2017-10-30 00:57:39', NULL); +(2, 'WooCommerce Customers', 'woocommerce_users', 'This list contains all of your WooCommerce customers.', '2019-01-17 00:57:39', '2019-01-17 00:57:39', NULL), +(3, 'My First List', 'default', 'This list is automatically created when you install MailPoet.', '2017-10-30 00:57:39', '2017-10-30 00:57:39', NULL); DROP TABLE IF EXISTS `mp_mailpoet_sending_queues`; CREATE TABLE `mp_mailpoet_sending_queues` ( diff --git a/tests/acceptance/ListsListingCest.php b/tests/acceptance/ListsListingCest.php index 052b7928f6..6a1ec12e03 100644 --- a/tests/acceptance/ListsListingCest.php +++ b/tests/acceptance/ListsListingCest.php @@ -11,7 +11,8 @@ class ListsListingCest { $I->amOnMailpoetPage('Lists'); $I->waitForText('WordPress Users', 5, '[data-automation-id="listing_item_1"]'); - $I->see('My First List', '[data-automation-id="listing_item_2"]'); + $I->see('WooCommerce Customers', '[data-automation-id="listing_item_2"]'); + $I->see('My First List', '[data-automation-id="listing_item_3"]'); $I->seeNoJSErrors(); } diff --git a/tests/acceptance/ReinstallFromScratchCest.php b/tests/acceptance/ReinstallFromScratchCest.php index 489e9db102..14eb832412 100644 --- a/tests/acceptance/ReinstallFromScratchCest.php +++ b/tests/acceptance/ReinstallFromScratchCest.php @@ -59,8 +59,9 @@ class ReinstallFromScratchCest { // Check lists $I->amOnMailpoetPage('Lists'); $I->waitForText('WordPress Users', 30, '[data-automation-id="listing_item_1"]'); - $I->see('My First List', '[data-automation-id="listing_item_2"]'); - $I->seeNumberOfElements('[data-automation-id^=listing_item_]', 2); + $I->see('WooCommerce Customers', '[data-automation-id="listing_item_2"]'); + $I->see('My First List', '[data-automation-id="listing_item_3"]'); + $I->seeNumberOfElements('[data-automation-id^=listing_item_]', 3); // Check subscribers $I->amOnMailPoetPage('Subscribers'); $I->waitForText('admin', 30, '[data-automation-id="listing_item_1"]'); diff --git a/tests/integration/API/MP/APITest.php b/tests/integration/API/MP/APITest.php index 3207ad8e93..f6bf52cc78 100644 --- a/tests/integration/API/MP/APITest.php +++ b/tests/integration/API/MP/APITest.php @@ -104,6 +104,24 @@ class APITest extends \MailPoetTest { } } + function testItDoesNotSubscribeSubscriberToWooCommerceCustomersList() { + $subscriber = Subscriber::create(); + $subscriber->hydrate(Fixtures::get('subscriber_template')); + $subscriber->save(); + $segment = Segment::createOrUpdate( + array( + 'name' => 'Default', + 'type' => Segment::TYPE_WC_USERS + ) + ); + try { + $this->getApi()->subscribeToLists($subscriber->id, array($segment->id)); + $this->fail('WooCommerce Customers segment exception should have been thrown.'); + } catch(\Exception $e) { + expect($e->getMessage())->equals("Can't subscribe to a WooCommerce Customers list with ID {$segment->id}."); + } + } + function testItDoesNotSubscribeSubscriberToListsWhenOneOrMoreListsAreMissing() { $subscriber = Subscriber::create(); $subscriber->hydrate(Fixtures::get('subscriber_template')); @@ -261,7 +279,7 @@ class APITest extends \MailPoetTest { expect($result[0]['id'])->equals($segment->id); } - function testItExcludesWPUsersSegmentWhenGettingSegments() { + function testItExcludesWPUsersAndWooCommerceCustomersSegmentsWhenGettingSegments() { $default_segment = Segment::createOrUpdate( array( 'name' => 'Default', @@ -274,6 +292,12 @@ class APITest extends \MailPoetTest { 'type' => Segment::TYPE_WP_USERS ) ); + $wc_segment = Segment::createOrUpdate( + array( + 'name' => 'Default', + 'type' => Segment::TYPE_WC_USERS + ) + ); $result = $this->getApi()->getLists(); expect($result)->count(1); expect($result[0]['id'])->equals($default_segment->id); @@ -601,6 +625,24 @@ class APITest extends \MailPoetTest { } } + function testItDoesNotUnsubscribeSubscriberFromWooCommerceCustomersList() { + $subscriber = Subscriber::create(); + $subscriber->hydrate(Fixtures::get('subscriber_template')); + $subscriber->save(); + $segment = Segment::createOrUpdate( + array( + 'name' => 'Default', + 'type' => Segment::TYPE_WC_USERS + ) + ); + try { + $this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id)); + $this->fail('WooCommerce Customers segment exception should have been thrown.'); + } catch(\Exception $e) { + expect($e->getMessage())->equals("Can't subscribe to a WooCommerce Customers list with ID {$segment->id}."); + } + } + function testItUsesMultipleListsUnsubscribeMethodWhenUnsubscribingFromSingleList() { // unsubscribing from single list = converting list ID to an array and using // multiple lists unsubscribe method diff --git a/tests/integration/Config/MP2MigratorTest.php b/tests/integration/Config/MP2MigratorTest.php index 71c2e0cedb..338a7a85e1 100644 --- a/tests/integration/Config/MP2MigratorTest.php +++ b/tests/integration/Config/MP2MigratorTest.php @@ -121,7 +121,7 @@ class MP2MigratorTest extends \MailPoetTest { $this->initImport(); $this->loadMP2Fixtures(); $this->invokeMethod($this->MP2Migrator, 'importSegments'); - expect(Segment::count())->equals(3); + expect(Segment::count())->equals(4); // two regular lists, WP users list, WooCommerce customers list (not imported) // Check a segment data $this->initImport(); diff --git a/tests/integration/Models/SubscriberSegmentTest.php b/tests/integration/Models/SubscriberSegmentTest.php index 42774147d8..0d14a3f428 100644 --- a/tests/integration/Models/SubscriberSegmentTest.php +++ b/tests/integration/Models/SubscriberSegmentTest.php @@ -18,6 +18,7 @@ class SubscriberSegmentTest extends \MailPoetTest { $this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2')); $this->wp_segment = Segment::getWPSegment(); + $this->wc_segment = Segment::getWooCommerceSegment(); } function testItCanSubscribeToSegments() { @@ -77,12 +78,13 @@ class SubscriberSegmentTest extends \MailPoetTest { expect($subscribed_segments[0]['name'])->equals($this->segment_2->name); } - function testItDoesNotUnsubscribeFromWPSegment() { + function testItDoesNotUnsubscribeFromWPAndWooCommerceSegments() { $subscriber = $this->subscriber; $segment_1 = $this->segment_1; $segment_1->type = Segment::TYPE_WP_USERS; $segment_1->save(); $segment_2 = $this->segment_2; + $segment_3 = $this->wc_segment; $subscriber_segment = SubscriberSegment::createOrUpdate( array( 'subscriber_id' => $subscriber->id, @@ -97,22 +99,33 @@ class SubscriberSegmentTest extends \MailPoetTest { 'status' => Subscriber::STATUS_SUBSCRIBED ) ); + $subscriber_segment = SubscriberSegment::createOrUpdate( + array( + 'subscriber_id' => $subscriber->id, + 'segment_id' => $this->wc_segment->id, + 'status' => Subscriber::STATUS_SUBSCRIBED + ) + ); - // verify that subscriber is subscribed to 2 segments + // verify that subscriber is subscribed to 3 segments $subscriber = Subscriber::findOne($subscriber->id)->withSubscriptions(); expect($subscriber->subscriptions[0]['status'])->equals(Subscriber::STATUS_SUBSCRIBED); expect($subscriber->subscriptions[0]['segment_id'])->equals($segment_1->id); expect($subscriber->subscriptions[1]['status'])->equals(Subscriber::STATUS_SUBSCRIBED); expect($subscriber->subscriptions[1]['segment_id'])->equals($segment_2->id); + expect($subscriber->subscriptions[2]['status'])->equals(Subscriber::STATUS_SUBSCRIBED); + expect($subscriber->subscriptions[2]['segment_id'])->equals($segment_3->id); // verify that subscriber is not subscribed only to the non-WP segment (#2) - SubscriberSegment::unsubscribeFromSegments($subscriber, array($segment_1->id, $segment_2->id)); + SubscriberSegment::unsubscribeFromSegments($subscriber, array($segment_1->id, $segment_2->id, $segment_3->id)); $subscriber = Subscriber::findOne($subscriber->id)->withSubscriptions(); expect($subscriber->subscriptions[0]['status'])->equals(Subscriber::STATUS_SUBSCRIBED); expect($subscriber->subscriptions[0]['segment_id'])->equals($segment_1->id); expect($subscriber->subscriptions[1]['status'])->equals(Subscriber::STATUS_UNSUBSCRIBED); expect($subscriber->subscriptions[1]['segment_id'])->equals($segment_2->id); + expect($subscriber->subscriptions[2]['status'])->equals(Subscriber::STATUS_SUBSCRIBED); + expect($subscriber->subscriptions[2]['segment_id'])->equals($segment_3->id); } function testItCanUnsubscribeFromAllSegments() { @@ -271,11 +284,12 @@ class SubscriberSegmentTest extends \MailPoetTest { expect($subscriptions_count)->equals(1); } - function testItCannotUnsubscribeFromWPSegment() { - // subscribe to a segment and the WP segment + function testItCannotUnsubscribeFromWPAndWooCommerceSegments() { + // subscribe to a segment, the WP segment, the WooCommerce segment $result = SubscriberSegment::subscribeToSegments($this->subscriber, array( $this->segment_1->id, - $this->wp_segment->id + $this->wp_segment->id, + $this->wc_segment->id )); expect($result)->true(); @@ -285,15 +299,17 @@ class SubscriberSegmentTest extends \MailPoetTest { // the subscriber should still be subscribed to the WP segment $subscribed_segments = $this->subscriber->segments()->findArray(); - expect($subscribed_segments)->count(1); + expect($subscribed_segments)->count(2); expect($subscribed_segments[0]['name'])->equals($this->wp_segment->name); + expect($subscribed_segments[1]['name'])->equals($this->wc_segment->name); } - function testItCannotDeleteSubscriptionToWPSegment() { - // subscribe to a segment and the WP segment + function testItCannotDeleteSubscriptionToWPAndWooCommerceSegments() { + // subscribe to a segment, the WP segment, the WooCommerce segment $result = SubscriberSegment::subscribeToSegments($this->subscriber, array( $this->segment_1->id, - $this->wp_segment->id + $this->wp_segment->id, + $this->wc_segment->id )); expect($result)->true(); @@ -303,8 +319,9 @@ class SubscriberSegmentTest extends \MailPoetTest { // the subscriber should still be subscribed to the WP segment $subscribed_segments = $this->subscriber->segments()->findArray(); - expect($subscribed_segments)->count(1); + expect($subscribed_segments)->count(2); expect($subscribed_segments[0]['name'])->equals($this->wp_segment->name); + expect($subscribed_segments[1]['name'])->equals($this->wc_segment->name); } function _after() { diff --git a/tests/integration/Models/SubscriberTest.php b/tests/integration/Models/SubscriberTest.php index 7332a3b325..ba59c33855 100644 --- a/tests/integration/Models/SubscriberTest.php +++ b/tests/integration/Models/SubscriberTest.php @@ -551,6 +551,7 @@ class SubscriberTest extends \MailPoetTest { // the fields below should NOT be taken into account 'id' => 1337, 'wp_user_id' => 7331, + 'is_woocommerce_user' => 1, 'status' => Subscriber::STATUS_SUBSCRIBED, 'created_at' => '1984-03-09 00:00:01', 'updated_at' => '1984-03-09 00:00:02', @@ -567,6 +568,7 @@ class SubscriberTest extends \MailPoetTest { expect($subscriber->last_name)->equals('Trump'); expect($subscriber->wp_user_id)->equals(null); + expect($subscriber->is_woocommerce_user)->equals(0); expect($subscriber->status)->equals(Subscriber::STATUS_UNCONFIRMED); expect($subscriber->created_at)->notEquals('1984-03-09 00:00:01'); expect($subscriber->updated_at)->notEquals('1984-03-09 00:00:02'); @@ -822,6 +824,33 @@ class SubscriberTest extends \MailPoetTest { expect($subscriber)->notEquals(false); } + function testItCannotTrashWooCommerceCustomer() { + $wp_subscriber = Subscriber::createOrUpdate(array( + 'email' => 'some.woocommerce.customer@mailpoet.com', + 'first_name' => 'Some', + 'last_name' => 'WooCommerce Customer', + 'is_woocommerce_user' => 1 + )); + expect($wp_subscriber->trash())->equals(false); + + $subscriber = Subscriber::findOne($wp_subscriber->id); + expect($subscriber)->notEquals(false); + expect($subscriber->deleted_at)->equals(null); + } + + function testItCannotDeleteWooCommerceCustomer() { + $wp_subscriber = Subscriber::createOrUpdate(array( + 'email' => 'some.woocommerce.customer@mailpoet.com', + 'first_name' => 'Some', + 'last_name' => 'WooCommerce Customer', + 'is_woocommerce_user' => 1 + )); + expect($wp_subscriber->delete())->equals(false); + + $subscriber = Subscriber::findOne($wp_subscriber->id); + expect($subscriber)->notEquals(false); + } + function testItCanDeleteCustomFieldRelations() { $subscriber = Subscriber::create(); $subscriber->hydrate(Fixtures::get('subscriber_template'));