From ef158142cdff2f6ea0d8a7bf9fdae90e7d12b2f8 Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 20 Feb 2018 14:51:51 -0300 Subject: [PATCH 001/113] Add interest groups. (IN PROGRESS) --- .../MailChimp/Block/Checkout/Subscribe.php | 5 +- .../Block/Checkout/Success/Groups.php | 233 ++++++++++++++++++ .../Block/Customer/Newsletter/Index.php | 209 ++++++++++++++++ .../Ebizmarts/MailChimp/Helper/Data.php | 136 +++++++++- .../Ebizmarts/MailChimp/Model/Api/Batches.php | 2 +- .../MailChimp/Model/Api/Subscribers.php | 20 +- .../Ebizmarts/MailChimp/Model/Config.php | 51 ++-- .../MailChimp/Model/Interestgroup.php | 32 +++ .../MailChimp/Model/Mysql4/Interestgroup.php | 42 ++++ .../Model/Mysql4/Interestgroup/Collection.php | 27 ++ .../Ebizmarts/MailChimp/Model/Observer.php | 46 +++- .../System/Config/Source/CustomerGroup.php | 43 ++++ .../MailChimp/controllers/GroupController.php | 60 +++++ .../Ebizmarts/MailChimp/etc/config.xml | 24 +- .../Ebizmarts/MailChimp/etc/system.xml | 54 +++- .../mysql4-upgrade-1.1.11-1.1.12.php | 18 ++ .../default/layout/ebizmarts/mailchimp.xml | 22 +- .../mailchimp/checkout/subscribe.phtml | 11 +- .../mailchimp/checkout/success/groups.phtml | 83 +++++++ .../mailchimp/customer/newsletter/index.phtml | 68 +++++ 20 files changed, 1109 insertions(+), 77 deletions(-) create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Model/Interestgroup.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php create mode 100755 app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup/Collection.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php create mode 100644 app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php create mode 100755 app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.11-1.1.12.php create mode 100644 app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml create mode 100644 app/design/frontend/base/default/template/ebizmarts/mailchimp/customer/newsletter/index.phtml diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php index 96e519d1f..e3fc73cde 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php @@ -17,6 +17,9 @@ class Ebizmarts_MailChimp_Block_Checkout_Subscribe extends Mage_Core_Block_Templ protected $_generalList = array(); protected $_form; protected $_api; + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ protected $helper; protected $storeId; @@ -116,8 +119,6 @@ public function getGeneralList() $helper = $this->helper; $listId = $helper->getGeneralList($storeId); - //@Todo add support for intetest groups - return $listId; } } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php new file mode 100644 index 000000000..55951054a --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -0,0 +1,233 @@ + + * @license http://opensource.org/licenses/osl-3.0.php + */ +class Ebizmarts_MailChimp_Block_Checkout_Success_Groups extends Mage_Core_Block_Template +{ + + protected $_lists = array(); + protected $_info = array(); + protected $_myLists = array(); + protected $_generalList = array(); + protected $_form; + protected $_api; + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected $helper; + protected $storeId; + + public function __construct() + { + parent::__construct(); + $this->helper = Mage::helper('mailchimp'); + $this->storeId = Mage::app()->getStore()->getId(); + } + + /** + * Get list data from MC + * + * @return array + */ + public function getGeneralList() + { + $storeId = $this->storeId; + $helper = $this->helper; + $listId = $helper->getGeneralList($storeId); + + return $listId; + } + + public function getListInterestGroups() + { + $storeId = $this->storeId; + $helper = $this->helper; + $return = $helper->getListInterestGroups($storeId); + return $return; + } + + public function getCategoryTitle($category) + { + return $category['title']; + } + + public function renderGroups($category) + { + $object = $this->createObject($category); + + $this->addGroupOptions($category, $object); + + $html = $this->getElementHtml($category, $object); + + return $html; + } + + public function getGroupClass($type) + { + switch ($type) { + case 'radio': + $class = 'Varien_Data_Form_Element_Radios'; + break; + case 'checkboxes': + $class = 'Varien_Data_Form_Element_Checkboxes'; + break; + case 'dropdown': + $class = 'Varien_Data_Form_Element_Select'; + break; + default: + $class = 'Varien_Data_Form_Element_Text'; + break; + } + + return $class; + } + + public function htmlGroupName($category) + { + $storeId = $this->storeId; + $helper = $this->helper; + $listId = $helper->getGeneralList($storeId); + $htmlName = "list[{$listId}]"; + $htmlName .= "[{$category['id']}]"; + + if ($category['type'] == 'checkboxes') { + $htmlName .= '[]'; + } + + return $htmlName; + } + + /** + * Form getter/instantiation + * + * @return Varien_Data_Form + */ + public function getForm() + { + if ($this->_form instanceof Varien_Data_Form) { + return $this->_form; + } + $form = new Varien_Data_Form(); + return $form; + } + + /** + * @param $category + * @param $object + */ + protected function addGroupOptions($category, $object) + { + $type = $category['type']; + + if ($type == 'checkboxes' || $type == 'dropdown') { + $options = $this->createOptionArray($category); + + if (isset($category['groups'])) { + foreach ($category['groups'] as $key => $group) { + $options[$key] = $group; + } + + $object->setValues($options); + } + } + } + + /** + * @param $category + * @return mixed + */ + protected function createOptionArray($category) + { + $options = array(); + $type = $category['type']; + + if ($type == 'dropdown') { + $options[''] = '-- Select Group --'; + } + return $options; + } + + /** + * @param $category + * @param $html + * @return string + */ + protected function addGroupContainer($category, $html) + { + $type = $category['type']; + if ($type != 'checkboxes') { + $html = "
{$html}
"; + } + return $html; + } + + /** + * @param $category + * @return mixed + */ + protected function createObject($category) + { + $type = $category['type']; + $class = $this->getGroupClass($type); + $object = new $class; + $object->setForm($this->getForm()); + $object->setName($this->htmlGroupName($category)); + $object->setHtmlId('interest-group'); + + return $object; + } + + /** + * @param $category + * @param $object + * @return string + */ + protected function getElementHtml($category, $object) + { + $html = $object->getElementHtml(); + $html = $this->addGroupContainer($category, $html); + + return $html; + } + + public function getFormUrl() + { + return $this->getSuccessInterestUrl(); + } + + public function getSuccessInterestUrl() + { + $url = 'mailchimp/group/index'; + return Mage::app()->getStore()->getUrl($url); + } + + public function getInterest() + { + $subscriber = Mage::getModel('newsletter/subscriber'); + $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); + $subscriber->loadByEmail($order->getCustomerEmail()); + $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(),$subscriber->getStoreId()); + Mage::log(__METHOD__, null, 'ebizmarts.log', true); + Mage::log($interest, null, 'ebizmarts.log', true); + return $interest; + } + + public function getMessageBefore() + { + $storeId = $this->storeId; + return $this->helper->getCheckoutSuccessHtmlBefore($storeId); + } + + public function getMessageAfter() + { + $storeId = $this->storeId; + return $this->helper->getCheckoutSuccessHtmlAfter($storeId); + } + +} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php new file mode 100644 index 000000000..d9efdb41b --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php @@ -0,0 +1,209 @@ + + * @license http://opensource.org/licenses/osl-3.0.php + */ +class Ebizmarts_MailChimp_Block_Customer_Newsletter_Index extends Mage_Customer_Block_Newsletter +{ + + protected $_lists = array(); + protected $_info = array(); + protected $_myLists = array(); + protected $_generalList = array(); + protected $_form; + protected $_api; + protected $_template = "ebizmarts/mailchimp/customer/newsletter/index.phtml"; + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected $helper; + protected $storeId; + + public function __construct() + { +// parent::__construct(); + $this->setTemplate('ebizmarts/mailchimp/customer/newsletter/index.phtml'); + $this->helper = Mage::helper('mailchimp'); + $this->storeId = Mage::app()->getStore()->getId(); + } + + public function getInterest() + { + $subscriber = Mage::getModel('newsletter/subscriber'); + $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); + $subscriber->loadByEmail($order->getCustomerEmail()); + $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(),$subscriber->getStoreId()); + return $interest; + } + +// /** +// * Get list data from MC +// * +// * @return array +// */ +// public function getGeneralList() +// { +// $storeId = $this->storeId; +// $helper = $this->helper; +// $listId = $helper->getGeneralList($storeId); +// +// return $listId; +// } +// +// public function getListInterestGroups() +// { +// $storeId = $this->storeId; +// $helper = $this->helper; +// $return = $helper->getListInterestGroups($storeId); +// return $return; +// } +// +// public function getCategoryTitle($category) +// { +// return $category['title']; +// } +// +// public function renderGroups($category) +// { +// $object = $this->createObject($category); +// +// $this->addGroupOptions($category, $object); +// +// $html = $this->getElementHtml($category, $object); +// +// return $html; +// } +// +// public function getGroupClass($type) +// { +// switch ($type) { +// case 'radio': +// $class = 'Varien_Data_Form_Element_Radios'; +// break; +// case 'checkboxes': +// $class = 'Varien_Data_Form_Element_Checkboxes'; +// break; +// case 'dropdown': +// $class = 'Varien_Data_Form_Element_Select'; +// break; +// default: +// $class = 'Varien_Data_Form_Element_Text'; +// break; +// } +// +// return $class; +// } +// +// public function htmlGroupName($category) +// { +// $storeId = $this->storeId; +// $helper = $this->helper; +// $listId = $helper->getGeneralList($storeId); +// $htmlName = "list[{$listId}]"; +// $htmlName .= "[{$category['id']}]"; +// +// if ($category['type'] == 'checkboxes') { +// $htmlName .= '[]'; +// } +// +// return $htmlName; +// } +// +// /** +// * Form getter/instantiation +// * +// * @return Varien_Data_Form +// */ +// public function getForm() +// { +// if ($this->_form instanceof Varien_Data_Form) { +// return $this->_form; +// } +// $form = new Varien_Data_Form(); +// return $form; +// } +// +// /** +// * @param $category +// * @param $object +// */ +// protected function addGroupOptions($category, $object) +// { +// $type = $category['type']; +// +// if ($type == 'checkboxes' || $type == 'dropdown') { +// $options = $this->createOptionArray($category); +// +// if (isset($category['groups'])) { +// foreach ($category['groups'] as $group) { +// $options[$group] = $group; +// } +// +// $object->setValues($options); +// } +// } +// } +// +// /** +// * @param $category +// * @return mixed +// */ +// protected function createOptionArray($category) +// { +// $options = array(); +// $type = $category['type']; +// +// if ($type == 'dropdown') { +// $options[''] = '-- Select Group --'; +// } +// return $options; +// } +// +// /** +// * @param $category +// * @param $html +// * @return string +// */ +// protected function addGroupContainer($category, $html) +// { +// $type = $category['type']; +// if ($type != 'checkboxes') { +// $html = "
{$html}
"; +// } +// return $html; +// } +// +// /** +// * @param $category +// * @return mixed +// */ +// protected function createObject($category) +// { +// $type = $category['type']; +// $class = $this->getGroupClass($type); +// $object = new $class; +// $object->setForm($this->getForm()); +// $object->setName($this->htmlGroupName($category)); +// $object->setHtmlId('interest-group'); +// +// return $object; +// } +// +// /** +// * @param $category +// * @param $object +// * @return string +// */ +// protected function getElementHtml($category, $object) +// { +// $html = $object->getElementHtml(); +// $html = $this->addGroupContainer($category, $html); +// +// return $html; +// } +} diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 19e9ac95f..4c0b1c97a 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -396,12 +396,12 @@ public function isEmailCatcherEnabled($scopeId = 0, $scope = null) public function getDateSyncFinishByStoreId($scopeId = 0, $scope = null) { $mailchimpStoreId = $this->getMCStoreId($scopeId, $scope); - return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE."_$mailchimpStoreId", 0, 'default'); + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE . "_$mailchimpStoreId", 0, 'default'); } public function getDateSyncFinishByMailChimpStoreId($mailchimpStoreId) { - return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE."_$mailchimpStoreId", 0, 'default'); + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE . "_$mailchimpStoreId", 0, 'default'); } /** @@ -1148,11 +1148,6 @@ protected function setCurrentStore($magentoStoreId) $this->getMageApp()->setCurrentStore($magentoStoreId); } - private function getProductImageModel() - { - return Mage::getModel('catalog/product_image'); - } - /** * If orders with the given email exists, returns the date of the last order made. * @@ -2430,12 +2425,12 @@ public function getAllMailChimpStoreIds() ->addFieldToFilter('path', array('eq' => Ebizmarts_MailChimp_Model_Config::GENERAL_MCSTOREID)); $mailchimpStoreIdsArray = array(); foreach ($collection as $row) { - $scopeData = $row->getScope().'_'.$row->getScopeId(); + $scopeData = $row->getScope() . '_' . $row->getScopeId(); $mailchimpStoreIdsArray[$scopeData] = $row->getValue(); } return $mailchimpStoreIdsArray; } - + public function subscribeMember($subscriber, $forceUpdateStatus = false) { $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); @@ -2574,4 +2569,127 @@ public function getApiByMailChimpStoreId($mailchimpStoreId) $scopeArray = $this->getScopeByMailChimpStoreId($mailchimpStoreId); return $this->getApi($scopeArray['scope_id'], $scopeArray['scope']); } + + public function getListInterestCategories($scopeId, $scope = 'stores') + { + $interestGroupsArray = array(); + $api = $this->getApi($scopeId, $scope); + $listId = $this->getGeneralList($scopeId, $scope); + try { + $interestCategories = $api->lists->interestCategory->getAll($listId, 'categories'); + foreach ($interestCategories['categories'] as $interestCategory) { + $interestGroupsArray[] = array( + 'id' => $interestCategory['id'], + 'title' => $interestCategory['title'], + 'type' => $interestCategory['type'] + ); + } + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + return $interestGroupsArray; + } + + public function getListInterestGroups($scopeId, $scope = 'stores') + { + $interestGroupsArray = array(); + $api = $this->getApi($scopeId, $scope); + $listId = $this->getGeneralList($scopeId, $scope); + try { + $interestCategories = $api->lists->interestCategory->getAll($listId, 'categories'); + foreach ($interestCategories['categories'] as $interestCategory) { + $interestGroups = $api->lists->interestCategory->interests->getAll($listId, $interestCategory['id']); + $groups = array(); + foreach ($interestGroups['interests'] as $interestGroup) { + $groups[$interestGroup['id']] = $interestGroup['name']; + } + $interestGroupsArray[] = array( + 'id' => $interestCategory['id'], + 'title' => $interestCategory['title'], + 'type' => $interestCategory['type'], + 'groups' => $groups + ); + } + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + return $interestGroupsArray; + } + + public function getLocalInterestCategories($scopeId, $scope = 'stores') + { + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_CATEGORIES, $scopeId, $scope); + } + + public function getCheckoutSuccessHtmlBefore($scopeId, $scope = 'stores') + { + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_SUCCESS_BEFORE, $scopeId, $scope); + } + + public function getCheckoutSuccessHtmlAfter($scopeId, $scope = 'stores') + { + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_SUCCESS_AFTER, $scopeId, $scope); + } + + public function getInterest($storeId) + { + $rc = []; + $interest = $this->getLocalInterestCategories($storeId); + if ($interest != '') { + $interest = explode(",", $interest); + } else { + $interest = []; + } + $api = $this->getApi($storeId); + $listId = $this->getGeneralList($storeId); + $allInterest = $api->lists->interestCategory->getAll($listId); + foreach ($allInterest['categories'] as $item) { + if (in_array($item['id'], $interest)) { + $rc[$item['id']]['interest'] = ['id' => $item['id'], 'title' => $item['title'], 'type' => $item['type']]; + } + } + foreach ($interest as $interestId) { + $mailchimpInterest = $api->lists->interestCategory->interests->getAll($listId, $interestId); + foreach ($mailchimpInterest['interests'] as $mi) { + $rc[$mi['category_id']]['category'][$mi['display_order']] = ['id' => $mi['id'], 'name' => $mi['name'], 'checked' => false]; + } + } + return $rc; + } + + public function getSubscriberInterest($subscriberId, $storeId, $interest = null) + { + if (!$interest) { + $interest = $this->getInterest($storeId); + } + $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $interestGroup->getBySubscriberIdStoreId($subscriberId, $storeId); + $groups = unserialize($interestGroup->getGroupdata()); + if (isset($groups['group'])) { + foreach ($groups['group'] as $key => $value) { + if (isset($interest[$key])) { + if (is_array($value)) { + foreach ($value as $groupId) { + foreach ($interest[$key]['category'] as $gkey => $gvalue) { + if ($gvalue['id'] == $groupId) { + $interest[$key]['category'][$gkey]['checked'] = true; + } elseif (!isset($interest[$key]['category'][$gkey]['checked'])) { + $interest[$key]['category'][$gkey]['checked'] = false; + } + } + } + } else { + foreach ($interest[$key]['category'] as $gkey => $gvalue) { + if ($gvalue['id'] == $value) { + $interest[$key]['category'][$gkey]['checked'] = true; + } else { + $interest[$key]['category'][$gkey]['checked'] = false; + } + } + } + } + } + } + return $interest; + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php index 7e948a7b2..9b8834877 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php @@ -515,7 +515,7 @@ protected function processEachResponseFile($files, $batchId, $mailchimpStoreId) $errorDetails = $response->detail; } - if (strstr($errorDetails, 'already exists in the account')) { + if (strstr($errorDetails, 'already exists')) { $this->saveSyncData($id, $type, $mailchimpStoreId, null, null, 1, null, null, true); continue; } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php index 2ebe3e037..4fd4c6d61 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php @@ -441,23 +441,6 @@ protected function statusEqualsUnconfirmed($status) return $status == Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED; } - public function removeSubscriber($subscriber) - { - $helper = $this->mcHelper; - $storeId = $subscriber->getStoreId(); - $listId = $helper->getGeneralList($storeId); - $api = $helper->getApi($storeId); - try { - $md5HashEmail = md5(strtolower($subscriber->getSubscriberEmail())); - $api->lists->members->update($listId, $md5HashEmail, null, 'unsubscribed'); - } catch (MailChimp_Error $e) { - $helper->logError($e->getFriendlyMessage(), $storeId); - Mage::getSingleton('adminhtml/session')->addError($e->getFriendlyMessage()); - } catch (Exception $e) { - $helper->logError($e->getMessage(), $storeId); - } - } - /** * @param $subscriber */ @@ -480,9 +463,8 @@ public function deleteSubscriber($subscriber) public function update($emailAddress, $storeId) { - $helper = $this->mcHelper; $subscriber = Mage::getSingleton('newsletter/subscriber')->loadByEmail($emailAddress); - if ($subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $subscriber->getMailchimpSyncDelta() != $helper->getSubMinSyncDateFlag($storeId)) { + if ($subscriber->getId()) { $subscriber->setMailchimpSyncModified(1) ->save(); } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Config.php b/app/code/community/Ebizmarts/MailChimp/Model/Config.php index b62bf58ad..d205d2efc 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Config.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Config.php @@ -11,30 +11,33 @@ */ class Ebizmarts_MailChimp_Model_Config { - const GENERAL_ACTIVE = 'mailchimp/general/active'; - const GENERAL_APIKEY = 'mailchimp/general/apikey'; - const GENERAL_OAUTH_WIZARD = 'mailchimp/general/oauth_wizard'; - const GENERAL_ACCOUNT_DETAILS = 'mailchimp/general/account_details'; - const GENERAL_LIST = 'mailchimp/general/list'; - const GENERAL_OLD_LIST = 'mailchimp/general/old_list'; - const GENERAL_LIST_CHANGED_SCOPES = 'mailchimp/general/list_changed_scopes'; - const GENERAL_CHECKOUT_SUBSCRIBE = 'mailchimp/general/checkout_subscribe'; - const GENERAL_MCSTOREID = 'mailchimp/general/storeid'; - const GENERAL_MCISSYNCING = 'mailchimp/general/is_syicing'; - const GENERAL_ECOMMMINSYNCDATEFLAG = 'mailchimp/general/mcminsyncdateflag'; - const GENERAL_SUBMINSYNCDATEFLAG = 'mailchimp/general/subminsyncdateflag'; - const GENERAL_MCSTORE_RESETED = 'mailchimp/general/mcstore_reset'; - const GENERAL_TWO_WAY_SYNC = 'mailchimp/general/webhook_active'; - const GENERAL_UNSUBSCRIBE = 'mailchimp/general/webhook_delete'; - const GENERAL_WEBHOOK_ID = 'mailchimp/general/webhook_id'; - const GENERAL_LOG = 'mailchimp/general/enable_log'; - const GENERAL_MAP_FIELDS = 'mailchimp/general/map_fields'; - const GENERAL_CUSTOM_MAP_FIELDS = 'mailchimp/general/customer_map_fields'; - const GENERAL_MIGRATE_FROM_115 = 'mailchimp/general/migrate_from_115'; - const GENERAL_MIGRATE_FROM_116 = 'mailchimp/general/migrate_from_116'; - const GENERAL_MIGRATE_FROM_1164 = 'mailchimp/general/migrate_from_1164'; - const GENERAL_MIGRATE_LAST_ORDER_ID = 'mailchimp/general/migrate_last_order_id'; - const GENERAL_SUBSCRIBER_AMOUNT = 'mailchimp/general/subscriber_batch_amount'; + const GENERAL_ACTIVE = 'mailchimp/general/active'; + const GENERAL_APIKEY = 'mailchimp/general/apikey'; + const GENERAL_OAUTH_WIZARD = 'mailchimp/general/oauth_wizard'; + const GENERAL_ACCOUNT_DETAILS = 'mailchimp/general/account_details'; + const GENERAL_LIST = 'mailchimp/general/list'; + const GENERAL_OLD_LIST = 'mailchimp/general/old_list'; + const GENERAL_LIST_CHANGED_SCOPES = 'mailchimp/general/list_changed_scopes'; + const GENERAL_CHECKOUT_SUBSCRIBE = 'mailchimp/general/checkout_subscribe'; + const GENERAL_MCSTOREID = 'mailchimp/general/storeid'; + const GENERAL_MCISSYNCING = 'mailchimp/general/is_syicing'; + const GENERAL_ECOMMMINSYNCDATEFLAG = 'mailchimp/general/mcminsyncdateflag'; + const GENERAL_SUBMINSYNCDATEFLAG = 'mailchimp/general/subminsyncdateflag'; + const GENERAL_MCSTORE_RESETED = 'mailchimp/general/mcstore_reset'; + const GENERAL_TWO_WAY_SYNC = 'mailchimp/general/webhook_active'; + const GENERAL_UNSUBSCRIBE = 'mailchimp/general/webhook_delete'; + const GENERAL_WEBHOOK_ID = 'mailchimp/general/webhook_id'; + const GENERAL_LOG = 'mailchimp/general/enable_log'; + const GENERAL_MAP_FIELDS = 'mailchimp/general/map_fields'; + const GENERAL_CUSTOM_MAP_FIELDS = 'mailchimp/general/customer_map_fields'; + const GENERAL_MIGRATE_FROM_115 = 'mailchimp/general/migrate_from_115'; + const GENERAL_MIGRATE_FROM_116 = 'mailchimp/general/migrate_from_116'; + const GENERAL_MIGRATE_FROM_1164 = 'mailchimp/general/migrate_from_1164'; + const GENERAL_MIGRATE_LAST_ORDER_ID = 'mailchimp/general/migrate_last_order_id'; + const GENERAL_SUBSCRIBER_AMOUNT = 'mailchimp/general/subscriber_batch_amount'; + const GENERAL_INTEREST_CATEGORIES = 'mailchimp/general/interest_categories'; + const GENERAL_INTEREST_SUCCESS_BEFORE = 'mailchimp/general/interest_success_before'; + const GENERAL_INTEREST_SUCCESS_AFTER = 'mailchimp/general/interest_success_after'; const ECOMMERCE_ACTIVE = 'mailchimp/ecommerce/active'; const ECOMMERCE_CUSTOMERS_OPTIN = 'mailchimp/ecommerce/customers_optin'; diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Interestgroup.php b/app/code/community/Ebizmarts/MailChimp/Model/Interestgroup.php new file mode 100644 index 000000000..d683988b4 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Model/Interestgroup.php @@ -0,0 +1,32 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 5/16/16 6:23 PM + * @file: Interestgroup.php + */ + +class Ebizmarts_MailChimp_Model_Interestgroup extends Mage_Core_Model_Abstract +{ + /** + * Initialize model + * + * @return void + */ + public function _construct() + { + parent::_construct(); + $this->_init('mailchimp/interestgroup'); + } + + public function getBySubscriberIdStoreId($subscriberId, $storeId) + { + $this->addData($this->getResource()->getBySubscriberIdStoreId($subscriberId, $storeId)); + return $this; + } +} diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php new file mode 100644 index 000000000..301d50126 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php @@ -0,0 +1,42 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 6/9/16 4:46 PM + * @file: Interestgroup.php + */ +class Ebizmarts_MailChimp_Model_Mysql4_Interestgroup extends Mage_Core_Model_Mysql4_Abstract +{ + + /** + * Initialize + * + * @return void + */ + public function _construct() + { + $this->_init('mailchimp/interestgroup', 'id'); + } + + public function getBySubscriberIdStoreId($subscriberId, $storeId) + { + $read = $this->_getReadAdapter(); + $select = $read->select() + ->from($this->getMainTable()) + ->where('subscriber_id = ?', $subscriberId) + ->where('store_id = ?', $storeId); + + $result = $read->fetchRow($select); + + if (!$result) { + return array(); + } + + return $result; + } +} \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup/Collection.php b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup/Collection.php new file mode 100755 index 000000000..a008428dd --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup/Collection.php @@ -0,0 +1,27 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 5/16/16 6:53 PM + * @file: Collection.php + */ + +class Ebizmarts_MailChimp_Model_Mysql4_Interestgroup_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract +{ + + /** + * Set resource type + * + * @return void + */ + public function _construct() + { + parent::_construct(); + $this->_init('mailchimp/interestgroup'); + } +} \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 15959d465..629aba029 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -41,6 +41,7 @@ public function saveConfig(Varien_Event_Observer $observer) public function handleSubscriber(Varien_Event_Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); + if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { $isEnabled = $this->makeHelper()->isMailChimpEnabled($subscriber->getStoreId()); if ($isEnabled) { @@ -66,6 +67,26 @@ public function handleSubscriber(Varien_Event_Observer $observer) } } + public function subscriberSaveAfter(Varien_Event_Observer $observer) + { + Mage::log(__METHOD__, null, 'ebizmarts.log', true); + $params = Mage::app()->getRequest()->getParams(); + $subscriber = $observer->getEvent()->getSubscriber(); + $storeId = $subscriber->getStoreId(); + + if (isset($params['group'])) { + Mage::log('groups', null, 'ebizmarts.log', true); + Mage::log('subscriber id ' . $subscriber->getSubscriberId(), null, 'ebizmarts.log', true); + $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $storeId); + $interestGroup->setGroupdata(serialize($params)); + $interestGroup->setSubscriberId($subscriber->getSubscriberId()); + $interestGroup->setStoreId($storeId); + $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); + $interestGroup->save(); + } + } + /** * Handle subscriber deletion from back end. * @@ -257,16 +278,17 @@ protected function _getLandingCookie() */ public function addColumnToSalesOrderGrid($observer) { + $helper = $this->makeHelper(); $scopeArray = explode('-', $this->makeHelper()->getScopeString()); $block = $observer->getEvent()->getBlock(); if ($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid - && $this->makeHelper()->getMonkeyInGrid($scopeArray[1], $scopeArray[0]) - && ($this->makeHelper()->isAbandonedCartEnabled($scopeArray[1], $scopeArray[0]) - || $this->makeHelper()->isMailChimpEnabled($scopeArray[1], $scopeArray[0])) + && $helper->getMonkeyInGrid($scopeArray[1], $scopeArray[0]) + && ($helper->isAbandonedCartEnabled($scopeArray[1], $scopeArray[0]) + || $helper->isMailChimpEnabled($scopeArray[1], $scopeArray[0])) ) { $block->addColumnAfter( 'mailchimp_flag', array( - 'header' => $this->makeHelper()->__('MailChimp'), + 'header' => $helper->__('MailChimp'), 'index' => 'mailchimp_flag', 'align' => 'center', 'filter' => false, @@ -596,6 +618,7 @@ protected function getPromoRulesApi() public function cleanProductImagesCacheAfter(Varien_Event_Observer $observer) { + Mage::log(__METHOD__, null, 'ebizmarts2.log', true); $configValues = array(array(Ebizmarts_MailChimp_Model_Config::PRODUCT_IMAGE_CACHE_FLUSH, 1)); $this->makeHelper()->saveMailchimpConfig($configValues, 0, 'default'); } @@ -640,4 +663,19 @@ protected function getConfig() $config = Mage::getConfig(); return $config; } + + public function addTabToCustomer(Varien_Event_Observer $observer) + { + $block = $observer->getEvent()->getBlock(); + if ($block->getType() == 'adminhtml/customer_edit_tabs') { + + Mage::log($block->getType(), null, 'ebizmarts.log', true); + $block->addTab('custom_tab', array( + 'label' => Mage::helper('mailchimp')->__('MailChimp'), + 'title' => Mage::helper('mailchimp')->__('MailChimp'), + 'url' => $block->getUrl('mailchimp/adminhtml_custom/index', array('_current' => true)), + 'class' => 'ajax',)); + } + + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php new file mode 100644 index 000000000..3c78d026f --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php @@ -0,0 +1,43 @@ + + * Date : 8/29/14 + * Time : 3:36 PM + * File : CustomerGroup.php + * Module : magemonkey + */ +class Ebizmarts_MailChimp_Model_System_Config_Source_CustomerGroup +{ + protected $_categories = null; + + /** + * Load lists and store on class property + */ + public function __construct() + { + $helper = Mage::helper('mailchimp'); + $scopeArray = explode('-', Mage::helper('mailchimp')->getScopeString()); + $this->_categories = $helper->getListInterestCategories($scopeArray[1], $scopeArray[0]); + } + + /** + * Options getter + * + * @return array + */ + public function toOptionArray() + { + $groups = array(); + $helper = Mage::helper('mailchimp'); + if (is_array($this->_categories)) { + foreach ($this->_categories as $category) { + $groups[] = array('value'=> $category['id'], 'label' => $category['title']); + } + } else { + $groups []= array('value' => '', 'label' => $helper->__('--- No data ---')); + } + return $groups; + } + +} \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php new file mode 100644 index 000000000..f63c394a1 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php @@ -0,0 +1,60 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 7/6/16 10:14 AM + * @file: GroupController.php + */ + + +class Ebizmarts_MailChimp_GroupController extends Mage_Core_Controller_Front_Action +{ + public function indexAction() + { + $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); + $session = Mage::getSingleton('core/session'); + $helper = $this->getHelper(); + $params = $this->getRequest()->getParams(); + $storeId = $order->getStoreId(); + $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $subscriber = Mage::getModel('newsletter/subscriber') + ->loadByEmail($order->getCustomerEmail()); + try { + if ($subscriber->getSubscriberEmail() == $order->getCustomerEmail()) { + $this->getApiSubscriber()->update($subscriber->getSubscriberEmail(), $storeId, '', 1); + } else { + $subscriber->setSubscriberEmail($order->getCustomerEmail()); + $subscriber->setSubscriberFirstname($order->getCustomerFirstname()); + $subscriber->setSubscriberLastname($order->getCustomerLastname()); + $subscriber->subscribe($order->getCustomerEmail()); + } + $interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(),$storeId); + $interestGroup->setGroupdata(serialize($params)); + $interestGroup->setSubscriberId($subscriber->getSubscriberId()); + $interestGroup->setStoreId($storeId); + $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); + $interestGroup->save(); + + $session->addSuccess($this->__('Thanks for share your interest with us.')); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + $session->addWarning($this->__('Something went wrong with the interests subscription. Please go to the account subscription menu to subscriber to the interests successfully.')); + } + $this->_redirect('/'); + } + + protected function getHelper() + { + return Mage::helper('mailchimp'); + } + + protected function getApiSubscriber() + { + return Mage::getModel('mailchimp/api_subscribers'); + } +} \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index 54e21fcbe..ec16e39c1 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -3,10 +3,10 @@ - 1.1.10 + 1.1.11 - 1.1.10 + 1.1.11 @@ -27,6 +27,14 @@ + + + + mailchimp/observer + SubscriberSaveAfter + + + @@ -170,6 +178,9 @@ mailchimp_ecommerce_sync_data
+ + mailchimp_interest_group
+
@@ -301,6 +312,15 @@ + + + + model + mailchimp/observer + addTabToCustomer + + + diff --git a/app/code/community/Ebizmarts/MailChimp/etc/system.xml b/app/code/community/Ebizmarts/MailChimp/etc/system.xml index 6572470b4..473f8da59 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/system.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/system.xml @@ -85,7 +85,7 @@ button mailchimp/adminhtml_system_config_resetErrors - 45 + 42 1 1 1 @@ -95,7 +95,7 @@ select mailchimp/system_config_source_list mailchimp/system_config_backend_list - 50 + 44 1 1 1 @@ -106,12 +106,58 @@ button mailchimp/adminhtml_system_config_resetList - 52 + 46 1 1 1 + + + multiselect + mailchimp/system_config_source_customerGroup + 48 + 1 + 1 + 1 + 1 + + + + + select + adminhtml/system_config_source_yesno + 49 + 1 + 1 + 1 + + + + textarea + 50 + 1 + 1 + 1 + 1 + + + 1 + + + + + textarea + 51 + 1 + 1 + 1 + 1 + + + 1 + + select @@ -225,7 +271,7 @@ 1 - + select mailchimp/system_config_source_imageSize diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.11-1.1.12.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.11-1.1.12.php new file mode 100755 index 000000000..491729d7c --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.11-1.1.12.php @@ -0,0 +1,18 @@ +run( + " + CREATE TABLE IF NOT EXISTS `{$this->getTable('mailchimp_interest_group')}` ( + `id` INT(10) unsigned NOT NULL auto_increment, + `subscriber_id` INT(10) DEFAULT 0, + `store_id` SMALLINT (5) NOT NULL, + `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP, + `groupdata` TEXT(4096) NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +" +); + +$installer->endSetup(); \ No newline at end of file diff --git a/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml b/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml index 8ea478abc..98f4c2c12 100755 --- a/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml +++ b/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml @@ -29,16 +29,34 @@ + template="ebizmarts/mailchimp/popup/emailcatcher.phtml"/> + + + + + + + + + - + + + + + + diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/subscribe.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/subscribe.phtml index 7a8d73b00..444a173be 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/subscribe.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/subscribe.phtml @@ -31,19 +31,11 @@ $generalList = $this->getGeneralList(); value: subscribeValue + element.readAttribute('value'), type: "hidden" }); -// var listValue = new Element('input', { -// name: element.readAttribute('name'), -// id: "subscribe-" + element.readAttribute('value'), -// value: element.readAttribute('value'), -// type: "hidden" -// }); try { Element.insert(Form.findFirstElement(payment.form), inputer); -// Element.insert(Form.findFirstElement(payment.form), listValue); } catch (notelem) { $("co-payment-form").insert(inputer); -// $("co-payment-form").insert(listValue); } } else { var arrCheckedLists = checkedLists.split(','); @@ -85,8 +77,7 @@ $generalList = $this->getGeneralList(); value="" title="" class="mailchimp-list-subscriber"/> - + diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml new file mode 100644 index 000000000..935a5e532 --- /dev/null +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml @@ -0,0 +1,83 @@ +getInterest(); +?> +

+ getMessageBefore() ?> +

+
+ +
+ +
+
+ +
    + +
  • + + /> + +
  • + +
+ +
+ + +
+ +
    + +
  • + + /> + +
  • + +
+ + +
+
+
+ +
+
+
+ +
+
+
+getMessageAfter() ?> + +

\ No newline at end of file diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/customer/newsletter/index.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/customer/newsletter/index.phtml new file mode 100644 index 000000000..19db2efff --- /dev/null +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/customer/newsletter/index.phtml @@ -0,0 +1,68 @@ +getInterest(); +?> +
+ +
    +
  • + +
  • + +
      + +
    • + + /> + +
    • + +
    + +
    + + +
    + +
      + +
    • + + /> + +
    • + +
    + + +
+ + + + + From a97dd2c452d2d830eb652b1d85e29999b08c849f Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 20 Feb 2018 15:28:01 -0300 Subject: [PATCH 002/113] Updated merge data. --- .../Ebizmarts/MailChimp/Helper/Data.php | 6 +----- .../Model/System/Config/Source/CustomerGroup.php | 16 ++++++++++++---- .../community/Ebizmarts/MailChimp/etc/config.xml | 4 ++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 38eb3fbcd..ad34a73e5 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1397,11 +1397,6 @@ public function getCoreResource() return Mage::getSingleton('core/resource'); } - private function getProductImageModel() - { - return Mage::getModel('catalog/product_image'); - } - /** * If orders with the given email exists, returns the date of the last order made. * @@ -2967,4 +2962,5 @@ public function isNewApiKeyForSameAccount($oldApiKey, $newApiKey) } return $isNewApiKeyForSameAccount; } + } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php index 3c78d026f..25ca7aa53 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php @@ -16,9 +16,9 @@ class Ebizmarts_MailChimp_Model_System_Config_Source_CustomerGroup */ public function __construct() { - $helper = Mage::helper('mailchimp'); - $scopeArray = explode('-', Mage::helper('mailchimp')->getScopeString()); - $this->_categories = $helper->getListInterestCategories($scopeArray[1], $scopeArray[0]); + $helper = $this->makeHelper(); + $scopeArray = $helper->getCurrentScope(); + $this->_categories = $helper->getListInterestCategories($scopeArray['scope_id'], $scopeArray['scope']); } /** @@ -29,7 +29,7 @@ public function __construct() public function toOptionArray() { $groups = array(); - $helper = Mage::helper('mailchimp'); + $helper = $this->makeHelper(); if (is_array($this->_categories)) { foreach ($this->_categories as $category) { $groups[] = array('value'=> $category['id'], 'label' => $category['title']); @@ -40,4 +40,12 @@ public function toOptionArray() return $groups; } + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + protected function makeHelper() + { + return Mage::helper('mailchimp'); + } + } \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index d4b5378f4..b81d755b6 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -3,10 +3,10 @@ - 1.1.11 + 1.1.12 - 1.1.11 + 1.1.12 From 2af25311299c626714c7274a1e2f376e16e6bae5 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 1 Mar 2018 15:03:13 -0300 Subject: [PATCH 003/113] Updated merge data. --- .../Ebizmarts/MailChimp/Helper/Data.php | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index ad34a73e5..2ae2cfbce 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -2963,4 +2963,121 @@ public function isNewApiKeyForSameAccount($oldApiKey, $newApiKey) return $isNewApiKeyForSameAccount; } + public function getListInterestCategories($scopeId, $scope = 'stores') + { + $interestGroupsArray = array(); + $api = $this->getApi($scopeId, $scope); + $listId = $this->getGeneralList($scopeId, $scope); + try { + $interestCategories = $api->lists->interestCategory->getAll($listId, 'categories'); + foreach ($interestCategories['categories'] as $interestCategory) { + $interestGroupsArray[] = array( + 'id' => $interestCategory['id'], + 'title' => $interestCategory['title'], + 'type' => $interestCategory['type'] + ); + } + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + return $interestGroupsArray; + } + + public function getListInterestGroups($scopeId, $scope = 'stores') + { + $interestGroupsArray = array(); + $api = $this->getApi($scopeId, $scope); + $listId = $this->getGeneralList($scopeId, $scope); + try { + $interestCategories = $api->lists->interestCategory->getAll($listId, 'categories'); + foreach ($interestCategories['categories'] as $interestCategory) { + $interestGroups = $api->lists->interestCategory->interests->getAll($listId, $interestCategory['id']); + $groups = array(); + foreach ($interestGroups['interests'] as $interestGroup) { + $groups[$interestGroup['id']] = $interestGroup['name']; + } + $interestGroupsArray[] = array( + 'id' => $interestCategory['id'], + 'title' => $interestCategory['title'], + 'type' => $interestCategory['type'], + 'groups' => $groups + ); + } + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + return $interestGroupsArray; + } + public function getLocalInterestCategories($scopeId, $scope = 'stores') + { + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_CATEGORIES, $scopeId, $scope); + } + public function getCheckoutSuccessHtmlBefore($scopeId, $scope = 'stores') + { + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_SUCCESS_BEFORE, $scopeId, $scope); + } + public function getCheckoutSuccessHtmlAfter($scopeId, $scope = 'stores') + { + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_SUCCESS_AFTER, $scopeId, $scope); + } + public function getInterest($storeId) + { + $rc = []; + $interest = $this->getLocalInterestCategories($storeId); + if ($interest != '') { + $interest = explode(",", $interest); + } else { + $interest = []; + } + $api = $this->getApi($storeId); + $listId = $this->getGeneralList($storeId); + $allInterest = $api->lists->interestCategory->getAll($listId); + foreach ($allInterest['categories'] as $item) { + if (in_array($item['id'], $interest)) { + $rc[$item['id']]['interest'] = ['id' => $item['id'], 'title' => $item['title'], 'type' => $item['type']]; + } + } + foreach ($interest as $interestId) { + $mailchimpInterest = $api->lists->interestCategory->interests->getAll($listId, $interestId); + foreach ($mailchimpInterest['interests'] as $mi) { + $rc[$mi['category_id']]['category'][$mi['display_order']] = ['id' => $mi['id'], 'name' => $mi['name'], 'checked' => false]; + } + } + return $rc; + } + public function getSubscriberInterest($subscriberId, $storeId, $interest = null) + { + if (!$interest) { + $interest = $this->getInterest($storeId); + } + $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $interestGroup->getBySubscriberIdStoreId($subscriberId, $storeId); + $groups = unserialize($interestGroup->getGroupdata()); + if (isset($groups['group'])) { + foreach ($groups['group'] as $key => $value) { + if (isset($interest[$key])) { + if (is_array($value)) { + foreach ($value as $groupId) { + foreach ($interest[$key]['category'] as $gkey => $gvalue) { + if ($gvalue['id'] == $groupId) { + $interest[$key]['category'][$gkey]['checked'] = true; + } elseif (!isset($interest[$key]['category'][$gkey]['checked'])) { + $interest[$key]['category'][$gkey]['checked'] = false; + } + } + } + } else { + foreach ($interest[$key]['category'] as $gkey => $gvalue) { + if ($gvalue['id'] == $value) { + $interest[$key]['category'][$gkey]['checked'] = true; + } else { + $interest[$key]['category'][$gkey]['checked'] = false; + } + } + } + } + } + } + return $interest; + } } From 7dc6181e130905ccb8910aff0f241e16dee7601f Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 4 Apr 2018 15:51:37 -0300 Subject: [PATCH 004/113] Added MailChimp tab in customer edit view in admin panel. --- .../Adminhtml/Customer/Edit/Tab/Mailchimp.php | 20 +++++ .../Block/Checkout/Success/Groups.php | 2 - .../Ebizmarts/MailChimp/Model/Observer.php | 41 +++++++++- .../Model/System/Config/Source/Account.php | 2 +- .../Adminhtml/MailchimpController.php | 9 ++ .../Ebizmarts/MailChimp/etc/config.xml | 10 +-- .../mailchimp/customer/tab/mailchimp.phtml | 82 +++++++++++++++++++ 7 files changed, 154 insertions(+), 12 deletions(-) create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php create mode 100644 app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php new file mode 100644 index 000000000..930893e60 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php @@ -0,0 +1,20 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 6/10/16 12:38 AM + * @file: Grid.php + */ +class Ebizmarts_MailChimp_Block_Adminhtml_Customer_Edit_Tab_Mailchimp extends Mage_Adminhtml_Block_Widget_Grid +{ + public function __construct() + { + parent::__construct(); + $this->setTemplate('mailchimp/customer/tab/mailchimp.phtml'); + } +} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index 55951054a..257a2bde1 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -213,8 +213,6 @@ public function getInterest() $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); $subscriber->loadByEmail($order->getCustomerEmail()); $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(),$subscriber->getStoreId()); - Mage::log(__METHOD__, null, 'ebizmarts.log', true); - Mage::log($interest, null, 'ebizmarts.log', true); return $interest; } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index a05535d5b..98fdf9af0 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -110,7 +110,7 @@ protected function getCustomerModel() */ public function saveConfig(Varien_Event_Observer $observer) { - $post = Mage::app()->getRequest()->getPost(); + $post = $this->getRequest()->getPost(); $helper = $this->makeHelper(); $scopeArray = $helper->getCurrentScope(); @@ -133,9 +133,22 @@ public function saveConfig(Varien_Event_Observer $observer) public function handleSubscriber(Varien_Event_Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); + $storeId = $subscriber->getStoreId(); + $groups = $this->getRequest()->getParam('group'); $helper = $this->makeHelper(); + try { + $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(),$storeId); + $interestGroup->setGroupdata(serialize($groups)); + $interestGroup->setSubscriberId($subscriber->getSubscriberId()); + $interestGroup->setStoreId($storeId); + $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); + $interestGroup->save(); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { - $isEnabled = $helper->isSubscriptionEnabled($subscriber->getStoreId()); + $isEnabled = $helper->isSubscriptionEnabled($storeId); if ($isEnabled) { $apiSubscriber = $this->makeApiSubscriber(); $subscriber->setImportMode(true); @@ -531,7 +544,7 @@ public function loadCustomerToQuote(Varien_Event_Observer $observer) if (!Mage::getSingleton('customer/session')->isLoggedIn() && $isEcomEnabled && $isAbandonedCartEnabled ) { - $action = Mage::app()->getRequest()->getActionName(); + $action = $this->getRequest()->getActionName(); $onCheckout = ($action == 'saveOrder' || $action == 'savePayment' || $action == 'saveShippingMethod' || $action == 'saveBilling'); if (Mage::getModel('core/cookie')->get('email') @@ -798,7 +811,7 @@ public function salesruleDeleteAfter(Varien_Event_Observer $observer) public function secondaryCouponsDelete(Varien_Event_Observer $observer) { $promoCodesApi = $this->makeApiPromoCode(); - $params = Mage::app()->getRequest()->getParams(); + $params = $this->getRequest()->getParams(); if (isset($params['ids']) && isset($params['id'])) { $promoRuleId = $params['id']; $promoCodeIds = $params['ids']; @@ -886,4 +899,24 @@ protected function removeRegistry() { return Mage::unregister('sort_column_dir'); } + + public function addCustomerTab(Varien_Event_Observer $observer) + { +// Mage::log(__METHOD__, null, 'ebizmarts.log', true); + $block = $observer->getEvent()->getBlock(); + // add tab in customer edit page + if ($block instanceof Mage_Adminhtml_Block_Customer_Edit_Tabs) { + Mage::log('is instance', null, 'ebizmarts.log', true); + if ($this->getRequest()->getActionName() == 'edit' || $this->getRequest()->getParam('type')) { + Mage::log('add tab', null, 'ebizmarts.log', true); + $block->addTab('mailchimp', array('label' => Mage::helper('customer')->__('MailChimp'), 'url' => $block->getUrl('adminhtml/mailchimp/index', array('_current' => true)), 'class' => 'ajax')); + } + } + return $observer; + } + + protected function getRequest() + { + return Mage::app()->getRequest(); + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php index d7518d520..6c5950d3d 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php @@ -75,7 +75,7 @@ public function __construct() $helper->deleteLocalMCStoreData($mcStoreId, $scopeArray['scope_id'], $scopeArray['scope']); if ($listId) { $helper->createStore($listId, $scopeArray['scope_id'], $scopeArray['scope']); - $message = $helper->__('Looks like your MailChimp store was deleted. A new one has been created.'); + $message = $helper->__('Looks like your MailChimp store was deleted, a new one has been created. Refresh the page to see it.'); Mage::getSingleton('adminhtml/session')->addWarning($message); } } diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 561314797..3cd153252 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -12,6 +12,15 @@ */ class Ebizmarts_MailChimp_Adminhtml_MailchimpController extends Mage_Adminhtml_Controller_Action { + public function indexAction() + { + $this->getResponse()->setBody( + $this->getLayout()->createBlock('mailchimp/adminhtml_customer_edit_tab_mailchimp','admin.customer.mailchimp')->setCustomerId(Mage::registry('current_customer')->getId()) + ->setUseAjax(true) + ->toHtml() + ); + } + public function resendSubscribersAction() { $helper = $this->makeHelper(); diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index 115c247ba..3e7c59bbe 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -304,15 +304,15 @@ - + - + model mailchimp/observer - addTabToCustomer - + addCustomerTab + - + diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml new file mode 100644 index 000000000..d2946e8c4 --- /dev/null +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml @@ -0,0 +1,82 @@ +getInterest(); +?> +

+ getMessageBefore() ?> +

+
+ +
+ +
+
+ +
    + +
  • + + /> + +
  • + +
+ +
+ +
+ +
    + +
  • + + /> + +
  • + +
+ + +
+
+
+ +
+
+
+ +
+
+
+getMessageAfter() ?> + +

\ No newline at end of file From a90e09612b3c285665aa7666d98f2467feb03baa Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 9 Apr 2018 16:41:06 -0300 Subject: [PATCH 005/113] Added new tab at Customer edit view in the back end. --- .../Adminhtml/Customer/Edit/Tab/Mailchimp.php | 30 +++- .../Adminhtml/MailchimpController.php | 14 +- .../mailchimp/customer/tab/mailchimp.phtml | 144 +++++++++--------- 3 files changed, 107 insertions(+), 81 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php index 930893e60..22ed980cb 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php @@ -12,9 +12,37 @@ */ class Ebizmarts_MailChimp_Block_Adminhtml_Customer_Edit_Tab_Mailchimp extends Mage_Adminhtml_Block_Widget_Grid { + + protected $_lists = array(); + protected $_info = array(); + protected $_myLists = array(); + protected $_generalList = array(); + protected $_form; + protected $_api; + protected $_customer; + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected $helper; + protected $storeId; + public function __construct() { parent::__construct(); - $this->setTemplate('mailchimp/customer/tab/mailchimp.phtml'); + $this->setTemplate('ebizmarts/mailchimp/customer/tab/mailchimp.phtml'); + $this->helper = Mage::helper('mailchimp'); + $customerId = (int) $this->getRequest()->getParam('id'); + if ($customerId) { + $this->_customer = Mage::getModel('customer/customer')->load($customerId); + $this->storeId = $this->_customer->getStoreId(); + } + } + + public function getInterest() + { + $subscriber = Mage::getModel('newsletter/subscriber'); + $subscriber->loadByEmail($this->_customer->getEmail()); + $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(), $this->storeId); + return $interest; } } diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 3cd153252..f7b00f9a1 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -14,11 +14,14 @@ class Ebizmarts_MailChimp_Adminhtml_MailchimpController extends Mage_Adminhtml_C { public function indexAction() { - $this->getResponse()->setBody( - $this->getLayout()->createBlock('mailchimp/adminhtml_customer_edit_tab_mailchimp','admin.customer.mailchimp')->setCustomerId(Mage::registry('current_customer')->getId()) - ->setUseAjax(true) - ->toHtml() - ); + $customerId = (int) $this->getRequest()->getParam('id'); + if ($customerId) { + $this->getResponse()->setBody( + $this->getLayout()->createBlock('mailchimp/adminhtml_customer_edit_tab_mailchimp', 'admin.customer.mailchimp')->setCustomerId($customerId) + ->setUseAjax(true) + ->toHtml() + ); + } } public function resendSubscribersAction() @@ -42,6 +45,7 @@ public function resendSubscribersAction() protected function _isAllowed() { switch ($this->getRequest()->getActionName()) { + case 'index': case 'resendSubscribers': $acl = 'system/config/mailchimp'; break; diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml index d2946e8c4..5507a99a7 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml @@ -1,82 +1,76 @@ getInterest(); +$interest = $this->getInterest(); ?> -

- getMessageBefore() ?> -

-
- -
- -
-
- -
    - -
  • - - /> - -
  • - -
- -
- + /> + + - -
- -
    - -
  • - - /> - -
  • - -
- - + + +
+ + +
+ +
    + +
  • + + /> + +
  • + +
+ + +
- - -
-
-
- -
+ +
-
-getMessageAfter() ?> - -

\ No newline at end of file + \ No newline at end of file From 052ebd41b6791eaf572718d8ee6956fc87d67748 Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 11 Apr 2018 17:04:25 -0300 Subject: [PATCH 006/113] Fixed error when enabling only on one store view --- .../Block/Checkout/Success/Groups.php | 2 +- .../Ebizmarts/MailChimp/Model/Observer.php | 35 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index 257a2bde1..a81ca352d 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -212,7 +212,7 @@ public function getInterest() $subscriber = Mage::getModel('newsletter/subscriber'); $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); $subscriber->loadByEmail($order->getCustomerEmail()); - $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(),$subscriber->getStoreId()); + $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(),$order->getStoreId()); return $interest; } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 06fbf8388..9bff39e2e 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -237,15 +237,44 @@ public function alterNewsletterGrid(Varien_Event_Observer $observer) */ public function customerSaveBefore(Varien_Event_Observer $observer) { + Mage::log(__METHOD__, null, 'ebizmarts.log', true); $customer = $observer->getEvent()->getCustomer(); + $origEmail = $customer->getOrigData('email'); + $customerEmail = $customer->getEmail(); $storeId = $customer->getStoreId(); + + //@Todo if is admin + $params = $this->getRequest()->getParams(); + Mage::log($params, null, 'ebizmarts.log', true); + if (isset($params['customer']) && isset($params['customer']['interestgroup'])) { + $subscriberModel = $this->getSubscriberModel(); + $subscriber = $subscriberModel->loadByEmail($origEmail); + $subscriberId = $subscriber->getSubcriberId(); + if (!$subscriberId) { + $subscriber->setSubscriberEmail($customerEmail) + ->setFirstname($customer->getFristname()) + ->setLastname($customer->getLastname()) + ->setCustomerId($customer->getEntityId()) + ->setStoreId($storeId) + ->subscribe($customerEmail); + Mage::log('subscribe', null, 'ebizmarts.log', true); + Mage::log($subscriber->getData(), null, 'ebizmarts.log', true); + } + $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $interestGroup->getBySubscriberIdStoreId($subscriberId, $storeId); + $interestGroup->setGroupdata(serialize($params['customer']['interestgroup'])); + $interestGroup->setSubscriberId($subscriber->getSubscriberId()); + $interestGroup->setStoreId($storeId); + $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); + $interestGroup->save(); + Mage::log('after save', null, 'ebizmarts.log', true); + } + $helper = $this->makeHelper(); $isEnabled = $helper->isSubscriptionEnabled($storeId); if ($isEnabled) { $apiSubscriber = $this->makeApiSubscriber(); - $origEmail = $customer->getOrigData('email'); - $customerEmail = $customer->getEmail(); if ($origEmail) { // check if customer has changed email address if ($origEmail != $customerEmail) { @@ -908,7 +937,7 @@ public function addCustomerTab(Varien_Event_Observer $observer) Mage::log('is instance', null, 'ebizmarts.log', true); if ($this->getRequest()->getActionName() == 'edit' || $this->getRequest()->getParam('type')) { Mage::log('add tab', null, 'ebizmarts.log', true); - $block->addTab('mailchimp', array('label' => Mage::helper('customer')->__('MailChimp'), 'url' => $block->getUrl('adminhtml/mailchimp/index', array('_current' => true)), 'class' => 'ajax')); + $block->addTab('mailchimp', array('order' => 100,'label' => Mage::helper('mailchimp')->__('MailChimp'), 'url' => $block->getUrl('adminhtml/mailchimp/index', array('_current' => true)), 'class' => 'ajax')); } } return $observer; From 0d82effb869b39d7896069735f3081ccac6132b4 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 11 May 2018 15:38:36 -0300 Subject: [PATCH 007/113] Add actual sending for groups. --- .../Block/Customer/Newsletter/Index.php | 181 ++---------------- .../Ebizmarts/MailChimp/Helper/Data.php | 11 +- .../MailChimp/Model/Api/Subscribers.php | 27 ++- .../Ebizmarts/MailChimp/Model/Observer.php | 40 ++-- .../Model/System/Config/Source/Account.php | 2 +- .../MailChimp/controllers/GroupController.php | 6 +- .../mailchimp/checkout/success/groups.phtml | 1 - lib/Ebizmarts/MailChimp/Lists.php | 10 +- 8 files changed, 71 insertions(+), 207 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php index d9efdb41b..8187f4cb1 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php @@ -26,7 +26,6 @@ class Ebizmarts_MailChimp_Block_Customer_Newsletter_Index extends Mage_Customer_ public function __construct() { -// parent::__construct(); $this->setTemplate('ebizmarts/mailchimp/customer/newsletter/index.phtml'); $this->helper = Mage::helper('mailchimp'); $this->storeId = Mage::app()->getStore()->getId(); @@ -35,175 +34,19 @@ public function __construct() public function getInterest() { $subscriber = Mage::getModel('newsletter/subscriber'); - $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); - $subscriber->loadByEmail($order->getCustomerEmail()); - $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(),$subscriber->getStoreId()); + $subscriber->loadByEmail($this->_getEmail()); + $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(), $subscriber->getStoreId()); return $interest; } -// /** -// * Get list data from MC -// * -// * @return array -// */ -// public function getGeneralList() -// { -// $storeId = $this->storeId; -// $helper = $this->helper; -// $listId = $helper->getGeneralList($storeId); -// -// return $listId; -// } -// -// public function getListInterestGroups() -// { -// $storeId = $this->storeId; -// $helper = $this->helper; -// $return = $helper->getListInterestGroups($storeId); -// return $return; -// } -// -// public function getCategoryTitle($category) -// { -// return $category['title']; -// } -// -// public function renderGroups($category) -// { -// $object = $this->createObject($category); -// -// $this->addGroupOptions($category, $object); -// -// $html = $this->getElementHtml($category, $object); -// -// return $html; -// } -// -// public function getGroupClass($type) -// { -// switch ($type) { -// case 'radio': -// $class = 'Varien_Data_Form_Element_Radios'; -// break; -// case 'checkboxes': -// $class = 'Varien_Data_Form_Element_Checkboxes'; -// break; -// case 'dropdown': -// $class = 'Varien_Data_Form_Element_Select'; -// break; -// default: -// $class = 'Varien_Data_Form_Element_Text'; -// break; -// } -// -// return $class; -// } -// -// public function htmlGroupName($category) -// { -// $storeId = $this->storeId; -// $helper = $this->helper; -// $listId = $helper->getGeneralList($storeId); -// $htmlName = "list[{$listId}]"; -// $htmlName .= "[{$category['id']}]"; -// -// if ($category['type'] == 'checkboxes') { -// $htmlName .= '[]'; -// } -// -// return $htmlName; -// } -// -// /** -// * Form getter/instantiation -// * -// * @return Varien_Data_Form -// */ -// public function getForm() -// { -// if ($this->_form instanceof Varien_Data_Form) { -// return $this->_form; -// } -// $form = new Varien_Data_Form(); -// return $form; -// } -// -// /** -// * @param $category -// * @param $object -// */ -// protected function addGroupOptions($category, $object) -// { -// $type = $category['type']; -// -// if ($type == 'checkboxes' || $type == 'dropdown') { -// $options = $this->createOptionArray($category); -// -// if (isset($category['groups'])) { -// foreach ($category['groups'] as $group) { -// $options[$group] = $group; -// } -// -// $object->setValues($options); -// } -// } -// } -// -// /** -// * @param $category -// * @return mixed -// */ -// protected function createOptionArray($category) -// { -// $options = array(); -// $type = $category['type']; -// -// if ($type == 'dropdown') { -// $options[''] = '-- Select Group --'; -// } -// return $options; -// } -// -// /** -// * @param $category -// * @param $html -// * @return string -// */ -// protected function addGroupContainer($category, $html) -// { -// $type = $category['type']; -// if ($type != 'checkboxes') { -// $html = "
{$html}
"; -// } -// return $html; -// } -// -// /** -// * @param $category -// * @return mixed -// */ -// protected function createObject($category) -// { -// $type = $category['type']; -// $class = $this->getGroupClass($type); -// $object = new $class; -// $object->setForm($this->getForm()); -// $object->setName($this->htmlGroupName($category)); -// $object->setHtmlId('interest-group'); -// -// return $object; -// } -// -// /** -// * @param $category -// * @param $object -// * @return string -// */ -// protected function getElementHtml($category, $object) -// { -// $html = $object->getElementHtml(); -// $html = $this->addGroupContainer($category, $html); -// -// return $html; -// } + /** + * Retrieve email from Customer object in session + * + * @return string Email address + */ + protected function _getEmail() + { + return $this->helper('customer')->getCustomer()->getEmail(); + } + } diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 8c203747a..c046b821c 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3055,18 +3055,22 @@ public function getListInterestGroups($scopeId, $scope = 'stores') } return $interestGroupsArray; } + public function getLocalInterestCategories($scopeId, $scope = 'stores') { return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_CATEGORIES, $scopeId, $scope); } + public function getCheckoutSuccessHtmlBefore($scopeId, $scope = 'stores') { return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_SUCCESS_BEFORE, $scopeId, $scope); } + public function getCheckoutSuccessHtmlAfter($scopeId, $scope = 'stores') { return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_SUCCESS_AFTER, $scopeId, $scope); } + public function getInterest($storeId) { $rc = []; @@ -3092,6 +3096,7 @@ public function getInterest($storeId) } return $rc; } + public function getSubscriberInterest($subscriberId, $storeId, $interest = null) { if (!$interest) { @@ -3099,9 +3104,9 @@ public function getSubscriberInterest($subscriberId, $storeId, $interest = null) } $interestGroup = Mage::getModel('mailchimp/interestgroup'); $interestGroup->getBySubscriberIdStoreId($subscriberId, $storeId); - $groups = unserialize($interestGroup->getGroupdata()); - if (isset($groups['group'])) { - foreach ($groups['group'] as $key => $value) { + if ($interestGroup->getId()) { + $groups = unserialize($interestGroup->getGroupdata()); + foreach ($groups as $key => $value) { if (isset($interest[$key])) { if (is_array($value)) { foreach ($value as $groupId) { diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php index 4a600e49f..8395de8aa 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php @@ -108,10 +108,33 @@ protected function _buildSubscriberData($subscriber) $data["status"] = $status; } $data["language"] = $helper->getStoreLanguageCode($storeId); + $interest = $this->_getInterest($subscriber); + if(count($interest)) { + $data['interests'] = $interest; + } return $data; } + /** + * @param $subscriber + * @return array + */ + protected function _getInterest($subscriber) + { + $storeId = $subscriber->getStoreId(); + $rc = array(); + $helper = $this->mcHelper; + $interestsAvailable = $helper->getInterest($storeId); + $interest = $helper->getSubscriberInterest($subscriber->getSubscriberId(), $storeId, $interestsAvailable); + foreach($interest as $i) { + foreach($i['category'] as $key=>$value) { + $rc[$value['id']] = $value['checked']; + } + } + return $rc; + } + public function getMergeVars($subscriber) { $helper = $this->mcHelper; @@ -339,6 +362,8 @@ public function updateSubscriber($subscriber, $updateStatus = false) return; } $mergeVars = $this->getMergeVars($subscriber); + $interest = $this->_getInterest($subscriber); + $md5HashEmail = md5(strtolower($subscriber->getSubscriberEmail())); try { $api->lists->members->addOrUpdate( @@ -352,7 +377,7 @@ public function updateSubscriber($subscriber, $updateStatus = false) if ($newStatus === 'subscribed' && $subscriber->getIsStatusChanged()) { if (strstr($e->getMailchimpDetails(), 'is in a compliance state')) { try { - $api->lists->members->update($listId, $md5HashEmail, null, 'pending', $mergeVars); + $api->lists->members->update($listId, $md5HashEmail, null, 'pending', $mergeVars, $interest); $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED); $message = $helper->__('To begin receiving the newsletter, you must first confirm your subscription'); Mage::getSingleton('core/session')->addWarning($message); diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 9bff39e2e..2263f831b 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -134,19 +134,7 @@ public function handleSubscriber(Varien_Event_Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); $storeId = $subscriber->getStoreId(); - $groups = $this->getRequest()->getParam('group'); $helper = $this->makeHelper(); - try { - $interestGroup = Mage::getModel('mailchimp/interestgroup'); - $interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(),$storeId); - $interestGroup->setGroupdata(serialize($groups)); - $interestGroup->setSubscriberId($subscriber->getSubscriberId()); - $interestGroup->setStoreId($storeId); - $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); - $interestGroup->save(); - } catch (Exception $e) { - $helper->logError($e->getMessage()); - } if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { $isEnabled = $helper->isSubscriptionEnabled($storeId); if ($isEnabled) { @@ -237,7 +225,6 @@ public function alterNewsletterGrid(Varien_Event_Observer $observer) */ public function customerSaveBefore(Varien_Event_Observer $observer) { - Mage::log(__METHOD__, null, 'ebizmarts.log', true); $customer = $observer->getEvent()->getCustomer(); $origEmail = $customer->getOrigData('email'); $customerEmail = $customer->getEmail(); @@ -245,8 +232,12 @@ public function customerSaveBefore(Varien_Event_Observer $observer) //@Todo if is admin $params = $this->getRequest()->getParams(); - Mage::log($params, null, 'ebizmarts.log', true); - if (isset($params['customer']) && isset($params['customer']['interestgroup'])) { + if (isset($params['customer']) && isset($params['customer']['interestgroup']) || isset($params['group'])) { + if (isset($params['group'])) { + $groups = $params['group']; + } else { + $groups = $params['customer']['interestgroup']; + } $subscriberModel = $this->getSubscriberModel(); $subscriber = $subscriberModel->loadByEmail($origEmail); $subscriberId = $subscriber->getSubcriberId(); @@ -257,17 +248,14 @@ public function customerSaveBefore(Varien_Event_Observer $observer) ->setCustomerId($customer->getEntityId()) ->setStoreId($storeId) ->subscribe($customerEmail); - Mage::log('subscribe', null, 'ebizmarts.log', true); - Mage::log($subscriber->getData(), null, 'ebizmarts.log', true); } $interestGroup = Mage::getModel('mailchimp/interestgroup'); $interestGroup->getBySubscriberIdStoreId($subscriberId, $storeId); - $interestGroup->setGroupdata(serialize($params['customer']['interestgroup'])); + $interestGroup->setGroupdata(serialize($groups)); $interestGroup->setSubscriberId($subscriber->getSubscriberId()); $interestGroup->setStoreId($storeId); $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); $interestGroup->save(); - Mage::log('after save', null, 'ebizmarts.log', true); } $helper = $this->makeHelper(); @@ -930,14 +918,18 @@ protected function removeRegistry() public function addCustomerTab(Varien_Event_Observer $observer) { -// Mage::log(__METHOD__, null, 'ebizmarts.log', true); $block = $observer->getEvent()->getBlock(); + $helper = $this->makeHelper(); // add tab in customer edit page if ($block instanceof Mage_Adminhtml_Block_Customer_Edit_Tabs) { - Mage::log('is instance', null, 'ebizmarts.log', true); - if ($this->getRequest()->getActionName() == 'edit' || $this->getRequest()->getParam('type')) { - Mage::log('add tab', null, 'ebizmarts.log', true); - $block->addTab('mailchimp', array('order' => 100,'label' => Mage::helper('mailchimp')->__('MailChimp'), 'url' => $block->getUrl('adminhtml/mailchimp/index', array('_current' => true)), 'class' => 'ajax')); + $customerId = (int) $this->getRequest()->getParam('id'); + $customer = Mage::getModel('customer/customer')->load($customerId); + if ($helper->getLocalInterestCategories($customer->getStoreId()) && ($this->getRequest()->getActionName() == 'edit' || $this->getRequest()->getParam('type'))) { + $block->addTab('mailchimp', array( + 'label' => $helper->__('MailChimp'), + 'url' => $block->getUrl('adminhtml/mailchimp/index', array('_current' => true)), + 'class' => 'ajax' + )); } } return $observer; diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php index 6c5950d3d..2b98ff117 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php @@ -71,7 +71,7 @@ public function __construct() $totalCarts = $api->ecommerce->carts->getAll($mcStoreId, 'total_items'); $this->_accountDetails['total_carts'] = $totalCarts['total_items']; } catch (MailChimp_Error $e) { - if ($helper->isEcommerceEnabled($scopeArray['scope_id'], $scopeArray['scope'])) { + if ($helper->isEcomSyncDataEnabled($scopeArray['scope_id'], $scopeArray['scope'])) { $helper->deleteLocalMCStoreData($mcStoreId, $scopeArray['scope_id'], $scopeArray['scope']); if ($listId) { $helper->createStore($listId, $scopeArray['scope_id'], $scopeArray['scope']); diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php index f63c394a1..68e46ea5e 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php @@ -25,9 +25,7 @@ public function indexAction() $subscriber = Mage::getModel('newsletter/subscriber') ->loadByEmail($order->getCustomerEmail()); try { - if ($subscriber->getSubscriberEmail() == $order->getCustomerEmail()) { - $this->getApiSubscriber()->update($subscriber->getSubscriberEmail(), $storeId, '', 1); - } else { + if (!$subscriber->getSubscriberId()) { $subscriber->setSubscriberEmail($order->getCustomerEmail()); $subscriber->setSubscriberFirstname($order->getCustomerFirstname()); $subscriber->setSubscriberLastname($order->getCustomerLastname()); @@ -40,6 +38,8 @@ public function indexAction() $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); $interestGroup->save(); + $this->getApiSubscriber()->update($subscriber->getSubscriberEmail(), $storeId, '', 1); + $session->addSuccess($this->__('Thanks for share your interest with us.')); } catch (Exception $e) { $helper->logError($e->getMessage()); diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml index 935a5e532..d2946e8c4 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml @@ -42,7 +42,6 @@ $interest = $block->getInterest(); - diff --git a/lib/Ebizmarts/MailChimp/Lists.php b/lib/Ebizmarts/MailChimp/Lists.php index 2f083f736..ba56f78aa 100644 --- a/lib/Ebizmarts/MailChimp/Lists.php +++ b/lib/Ebizmarts/MailChimp/Lists.php @@ -80,8 +80,8 @@ public function add($name, $contact, $permissionRemanider, $campaingDefaults, $n ) { - $_params = array('name' => $name, 'contact' => $contact, 'permission_remainder' => $permissionRemanider, - 'use_archive_bar' => $useArchiveBar, 'campaignDefaults' => $campaingDefaults, + $_params = array('name' => $name, 'contact' => $contact, 'permission_reminder' => $permissionRemanider, + 'use_archive_bar' => $useArchiveBar, 'campaign_defaults' => $campaingDefaults, 'notify_on_subscribe' => $notifyOnSubscribe, 'notify_on_unsubscribe' => $notifyOnUnsubscribe, 'email_type_option' => $emailTypeOption, 'visibility' => $visibility); return $this->_master->call('lists', $_params, Ebizmarts_MailChimp::POST); @@ -133,7 +133,7 @@ public function getLists($id = null, $fields = null, $excludeFields = null, $cou * @param $name The name of the list. * @param $contact Contact information displayed in campaign footers to comply with international * spam laws. - * @param $permissionRemainder The permission reminder for the list. + * @param $permissionReminder The permission reminder for the list. * @param null $useArchiveBar Whether campaigns for this list use the Archive Bar in archives by default. * @param null $campaignDefaults Default values for campaigns created for this list. * @param null $notifyOnSubscribe The email address to send subscribe notifications to. @@ -148,12 +148,12 @@ public function getLists($id = null, $fields = null, $excludeFields = null, $cou * @throws MailChimp_Error * @throws MailChimp_HttpError */ - public function edit($listId, $name, $contact, $permissionRemainder, $emailTypeOption, $useArchiveBar = null, + public function edit($listId, $name, $contact, $permissionReminder, $emailTypeOption, $useArchiveBar = null, $campaignDefaults = null, $notifyOnSubscribe = null, $notifyOnUnsubscribe = null, $visibility = null ) { - $_params = array('name' => $name, 'contact' => $contact, 'permission_remainder' => $permissionRemainder, + $_params = array('name' => $name, 'contact' => $contact, 'permission_reminder' => $permissionReminder, 'email_type_option' => $emailTypeOption); if ($useArchiveBar) { $_params['use_archive_bar'] = $useArchiveBar; From efd8099a6815b95ca4cab61f4e572318fde4a0e2 Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 29 May 2018 14:37:27 -0300 Subject: [PATCH 008/113] Refactor class. --- .../Adminhtml/Customer/Edit/Tab/Mailchimp.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php index 22ed980cb..7f99da61f 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php @@ -30,19 +30,43 @@ public function __construct() { parent::__construct(); $this->setTemplate('ebizmarts/mailchimp/customer/tab/mailchimp.phtml'); - $this->helper = Mage::helper('mailchimp'); + $this->helper = $this->makeHelper(); $customerId = (int) $this->getRequest()->getParam('id'); if ($customerId) { - $this->_customer = Mage::getModel('customer/customer')->load($customerId); + $this->_customer = $this->getCustomerModel()->load($customerId); $this->storeId = $this->_customer->getStoreId(); } } public function getInterest() { - $subscriber = Mage::getModel('newsletter/subscriber'); + $subscriber = $this->getSubscriberModel(); $subscriber->loadByEmail($this->_customer->getEmail()); $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(), $this->storeId); return $interest; } + + /** + * @return Mage_Newsletter_Model_Subscriber + */ + protected function getSubscriberModel() + { + return Mage::getModel('newsletter/subscriber'); + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + protected function makeHelper() + { + return Mage::helper('mailchimp'); + } + + /** + * @return false|Mage_Core_Model_Abstract + */ + protected function getCustomerModel() + { + return Mage::getModel('customer/customer'); + } } From 8357be89e49b049289b39e298fd72b1bedf618e0 Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 29 May 2018 16:21:37 -0300 Subject: [PATCH 009/113] added changelog. --- CHANGELOG.md | 392 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 392 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..de5942a0d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,392 @@ +# Change Log + +## [1.1.12](https://github.com/mailchimp/mc-magento/tree/1.1.12) (2018-05-29) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.11...1.1.12) + +**Implemented enhancements:** + +- Add new message for store creation error. [\#681](https://github.com/mailchimp/mc-magento/issues/681) +- Request: add ability to send the actual BRAND/Manufacturer in the Vendor field [\#672](https://github.com/mailchimp/mc-magento/issues/672) +- Sort categories by name [\#659](https://github.com/mailchimp/mc-magento/issues/659) +- Add resend for subscriber data [\#482](https://github.com/mailchimp/mc-magento/issues/482) +- Subscriber resend issue482 [\#671](https://github.com/mailchimp/mc-magento/pull/671) ([Santiagoebizmarts](https://github.com/Santiagoebizmarts)) +- add guest checkout name to subscriber data [\#664](https://github.com/mailchimp/mc-magento/pull/664) ([jan-lukowiak](https://github.com/jan-lukowiak)) + +**Fixed bugs:** + +- Problem when updating customer email that is not subscribed [\#700](https://github.com/mailchimp/mc-magento/issues/700) +- STORECODE contains name of the store instead of code of the store [\#697](https://github.com/mailchimp/mc-magento/issues/697) +- Use store url with store code [\#691](https://github.com/mailchimp/mc-magento/issues/691) +- Compatibility issue with Ebizmarts\_SagePay when creating a new MailChimp store [\#680](https://github.com/mailchimp/mc-magento/issues/680) +- Checkout subscription not sending confirmation email if double opt-in enabled. [\#668](https://github.com/mailchimp/mc-magento/issues/668) +- Orders grid filter by increment ID is broken after upgrade to 1.1.11 [\#662](https://github.com/mailchimp/mc-magento/issues/662) +- Checkout subscription is only possible when isEcomSyncDataEnabled is enabled [\#657](https://github.com/mailchimp/mc-magento/issues/657) +- Wrong error management [\#635](https://github.com/mailchimp/mc-magento/issues/635) + +## [1.1.11](https://github.com/mailchimp/mc-magento/tree/1.1.11) (2018-03-06) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.10...1.1.11) + +**Implemented enhancements:** + +- query optimizations 1 [\#583](https://github.com/mailchimp/mc-magento/issues/583) +- Load campaignCatcher.js async [\#624](https://github.com/mailchimp/mc-magento/issues/624) +- Improve performance when retrieving the last date of purchase [\#619](https://github.com/mailchimp/mc-magento/issues/619) +- add index [\#584](https://github.com/mailchimp/mc-magento/issues/584) +- Added Mailchimperrors grid column Created At [\#569](https://github.com/mailchimp/mc-magento/issues/569) +- Put a column in the order grid to show if the order was synced [\#557](https://github.com/mailchimp/mc-magento/issues/557) +- Send parent price for not visible products belonging to a configurable [\#538](https://github.com/mailchimp/mc-magento/issues/538) +- Check if webhook exists after batch process and create it if missing [\#535](https://github.com/mailchimp/mc-magento/issues/535) +- Ebizmarts\_MailChimp properties are not defined correctly [\#361](https://github.com/mailchimp/mc-magento/issues/361) +- Cache management page - Do not flush cache [\#594](https://github.com/mailchimp/mc-magento/pull/594) ([ihor-sviziev](https://github.com/ihor-sviziev)) +- load MCJs async [\#546](https://github.com/mailchimp/mc-magento/pull/546) ([m-overlund](https://github.com/m-overlund)) + +**Fixed bugs:** + +- Promo rules response handling incorrectly. [\#654](https://github.com/mailchimp/mc-magento/issues/654) +- Problem with migration when only configured in store view. [\#633](https://github.com/mailchimp/mc-magento/issues/633) +- Handle store name change correctly [\#629](https://github.com/mailchimp/mc-magento/issues/629) +- Error generating new Promo Codes Collection [\#620](https://github.com/mailchimp/mc-magento/issues/620) +- getResourceModel not working correctly in some installations [\#616](https://github.com/mailchimp/mc-magento/issues/616) +- Altering email address of customer results in "Call to member function on null" when no API key is configured [\#613](https://github.com/mailchimp/mc-magento/issues/613) +- Resend Ecommerce Data not working with promo rules and promo codes [\#607](https://github.com/mailchimp/mc-magento/issues/607) +- Promo code data in order not sent correctly to Mailchimp [\#591](https://github.com/mailchimp/mc-magento/issues/591) +- Unable to "Reset MailChimp Store" because running out of memory [\#590](https://github.com/mailchimp/mc-magento/issues/590) +- Small and thumbnail images not sent [\#589](https://github.com/mailchimp/mc-magento/issues/589) +- All orders marked with Mailchimp logo even if they're not coming from Mailchimp [\#576](https://github.com/mailchimp/mc-magento/issues/576) +- Child product update when parent has not been sent yet [\#575](https://github.com/mailchimp/mc-magento/issues/575) +- Images are not sent in certain versions of PHP [\#559](https://github.com/mailchimp/mc-magento/issues/559) +- When Mandrill disabled in default scope and enabled in certain store views email sending fails. [\#550](https://github.com/mailchimp/mc-magento/issues/550) +- When api key is changed deleteCurrentWebhook method fails [\#548](https://github.com/mailchimp/mc-magento/issues/548) +- Order grid: All orders shows the mailchimp logo [\#539](https://github.com/mailchimp/mc-magento/issues/539) +- Removes exception in order grid if for example a used payment method has [\#563](https://github.com/mailchimp/mc-magento/pull/563) ([freestream](https://github.com/freestream)) + +## [1.1.10](https://github.com/mailchimp/mc-magento/tree/1.1.10) (2017-10-31) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.9.1...1.1.10) + +**Implemented enhancements:** + +- Consider prices set per website when configured that way [\#511](https://github.com/mailchimp/mc-magento/issues/511) +- Feature Request: Small Image instead of Base Image [\#414](https://github.com/mailchimp/mc-magento/issues/414) +- Add support for Promo Rules and Promo Codes [\#515](https://github.com/mailchimp/mc-magento/issues/515) +- Image for simple products not showing when inherited from configurable [\#513](https://github.com/mailchimp/mc-magento/issues/513) +- Change display of total subscribers in account details. [\#502](https://github.com/mailchimp/mc-magento/issues/502) +- Create cron job to clean mailchimp\_webhook\_request table [\#460](https://github.com/mailchimp/mc-magento/issues/460) +- Unnecessary error reporting during user subscription [\#284](https://github.com/mailchimp/mc-magento/issues/284) + +**Fixed bugs:** + +- Check how is\_syncing flag is modified. [\#510](https://github.com/mailchimp/mc-magento/issues/510) +- Webhook not created when module configured on store view [\#508](https://github.com/mailchimp/mc-magento/issues/508) +- Remove old mcjs url to be replaced with the new one. [\#492](https://github.com/mailchimp/mc-magento/issues/492) +- Subscribe on Checkout doesn't send email to Mailchimp if already as customer on the list [\#484](https://github.com/mailchimp/mc-magento/issues/484) +- Order status not updated in Mailchimp [\#481](https://github.com/mailchimp/mc-magento/issues/481) +- Product categories not being sent to Mailchimp [\#476](https://github.com/mailchimp/mc-magento/issues/476) +- First Purchase Automation Not Triggering [\#453](https://github.com/mailchimp/mc-magento/issues/453) +- Product feed not working on multiple stores \(linking to default Mage store\) [\#442](https://github.com/mailchimp/mc-magento/issues/442) + +## [1.1.9.1](https://github.com/mailchimp/mc-magento/tree/1.1.9.1) (2017-09-21) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.9...1.1.9.1) + +## [1.1.9](https://github.com/mailchimp/mc-magento/tree/1.1.9) (2017-09-18) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.8...1.1.9) + +**Implemented enhancements:** + +- Resend ecommerce corrupted data [\#359](https://github.com/mailchimp/mc-magento/issues/359) +- Separate each address field when sending subscriber data [\#423](https://github.com/mailchimp/mc-magento/issues/423) +- Rename MailChimp\_Requests.log file to MailChimp\_Failing\_Requests.log and log subscriber failing requests. [\#417](https://github.com/mailchimp/mc-magento/issues/417) +- Show camp name in magento order view [\#416](https://github.com/mailchimp/mc-magento/issues/416) +- Create button to re-send ecommerce data without loosing MailChimp store. [\#413](https://github.com/mailchimp/mc-magento/issues/413) +- Simple products showing at $0 [\#370](https://github.com/mailchimp/mc-magento/issues/370) +- Is it possible to populate default language via Magento -\> MC [\#357](https://github.com/mailchimp/mc-magento/issues/357) +- Enable overriding e-commerce sync batch size [\#256](https://github.com/mailchimp/mc-magento/issues/256) + +**Fixed bugs:** + +- Fix for subscriber address. [\#478](https://github.com/mailchimp/mc-magento/issues/478) +- Deleting Newsletter subscribers in Magento cleans them in MailChimp [\#448](https://github.com/mailchimp/mc-magento/issues/448) +- Error with multi-currency for carts in multi-store [\#441](https://github.com/mailchimp/mc-magento/issues/441) +- Conflict with multi-currency for orders and revenue [\#439](https://github.com/mailchimp/mc-magento/issues/439) +- flag 'bad' addresses, and stop trying them. [\#436](https://github.com/mailchimp/mc-magento/issues/436) +- Send product data for the correct store view. [\#421](https://github.com/mailchimp/mc-magento/issues/421) +- mailchimp\_process\_webhook\_data Cron failures [\#415](https://github.com/mailchimp/mc-magento/issues/415) +- Unnecessary batch processing with empty batch\_id causes errors [\#404](https://github.com/mailchimp/mc-magento/issues/404) +- Parent product image doesn't update, only variant does [\#363](https://github.com/mailchimp/mc-magento/issues/363) +- Invalid product url on simple products not visible [\#341](https://github.com/mailchimp/mc-magento/issues/341) +- Address MERGE tags not created/synced [\#273](https://github.com/mailchimp/mc-magento/issues/273) + +## [1.1.8](https://github.com/mailchimp/mc-magento/tree/1.1.8) (2017-07-27) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.7...1.1.8) + +**Implemented enhancements:** + +- Ignore already exists error [\#360](https://github.com/mailchimp/mc-magento/issues/360) +- Put webhook calls on a queue [\#267](https://github.com/mailchimp/mc-magento/issues/267) +- Add a PHP script to remove the extension, add it to the extension [\#137](https://github.com/mailchimp/mc-magento/issues/137) +- Add checkout subscription checkbox [\#405](https://github.com/mailchimp/mc-magento/issues/405) +- Change color of migration notice because red can be taken as an error. [\#385](https://github.com/mailchimp/mc-magento/issues/385) +- When item already exists asume it should be an edit request. [\#368](https://github.com/mailchimp/mc-magento/issues/368) +- Send out of stock products [\#353](https://github.com/mailchimp/mc-magento/issues/353) +- Modify webhook creation [\#340](https://github.com/mailchimp/mc-magento/issues/340) +- New Feature: flag is\_syncing [\#323](https://github.com/mailchimp/mc-magento/issues/323) +- API Products constant array declaration unsupported in PHP 5.5 [\#316](https://github.com/mailchimp/mc-magento/issues/316) +- Set limit on Collection to 1. [\#333](https://github.com/mailchimp/mc-magento/pull/333) ([centerax](https://github.com/centerax)) +- Do not translate customer group [\#315](https://github.com/mailchimp/mc-magento/pull/315) ([Schrank](https://github.com/Schrank)) + +**Fixed bugs:** + +- Error on deleteStore function when removing old webhooks [\#407](https://github.com/mailchimp/mc-magento/issues/407) +- Set limit for migraiton from 1.1.6 [\#396](https://github.com/mailchimp/mc-magento/issues/396) +- If ecommerce section enabled but no Api key is set the extension tries to get the MCJS anyways [\#388](https://github.com/mailchimp/mc-magento/issues/388) +- Problem with order edit causing "Resource not found error" [\#373](https://github.com/mailchimp/mc-magento/issues/373) +- Catalog product flat table config causes problem when processing ecommerce data [\#369](https://github.com/mailchimp/mc-magento/issues/369) +- When ecommerce data is not enabled mcminsyncdateflag is empty affecting subscribers [\#364](https://github.com/mailchimp/mc-magento/issues/364) +- When ecommerce is not enabled can not reset errors. [\#349](https://github.com/mailchimp/mc-magento/issues/349) +- Duplicate entries for subscriber table when customer/subscriber created from admin [\#342](https://github.com/mailchimp/mc-magento/issues/342) +- Custom Product causing failure in SendModifiedProduct [\#335](https://github.com/mailchimp/mc-magento/issues/335) +- email index query is incorrect \(mysql4-upgrade-1.1.6.6-1.1.6.7.php\) [\#324](https://github.com/mailchimp/mc-magento/issues/324) +- PHP Fatal error in syncSubscriberBatchData \(cron\) [\#312](https://github.com/mailchimp/mc-magento/issues/312) +- Error Synchronising Products When Configurable Products Children Have Been Deleted [\#297](https://github.com/mailchimp/mc-magento/issues/297) +- Web hooks continuously processed [\#295](https://github.com/mailchimp/mc-magento/issues/295) +- Webhook process might fail if the configured list changes. [\#293](https://github.com/mailchimp/mc-magento/issues/293) +- Parent configurable images not being sent when child has no image. [\#292](https://github.com/mailchimp/mc-magento/issues/292) + +## [1.1.7](https://github.com/mailchimp/mc-magento/tree/1.1.7) (2017-06-01) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.6...1.1.7) + +**Implemented enhancements:** + +- Send order id in stead of increment id in operation id for batches. [\#286](https://github.com/mailchimp/mc-magento/issues/286) +- Sent orderId in stead of incrementId in operation id in Orders.php [\#281](https://github.com/mailchimp/mc-magento/issues/281) +- Split cron jobs. [\#277](https://github.com/mailchimp/mc-magento/issues/277) +- Remove old MageMonkey webhooks. [\#261](https://github.com/mailchimp/mc-magento/issues/261) +- Handle data migration within a cron job in order to prevent problems during update. [\#233](https://github.com/mailchimp/mc-magento/issues/233) +- Installation of MC.js pixel [\#225](https://github.com/mailchimp/mc-magento/issues/225) +- Add customer id to mailchimp\_merge\_field\_send\_before observer [\#221](https://github.com/mailchimp/mc-magento/issues/221) +- Cache check for mailchimp store for given scope [\#216](https://github.com/mailchimp/mc-magento/issues/216) +- Incorrect log file referenced in configuration note [\#212](https://github.com/mailchimp/mc-magento/issues/212) +- Send store domain when creating it. [\#205](https://github.com/mailchimp/mc-magento/issues/205) +- Missing index on mailchimp\_ecommerce\_sync\_data [\#197](https://github.com/mailchimp/mc-magento/issues/197) +- Order ID being used instead of Order \# [\#165](https://github.com/mailchimp/mc-magento/issues/165) +- Fix case mixing and make class namespace/prefix the same for all classes [\#207](https://github.com/mailchimp/mc-magento/pull/207) ([kim-sondrup](https://github.com/kim-sondrup)) +- Error in `getBatchResponse` method when `/var/mailchimp` directory does not exist [\#203](https://github.com/mailchimp/mc-magento/pull/203) ([vseager](https://github.com/vseager)) +- Added pagination for too huge collections in data-upgrade [\#192](https://github.com/mailchimp/mc-magento/pull/192) ([loburets](https://github.com/loburets)) + +**Fixed bugs:** + +- Webhook calls cause unnecessary calls when handleSubscriber method is called from webhook [\#279](https://github.com/mailchimp/mc-magento/issues/279) +- Migration never ends due to cron failure [\#266](https://github.com/mailchimp/mc-magento/issues/266) +- The parent product must already exists in order to use PUT on the variants endpoint error in some installations. [\#254](https://github.com/mailchimp/mc-magento/issues/254) +- Can't change attribute or status of multiple products [\#241](https://github.com/mailchimp/mc-magento/issues/241) +- All orders are marked with landing page & as coming from MailChimp. [\#239](https://github.com/mailchimp/mc-magento/issues/239) +- mailchimp/api\_subscribers-\>\_getMCStatus\(\) returns integers [\#235](https://github.com/mailchimp/mc-magento/issues/235) +- Notice: Undefined index: image\_url [\#231](https://github.com/mailchimp/mc-magento/issues/231) +- Mailchimp store is created multiple times when enabling mailchimp and ecommerce data [\#227](https://github.com/mailchimp/mc-magento/issues/227) +- mailchimp\_campaign\_id not being saved if utm\_source=mailchimp not available. [\#226](https://github.com/mailchimp/mc-magento/issues/226) +- Don't skip store subscriber changes if previous store has no changes to synchronise [\#222](https://github.com/mailchimp/mc-magento/issues/222) +- Fixes for cart changes not being uploaded for abandoned cart [\#219](https://github.com/mailchimp/mc-magento/issues/219) +- Multi-store abandoned cart enabled flag ignored [\#218](https://github.com/mailchimp/mc-magento/issues/218) +- Fix for invalid list ID when saving mailchimp system configuration [\#214](https://github.com/mailchimp/mc-magento/issues/214) +- Line feeds in default configuration values in config.xml break unserialize [\#213](https://github.com/mailchimp/mc-magento/issues/213) +- Subscription fails when a customer has wrong address data, infinite loop [\#211](https://github.com/mailchimp/mc-magento/issues/211) +- Bulk Editing Products Returns Blank Error \[Fix inside\] [\#209](https://github.com/mailchimp/mc-magento/issues/209) +- PHP Fatal error: Call to a member function getStreet\(\) on a non-object in app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php on line 321 [\#208](https://github.com/mailchimp/mc-magento/issues/208) +- Could not delete customer. [\#206](https://github.com/mailchimp/mc-magento/issues/206) +- Exception is thrown when trying to update product status from a script [\#204](https://github.com/mailchimp/mc-magento/issues/204) +- Merge fields not pushed on customer save [\#201](https://github.com/mailchimp/mc-magento/issues/201) +- Fatal Error in handleSubscriberDeletion\(\) method from Observer.php [\#195](https://github.com/mailchimp/mc-magento/issues/195) +- 1.5.5.6-1.5.6 MySQL upgrade memory exhausted [\#189](https://github.com/mailchimp/mc-magento/issues/189) +- Subscriber batches remain in pending state [\#187](https://github.com/mailchimp/mc-magento/issues/187) +- Minor issue with cron [\#186](https://github.com/mailchimp/mc-magento/issues/186) +- Syncing customer billing/shipping address fields does not work [\#184](https://github.com/mailchimp/mc-magento/issues/184) +- Get API Credential - Back End [\#179](https://github.com/mailchimp/mc-magento/issues/179) + +## [1.1.6](https://github.com/mailchimp/mc-magento/tree/1.1.6) (2017-03-30) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.5...1.1.6) + +**Implemented enhancements:** + +- Add full support for multi-stores. [\#103](https://github.com/mailchimp/mc-magento/issues/103) +- Create event to handle custom merge fields [\#176](https://github.com/mailchimp/mc-magento/issues/176) +- Unable to send email in queue unless entity\_type = 'order'.... [\#174](https://github.com/mailchimp/mc-magento/issues/174) +- Set the DOB field to be created as birthday on MailChimp. [\#173](https://github.com/mailchimp/mc-magento/issues/173) +- customer re-subscribe fails silently [\#167](https://github.com/mailchimp/mc-magento/issues/167) +- No redirect back from customer login when accessing abandoned cart URL [\#162](https://github.com/mailchimp/mc-magento/issues/162) +- Recommend products no images when only configurable product has images [\#140](https://github.com/mailchimp/mc-magento/issues/140) + +**Fixed bugs:** + +- Cart Url redirect failing. [\#180](https://github.com/mailchimp/mc-magento/issues/180) +- Response downloads are always empty [\#177](https://github.com/mailchimp/mc-magento/issues/177) +- Merge Fields not updated in Mailchimp [\#170](https://github.com/mailchimp/mc-magento/issues/170) +- Send e-mail copy type "Separate Email" bug [\#163](https://github.com/mailchimp/mc-magento/issues/163) +- Admin skin missing a file [\#156](https://github.com/mailchimp/mc-magento/issues/156) +- Move debug scripts [\#155](https://github.com/mailchimp/mc-magento/issues/155) +- Guest orders are not synced [\#150](https://github.com/mailchimp/mc-magento/issues/150) +- Integrity constraint violation when syncing e-commerce data [\#147](https://github.com/mailchimp/mc-magento/issues/147) +- Lower case subscribers class name [\#145](https://github.com/mailchimp/mc-magento/issues/145) + +## [1.1.5](https://github.com/mailchimp/mc-magento/tree/1.1.5) (2017-02-08) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.3...1.1.5) + +**Implemented enhancements:** + +- Typo in Configuration header [\#121](https://github.com/mailchimp/mc-magento/issues/121) +- Allow custom mailchimp attributes to be deleted from back end. [\#119](https://github.com/mailchimp/mc-magento/issues/119) +- If the recipient doesn't exists in the email queue skip it [\#118](https://github.com/mailchimp/mc-magento/issues/118) +- Add first date for orders [\#113](https://github.com/mailchimp/mc-magento/issues/113) +- Pass order\_URL for orders [\#135](https://github.com/mailchimp/mc-magento/issues/135) +- Need to pass Shipping and Billing Addresses for Orders [\#128](https://github.com/mailchimp/mc-magento/issues/128) +- Populate landing\_site column [\#123](https://github.com/mailchimp/mc-magento/issues/123) +- Update order status [\#120](https://github.com/mailchimp/mc-magento/issues/120) +- Get URL for MailChimp store based on configurations in stead of current URL [\#115](https://github.com/mailchimp/mc-magento/issues/115) + +**Fixed bugs:** + +- The product images link to my administrator page, not to the front-end of my magento's website. [\#127](https://github.com/mailchimp/mc-magento/issues/127) +- The download link in the error grid doesn't work [\#126](https://github.com/mailchimp/mc-magento/issues/126) +- The table sales\_flat\_quote don't content the field mailchimp\_campaign\_id [\#125](https://github.com/mailchimp/mc-magento/issues/125) +- If the batch id doesn't exists when retrieving batch responses the process stops. [\#116](https://github.com/mailchimp/mc-magento/issues/116) +- Wrong format for mailchimp\_sync\_delta field [\#111](https://github.com/mailchimp/mc-magento/issues/111) +- Carts with country data send country name on country code field and vice versa. [\#108](https://github.com/mailchimp/mc-magento/issues/108) +- Calling $object-\>save\(\) on entities during batch processing [\#88](https://github.com/mailchimp/mc-magento/issues/88) +- Make sure cancelled orders go to Cancelled, not Pending [\#133](https://github.com/mailchimp/mc-magento/issues/133) +- Store name changes not pushed up to MailChimp [\#130](https://github.com/mailchimp/mc-magento/issues/130) +- Wrong Store Name [\#129](https://github.com/mailchimp/mc-magento/issues/129) + +## [1.1.3](https://github.com/mailchimp/mc-magento/tree/1.1.3) (2016-12-15) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.2...1.1.3) + +**Implemented enhancements:** + +- The Monkey image in the order grid [\#107](https://github.com/mailchimp/mc-magento/issues/107) +- Do not update the status for already subscribed customers in MailChimp when syncing for the first time. [\#102](https://github.com/mailchimp/mc-magento/issues/102) +- Change the with of the Mailchimp column in the order grid [\#101](https://github.com/mailchimp/mc-magento/issues/101) +- Error grid sohwing Id for better debugging. [\#100](https://github.com/mailchimp/mc-magento/issues/100) +- Swap lines in Configuration page [\#99](https://github.com/mailchimp/mc-magento/issues/99) + +**Fixed bugs:** + +- Error in the lib [\#106](https://github.com/mailchimp/mc-magento/issues/106) +- Check for customer data [\#105](https://github.com/mailchimp/mc-magento/issues/105) +- Stores with long domain name doesn't create properly in Ecommerce [\#85](https://github.com/mailchimp/mc-magento/issues/85) + +## [1.1.2](https://github.com/mailchimp/mc-magento/tree/1.1.2) (2016-10-25) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.1...1.1.2) + +**Implemented enhancements:** + +- Add is\_syncing flag usage for MailChimp store. [\#80](https://github.com/mailchimp/mc-magento/issues/80) +- Abandoned cart in sales order grid [\#77](https://github.com/mailchimp/mc-magento/issues/77) +- Allow store owners to decide if customers will be subscribed to the newsletter. [\#75](https://github.com/mailchimp/mc-magento/issues/75) +- Ecommerce data saving in website and store scopes [\#74](https://github.com/mailchimp/mc-magento/issues/74) +- Make the order to send your own products [\#67](https://github.com/mailchimp/mc-magento/issues/67) +- Utilty to download the batch response [\#66](https://github.com/mailchimp/mc-magento/issues/66) +- Add the Batch Id to the mailchimp error grid [\#65](https://github.com/mailchimp/mc-magento/issues/65) +- Generate one log per each batch [\#64](https://github.com/mailchimp/mc-magento/issues/64) +- Add composer.json and modman support [\#61](https://github.com/mailchimp/mc-magento/issues/61) + +**Fixed bugs:** + +- Error message "Error: no identification SUB found" solved. [\#76](https://github.com/mailchimp/mc-magento/issues/76) +- Carts being sent even if disabled in the configuration. [\#73](https://github.com/mailchimp/mc-magento/issues/73) +- Invalid country code error shown in MailChimp\_Errors.log [\#72](https://github.com/mailchimp/mc-magento/issues/72) +- Customers generating resource not found error [\#71](https://github.com/mailchimp/mc-magento/issues/71) +- Carts not existing on MailChimp being deleted before getting sent. [\#70](https://github.com/mailchimp/mc-magento/issues/70) +- campaign\_id isn't associated to order when cookie lifetime != 3600 [\#68](https://github.com/mailchimp/mc-magento/issues/68) +- Update product stock qty [\#56](https://github.com/mailchimp/mc-magento/issues/56) + +## [1.1.1](https://github.com/mailchimp/mc-magento/tree/1.1.1) (2016-09-13) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.0...1.1.1) + +**Fixed bugs:** + +- Mixed emails sent when made simultaneously on checkout. [\#60](https://github.com/mailchimp/mc-magento/issues/60) + +## [1.1.0](https://github.com/mailchimp/mc-magento/tree/1.1.0) (2016-09-13) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.0.6...1.1.0) + +**Implemented enhancements:** + +- Add permission functionality for back end controllers [\#53](https://github.com/mailchimp/mc-magento/issues/53) +- Remove unnecessary menu option [\#52](https://github.com/mailchimp/mc-magento/issues/52) +- Add MC logo to orders table for orders made from a Campaign [\#51](https://github.com/mailchimp/mc-magento/issues/51) +- Add link to MailChimp For Magento docs [\#50](https://github.com/mailchimp/mc-magento/issues/50) + +**Fixed bugs:** + +- Json enconde error [\#59](https://github.com/mailchimp/mc-magento/issues/59) +- Tier prices being deleted after products being sent. [\#57](https://github.com/mailchimp/mc-magento/issues/57) +- Sync process stops randomly and does not go ahead [\#55](https://github.com/mailchimp/mc-magento/issues/55) +- Sending products to Mailchimp makes Dropdown attributes to get the "Default option" \(if have one selected\) [\#54](https://github.com/mailchimp/mc-magento/issues/54) + +## [1.0.6](https://github.com/mailchimp/mc-magento/tree/1.0.6) (2016-08-17) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.0.4.2...1.0.6) + +**Implemented enhancements:** + +- Message when can't create a webhook [\#37](https://github.com/mailchimp/mc-magento/issues/37) +- Sent link to list creation page when no list available [\#36](https://github.com/mailchimp/mc-magento/issues/36) +- Hide Merge Fields [\#43](https://github.com/mailchimp/mc-magento/issues/43) +- Abandoned Guest checkouts using Subscribed email addresses not passed to MC [\#40](https://github.com/mailchimp/mc-magento/issues/40) +- Include address information for guests on abandoned carts [\#32](https://github.com/mailchimp/mc-magento/issues/32) +- Tax and Shipping totals not passed to MailChimp [\#25](https://github.com/mailchimp/mc-magento/issues/25) +- Send carts [\#10](https://github.com/mailchimp/mc-magento/issues/10) +- Manage cancelled orders [\#4](https://github.com/mailchimp/mc-magento/issues/4) + +**Fixed bugs:** + +- Fix ApiKey and General Subscription List [\#41](https://github.com/mailchimp/mc-magento/issues/41) +- A magento report is generated when put an invalid ApiKey [\#39](https://github.com/mailchimp/mc-magento/issues/39) +- Handle campaignId when API Key/List changed. [\#38](https://github.com/mailchimp/mc-magento/issues/38) +- Issue with old cookie of the campaign [\#35](https://github.com/mailchimp/mc-magento/issues/35) +- Delete all carts for an email [\#31](https://github.com/mailchimp/mc-magento/issues/31) +- No send empty carts [\#30](https://github.com/mailchimp/mc-magento/issues/30) +- Not send guest carts for registered customer [\#29](https://github.com/mailchimp/mc-magento/issues/29) +- Old carts are sent [\#28](https://github.com/mailchimp/mc-magento/issues/28) +- Cron breaks when the email is entered in the popup [\#49](https://github.com/mailchimp/mc-magento/issues/49) +- Missing cache breaks Webhooks [\#48](https://github.com/mailchimp/mc-magento/issues/48) +- Manage the Ecommerce Enabled [\#47](https://github.com/mailchimp/mc-magento/issues/47) +- Subscribing, unsubscribing and subscribing again error. [\#46](https://github.com/mailchimp/mc-magento/issues/46) +- Handle total\_spent for MailChimp customers from Magento [\#45](https://github.com/mailchimp/mc-magento/issues/45) +- Handle order\_count for MailChimp customers from Magento [\#44](https://github.com/mailchimp/mc-magento/issues/44) +- Orders not sending all the customer information for guests [\#42](https://github.com/mailchimp/mc-magento/issues/42) +- Issue with new products [\#27](https://github.com/mailchimp/mc-magento/issues/27) +- Remove mailchimp cookie when new order is created [\#26](https://github.com/mailchimp/mc-magento/issues/26) + +## [1.0.4.2](https://github.com/mailchimp/mc-magento/tree/1.0.4.2) (2016-07-04) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.0.4.1...1.0.4.2) + +## [1.0.4.1](https://github.com/mailchimp/mc-magento/tree/1.0.4.1) (2016-07-01) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.0.4...1.0.4.1) + +## [1.0.4](https://github.com/mailchimp/mc-magento/tree/1.0.4) (2016-07-01) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.0.3...1.0.4) + +**Implemented enhancements:** + +- Customer Modification [\#24](https://github.com/mailchimp/mc-magento/issues/24) +- Pass order information if a product type is not supported [\#18](https://github.com/mailchimp/mc-magento/issues/18) + +**Fixed bugs:** + +- opt\_in\_status is always sent as FALSE [\#15](https://github.com/mailchimp/mc-magento/issues/15) +- Issue with the stock when save a product [\#16](https://github.com/mailchimp/mc-magento/issues/16) + +## [1.0.3](https://github.com/mailchimp/mc-magento/tree/1.0.3) (2016-06-20) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.0.2...1.0.3) + +## [1.0.2](https://github.com/mailchimp/mc-magento/tree/1.0.2) (2016-06-14) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.0.1...1.0.2) + +**Implemented enhancements:** + +- Change array declaration to pre php 5.4. [\#3](https://github.com/mailchimp/mc-magento/issues/3) + +## [1.0.1](https://github.com/mailchimp/mc-magento/tree/1.0.1) (2016-06-08) +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.0.0...1.0.1) + +## [1.0.0](https://github.com/mailchimp/mc-magento/tree/1.0.0) (2016-06-06) + + +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* From cd4a5565c2e14e57817740f1799bd18f3479a7aa Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 31 May 2018 10:28:48 -0300 Subject: [PATCH 010/113] Test incomplete --- .../Adminhtml/MailchimpController.php | 4 +- .../Adminhtml/MailchimpControllerTest.php | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index f7b00f9a1..3fc14d898 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -17,7 +17,9 @@ public function indexAction() $customerId = (int) $this->getRequest()->getParam('id'); if ($customerId) { $this->getResponse()->setBody( - $this->getLayout()->createBlock('mailchimp/adminhtml_customer_edit_tab_mailchimp', 'admin.customer.mailchimp')->setCustomerId($customerId) + $this->getLayout() + ->createBlock('mailchimp/adminhtml_customer_edit_tab_mailchimp', 'admin.customer.mailchimp') + ->setCustomerId($customerId) ->setUseAjax(true) ->toHtml() ); diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpControllerTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpControllerTest.php index 2e7e60efd..df7416d0b 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpControllerTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpControllerTest.php @@ -20,6 +20,56 @@ public function tearDown() $this->mailchimpController = null; } + public function testIndexAction() + { + $customerId = 1; + $type = 'mailchimp/adminhtml_customer_edit_tab_mailchimp'; + $name = 'admin.customer.mailchimp'; + $result = ''; + + $mailchimpControllerMock = $this->mailchimpController + ->disableOriginalConstructor() + ->setMethods(array('getRequest', 'getResponse', 'getLayout')) + ->getMock(); + + $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParam')) + ->getMock(); + + $responseMock = $this->getMockBuilder(Mage_Core_Controller_Response_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('setBody')) + ->getMock(); + + $layoutMock = $this->getMockBuilder(Mage_Core_Model_Layout::class) + ->disableOriginalConstructor() + ->setMethods(array('createBlock')) + ->getMock(); + + $blockMock = $this->getMockBuilder(Ebizmarts_MailChimp_Block_Adminhtml_Customer_Edit_Tab_Mailchimp::class) + ->disableOriginalConstructor() + ->setMethods(array('setCustomerId', 'setUseAjax', 'toHtml')) + ->getMock(); + + $mailchimpControllerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); + + $requestMock->expects($this->once())->method('getParam')->with('id')->willReturn($customerId); + + $mailchimpControllerMock->expects($this->once())->method('getLayout')->willReturn($layoutMock); + + $layoutMock->expects($this->once())->method('createBlock')->with($type, $name)->willReturn($blockMock); + + $blockMock->expects($this->once())->method('setCustomerId')->with($customerId)->willReturnSelf(); + $blockMock->expects($this->once())->method('setUseAjax')->with(true)->willReturnSelf(); + $blockMock->expects($this->once())->method('toHtml')->willReturn($result); + + $mailchimpControllerMock->expects($this->once())->method('getResponse')->willReturn($responseMock); + $responseMock->expects($this->once())->method('setBody')->with($result); + + $mailchimpControllerMock->indexAction(); + } + public function testResendSubscribersAction() { $paramScope = 'scope'; From b489f48ea67a60c8434490f8e1399e6b4ae5973f Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 26 Jul 2018 16:53:54 -0300 Subject: [PATCH 011/113] Interest groups handled with admin created customers. resolves #514 --- .../Adminhtml/Customer/Edit/Tab/Mailchimp.php | 33 ++++- .../Block/Checkout/Success/Groups.php | 5 +- .../Block/Customer/Newsletter/Index.php | 19 ++- .../Ebizmarts/MailChimp/Helper/Data.php | 90 +++++++++++- .../MailChimp/Model/Api/Subscribers.php | 61 +++++--- .../MailChimp/Model/Interestgroup.php | 4 +- .../MailChimp/Model/Mysql4/Interestgroup.php | 6 +- .../Ebizmarts/MailChimp/Model/Observer.php | 135 +++++++++++------- .../MailChimp/controllers/GroupController.php | 4 +- .../Ebizmarts/MailChimp/etc/config.xml | 12 +- ...php => mysql4-upgrade-1.1.12-1.1.12.2.php} | 39 ++--- ...php => mysql4-upgrade-1.1.12.2-1.1.13.php} | 5 +- .../MailChimp/Model/ObserverTest.php | 4 +- 13 files changed, 304 insertions(+), 113 deletions(-) rename app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/{mysql4-upgrade-1.1.12-1.1.13.php => mysql4-upgrade-1.1.12-1.1.12.2.php} (62%) rename app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/{mysql4-upgrade-1.1.11-1.1.12.php => mysql4-upgrade-1.1.12.2-1.1.13.php} (73%) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php index 7f99da61f..e15943121 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php @@ -34,15 +34,20 @@ public function __construct() $customerId = (int) $this->getRequest()->getParam('id'); if ($customerId) { $this->_customer = $this->getCustomerModel()->load($customerId); - $this->storeId = $this->_customer->getStoreId(); + $this->storeId = $this->getCustomer()->getStoreId(); } } public function getInterest() { + $customer = $this->getCustomer(); $subscriber = $this->getSubscriberModel(); - $subscriber->loadByEmail($this->_customer->getEmail()); - $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(), $this->storeId); + $subscriber->loadByEmail($customer->getEmail()); + $subscriberId = $subscriber->getSubscriberId(); + $customerId = $customer->getId(); + $storeId = $this->getStoreId(); + $interest = $this->helper->getInterestGroups($customerId, $subscriberId, $storeId); + return $interest; } @@ -69,4 +74,26 @@ protected function getCustomerModel() { return Mage::getModel('customer/customer'); } + + /** + * @return Mage_Core_Model_Abstract + */ + protected function getCustomer() + { + return $this->_customer; + } + + /** + * If customer was created in admin panel use the store view selected for MailChimp. + * + * @return mixed + */ + protected function getStoreId() + { + $storeId = $this->storeId; + if (!$storeId) { + $storeId = $this->_customer->getMailchimpStoreView(); + } + return $storeId; + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index a81ca352d..fc86af530 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -212,7 +212,10 @@ public function getInterest() $subscriber = Mage::getModel('newsletter/subscriber'); $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); $subscriber->loadByEmail($order->getCustomerEmail()); - $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(),$order->getStoreId()); + $subscriberId = $subscriber->getSubscriberId(); + $customerId = $order->getCustomerId(); + + $interest = $this->helper->getInterestGroups($customerId, $subscriberId,$order->getStoreId()); return $interest; } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php index 8187f4cb1..8ffa7ab80 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php @@ -35,7 +35,16 @@ public function getInterest() { $subscriber = Mage::getModel('newsletter/subscriber'); $subscriber->loadByEmail($this->_getEmail()); - $interest = $this->helper->getSubscriberInterest($subscriber->getSubscriberId(), $subscriber->getStoreId()); + $helper = $this->getMailChimpHelper(); + if (!$helper->isAdmin() && Mage::getSingleton('customer/session')->isLoggedIn()) { + $customer = Mage::getSingleton('customer/session')->getCustomer(); + $customerId = $customer->getId(); + $storeId = ($subscriber->getStoreId()) ? $subscriber->getStoreId() : $customer->getStoreId(); + } else { + $customerId = null; + $storeId = $subscriber->getStoreId(); + } + $interest = $helper->getInterestGroups($customerId, $subscriber->getSubscriberId(), $storeId); return $interest; } @@ -49,4 +58,12 @@ protected function _getEmail() return $this->helper('customer')->getCustomer()->getEmail(); } + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + protected function getMailChimpHelper() + { + return $this->helper; + } + } diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 2ca5d71ad..8d8c1dc50 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -2812,8 +2812,8 @@ public function getSubscriberAmountLimit() public function getStoreLanguageCode($scopeId, $scope = 'stores') { - $isAdmin = Mage::app()->getStore()->isAdmin(); - $userLangCode = Mage::app()->getLocale()->getLocaleCode(); + $isAdmin = $this->isAdmin(); + $userLangCode = Mage::app()->getLocale()->getLocaleCode(); if ($isAdmin || '' == $lang = $this->_lang2MCLanguage($userLangCode)) { // IS Admin OR if users lang is not supported, try store views default locale $userLangCode = $this->getConfigValueForScope(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $scopeId, $scope); @@ -3195,6 +3195,11 @@ public function getCheckoutSuccessHtmlAfter($scopeId, $scope = 'stores') return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_INTEREST_SUCCESS_AFTER, $scopeId, $scope); } + /** + * @param $storeId + * @return array + * @throws MailChimp_Error + */ public function getInterest($storeId) { $rc = []; @@ -3221,13 +3226,21 @@ public function getInterest($storeId) return $rc; } - public function getSubscriberInterest($subscriberId, $storeId, $interest = null) + /** + * @param $customerId + * @param $subscriberId + * @param $storeId + * @param null $interest + * @return array|null + * @throws MailChimp_Error + */ + public function getInterestGroups($customerId, $subscriberId, $storeId, $interest = null) { if (!$interest) { $interest = $this->getInterest($storeId); } $interestGroup = Mage::getModel('mailchimp/interestgroup'); - $interestGroup->getBySubscriberIdStoreId($subscriberId, $storeId); + $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); if ($interestGroup->getId()) { $groups = unserialize($interestGroup->getGroupdata()); foreach ($groups as $key => $value) { @@ -3256,4 +3269,73 @@ public function getSubscriberInterest($subscriberId, $storeId, $interest = null) } return $interest; } + + /** + * @param $params + * @param $storeId + * @param null $customerId + * @param null $subscriber + * @throws Mage_Core_Model_Store_Exception + */ + public function saveInterestGroupData($params, $storeId, $customerId = null, $subscriber = null) + { + $groups = $this->getInterestGroupsIfAvailable($params); + if (!$customerId) { + if ($this->isAdmin()) { + $customerId = $params['customer_id']; + } elseif (Mage::getSingleton('customer/session')->isLoggedIn()) { + $customerData = Mage::getSingleton('customer/session')->getCustomer(); + $customerId = $customerData->getId(); + } + } + $subscriberId = null; + if ($subscriber) { + $subscriberId = $subscriber->getSubscriberId(); + } + $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); + $origSubscriberId = $interestGroup->getSubscriberId(); + $origCustomerId = $interestGroup->getCustomerId(); + if (!$origSubscriberId || $subscriberId && $origSubscriberId != $subscriberId) { + $interestGroup->setSubscriberId($subscriberId); + } + if (!$origCustomerId || $customerId && $origCustomerId != $customerId) { + $interestGroup->setCustomerId($customerId); + } + if ($groups) { + $interestGroup->setGroupdata(serialize($groups)); + } + //Avoid creating a new entry if no groupData available. (Customer creation) + if ($interestGroup->getGroupdata()) { + if ($storeId) { + $interestGroup->setStoreId($storeId); + } + $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); + $interestGroup->save(); + } + } + + /** + * @param $params + * @return mixed + */ + public function getInterestGroupsIfAvailable($params) + { + $groups = null; + if (isset($params['customer']) && isset($params['customer']['interestgroup'])) { + $groups = $params['customer']['interestgroup']; + } elseif (isset($params['group'])) { + $groups = $params['group']; + } + return $groups; + } + + /** + * @return bool + * @throws Mage_Core_Model_Store_Exception + */ + public function isAdmin() + { + return Mage::app()->getStore()->isAdmin(); + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php index b683fd9c7..3e6de5dd0 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php @@ -126,7 +126,7 @@ protected function _getInterest($subscriber) $rc = array(); $helper = $this->mcHelper; $interestsAvailable = $helper->getInterest($storeId); - $interest = $helper->getSubscriberInterest($subscriber->getSubscriberId(), $storeId, $interestsAvailable); + $interest = $helper->getInterestGroups(null, $subscriber->getSubscriberId(), $storeId, $interestsAvailable); foreach($interest as $i) { foreach($i['category'] as $key=>$value) { $rc[$value['id']] = $value['checked']; @@ -346,6 +346,7 @@ public function getMergeVars($subscriber) */ public function updateSubscriber($subscriber, $updateStatus = false) { + $saveSubscriber = false; $isAdmin = Mage::app()->getStore()->isAdmin(); $helper = $this->mcHelper; $storeId = $subscriber->getStoreId(); @@ -368,38 +369,65 @@ public function updateSubscriber($subscriber, $updateStatus = false) try { $api->lists->members->addOrUpdate( $listId, $md5HashEmail, $subscriber->getSubscriberEmail(), $newStatus, null, $forceStatus, $mergeVars, - null, $language, null, null + $interest, $language, null, null ); $subscriber->setData("mailchimp_sync_delta", Varien_Date::now()); $subscriber->setData("mailchimp_sync_error", ""); $subscriber->setData("mailchimp_sync_modified", 0); + $saveSubscriber = true; } catch (MailChimp_Error $e) { - if ($newStatus === 'subscribed' && $subscriber->getIsStatusChanged()) { + if ($newStatus === 'subscribed' && $subscriber->getIsStatusChanged() && !$helper->isSubscriptionConfirmationEnabled($storeId)) { if (strstr($e->getMailchimpDetails(), 'is in a compliance state')) { try { $api->lists->members->update($listId, $md5HashEmail, null, 'pending', $mergeVars, $interest); - $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED); + $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); + $saveSubscriber = true; $message = $helper->__('To begin receiving the newsletter, you must first confirm your subscription'); Mage::getSingleton('core/session')->addWarning($message); } catch (MailChimp_Error $e) { - $helper->logError($e->getFriendlyMessage()); - $this->addError($isAdmin, $e); - $subscriber->unsubscribe(); + $errorMessage = $e->getFriendlyMessage(); + $helper->logError($errorMessage); + if ($isAdmin) { + $this->addError($errorMessage); + } else { + $errorMessage = $helper->__("The subscription could not be applied."); + $this->addError($errorMessage); + } + $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED); + $saveSubscriber = true; } catch (Exception $e) { $helper->logError($e->getMessage()); } } else { - $helper->logError($e->getFriendlyMessage()); - $this->addError($isAdmin, $e); - $subscriber->unsubscribe(); + $errorMessage = $e->getFriendlyMessage(); + $helper->logError($errorMessage); + if ($isAdmin) { + $this->addError($errorMessage); + } else { + $errorMessage = $helper->__("The subscription could not be applied."); + $this->addError($errorMessage); + } + $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED); + $saveSubscriber = true; } } else { - $helper->logError($e->getFriendlyMessage()); - $this->addError($isAdmin, $e); + $errorMessage = $e->getFriendlyMessage(); + $helper->logError($errorMessage); + if ($isAdmin) { + $this->addError($errorMessage); + } else { + $errorMessage = $helper->__("The subscription could not be applied."); + $this->addError($errorMessage); + } + $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED); } } catch (Exception $e) { $helper->logError($e->getMessage()); } + if ($saveSubscriber) { + $subscriber->setSubscriberSource(Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE); + $subscriber->save(); + } } } @@ -525,13 +553,10 @@ protected function getAddressData($address) } /** - * @param $isAdmin - * @param $e + * @param $errorMessage */ - protected function addError($isAdmin, $e) + protected function addError($errorMessage) { - if ($isAdmin) { - Mage::getSingleton('core/session')->addError($e->getFriendlyMessage()); - } + Mage::getSingleton('core/session')->addError($errorMessage); } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Interestgroup.php b/app/code/community/Ebizmarts/MailChimp/Model/Interestgroup.php index d683988b4..ee07540cb 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Interestgroup.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Interestgroup.php @@ -24,9 +24,9 @@ public function _construct() $this->_init('mailchimp/interestgroup'); } - public function getBySubscriberIdStoreId($subscriberId, $storeId) + public function getByRelatedIdStoreId($customerId, $subscriberId, $storeId) { - $this->addData($this->getResource()->getBySubscriberIdStoreId($subscriberId, $storeId)); + $this->addData($this->getResource()->getByRelatedIdStoreId($customerId, $subscriberId, $storeId)); return $this; } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php index 301d50126..835f2babd 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php @@ -23,13 +23,13 @@ public function _construct() $this->_init('mailchimp/interestgroup', 'id'); } - public function getBySubscriberIdStoreId($subscriberId, $storeId) + public function getByRelatedIdStoreId($customerId = 0, $subscriberId = 0, $storeId) { $read = $this->_getReadAdapter(); $select = $read->select() ->from($this->getMainTable()) - ->where('subscriber_id = ?', $subscriberId) - ->where('store_id = ?', $storeId); + ->where('store_id = ?', $storeId) + ->where('(' . $read->quoteInto('customer_id = ?', $customerId) . ' OR ' . $read->quoteInto('subscriber_id = ?', $subscriberId) . ')'); $result = $read->fetchRow($select); diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index a9266f666..93f16f809 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -125,18 +125,20 @@ public function saveConfig(Varien_Event_Observer $observer) } /** - * Handle subscription change (subscribe/unsubscribe) + * Prevent Magento's subscription confirmation email to be sent if MailChimp For Magento is enabled. + * Override Magento's default status if confirmation email is enabled and the customer is logged in. * * @param Varien_Event_Observer $observer * @return Varien_Event_Observer * @throws Mage_Core_Model_Store_Exception */ - public function handleSubscriber(Varien_Event_Observer $observer) + public function SubscriberSaveBefore(Varien_Event_Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); + $storeId = $subscriber->getStoreId(); $helper = $this->makeHelper(); + if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { - $storeId = $subscriber->getStoreId(); $isEnabled = $helper->isSubscriptionEnabled($storeId); if ($isEnabled) { $statusChanged = $subscriber->getIsStatusChanged(); @@ -145,27 +147,39 @@ public function handleSubscriber(Varien_Event_Observer $observer) $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $this->addSuccessIfRequired($helper); } - $apiSubscriber = $this->makeApiSubscriber(); $subscriber->setImportMode(true); - $this->createEmailCookie($subscriber); - - if ($statusChanged) { - $apiSubscriber->updateSubscriber($subscriber, true); - } else { - $origData = $subscriber->getOrigData(); - - if (is_array($origData) && isset($origData['subscriber_status']) - && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() - ) { - $apiSubscriber->updateSubscriber($subscriber, true); - } - } } } return $observer; } + /** + * Hnalde Subscriber changes update. + * + * @param Varien_Event_Observer $observer + * @throws Mage_Core_Model_Store_Exception + */ + public function SubscriberSaveAfter(Varien_Event_Observer $observer) + { + $subscriber = $observer->getEvent()->getSubscriber(); + $storeId = $subscriber->getStoreId(); + $helper = $this->makeHelper(); + + if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { + $isEnabled = $helper->isSubscriptionEnabled($storeId); + if ($isEnabled) { + $params = $this->getRequest()->getParams(); + $helper->saveInterestGroupData($params, $storeId, null, $subscriber); + + $this->createEmailCookie($subscriber); + + $apiSubscriber = $this->makeApiSubscriber(); + $apiSubscriber->updateSubscriber($subscriber, true); + } + } + } + /** * Handle subscriber deletion from back end. * @@ -226,65 +240,44 @@ public function alterNewsletterGrid(Varien_Event_Observer $observer) * @param Varien_Event_Observer $observer * @return Varien_Event_Observer */ - public function customerSaveBefore(Varien_Event_Observer $observer) + public function customerSaveAfter(Varien_Event_Observer $observer) { $customer = $observer->getEvent()->getCustomer(); $origEmail = $customer->getOrigData('email'); $customerEmail = $customer->getEmail(); $storeId = $customer->getStoreId(); - - //@Todo if is admin - $params = $this->getRequest()->getParams(); - if (isset($params['customer']) && isset($params['customer']['interestgroup']) || isset($params['group'])) { - if (isset($params['group'])) { - $groups = $params['group']; - } else { - $groups = $params['customer']['interestgroup']; - } - $subscriberModel = $this->getSubscriberModel(); - $subscriber = $subscriberModel->loadByEmail($origEmail); - $subscriberId = $subscriber->getSubcriberId(); - if (!$subscriberId) { - $subscriber->setSubscriberEmail($customerEmail) - ->setFirstname($customer->getFristname()) - ->setLastname($customer->getLastname()) - ->setCustomerId($customer->getEntityId()) - ->setStoreId($storeId) - ->subscribe($customerEmail); - } - $interestGroup = Mage::getModel('mailchimp/interestgroup'); - $interestGroup->getBySubscriberIdStoreId($subscriberId, $storeId); - $interestGroup->setGroupdata(serialize($groups)); - $interestGroup->setSubscriberId($subscriber->getSubscriberId()); - $interestGroup->setStoreId($storeId); - $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); - $interestGroup->save(); + // if customer was created in admin, use store id selected for Mailchimp. + if (!$storeId) { + $storeId = $customer->getMailchimpStoreView(); } - $helper = $this->makeHelper(); $isEnabled = $helper->isSubscriptionEnabled($storeId); + $params = $this->getRequest()->getParams(); if ($isEnabled) { + $subscriberEmail = ($origEmail) ? $origEmail : $customerEmail; + $subscriber = $this->handleCustomerGroups($subscriberEmail, $params, $storeId, $customer->getId()); $apiSubscriber = $this->makeApiSubscriber(); if ($origEmail) { // check if customer has changed email address if ($origEmail != $customerEmail) { - $subscriberModel = $this->getSubscriberModel(); - $subscriber = $subscriberModel->loadByEmail($origEmail); if ($subscriber->getId()) { // unsubscribe old email address $apiSubscriber->deleteSubscriber($subscriber); // subscribe new email address + $subscriberModel = $this->getSubscriberModel(); $subscriber = $subscriberModel->loadByCustomer($customer); $subscriber->setSubscriberEmail($customerEmail); // make sure we set the new email address + $subscriber->save(); - $apiSubscriber->updateSubscriber($subscriber, true); } } } - //update subscriber data if a subscriber with the same email address exists - $apiSubscriber->update($customerEmail, $storeId); + //update subscriber data if a subscriber with the same email address exists and was not affected. + if (!$origEmail || $origEmail == $customerEmail) { + $apiSubscriber->update($customerEmail, $storeId); + } if ($helper->isEcomSyncDataEnabled($storeId)) { //update mailchimp ecommerce data for that customer @@ -962,7 +955,12 @@ public function addCustomerTab(Varien_Event_Observer $observer) if ($block instanceof Mage_Adminhtml_Block_Customer_Edit_Tabs) { $customerId = (int) $this->getRequest()->getParam('id'); $customer = Mage::getModel('customer/customer')->load($customerId); - if ($helper->getLocalInterestCategories($customer->getStoreId()) && ($this->getRequest()->getActionName() == 'edit' || $this->getRequest()->getParam('type'))) { + $storeId = $customer->getStoreId(); + //If the customer was created in the admin panel use the store view selected for MailChimp. + if (!$storeId) { + $storeId = $customer->getMailchimpStoreView(); + } + if ($helper->getLocalInterestCategories($storeId) && ($this->getRequest()->getActionName() == 'edit' || $this->getRequest()->getParam('type'))) { $block->addTab('mailchimp', array( 'label' => $helper->__('MailChimp'), 'url' => $block->getUrl('adminhtml/mailchimp/index', array('_current' => true)), @@ -977,4 +975,37 @@ protected function getRequest() { return Mage::app()->getRequest(); } + + /** + * Handle admin customer groups and front end only when the customer is not subscribed. + * + * @param $subscriberEmail + * @param $params + * @param $storeId + * @param null $customerId + * @return Mage_Newsletter_Model_Subscriber + * @throws Mage_Core_Model_Store_Exception + */ + protected function handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId = null) + { + $helper = $this->makeHelper(); + $subscriberModel = $this->getSubscriberModel(); + $subscriber = $subscriberModel->loadByEmail($subscriberEmail); + if ($subscriber->getId()) { + $helper->saveInterestGroupData($params, $storeId, $customerId, $subscriber); + } elseif (isset($params['customer_id'])) { + $groups = $helper->getInterestGroupsIfAvailable($params); + if ($groups) { + $helper->saveInterestGroupData($params, $storeId, $customerId); + Mage::getSingleton('adminhtml/session')->addWarning($helper->__('The customer must be subscribed for this change to apply.')); + } + } else { + if (!$helper->isAdmin()) { + //save frontend groupdata when customer is not subscribed. + $helper->saveInterestGroupData($params, $storeId, $customerId); + } + Mage::getSingleton('adminhtml/session')->addError($helper->__('Something went wrong when trying to save the interest group data. The customer must be subscribed for this change to apply.')); + } + return $subscriber; + } } diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php index 68e46ea5e..1a2a6c31b 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php @@ -24,6 +24,7 @@ public function indexAction() $interestGroup = Mage::getModel('mailchimp/interestgroup'); $subscriber = Mage::getModel('newsletter/subscriber') ->loadByEmail($order->getCustomerEmail()); + $customerId = $order->getCustomerId(); try { if (!$subscriber->getSubscriberId()) { $subscriber->setSubscriberEmail($order->getCustomerEmail()); @@ -31,9 +32,10 @@ public function indexAction() $subscriber->setSubscriberLastname($order->getCustomerLastname()); $subscriber->subscribe($order->getCustomerEmail()); } - $interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(),$storeId); + $interestGroup->getByRelatedIdStoreId($customerId, $subscriber->getSubscriberId(),$storeId); $interestGroup->setGroupdata(serialize($params)); $interestGroup->setSubscriberId($subscriber->getSubscriberId()); + $interestGroup->setCustomerId($order->getCustomerId()); $interestGroup->setStoreId($storeId); $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); $interestGroup->save(); diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index 6206f27ba..272295f2f 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -31,7 +31,7 @@ mailchimp/observer - handleSubscriber + SubscriberSaveBefore @@ -51,15 +51,15 @@
- + - + model mailchimp/observer - customerSaveBefore - + customerSaveAfter + - + diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12-1.1.13.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12-1.1.12.2.php similarity index 62% rename from app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12-1.1.13.php rename to app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12-1.1.12.2.php index df1a27201..e5c9c910d 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12-1.1.13.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12-1.1.12.2.php @@ -36,28 +36,31 @@ )); -$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "mailchimp_store_view"); +try { + $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "mailchimp_store_view"); -$setup->addAttributeToGroup( - $entityTypeId, - $attributeSetId, - $attributeGroupId, - 'mailchimp_store_view', - '999' //sort_order -); + $setup->addAttributeToGroup( + $entityTypeId, + $attributeSetId, + $attributeGroupId, + 'mailchimp_store_view', + '999' //sort_order + ); -$used_in_forms=array(); + $used_in_forms = array(); -$used_in_forms[]="adminhtml_customer"; + $used_in_forms[] = "adminhtml_customer"; -$attribute->setData("used_in_forms", $used_in_forms) - ->setData("is_used_for_customer_segment", true) - ->setData("is_system", 0) - ->setData("is_user_defined", 1) - ->setData("is_visible", 1) - ->setData("sort_order", 100) -; -$attribute->save(); + $attribute->setData("used_in_forms", $used_in_forms) + ->setData("is_used_for_customer_segment", true) + ->setData("is_system", 0) + ->setData("is_user_defined", 1) + ->setData("is_visible", 1) + ->setData("sort_order", 100); + $attribute->save(); +} catch (Exception $e) { + Mage::log($e->getMessage(), null, 'MailChimp_Errors.log', true); +} $installer->endSetup(); diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.11-1.1.12.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php similarity index 73% rename from app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.11-1.1.12.php rename to app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php index 491729d7c..d34f75601 100755 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.11-1.1.12.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php @@ -6,8 +6,9 @@ " CREATE TABLE IF NOT EXISTS `{$this->getTable('mailchimp_interest_group')}` ( `id` INT(10) unsigned NOT NULL auto_increment, - `subscriber_id` INT(10) DEFAULT 0, - `store_id` SMALLINT (5) NOT NULL, + `customer_id` INT(10) DEFAULT NULL, + `subscriber_id` INT(10) DEFAULT NULL, + `store_id` SMALLINT (5) NOT NULL DEFAULT 0, `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP, `groupdata` TEXT(4096) NOT NULL, PRIMARY KEY (`id`) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index ae926976e..7378efd76 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -649,7 +649,7 @@ public function testAddColumnToSalesOrderGridCollection() $observerMock->addColumnToSalesOrderGridCollection($eventObserverMock); } - public function testHandleSubscriber() + public function testSubscriberSaveBefore() { $storeId = 1; @@ -713,6 +713,6 @@ public function testHandleSubscriber() $apiSubscriberMock->expects($this->once())->method('updateSubscriber')->with($subscriberMock, true); - $observerMock->handleSubscriber($eventObserverMock); + $observerMock->subscriberSaveBefore($eventObserverMock); } } From 2e2e1139bb8ed527a6f33e624dd199e9bf48af18 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 27 Jul 2018 10:46:23 -0300 Subject: [PATCH 012/113] Fix tests. --- .../Ebizmarts/MailChimp/Model/Observer.php | 7 +- .../Adminhtml/MailchimpController.php | 22 ++- .../MailChimp/Model/ObserverTest.php | 131 +++++++++++++----- .../Adminhtml/MailchimpControllerTest.php | 5 +- 4 files changed, 122 insertions(+), 43 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 93f16f809..6745f654d 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -255,8 +255,9 @@ public function customerSaveAfter(Varien_Event_Observer $observer) $params = $this->getRequest()->getParams(); if ($isEnabled) { + $customerId = $customer->getId(); $subscriberEmail = ($origEmail) ? $origEmail : $customerEmail; - $subscriber = $this->handleCustomerGroups($subscriberEmail, $params, $storeId, $customer->getId()); + $subscriber = $this->handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId); $apiSubscriber = $this->makeApiSubscriber(); if ($origEmail) { // check if customer has changed email address @@ -281,7 +282,7 @@ public function customerSaveAfter(Varien_Event_Observer $observer) if ($helper->isEcomSyncDataEnabled($storeId)) { //update mailchimp ecommerce data for that customer - $this->makeApiCustomer()->update($customer->getId(), $storeId); + $this->makeApiCustomer()->update($customerId, $storeId); } } @@ -977,7 +978,7 @@ protected function getRequest() } /** - * Handle admin customer groups and front end only when the customer is not subscribed. + * Handle frontend customer interest groups only if is not subscribed and all admin customer groups. * * @param $subscriberEmail * @param $params diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 3fc14d898..a3495edb6 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -16,13 +16,12 @@ public function indexAction() { $customerId = (int) $this->getRequest()->getParam('id'); if ($customerId) { - $this->getResponse()->setBody( - $this->getLayout() - ->createBlock('mailchimp/adminhtml_customer_edit_tab_mailchimp', 'admin.customer.mailchimp') - ->setCustomerId($customerId) - ->setUseAjax(true) - ->toHtml() - ); + $block = $this->getLayout() + ->createBlock('mailchimp/adminhtml_customer_edit_tab_mailchimp', 'admin.customer.mailchimp') + ->setCustomerId($customerId) + ->setUseAjax(true); + $html = $this->getHtml($block); + $this->getResponse()->setBody($html); } } @@ -63,4 +62,13 @@ protected function makeHelper() { return Mage::helper('mailchimp'); } + + /** + * @param $block + * @return mixed + */ + protected function getHtml($block) + { + return $block->toHtml(); + } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 7378efd76..5e10b873b 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -291,17 +291,20 @@ public function testHandleSubscriberDeletion() $observerMock->handleSubscriberDeletion($eventObserverMock); } - public function testCustomerSaveBefore() + public function testCustomerSaveAfter() { + $adminStoreId = 0; $storeId = 1; $oldEmailAddress = 'oldEmail@example.com'; $newEmailAddress = 'newEmail@example.com'; + $subscriberEmail = ($oldEmailAddress) ? $oldEmailAddress : $newEmailAddress; $subscriberId = 1; $customerId = 1; + $params = array(); $customerMock = $this->getMockBuilder(Mage_Customer_Model_Customer::class) ->disableOriginalConstructor() - ->setMethods(array('getId', 'getOrigData', 'getEmail', 'getStoreId')) + ->setMethods(array('getId', 'getOrigData', 'getEmail', 'getStoreId', 'getMailchimpStoreView')) ->getMock(); $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) @@ -314,9 +317,14 @@ public function testCustomerSaveBefore() ->setMethods(array('getCustomer')) ->getMock(); + $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParams')) + ->getMock(); + $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) ->disableOriginalConstructor() - ->setMethods(array('makeHelper', 'makeApiSubscriber', 'getSubscriberModel', 'makeApiCustomer')) + ->setMethods(array('makeHelper', 'makeApiSubscriber', 'getSubscriberModel', 'makeApiCustomer', 'getRequest', 'handleCustomerGroups')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) @@ -331,7 +339,12 @@ public function testCustomerSaveBefore() $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() - ->setMethods(array('loadByEmail', 'loadByCustomer', 'setSubscriberEmail', 'getId')) + ->setMethods(array('getId')) + ->getMock(); + + $subscriberMockTwo = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByCustomer', 'setSubscriberEmail', 'save')) ->getMock(); $apiCustomerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Customers::class) @@ -343,39 +356,41 @@ public function testCustomerSaveBefore() $eventMock->expects($this->once())->method('getCustomer')->willReturn($customerMock); - $customerMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + $customerMock->expects($this->once())->method('getOrigData')->with('email')->willReturn($oldEmailAddress); + $customerMock->expects($this->once())->method('getEmail')->willReturn($newEmailAddress); + $customerMock->expects($this->once())->method('getStoreId')->willReturn($adminStoreId); + $customerMock->expects($this->once())->method('getMailchimpStoreView')->willReturn($storeId); $observerMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); - $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); + $observerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); - $customerMock->expects($this->once())->method('getOrigData')->with('email')->willReturn($oldEmailAddress); - $customerMock->expects($this->once())->method('getEmail')->willReturn($newEmailAddress); + $requestMock->expects($this->once())->method('getParams')->willReturn($params); - $observerMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); + $customerMock->expects($this->once())->method('getId')->willReturn($customerId); + + $observerMock->expects($this->once())->method('handleCustomerGroups')->with($subscriberEmail, $params, $storeId, $customerId)->willReturn($subscriberMock); + $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); - $subscriberMock->expects($this->once())->method('loadByEmail')->with($oldEmailAddress)->willReturnSelf(); $subscriberMock->expects($this->once())->method('getId')->willReturn($subscriberId); $apiSubscriberMock->expects($this->once())->method('deleteSubscriber')->with($subscriberMock); - $subscriberMock->expects($this->once())->method('loadByCustomer')->with($customerMock)->willReturnSelf(); - $subscriberMock->expects($this->once())->method('setSubscriberEmail')->with($newEmailAddress); + $observerMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMockTwo); - $apiSubscriberMock->expects($this->once())->method('updateSubscriber')->with($subscriberMock, true); - $apiSubscriberMock->expects($this->once())->method('update')->with($newEmailAddress, $storeId); + $subscriberMockTwo->expects($this->once())->method('loadByCustomer')->with($customerMock)->willReturnSelf(); + $subscriberMockTwo->expects($this->once())->method('setSubscriberEmail')->with($newEmailAddress); + $subscriberMockTwo->expects($this->once())->method('save')->willReturnSelf(); $helperMock->expects($this->once())->method('isEcomSyncDataEnabled')->with($storeId)->willReturn(true); $observerMock->expects($this->once())->method('makeApiCustomer')->willReturn($apiCustomerMock); - $customerMock->expects($this->once())->method('getId')->willReturn($customerId); - $apiCustomerMock->expects($this->once())->method('update')->with($customerId, $storeId); - $observerMock->customerSaveBefore($eventObserverMock); + $observerMock->customerSaveAfter($eventObserverMock); } public function testCustomerAddressSaveBefore() @@ -660,14 +675,12 @@ public function testSubscriberSaveBefore() $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) ->disableOriginalConstructor() - ->setMethods(array('makeHelper', 'getCoreResource', 'getRegistry', 'removeRegistry', 'createEmailCookie', - 'makeApiSubscriber', 'addSuccessIfRequired')) + ->setMethods(array('makeHelper', 'addSuccessIfRequired')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('isSubscriptionEnabled', 'isEcomSyncDataEnabledInAnyScope', - 'isSubscriptionConfirmationEnabled')) + ->setMethods(array('isSubscriptionEnabled', 'isSubscriptionConfirmationEnabled')) ->getMock(); $eventMock = $this->getMockBuilder(Varien_Event::class) @@ -677,13 +690,8 @@ public function testSubscriberSaveBefore() $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() - ->setMethods(array('getSubscriberSource', 'getStoreId', 'getIsStatusChanged', 'getStatus', 'setStatus', - 'setImportMode', 'getOrigData', 'getSubscriberStatus')) - ->getMock(); - - $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) - ->disableOriginalConstructor() - ->setMethods(array('updateSubscriber')) + ->setMethods(array('getSubscriberSource', 'getIsStatusChanged', 'getStatus', 'setStatus', 'setImportMode', + 'getStoreId')) ->getMock(); $eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock); @@ -692,8 +700,8 @@ public function testSubscriberSaveBefore() $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); - $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); $subscriberMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); @@ -705,14 +713,75 @@ public function testSubscriberSaveBefore() $subscriberMock->expects($this->once())->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $observerMock->expects($this->once())->method('addSuccessIfRequired')->with($helperMock); - $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); $subscriberMock->expects($this->once())->method('setImportMode')->with(true); + $observerMock->subscriberSaveBefore($eventObserverMock); + } + + public function testSubscriberSaveAfter() + { + $storeId = 1; + $params = array(); + + $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getEvent')) + ->getMock(); + + $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('makeHelper', 'getRequest', 'createEmailCookie', 'makeApiSubscriber')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData')) + ->getMock(); + + $eventMock = $this->getMockBuilder(Varien_Event::class) + ->disableOriginalConstructor() + ->setMethods(array('getSubscriber')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('getSubscriberSource', 'getStoreId')) + ->getMock(); + + $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParams')) + ->getMock(); + + $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) + ->disableOriginalConstructor() + ->setMethods(array('updateSubscriber')) + ->getMock(); + + $eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock); + + $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); + + $subscriberMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $observerMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); + + $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); + + $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); + + $observerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); + + $requestMock->expects($this->once())->method('getParams')->willReturn($params); + + $helperMock->expects($this->once())->method('saveInterestGroupData')->with($params, $storeId, null, $subscriberMock); + $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); + $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); $apiSubscriberMock->expects($this->once())->method('updateSubscriber')->with($subscriberMock, true); - $observerMock->subscriberSaveBefore($eventObserverMock); + $observerMock->subscriberSaveAfter($eventObserverMock); } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpControllerTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpControllerTest.php index df7416d0b..d55911bb0 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpControllerTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpControllerTest.php @@ -29,7 +29,7 @@ public function testIndexAction() $mailchimpControllerMock = $this->mailchimpController ->disableOriginalConstructor() - ->setMethods(array('getRequest', 'getResponse', 'getLayout')) + ->setMethods(array('getRequest', 'getResponse', 'getLayout', 'getHtml')) ->getMock(); $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) @@ -62,7 +62,8 @@ public function testIndexAction() $blockMock->expects($this->once())->method('setCustomerId')->with($customerId)->willReturnSelf(); $blockMock->expects($this->once())->method('setUseAjax')->with(true)->willReturnSelf(); - $blockMock->expects($this->once())->method('toHtml')->willReturn($result); + + $mailchimpControllerMock->expects($this->once())->method('getHtml')->with($blockMock)->willReturn($result); $mailchimpControllerMock->expects($this->once())->method('getResponse')->willReturn($responseMock); $responseMock->expects($this->once())->method('setBody')->with($result); From 6a271a33b3d26495280e5bce47a7b3234542e8a1 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 27 Jul 2018 11:09:26 -0300 Subject: [PATCH 013/113] Declare array correctly. --- app/code/community/Ebizmarts/MailChimp/Helper/Data.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 8d8c1dc50..6ff3d363d 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3202,12 +3202,12 @@ public function getCheckoutSuccessHtmlAfter($scopeId, $scope = 'stores') */ public function getInterest($storeId) { - $rc = []; + $rc = array(); $interest = $this->getLocalInterestCategories($storeId); if ($interest != '') { $interest = explode(",", $interest); } else { - $interest = []; + $interest = array(); } $api = $this->getApi($storeId); $listId = $this->getGeneralList($storeId); From cb059cc858f8cb4aaa43f5f2b45864a55f61eab8 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 27 Jul 2018 11:26:37 -0300 Subject: [PATCH 014/113] Declare array correctly. --- app/code/community/Ebizmarts/MailChimp/Helper/Data.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 6ff3d363d..deedfbee7 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3214,13 +3214,13 @@ public function getInterest($storeId) $allInterest = $api->lists->interestCategory->getAll($listId); foreach ($allInterest['categories'] as $item) { if (in_array($item['id'], $interest)) { - $rc[$item['id']]['interest'] = ['id' => $item['id'], 'title' => $item['title'], 'type' => $item['type']]; + $rc[$item['id']]['interest'] = array('id' => $item['id'], 'title' => $item['title'], 'type' => $item['type']); } } foreach ($interest as $interestId) { $mailchimpInterest = $api->lists->interestCategory->interests->getAll($listId, $interestId); foreach ($mailchimpInterest['interests'] as $mi) { - $rc[$mi['category_id']]['category'][$mi['display_order']] = ['id' => $mi['id'], 'name' => $mi['name'], 'checked' => false]; + $rc[$mi['category_id']]['category'][$mi['display_order']] = array('id' => $mi['id'], 'name' => $mi['name'], 'checked' => false); } } return $rc; From e038e6d6f93de36feb9f384bf0ecb016c92b9442 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 27 Jul 2018 11:38:28 -0300 Subject: [PATCH 015/113] Add missing new lines. --- .../Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php | 2 +- .../MailChimp/Model/Mysql4/Interestgroup/Collection.php | 2 +- .../MailChimp/Model/System/Config/Source/CustomerGroup.php | 2 +- .../Ebizmarts/MailChimp/controllers/GroupController.php | 2 +- .../sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php | 2 +- .../template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml | 2 +- .../template/ebizmarts/mailchimp/checkout/success/groups.phtml | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php index 835f2babd..746001e44 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup.php @@ -39,4 +39,4 @@ public function getByRelatedIdStoreId($customerId = 0, $subscriberId = 0, $store return $result; } -} \ No newline at end of file +} diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup/Collection.php b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup/Collection.php index a008428dd..2352d3cb4 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup/Collection.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Mysql4/Interestgroup/Collection.php @@ -24,4 +24,4 @@ public function _construct() parent::_construct(); $this->_init('mailchimp/interestgroup'); } -} \ No newline at end of file +} diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php index 25ca7aa53..3755e1603 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php @@ -48,4 +48,4 @@ protected function makeHelper() return Mage::helper('mailchimp'); } -} \ No newline at end of file +} diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php index 1a2a6c31b..5cf95fac3 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php @@ -59,4 +59,4 @@ protected function getApiSubscriber() { return Mage::getModel('mailchimp/api_subscribers'); } -} \ No newline at end of file +} diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php index d34f75601..33e0f64f3 100755 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php @@ -16,4 +16,4 @@ " ); -$installer->endSetup(); \ No newline at end of file +$installer->endSetup(); diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml index 5507a99a7..71826ddf3 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml @@ -73,4 +73,4 @@ $interest = $this->getInterest(); - \ No newline at end of file + diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml index d2946e8c4..c7608ec5e 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml @@ -79,4 +79,4 @@ $interest = $block->getInterest(); getMessageAfter() ?> -

\ No newline at end of file +

From 3f24e7e10920a745b7b996105ef001a62bc03e2b Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 31 Jul 2018 11:44:45 -0300 Subject: [PATCH 016/113] Add tests. --- .../Block/Checkout/Success/Groups.php | 41 ++- .../Block/Customer/Newsletter/Index.php | 23 +- .../Ebizmarts/MailChimp/Helper/Data.php | 50 +++- ...php => mysql4-upgrade-1.1.12.3-1.1.13.php} | 0 .../Block/Checkout/Success/GroupsTest.php | 101 +++++++ .../Block/Customer/Newsletter/IndexTest.php | 92 ++++++ .../Ebizmarts/MailChimp/Helper/DataTest.php | 265 +++++++++++++++++- lib/Ebizmarts/MailChimp.php | 8 + lib/Ebizmarts/MailChimp/Lists.php | 8 + .../MailChimp/ListsInterestCategory.php | 8 + 10 files changed, 574 insertions(+), 22 deletions(-) rename app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/{mysql4-upgrade-1.1.12.2-1.1.13.php => mysql4-upgrade-1.1.12.3-1.1.13.php} (100%) create mode 100644 dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Checkout/Success/GroupsTest.php create mode 100644 dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Customer/Newsletter/IndexTest.php diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index fc86af530..2e5d9dda1 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -38,7 +38,7 @@ public function __construct() public function getGeneralList() { $storeId = $this->storeId; - $helper = $this->helper; + $helper = $this->getMailChimpHelper(); $listId = $helper->getGeneralList($storeId); return $listId; @@ -47,7 +47,7 @@ public function getGeneralList() public function getListInterestGroups() { $storeId = $this->storeId; - $helper = $this->helper; + $helper = $this->getMailChimpHelper(); $return = $helper->getListInterestGroups($storeId); return $return; } @@ -91,7 +91,7 @@ public function getGroupClass($type) public function htmlGroupName($category) { $storeId = $this->storeId; - $helper = $this->helper; + $helper = $this->getMailChimpHelper(); $listId = $helper->getGeneralList($storeId); $htmlName = "list[{$listId}]"; $htmlName .= "[{$category['id']}]"; @@ -209,26 +209,51 @@ public function getSuccessInterestUrl() public function getInterest() { - $subscriber = Mage::getModel('newsletter/subscriber'); - $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); + $subscriber = $this->getSubscriberModel(); + $order = $this->getSessionLastRealOrder(); $subscriber->loadByEmail($order->getCustomerEmail()); $subscriberId = $subscriber->getSubscriberId(); $customerId = $order->getCustomerId(); - $interest = $this->helper->getInterestGroups($customerId, $subscriberId,$order->getStoreId()); + $helper = $this->getMailChimpHelper(); + $interest = $helper->getInterestGroups($customerId, $subscriberId,$order->getStoreId()); return $interest; } public function getMessageBefore() { $storeId = $this->storeId; - return $this->helper->getCheckoutSuccessHtmlBefore($storeId); + return $this->getMailChimpHelper()->getCheckoutSuccessHtmlBefore($storeId); } public function getMessageAfter() { $storeId = $this->storeId; - return $this->helper->getCheckoutSuccessHtmlAfter($storeId); + return $this->getMailChimpHelper()->getCheckoutSuccessHtmlAfter($storeId); + } + + /** + * @return false|Mage_Core_Model_Abstract + */ + protected function getSubscriberModel() + { + return Mage::getModel('newsletter/subscriber'); + } + + /** + * @return mixed + */ + protected function getSessionLastRealOrder() + { + return Mage::getSingleton('checkout/session')->getLastRealOrder(); + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data|Mage_Core_Helper_Abstract + */ + protected function getMailChimpHelper() + { + return $this->helper; } } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php index 8ffa7ab80..163a4714a 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php @@ -33,11 +33,12 @@ public function __construct() public function getInterest() { - $subscriber = Mage::getModel('newsletter/subscriber'); + $subscriber = $this->getSubscriberModel(); $subscriber->loadByEmail($this->_getEmail()); $helper = $this->getMailChimpHelper(); - if (!$helper->isAdmin() && Mage::getSingleton('customer/session')->isLoggedIn()) { - $customer = Mage::getSingleton('customer/session')->getCustomer(); + $customerSession = $this->getCustomerSession(); + if (!$helper->isAdmin() && $customerSession->isLoggedIn()) { + $customer = $customerSession->getCustomer(); $customerId = $customer->getId(); $storeId = ($subscriber->getStoreId()) ? $subscriber->getStoreId() : $customer->getStoreId(); } else { @@ -66,4 +67,20 @@ protected function getMailChimpHelper() return $this->helper; } + /** + * @return Mage_Customer_Model_Session + */ + protected function getCustomerSession() + { + return Mage::getSingleton('customer/session'); + } + + /** + * @return Mage_Newsletter_Model_Subscriber + */ + protected function getSubscriberModel() + { + return Mage::getModel('newsletter/subscriber'); + } + } diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index deedfbee7..2bda5a51a 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3140,7 +3140,7 @@ public function getListInterestCategories($scopeId, $scope = 'stores') $api = $this->getApi($scopeId, $scope); $listId = $this->getGeneralList($scopeId, $scope); try { - $interestCategories = $api->lists->interestCategory->getAll($listId, 'categories'); + $interestCategories = $api->getLists()->getInterestCategory()->getAll($listId, 'categories'); foreach ($interestCategories['categories'] as $interestCategory) { $interestGroupsArray[] = array( 'id' => $interestCategory['id'], @@ -3160,9 +3160,10 @@ public function getListInterestGroups($scopeId, $scope = 'stores') $api = $this->getApi($scopeId, $scope); $listId = $this->getGeneralList($scopeId, $scope); try { - $interestCategories = $api->lists->interestCategory->getAll($listId, 'categories'); + $apiInterestCategory = $api->getLists()->getInterestCategory(); + $interestCategories = $apiInterestCategory->getAll($listId, 'categories'); foreach ($interestCategories['categories'] as $interestCategory) { - $interestGroups = $api->lists->interestCategory->interests->getAll($listId, $interestCategory['id']); + $interestGroups = $apiInterestCategory->getInterests()->getAll($listId, $interestCategory['id']); $groups = array(); foreach ($interestGroups['interests'] as $interestGroup) { $groups[$interestGroup['id']] = $interestGroup['name']; @@ -3206,23 +3207,27 @@ public function getInterest($storeId) $interest = $this->getLocalInterestCategories($storeId); if ($interest != '') { $interest = explode(",", $interest); + } else { $interest = array(); } $api = $this->getApi($storeId); $listId = $this->getGeneralList($storeId); - $allInterest = $api->lists->interestCategory->getAll($listId); + $apiInterestCategory = $api->getLists()->getInterestCategory(); + $allInterest = $apiInterestCategory->getAll($listId); foreach ($allInterest['categories'] as $item) { if (in_array($item['id'], $interest)) { $rc[$item['id']]['interest'] = array('id' => $item['id'], 'title' => $item['title'], 'type' => $item['type']); } } + $apiInterestCategoryInterest = $apiInterestCategory->getInterests(); foreach ($interest as $interestId) { - $mailchimpInterest = $api->lists->interestCategory->interests->getAll($listId, $interestId); + $mailchimpInterest = $apiInterestCategoryInterest->getAll($listId, $interestId); foreach ($mailchimpInterest['interests'] as $mi) { $rc[$mi['category_id']]['category'][$mi['display_order']] = array('id' => $mi['id'], 'name' => $mi['name'], 'checked' => false); } } + return $rc; } @@ -3239,7 +3244,7 @@ public function getInterestGroups($customerId, $subscriberId, $storeId, $interes if (!$interest) { $interest = $this->getInterest($storeId); } - $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $interestGroup = $this->getInterestGroupModel(); $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); if ($interestGroup->getId()) { $groups = unserialize($interestGroup->getGroupdata()); @@ -3281,10 +3286,11 @@ public function saveInterestGroupData($params, $storeId, $customerId = null, $su { $groups = $this->getInterestGroupsIfAvailable($params); if (!$customerId) { + $customerSession = $this->getCustomerSession(); if ($this->isAdmin()) { $customerId = $params['customer_id']; - } elseif (Mage::getSingleton('customer/session')->isLoggedIn()) { - $customerData = Mage::getSingleton('customer/session')->getCustomer(); + } elseif ($customerSession->isLoggedIn()) { + $customerData = $customerSession->getCustomer(); $customerId = $customerData->getId(); } } @@ -3292,7 +3298,7 @@ public function saveInterestGroupData($params, $storeId, $customerId = null, $su if ($subscriber) { $subscriberId = $subscriber->getSubscriberId(); } - $interestGroup = Mage::getModel('mailchimp/interestgroup'); + $interestGroup = $this->getInterestGroupModel(); $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); $origSubscriberId = $interestGroup->getSubscriberId(); $origCustomerId = $interestGroup->getCustomerId(); @@ -3310,7 +3316,7 @@ public function saveInterestGroupData($params, $storeId, $customerId = null, $su if ($storeId) { $interestGroup->setStoreId($storeId); } - $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); + $interestGroup->setUpdatedAt($this->getCurrentDateTime()); $interestGroup->save(); } } @@ -3338,4 +3344,28 @@ public function isAdmin() { return Mage::app()->getStore()->isAdmin(); } + + /** + * @return Ebizmarts_MailChimp_Model_Interestgroup + */ + protected function getInterestGroupModel() + { + return Mage::getModel('mailchimp/interestgroup'); + } + + /** + * @return Mage_Customer_Model_Session + */ + protected function getCustomerSession() + { + return Mage::getSingleton('customer/session'); + } + + /** + * @return mixed + */ + protected function getCurrentDateTime() + { + return Mage::getModel('core/date')->date('d-m-Y H:i:s'); + } } diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.3-1.1.13.php similarity index 100% rename from app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.2-1.1.13.php rename to app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.3-1.1.13.php diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Checkout/Success/GroupsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Checkout/Success/GroupsTest.php new file mode 100644 index 000000000..a59a91e34 --- /dev/null +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Checkout/Success/GroupsTest.php @@ -0,0 +1,101 @@ +getLayout(); + $this->_block = new Ebizmarts_MailChimp_Block_Checkout_Success_Groups; + $this->_groupsMock = $this->getMockBuilder(Ebizmarts_MailChimp_Block_Checkout_Success_Groups::class); + + /* We are required to set layouts before we can do anything with blocks */ + $this->_block->setLayout($layout); + } + + public function testRenderGroups() + { + $category = array(); + + $groupsMock = $this->_groupsMock + ->disableOriginalConstructor() + ->setMethods(array('createObject', 'addGroupOptions', 'getElementHtml')) + ->getMock(); + + $objectMock = $this->getMockBuilder(Varien_Data_Form_Element_Checkboxes::class) + ->disableOriginalConstructor() + ->getMock(); + + $groupsMock->expects($this->once())->method('createObject')->with($category)->willReturn($objectMock); + $groupsMock->expects($this->once())->method('addGroupOptions')->with($category, $objectMock); + $groupsMock->expects($this->once())->method('getElementHtml')->with($category, $objectMock); + + $groupsMock->renderGroups($category); + } + + public function testGetInterest() + { + $interest = array(); + $customerEmail = 'customer@email.com'; + $subscriberId = 1; + $customerId = 2; + $storeId = 1; + + $groupsMock = $this->_groupsMock + ->disableOriginalConstructor() + ->setMethods(array('getSubscriberModel', 'getSessionLastRealOrder', 'getElementHtml', 'getMailChimpHelper')) + ->getMock(); + + $orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) + ->disableOriginalConstructor() + ->setMethods(array('getCustomerEmail', 'getCustomerId', 'getStoreId')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByEmail', 'getSubscriberId')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getInterestGroups')) + ->getMock(); + + + $groupsMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); + $groupsMock->expects($this->once())->method('getSessionLastRealOrder')->willReturn($orderMock); + + $orderMock->expects($this->once())->method('getCustomerEmail')->willReturn($customerEmail); + + $subscriberMock->expects($this->once())->method('loadByEmail')->with($customerEmail); + $subscriberMock->expects($this->once())->method('getSubscriberId')->willReturn($subscriberId); + + $orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); + $orderMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $groupsMock->expects($this->once())->method('getMailChimpHelper')->willReturn($helperMock); + + $orderMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $helperMock->expects($this->once())->method('getInterestGroups')->with($customerId, $subscriberId, $storeId)->willReturn($interest); + + $groupsMock->getInterest(); + } + +} diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Customer/Newsletter/IndexTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Customer/Newsletter/IndexTest.php new file mode 100644 index 000000000..ffb92a9c3 --- /dev/null +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Customer/Newsletter/IndexTest.php @@ -0,0 +1,92 @@ +getLayout(); + $this->_block = new Ebizmarts_MailChimp_Block_Customer_Newsletter_Index; + $this->_indexMock = $this->getMockBuilder(Ebizmarts_MailChimp_Block_Customer_Newsletter_Index::class); + + /* We are required to set layouts before we can do anything with blocks */ + $this->_block->setLayout($layout); + } + + public function testGetInterest() + { + $interest = array(); + $emailAddress = 'address@email.com'; + $subscriberId = 1; + $customerId = 2; + $storeId = 1; + + $indexMock = $this->_indexMock + ->disableOriginalConstructor() + ->setMethods(array('getSubscriberModel', '_getEmail', 'getMailChimpHelper', 'getCustomerSession')) + ->getMock(); + + + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByEmail', 'getSubscriberId', 'getStoreId')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('isAdmin', 'getInterestGroups')) + ->getMock(); + + $customerSessionMock = $this->getMockBuilder(Mage_Customer_Model_Session::class) + ->disableOriginalConstructor() + ->setMethods(array('isLoggedIn', 'getCustomer')) + ->getMock(); + + $customerMock = $this->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('getId', 'getStoreId')) + ->getMock(); + + $indexMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); + $indexMock->expects($this->once())->method('_getEmail')->willReturn($emailAddress); + + $subscriberMock->expects($this->once())->method('loadByEmail')->with($emailAddress); + + $indexMock->expects($this->once())->method('getMailChimpHelper')->willReturn($helperMock); + $indexMock->expects($this->once())->method('getCustomerSession')->willReturn($customerSessionMock); + + $helperMock->expects($this->once())->method('isAdmin')->willReturn(false); + + $customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true); + $customerSessionMock->expects($this->once())->method('getCustomer')->willReturn($customerMock); + + $customerMock->expects($this->once())->method('getId')->willReturn($customerId); + + $subscriberMock->expects($this->once())->method('getStoreId')->willReturn(null); + + $customerMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $subscriberMock->expects($this->once())->method('getSubscriberId')->willReturn($subscriberId); + + $helperMock->expects($this->once())->method('getInterestGroups')->with($customerId, $subscriberId, $storeId)->willReturn($interest); + + $indexMock->getInterest(); + } + +} diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php index 3e3ce770f..5d10ae596 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -951,7 +951,7 @@ public function testSaveLastItemsSent() array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_PCD_LAST_ID, $promoCodeLastId), array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_RESEND_ENABLED, 1), array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_RESEND_TURN, 1) - ); + ); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() @@ -969,4 +969,267 @@ public function testSaveLastItemsSent() $helperMock->saveLastItemsSent($scopeId, $scope); } + + public function testGetListInterestCategories() + { + $scopeId = 1; + $scope = 'stores'; + $listId = 'a1s2d3f4g5'; + $interestCategoryId = 1; + $categoriesResponse = array('categories' => array(array('id' => $interestCategoryId, 'title' => 'Category Title', 'type' => 'checkbox'))); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getApi', 'getGeneralList')) + ->getMock(); + + $apiMock = $this->getMockBuilder(Ebizmarts_MailChimp::class) + ->disableOriginalConstructor() + ->setMethods(array('getLists')) + ->getMock(); + + $apiListsMock = $this->getMockBuilder(Ebizmarts_MailChimp::class) + ->disableOriginalConstructor() + ->setMethods(array('getInterestCategory')) + ->getMock(); + + $apiListsInterestCategoryMock = $this->getMockBuilder(MailChimp_ListsInterestCategory::class) + ->disableOriginalConstructor() + ->setMethods(array('getAll')) + ->getMock(); + + $helperMock->expects($this->once())->method('getApi')->with($scopeId, $scope)->willReturn($apiMock); + $helperMock->expects($this->once())->method('getGeneralList')->with($scopeId, $scope)->willReturn($listId); + + $apiMock->expects($this->once())->method('getLists')->willReturn($apiListsMock); + + $apiListsMock->expects($this->once())->method('getInterestCategory')->willReturn($apiListsInterestCategoryMock); + + $apiListsInterestCategoryMock->expects($this->once())->method('getAll')->with($listId, 'categories')->willReturn($categoriesResponse); + + $helperMock->getListInterestCategories($scopeId, $scope); + } + + public function testGetListInterestGroups() + { + $scopeId = 1; + $scope = 'stores'; + $listId = 'a1s2d3f4g5'; + $interestCategoryId = 1; + $categoriesResponse = array('categories' => array(array('id' => $interestCategoryId, 'title' => 'Category Title', 'type' => 'checkbox'))); + $interestsResponse = array('interests' => array(array('id' => 2, 'name' => 'Group Name'))); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getApi', 'getGeneralList')) + ->getMock(); + + $apiMock = $this->getMockBuilder(Ebizmarts_MailChimp::class) + ->disableOriginalConstructor() + ->setMethods(array('getLists')) + ->getMock(); + + $apiListsMock = $this->getMockBuilder(Ebizmarts_MailChimp::class) + ->disableOriginalConstructor() + ->setMethods(array('getInterestCategory')) + ->getMock(); + + $apiListsInterestCategoryMock = $this->getMockBuilder(MailChimp_ListsInterestCategory::class) + ->disableOriginalConstructor() + ->setMethods(array('getAll', 'getInterests')) + ->getMock(); + + $apiListsInterestCategoryInterestsMock = $this->getMockBuilder(MailChimp_ListInterestCategoryInterests::class) + ->disableOriginalConstructor() + ->setMethods(array('getAll')) + ->getMock(); + + $helperMock->expects($this->once())->method('getApi')->with($scopeId, $scope)->willReturn($apiMock); + $helperMock->expects($this->once())->method('getGeneralList')->with($scopeId, $scope)->willReturn($listId); + + $apiMock->expects($this->once())->method('getLists')->willReturn($apiListsMock); + + $apiListsMock->expects($this->once())->method('getInterestCategory')->willReturn($apiListsInterestCategoryMock); + + $apiListsInterestCategoryMock->expects($this->once())->method('getAll')->with($listId, 'categories')->willReturn($categoriesResponse); + $apiListsInterestCategoryMock->expects($this->once())->method('getInterests')->willReturn($apiListsInterestCategoryInterestsMock); + + $apiListsInterestCategoryInterestsMock->expects($this->once())->method('getAll')->with($listId, $interestCategoryId)->willReturn($interestsResponse); + + $helperMock->getListInterestGroups($scopeId, $scope); + } + + public function testGetInterest() + { + $scopeId = 1; + $listId = 'a1s2d3f4g5'; + $interestIdOne = 'z0x9c8v7b6'; + $interestNameOne = 'Group One Name'; + $displayOrderOne = 1; + $interestIdTwo = 'p4o5i6u7y8'; + $interestNameTwo = 'Group Two Name'; + $displayOrderTwo = 2; + $localGroups = "$interestIdOne,$interestIdTwo"; + $interestCategoryId = 1; + $categoriesResponse = array('categories' => array(array('id' => $interestCategoryId, 'title' => 'Category Title', 'type' => 'checkbox'))); + $interestsResponseOne = array('interests' => array(array('category_id' => $interestCategoryId, 'id' => $interestIdOne, 'name' => $interestNameOne, 'display_order' => $displayOrderOne))); + $interestsResponseTwo = array('interests' => array(array('category_id' => $interestCategoryId, 'id' => $interestIdTwo, 'name' => $interestNameTwo, 'display_order' => $displayOrderTwo))); + $expectedResult = array(1 => array('category' => array($displayOrderOne => array('id' => $interestIdOne, 'name' => $interestNameOne, 'checked' => false), $displayOrderTwo => array('id' => $interestIdTwo, 'name' => $interestNameTwo, 'checked' => false)))); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getApi', 'getGeneralList', 'getLocalInterestCategories')) + ->getMock(); + + $apiMock = $this->getMockBuilder(Ebizmarts_MailChimp::class) + ->disableOriginalConstructor() + ->setMethods(array('getLists')) + ->getMock(); + + $apiListsMock = $this->getMockBuilder(Ebizmarts_MailChimp::class) + ->disableOriginalConstructor() + ->setMethods(array('getInterestCategory')) + ->getMock(); + + $apiListsInterestCategoryMock = $this->getMockBuilder(MailChimp_ListsInterestCategory::class) + ->disableOriginalConstructor() + ->setMethods(array('getAll', 'getInterests')) + ->getMock(); + + $apiListsInterestCategoryInterestsMock = $this->getMockBuilder(MailChimp_ListInterestCategoryInterests::class) + ->disableOriginalConstructor() + ->setMethods(array('getAll')) + ->getMock(); + + $helperMock->expects($this->once())->method('getLocalInterestCategories')->with($scopeId)->willReturn($localGroups); + $helperMock->expects($this->once())->method('getApi')->with($scopeId)->willReturn($apiMock); + $helperMock->expects($this->once())->method('getGeneralList')->with($scopeId)->willReturn($listId); + + $apiMock->expects($this->once())->method('getLists')->willReturn($apiListsMock); + + $apiListsMock->expects($this->once())->method('getInterestCategory')->willReturn($apiListsInterestCategoryMock); + + $apiListsInterestCategoryMock->expects($this->once())->method('getAll')->with($listId)->willReturn($categoriesResponse); + + $apiListsInterestCategoryMock->expects($this->once())->method('getInterests')->willReturn($apiListsInterestCategoryInterestsMock); + + $apiListsInterestCategoryInterestsMock->expects($this->exactly(2))->method('getAll')->withConsecutive( + array($listId, $interestIdOne), + array($listId, $interestIdTwo) + )->willReturnOnConsecutiveCalls( + $interestsResponseOne, + $interestsResponseTwo + ); + + $result = $helperMock->getInterest($scopeId); + + $this->assertEquals($expectedResult, $result); + } + + public function testGetInterestGroups() + { + $customerId = 1; + $subscriberId = 1; + $storeId = 1; + $interestGroupId = 1; + $interestIdOne = 'z0x9c8v7b6'; + $interestNameOne = 'Group One Name'; + $displayOrderOne = 1; + $interestIdTwo = 'p4o5i6u7y8'; + $interestNameTwo = 'Group Two Name'; + $displayOrderTwo = 2; + $interest = array(1 => array('category' => array($displayOrderOne => array('id' => $interestIdOne, 'name' => $interestNameOne, 'checked' => false), $displayOrderTwo => array('id' => $interestIdTwo, 'name' => $interestNameTwo, 'checked' => false)))); + $groupData = 'a:2:{s:10:"bc15dbe6a5";a:1:{s:10:"d6b7541ee7";s:10:"d6b7541ee7";}s:10:"2a2f23d671";s:10:"36c250eeff";}'; + $expectedResult = $interest; + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getInterest', 'getInterestGroupModel', 'getLocalInterestCategories')) + ->getMock(); + + $interestGroupMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Interestgroup::class) + ->disableOriginalConstructor() + ->setMethods(array('getByRelatedIdStoreId', 'getId', 'getGroupdata')) + ->getMock(); + + $helperMock->expects($this->once())->method('getInterest')->with($storeId)->willReturn($interest); + $helperMock->expects($this->once())->method('getInterestGroupModel')->willReturn($interestGroupMock); + + $interestGroupMock->expects($this->once())->method('getByRelatedIdStoreId')->with($customerId, $subscriberId, $storeId)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('getId')->willReturn($interestGroupId); + $interestGroupMock->expects($this->once())->method('getGroupdata')->willReturn($groupData); + + $result = $helperMock->getInterestGroups($customerId, $subscriberId, $storeId); + + $this->assertEquals($expectedResult, $result); + } + + public function testSaveInterestGroupData() + { + $params = array(); + $customerId = 2; + $subscriberId = 2; + $origCustomerId = 1; + $origSubscriberId = 1; + $storeId = 1; + $groupData = 'a:2:{s:10:"bc15dbe6a5";a:1:{s:10:"d6b7541ee7";s:10:"d6b7541ee7";}s:10:"2a2f23d671";s:10:"36c250eeff";}'; + $unserializedGroupData = unserialize($groupData); + $currentDateTime = '2018-07-26 12:43:40'; + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getInterestGroupsIfAvailable', 'isAdmin', 'getCustomerSession', 'getInterestGroupModel', + 'getCurrentDateTime')) + ->getMock(); + + $interestGroupMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Interestgroup::class) + ->disableOriginalConstructor() + ->setMethods(array('getByRelatedIdStoreId', 'getSubscriberId', 'getCustomerId', 'setSubscriberId', + 'setCustomerId', 'setGroupdata', 'getGroupdata', 'setStoreId', 'setUpdatedAt', 'save')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('getSubscriberId')) + ->getMock(); + + $customerSessionMock = $this->getMockBuilder(Mage_Customer_Model_Session::class) + ->disableOriginalConstructor() + ->setMethods(array('isLoggedIn', 'getCustomer')) + ->getMock(); + + $customerMock = $this->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('getId')) + ->getMock(); + + $helperMock->expects($this->once())->method('getInterestGroupsIfAvailable')->with($params)->willReturn($unserializedGroupData); + $helperMock->expects($this->once())->method('getCustomerSession')->willReturn($customerSessionMock); + $helperMock->expects($this->once())->method('isAdmin')->willReturn(false); + + $customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true); + $customerSessionMock->expects($this->once())->method('getCustomer')->willReturn($customerMock); + + $customerMock->expects($this->once())->method('getId')->willReturn($customerId); + + $subscriberMock->expects($this->once())->method('getSubscriberId')->willReturn($subscriberId); + + $helperMock->expects($this->once())->method('getInterestGroupModel')->willReturn($interestGroupMock); + + $interestGroupMock->expects($this->once())->method('getByRelatedIdStoreId')->with($customerId, $subscriberId, $storeId)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('getSubscriberId')->willReturn($origSubscriberId); + $interestGroupMock->expects($this->once())->method('getCustomerId')->willReturn($origCustomerId); + $interestGroupMock->expects($this->once())->method('setSubscriberId')->with($subscriberId)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('setCustomerId')->with($customerId)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('setGroupdata')->with($groupData)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('getGroupdata')->willReturn($groupData); + $interestGroupMock->expects($this->once())->method('setStoreId')->with($storeId)->willReturnSelf(); + + $helperMock->expects($this->once())->method('getCurrentDateTime')->willReturn($currentDateTime); + + $interestGroupMock->expects($this->once())->method('setUpdatedAt')->with($currentDateTime)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('save')->willReturnSelf(); + + $helperMock->saveInterestGroupData($params, $storeId, null, $subscriberMock); + } } diff --git a/lib/Ebizmarts/MailChimp.php b/lib/Ebizmarts/MailChimp.php index 3cdec2e26..f9a10c91d 100755 --- a/lib/Ebizmarts/MailChimp.php +++ b/lib/Ebizmarts/MailChimp.php @@ -308,6 +308,14 @@ public function getBatchOperation() return $this->batchOperation; } + /** + * @return MailChimp_Lists + */ + public function getLists() + { + return $this->lists; + } + public function call($url,$params,$method=Ebizmarts_MailChimp::GET,$encodeJson=true) { $paramsOrig = $params; diff --git a/lib/Ebizmarts/MailChimp/Lists.php b/lib/Ebizmarts/MailChimp/Lists.php index ba56f78aa..d98635f6b 100644 --- a/lib/Ebizmarts/MailChimp/Lists.php +++ b/lib/Ebizmarts/MailChimp/Lists.php @@ -50,6 +50,14 @@ class MailChimp_Lists extends MailChimp_Abstract */ public $webhooks; + /** + * @return MailChimp_ListsInterestCategory + */ + public function getInterestCategory() + { + return $this->interestCategory; + } + /** * @param $name * @param $contact diff --git a/lib/Ebizmarts/MailChimp/ListsInterestCategory.php b/lib/Ebizmarts/MailChimp/ListsInterestCategory.php index 7f6a4bb5a..8faa1c35e 100644 --- a/lib/Ebizmarts/MailChimp/ListsInterestCategory.php +++ b/lib/Ebizmarts/MailChimp/ListsInterestCategory.php @@ -17,6 +17,14 @@ class MailChimp_ListsInterestCategory extends MailChimp_Abstract */ public $interests; + /** + * @return MailChimp_ListInterestCategoryInterests + */ + public function getInterests() + { + return $this->interests; + } + /** * @param $listId The unique id for the list. * @param $title The text description of this category. This field appears on signup forms and is From 950643acd24539d0e29fff31385f8841cefd6962 Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 31 Jul 2018 14:08:53 -0300 Subject: [PATCH 017/113] Add tests. --- .../MailChimp/controllers/GroupController.php | 67 ++++++++-- .../controllers/GroupControllerTest.php | 120 ++++++++++++++++++ 2 files changed, 174 insertions(+), 13 deletions(-) create mode 100644 dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/GroupControllerTest.php diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php index 5cf95fac3..0071bf8b7 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php @@ -16,35 +16,36 @@ class Ebizmarts_MailChimp_GroupController extends Mage_Core_Controller_Front_Act { public function indexAction() { - $order = Mage::getSingleton('checkout/session')->getLastRealOrder(); - $session = Mage::getSingleton('core/session'); - $helper = $this->getHelper(); + $order = $this->getSessionLastRealOrder(); + $session = $this->getCoreSession(); + $interestGroup = $this->getInterestGroupModel(); $params = $this->getRequest()->getParams(); $storeId = $order->getStoreId(); - $interestGroup = Mage::getModel('mailchimp/interestgroup'); - $subscriber = Mage::getModel('newsletter/subscriber') - ->loadByEmail($order->getCustomerEmail()); + $customerEmail = $order->getCustomerEmail(); $customerId = $order->getCustomerId(); + $subscriber = $this->getSubscriberModel() + ->loadByEmail($customerEmail); try { if (!$subscriber->getSubscriberId()) { - $subscriber->setSubscriberEmail($order->getCustomerEmail()); + $subscriber->setSubscriberEmail($customerEmail); $subscriber->setSubscriberFirstname($order->getCustomerFirstname()); $subscriber->setSubscriberLastname($order->getCustomerLastname()); - $subscriber->subscribe($order->getCustomerEmail()); + $subscriber->subscribe($customerEmail); } - $interestGroup->getByRelatedIdStoreId($customerId, $subscriber->getSubscriberId(),$storeId); + $subscriberId = $subscriber->getSubscriberId(); + $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); $interestGroup->setGroupdata(serialize($params)); - $interestGroup->setSubscriberId($subscriber->getSubscriberId()); - $interestGroup->setCustomerId($order->getCustomerId()); + $interestGroup->setSubscriberId($subscriberId); + $interestGroup->setCustomerId($customerId); $interestGroup->setStoreId($storeId); - $interestGroup->setUpdatedAt(Mage::getModel('core/date')->date('d-m-Y H:i:s')); + $interestGroup->setUpdatedAt($this->getCurrentDateTime()); $interestGroup->save(); $this->getApiSubscriber()->update($subscriber->getSubscriberEmail(), $storeId, '', 1); $session->addSuccess($this->__('Thanks for share your interest with us.')); } catch (Exception $e) { - $helper->logError($e->getMessage()); + $this->getHelper()->logError($e->getMessage()); $session->addWarning($this->__('Something went wrong with the interests subscription. Please go to the account subscription menu to subscriber to the interests successfully.')); } $this->_redirect('/'); @@ -59,4 +60,44 @@ protected function getApiSubscriber() { return Mage::getModel('mailchimp/api_subscribers'); } + + /** + * @return mixed + */ + protected function getSessionLastRealOrder() + { + return Mage::getSingleton('checkout/session')->getLastRealOrder(); + } + + /** + * @return Mage_Core_Model_Session + */ + protected function getCoreSession() + { + return Mage::getSingleton('core/session'); + } + + /** + * @return Ebizmarts_MailChimp_Model_Interestgroup + */ + protected function getInterestGroupModel() + { + return Mage::getModel('mailchimp/interestgroup'); + } + + /** + * @return Mage_Newsletter_Model_Subscriber + */ + protected function getSubscriberModel() + { + return Mage::getModel('newsletter/subscriber'); + } + + /** + * @return mixed + */ + protected function getCurrentDateTime() + { + return Mage::getModel('core/date')->date('d-m-Y H:i:s'); + } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/GroupControllerTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/GroupControllerTest.php new file mode 100644 index 000000000..dfa989fa6 --- /dev/null +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/GroupControllerTest.php @@ -0,0 +1,120 @@ +groupController = $this->getMockBuilder(Ebizmarts_MailChimp_GroupController::class); + } + + public function tearDown() + { + $this->groupController = null; + } + + public function testIndexAction() + { + $storeId = 1; + $customerId = 1; + $subscriberId = 1; + $customerEmail = 'customer@email.com'; + $customerFirstName = "First Name"; + $customerLastName = "Last Name"; + $groupData = 'a:2:{s:10:"bc15dbe6a5";a:1:{s:10:"d6b7541ee7";s:10:"d6b7541ee7";}s:10:"2a2f23d671";s:10:"36c250eeff";}'; + $params = unserialize($groupData); + $currentDateTime = '2018-07-26 12:43:40'; + $successMessage = 'Thanks for share your interest with us.'; + + $groupControllerMock = $this->groupController + ->disableOriginalConstructor() + ->setMethods(array('getSessionLastRealOrder', 'getCoreSession', 'getHelper', 'getRequest', + 'getInterestGroupModel', 'getSubscriberModel', 'getApiSubscriber', '_redirect', + 'getCurrentDateTime','__')) + ->getMock(); + + $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParams')) + ->getMock(); + + $orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) + ->disableOriginalConstructor() + ->setMethods(array('getStoreId', 'getCustomerEmail', 'getCustomerId', 'getCustomerFirstname', 'getCustomerLastname')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('getSubscriberId', 'setSubscriberEmail', 'setSubscriberFirstname', + 'setSubscriberLastname', 'subscribe', 'getSubscriberEmail', 'loadByEmail')) + ->getMock(); + + $coreSessionMock = $this->getMockBuilder(Mage_Core_Model_Session::class) + ->disableOriginalConstructor() + ->setMethods(array('addSuccess')) + ->getMock(); + + $interestGroupMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Interestgroup::class) + ->disableOriginalConstructor() + ->setMethods(array('getByRelatedIdStoreId', 'setGroupdata', 'setSubscriberId', 'setCustomerId', + 'setStoreId', 'setUpdatedAt', 'save')) + ->getMock(); + + $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) + ->disableOriginalConstructor() + ->setMethods(array('update')) + ->getMock(); + + $groupControllerMock->expects($this->once())->method('getSessionLastRealOrder')->willReturn($orderMock); + $groupControllerMock->expects($this->once())->method('getCoreSession')->willReturn($coreSessionMock); + $groupControllerMock->expects($this->once())->method('getInterestGroupModel')->willReturn($interestGroupMock); + $groupControllerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); + + $requestMock->expects($this->once())->method('getParams')->willReturn($params); + + $orderMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + $orderMock->expects($this->once())->method('getCustomerEmail')->willReturn($customerEmail); + $orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); + $orderMock->expects($this->once())->method('getCustomerFirstname')->willReturn($customerFirstName); + $orderMock->expects($this->once())->method('getCustomerLastname')->willReturn($customerLastName); + + $groupControllerMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); + $groupControllerMock->expects($this->once())->method('getCurrentDateTime')->willReturn($currentDateTime); + + $subscriberMock->expects($this->once())->method('loadByEmail')->with($customerEmail)->willReturnSelf(); + $subscriberMock->expects($this->exactly(2))->method('getSubscriberId') + ->willReturnOnConsecutiveCalls(null, $subscriberId); + $subscriberMock->expects($this->once())->method('setSubscriberEmail')->with($customerEmail)->willReturnSelf(); + $subscriberMock->expects($this->once())->method('setSubscriberFirstname')->with($customerFirstName)->willReturnSelf(); + $subscriberMock->expects($this->once())->method('setSubscriberLastname')->with($customerLastName)->willReturnSelf(); + $subscriberMock->expects($this->once())->method('subscribe')->willReturnSelf(); + + $interestGroupMock->expects($this->once())->method('getByRelatedIdStoreId')->with($customerId, $subscriberId, $storeId)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('setGroupdata')->with($groupData)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('setSubscriberId')->with($subscriberId)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('setCustomerId')->with($customerId)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('setStoreId')->with($storeId)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('setUpdatedAt')->with($currentDateTime)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('save')->willReturnSelf(); + + $groupControllerMock->expects($this->once())->method('getApiSubscriber')->willReturn($apiSubscriberMock); + + $subscriberMock->expects($this->once())->method('getSubscriberEmail')->willReturn($customerEmail); + + $apiSubscriberMock->expects($this->once())->method('update')->with($customerEmail, $storeId, '', 1); + + $groupControllerMock->expects($this->once())->method('__')->with($successMessage)->willReturn($successMessage); + + $coreSessionMock->expects($this->once())->method('addSuccess')->with($successMessage); + + $groupControllerMock->expects($this->once())->method('_redirect')->with('/'); + $groupControllerMock->indexAction(); + } +} From 872b1113e6b71dc6adf2fc7c197bbafac4a6d501 Mon Sep 17 00:00:00 2001 From: keller Date: Thu, 18 Oct 2018 10:47:34 -0300 Subject: [PATCH 018/113] closes #793 Add option to send subscription emails via Magento --- .../Ebizmarts/MailChimp/Model/Config.php | 1 + .../Ebizmarts/MailChimp/Model/Observer.php | 4 +++- .../Ebizmarts/MailChimp/Model/Subscriber.php | 6 ++--- .../Ebizmarts/MailChimp/etc/system.xml | 11 ++++++++++ .../MailChimp/Model/ObserverTest.php | 22 ++++++++++++++++--- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Config.php b/app/code/community/Ebizmarts/MailChimp/Model/Config.php index a12280a70..55ab2c402 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Config.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Config.php @@ -35,6 +35,7 @@ class Ebizmarts_MailChimp_Model_Config const GENERAL_MIGRATE_FROM_1164 = 'mailchimp/general/migrate_from_1164'; const GENERAL_MIGRATE_LAST_ORDER_ID = 'mailchimp/general/migrate_last_order_id'; const GENERAL_SUBSCRIBER_AMOUNT = 'mailchimp/general/subscriber_batch_amount'; + const GENERAL_MAGENTO_MAIL = 'mailchimp/general/magento_mail'; const ECOMMERCE_ACTIVE = 'mailchimp/ecommerce/active'; const ECOMMERCE_CUSTOMERS_OPTIN = 'mailchimp/ecommerce/customers_optin'; diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 4eeb69bc9..7b650fc0e 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -147,7 +147,9 @@ public function handleSubscriber(Varien_Event_Observer $observer) $this->addSuccessIfRequired($helper); } $apiSubscriber = $this->makeApiSubscriber(); - $subscriber->setImportMode(true); + if($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1){ + $subscriber->setImportMode(true); + } $this->createEmailCookie($subscriber); if ($statusChanged) { diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php index ba61537a5..1e1026e17 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php @@ -12,7 +12,7 @@ class Ebizmarts_MailChimp_Model_Subscriber extends Mage_Newsletter_Model_Subscri public function sendUnsubscriptionEmail() { - if (Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE)) { + if (Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE) && Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL) != 1) { return $this; } else { return parent::sendUnsubscriptionEmail(); @@ -21,7 +21,7 @@ public function sendUnsubscriptionEmail() public function sendConfirmationRequestEmail() { - if (Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE)) { + if (Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE) && Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL) != 1) { return $this; } else { return parent::sendConfirmationRequestEmail(); @@ -30,7 +30,7 @@ public function sendConfirmationRequestEmail() public function sendConfirmationSuccessEmail() { - if (Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE)) { + if (Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE) && Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL) != 1) { return $this; } else { return parent::sendConfirmationSuccessEmail(); diff --git a/app/code/community/Ebizmarts/MailChimp/etc/system.xml b/app/code/community/Ebizmarts/MailChimp/etc/system.xml index 7e85f3eae..512b011cd 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/system.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/system.xml @@ -182,6 +182,17 @@ 1 + + + select + adminhtml/system_config_source_yesno + 61 + 1 + 1 + 1 + 1 + + select diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 877adf3d3..bf978701f 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -649,8 +649,15 @@ public function testAddColumnToSalesOrderGridCollection() $observerMock->addColumnToSalesOrderGridCollection($eventObserverMock); } - public function testHandleSubscriber() + /** + * @param array $data + * @dataProvider handleSubscriberDataProvider + */ + + public function testHandleSubscriber($data) { + $setImportMode = $data['setImportMode']; + $magentoMail = $data['magentoMail']; $storeId = 1; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) @@ -667,7 +674,7 @@ public function testHandleSubscriber() $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() ->setMethods(array('isSubscriptionEnabled', 'isEcomSyncDataEnabledInAnyScope', - 'isSubscriptionConfirmationEnabled')) + 'isSubscriptionConfirmationEnabled', 'getConfigValueForScope')) ->getMock(); $eventMock = $this->getMockBuilder(Varien_Event::class) @@ -701,13 +708,14 @@ public function testHandleSubscriber() $subscriberMock->expects($this->once())->method('getStatus')->willReturn(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); $helperMock->expects($this->once())->method('isSubscriptionConfirmationEnabled')->with($storeId)->willReturn(true); + $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); $subscriberMock->expects($this->once())->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $observerMock->expects($this->once())->method('addSuccessIfRequired')->with($helperMock); $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); - $subscriberMock->expects($this->once())->method('setImportMode')->with(true); + $subscriberMock->expects($this->exactly($setImportMode))->method('setImportMode')->with(true); $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); @@ -716,6 +724,14 @@ public function testHandleSubscriber() $observerMock->handleSubscriber($eventObserverMock); } + public function handleSubscriberDataProvider() + { + return array( + array(array('magentoMail' => 0, 'setImportMode' => 1)), + array(array('magentoMail' => 1, 'setImportMode' => 0)) + ); + } + /** * @param array $cookieData * @dataProvider loadCustomerToQuoteDataProvider From 93288430ccbb7974c3e0767dfe1a1d91195868d0 Mon Sep 17 00:00:00 2001 From: keller Date: Fri, 19 Oct 2018 16:17:06 -0300 Subject: [PATCH 019/113] #793 Don't send subscriber to Mailchimp automatically when using Magento email --- .../Ebizmarts/MailChimp/Model/Observer.php | 29 ++++++++++--------- .../MailChimp/Model/ObserverTest.php | 24 ++++++++++----- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 7b650fc0e..4a41e864c 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -141,26 +141,27 @@ public function handleSubscriber(Varien_Event_Observer $observer) $isEnabled = $helper->isSubscriptionEnabled($storeId); if ($isEnabled) { $statusChanged = $subscriber->getIsStatusChanged(); - //Override Magento status to always send double opt-in confirmation. - if($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId)) { - $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); - $this->addSuccessIfRequired($helper); - } $apiSubscriber = $this->makeApiSubscriber(); if($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1){ - $subscriber->setImportMode(true); + //Override Magento status to always send double opt-in confirmation. + if($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId)) { + $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); + $this->addSuccessIfRequired($helper); } - $this->createEmailCookie($subscriber); + $subscriber->setImportMode(true); - if ($statusChanged) { - $apiSubscriber->updateSubscriber($subscriber, true); - } else { - $origData = $subscriber->getOrigData(); + $this->createEmailCookie($subscriber); - if (is_array($origData) && isset($origData['subscriber_status']) - && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() - ) { + if ($statusChanged) { $apiSubscriber->updateSubscriber($subscriber, true); + } else { + $origData = $subscriber->getOrigData(); + + if (is_array($origData) && isset($origData['subscriber_status']) + && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() + ) { + $apiSubscriber->updateSubscriber($subscriber, true); + } } } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index bf978701f..8908e058f 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -658,6 +658,12 @@ public function testHandleSubscriber($data) { $setImportMode = $data['setImportMode']; $magentoMail = $data['magentoMail']; + $addSuccessIfRequired = $data['addSuccessIfRequired']; + $createEmailCookie = $data['createEmailCookie']; + $isSubscriptionConfirmationEnabled = $data['isSubscriptionConfirmationEnabled']; + $getStatus = $data['getStatus']; + $setStatus = $data['setStatus']; + $updateSubscriber = $data['updateSubscriber']; $storeId = 1; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) @@ -705,21 +711,21 @@ public function testHandleSubscriber($data) $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); $subscriberMock->expects($this->once())->method('getIsStatusChanged')->willReturn(true); - $subscriberMock->expects($this->once())->method('getStatus')->willReturn(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); + $subscriberMock->expects($this->exactly($getStatus))->method('getStatus')->willReturn(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); - $helperMock->expects($this->once())->method('isSubscriptionConfirmationEnabled')->with($storeId)->willReturn(true); + $helperMock->expects($this->exactly($isSubscriptionConfirmationEnabled))->method('isSubscriptionConfirmationEnabled')->with($storeId)->willReturn(true); $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); - $subscriberMock->expects($this->once())->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); + $subscriberMock->expects($this->exactly($setStatus))->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); - $observerMock->expects($this->once())->method('addSuccessIfRequired')->with($helperMock); + $observerMock->expects($this->exactly($addSuccessIfRequired))->method('addSuccessIfRequired')->with($helperMock); $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); $subscriberMock->expects($this->exactly($setImportMode))->method('setImportMode')->with(true); - $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); + $observerMock->expects($this->exactly($createEmailCookie))->method('createEmailCookie')->with($subscriberMock); - $apiSubscriberMock->expects($this->once())->method('updateSubscriber')->with($subscriberMock, true); + $apiSubscriberMock->expects($this->exactly($updateSubscriber))->method('updateSubscriber')->with($subscriberMock, true); $observerMock->handleSubscriber($eventObserverMock); } @@ -727,8 +733,10 @@ public function testHandleSubscriber($data) public function handleSubscriberDataProvider() { return array( - array(array('magentoMail' => 0, 'setImportMode' => 1)), - array(array('magentoMail' => 1, 'setImportMode' => 0)) + array(array('magentoMail' => 0, 'setImportMode' => 1, 'addSuccessIfRequired' => 1, 'createEmailCookie' => 1, 'isSubscriptionConfirmationEnabled' => 1, 'getStatus' => 1, + 'setStatus' => 1, 'updateSubscriber' => 1)), + array(array('magentoMail' => 1, 'setImportMode' => 0, 'addSuccessIfRequired' => 0, 'createEmailCookie' => 0, 'isSubscriptionConfirmationEnabled' => 0, 'getStatus' => 0, + 'setStatus' => 0, 'updateSubscriber' => 0)) ); } From 1e720c40b662cde1bb2b9464c65c6cd06381bdb8 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 19 Oct 2018 17:35:06 -0300 Subject: [PATCH 020/113] Add translations. --- .../MailChimp/Block/Checkout/Success/Groups.php | 2 +- .../community/Ebizmarts/MailChimp/etc/system.xml | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index 2e5d9dda1..b9b8aeca2 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -148,7 +148,7 @@ protected function createOptionArray($category) $type = $category['type']; if ($type == 'dropdown') { - $options[''] = '-- Select Group --'; + $options[''] = $this->__('-- Select Group --'); } return $options; } diff --git a/app/code/community/Ebizmarts/MailChimp/etc/system.xml b/app/code/community/Ebizmarts/MailChimp/etc/system.xml index a7e980f20..dbdd1cdd6 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/system.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/system.xml @@ -141,7 +141,7 @@ 1 - + textarea 50 1 @@ -153,7 +153,7 @@ 1 - + textarea 51 @@ -166,7 +166,7 @@ 1 - + select mailchimp/system_config_source_batchLimit @@ -207,7 +207,7 @@ - + button mailchimp/adminhtml_system_config_createWebhook 58 @@ -217,7 +217,7 @@ - + select adminhtml/system_config_source_yesno mailchimp/system_config_backend_twowaysync @@ -260,7 +260,7 @@ - + 200 text 1 From 94353fcbfcc7abc22bbe26b7fbf1315f062af8c4 Mon Sep 17 00:00:00 2001 From: keller Date: Mon, 22 Oct 2018 15:04:04 -0300 Subject: [PATCH 021/113] #793 Force double-opt in for registered customers if "Need to Confirm" enabled --- .../Ebizmarts/MailChimp/Model/Subscriber.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php index 1e1026e17..3b980d3f5 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php @@ -36,4 +36,59 @@ public function sendConfirmationSuccessEmail() return parent::sendConfirmationSuccessEmail(); } } + + //Force double-opt in for registered customers if "Need to Confirm" is enabled + public function subscribe($email) + { + $this->loadByEmail($email); + $customerSession = Mage::getSingleton('customer/session'); + + if(!$this->getId()) { + $this->setSubscriberConfirmCode($this->randomSequence()); + } + + $isConfirmNeed = (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1) ? true : false; + $isOwnSubscribes = false; + $ownerId = Mage::getModel('customer/customer') + ->setWebsiteId(Mage::app()->getStore()->getWebsiteId()) + ->loadByEmail($email) + ->getId(); + $isSubscribeOwnEmail = $customerSession->isLoggedIn() && $ownerId == $customerSession->getId(); + + if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED + || $this->getStatus() == self::STATUS_NOT_ACTIVE + ) { + if ($isConfirmNeed === true) { + $this->setStatus(self::STATUS_NOT_ACTIVE); + } else { + $this->setStatus(self::STATUS_SUBSCRIBED); + } + $this->setSubscriberEmail($email); + } + + if ($isSubscribeOwnEmail) { + $this->setStoreId($customerSession->getCustomer()->getStoreId()); + $this->setCustomerId($customerSession->getCustomerId()); + } else { + $this->setStoreId(Mage::app()->getStore()->getId()); + $this->setCustomerId(0); + } + + $this->setIsStatusChanged(true); + + try { + $this->save(); + if ($isConfirmNeed === true + && $isOwnSubscribes === false + ) { + $this->sendConfirmationRequestEmail(); + } else { + $this->sendConfirmationSuccessEmail(); + } + + return $this->getStatus(); + } catch (Exception $e) { + throw new Exception($e->getMessage()); + } + } } From c9c08ded4c4d39f965deedb4dc2183429a3700e1 Mon Sep 17 00:00:00 2001 From: keller Date: Thu, 25 Oct 2018 16:21:08 -0300 Subject: [PATCH 022/113] #793 Change how customers are forcefully subscribed --- .../community/Ebizmarts/MailChimp/Model/Api/Customers.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php index acc89eb62..88fcb2f0f 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php @@ -49,6 +49,8 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId) $this->optInStatusForStore = $this->getOptin($this->getBatchMagentoStoreId()); + $subscriber = Mage::getModel('newsletter/subscriber'); + $counter = 0; foreach ($customersCollection as $customer) { $data = $this->_buildCustomerData($customer); @@ -60,6 +62,10 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId) $this->logCouldNotEncodeCustomerError($customer); } + if ($this->optInStatusForStore && !$subscriber->loadByEmail($customer->getEmail())){ + $subscriber->subscribe($customer->getEmail()); + } + $counter++; } return $customerArray; @@ -88,7 +94,6 @@ protected function _buildCustomerData($customer) $data["email_address"] = $this->getCustomerEmail($customer); $data["first_name"] = $this->getCustomerFirstname($customer); $data["last_name"] = $this->getCustomerLastname($customer); - $data["opt_in_status"] = $this->optInStatusForStore; $data["orders_count"] = (int)$customer->getOrdersCount(); $data["total_spent"] = (float)$customer->getTotalSpent(); From 29b4e48ea391baf85551b9180ffd3dc5c4ca209e Mon Sep 17 00:00:00 2001 From: keller Date: Mon, 5 Nov 2018 11:56:21 -0300 Subject: [PATCH 023/113] #793 Tests corrected --- .../MailChimp/Model/Api/Customers.php | 16 +++++++++++-- .../MailChimp/Model/Api/CustomersTest.php | 23 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php index 88fcb2f0f..9007eb7ed 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php @@ -49,7 +49,7 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId) $this->optInStatusForStore = $this->getOptin($this->getBatchMagentoStoreId()); - $subscriber = Mage::getModel('newsletter/subscriber'); + $subscriber = $this->getSubscriberModel(); $counter = 0; foreach ($customersCollection as $customer) { @@ -62,7 +62,9 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId) $this->logCouldNotEncodeCustomerError($customer); } - if ($this->optInStatusForStore && !$subscriber->loadByEmail($customer->getEmail())){ + $isSubscribed = $subscriber->loadByEmail($customer->getEmail())->getSubscriberId(); + + if ($this->optInStatusForStore && !$isSubscribed){ $subscriber->subscribe($customer->getEmail()); } @@ -97,6 +99,7 @@ protected function _buildCustomerData($customer) $data["orders_count"] = (int)$customer->getOrdersCount(); $data["total_spent"] = (float)$customer->getTotalSpent(); + $data["opt_in_status"] = false; $data += $this->getCustomerAddressData($customer); @@ -437,4 +440,13 @@ protected function setMagentoStoreId($magentoStoreId) $this->magentoStoreId = $magentoStoreId; } + /** + * @return false|Mage_Core_Model_Abstract + */ + protected function getSubscriberModel() + { + $subscriber = Mage::getModel('newsletter/subscriber'); + return $subscriber; + } + } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CustomersTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CustomersTest.php index 7c217474a..8db205bf2 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CustomersTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CustomersTest.php @@ -54,6 +54,8 @@ public function testGetOptInNoDefaultStore() public function testCreateBatchJson() { $mailchimpStoreId = 'a1s2d3f4g5h6j7k8l9n0'; + $customerEmail = 'keller@ebizmarts.com'; + $subscriberId = 1; $storeId = 1; $optInStatus = true; $customerId = 142; @@ -77,7 +79,7 @@ public function testCreateBatchJson() $this->customersApiMock = $this->customersApiMock->setMethods( array('makeBatchId', 'joinMailchimpSyncData', 'makeCustomersNotSentCollection', 'getOptin', 'getBatchMagentoStoreId', '_buildCustomerData', 'makePutBatchStructure', - '_updateSyncData', 'setMailchimpStoreId', 'setMagentoStoreId', 'getCustomerResourceCollection' + '_updateSyncData', 'setMailchimpStoreId', 'setMagentoStoreId', 'getCustomerResourceCollection', 'getSubscriberModel' ) ) ->getMock(); @@ -88,11 +90,27 @@ public function testCreateBatchJson() $customerMock = $this->getMockBuilder(Mage_Customer_Model_Customer::class) ->disableOriginalConstructor() - ->setMethods(array('getId')) + ->setMethods(array('getId', 'getEmail')) ->getMock(); + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByEmail')) + ->getMock(); + + $subscribersMailchimpMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('getSubscriberId')) + ->getMock(); + $customerArray = array($customerMock); + $this->customersApiMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); + + $subscriberMock->expects($this->once())->method('loadByEmail')->with($customerEmail)->willReturn($subscribersMailchimpMock); + + $subscribersMailchimpMock->expects($this->once())->method('getSubscriberId')->willReturn($subscriberId); + $this->customersApiMock->expects($this->once())->method('setMailchimpStoreId')->with($mailchimpStoreId); $this->customersApiMock->expects($this->once())->method('setMagentoStoreId')->with($storeId); @@ -112,6 +130,7 @@ public function testCreateBatchJson() $this->customersApiMock->expects($this->once())->method('makePutBatchStructure')->with($customerJson)->willReturn($operationData); $customerMock->expects($this->once())->method('getId')->willReturn($customerId); + $customerMock->expects($this->any())->method('getEmail')->willReturn($customerEmail); $this->customersApiMock->expects($this->once())->method('_updateSyncData')->with($customerId, $mailchimpStoreId); From 2ae9475e8a46067cd73a496f670455c6ef4304e4 Mon Sep 17 00:00:00 2001 From: keller Date: Tue, 6 Nov 2018 10:39:16 -0300 Subject: [PATCH 024/113] #793 Override original subscribe method only if extension is enabled --- .../community/Ebizmarts/MailChimp/Model/Subscriber.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php index 3b980d3f5..4ac84fc6c 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php @@ -40,10 +40,11 @@ public function sendConfirmationSuccessEmail() //Force double-opt in for registered customers if "Need to Confirm" is enabled public function subscribe($email) { + if (Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE)) { $this->loadByEmail($email); $customerSession = Mage::getSingleton('customer/session'); - if(!$this->getId()) { + if (!$this->getId()) { $this->setSubscriberConfirmCode($this->randomSequence()); } @@ -59,7 +60,7 @@ public function subscribe($email) || $this->getStatus() == self::STATUS_NOT_ACTIVE ) { if ($isConfirmNeed === true) { - $this->setStatus(self::STATUS_NOT_ACTIVE); + $this->setStatus(self::STATUS_NOT_ACTIVE); } else { $this->setStatus(self::STATUS_SUBSCRIBED); } @@ -90,5 +91,9 @@ public function subscribe($email) } catch (Exception $e) { throw new Exception($e->getMessage()); } + } else { + return parent::subscribe($email); + } + } } From e81e38f2aa1a2db7cbbcc0df568e7544b9cb1f98 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 8 Nov 2018 17:54:54 -0300 Subject: [PATCH 025/113] Separate each template type. --- .../Block/Checkout/Success/CheckboxGroups.php | 14 ++ .../Block/Checkout/Success/DropdownGroups.php | 14 ++ .../Block/Checkout/Success/Groups.php | 191 ++---------------- .../Block/Checkout/Success/RadioGroups.php | 14 ++ .../Block/Checkout/Success/SelectGroups.php | 14 ++ .../Ebizmarts/MailChimp/etc/system.xml | 2 - .../default/layout/ebizmarts/mailchimp.xml | 6 +- .../checkout/success/checkbox_groups.phtml | 19 ++ .../checkout/success/dropdown_groups.phtml | 15 ++ .../mailchimp/checkout/success/groups.phtml | 56 +---- .../checkout/success/radio_groups.phtml | 19 ++ 11 files changed, 134 insertions(+), 230 deletions(-) create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/CheckboxGroups.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/RadioGroups.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/SelectGroups.php create mode 100644 app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/checkbox_groups.phtml create mode 100644 app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml create mode 100644 app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/radio_groups.phtml diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/CheckboxGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/CheckboxGroups.php new file mode 100644 index 000000000..cf43d3741 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/CheckboxGroups.php @@ -0,0 +1,14 @@ + + * @license http://opensource.org/licenses/osl-3.0.php + */ +class Ebizmarts_MailChimp_Block_Checkout_Success_CheckboxGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups +{ + +} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php new file mode 100644 index 000000000..2ffe62f1e --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php @@ -0,0 +1,14 @@ + + * @license http://opensource.org/licenses/osl-3.0.php + */ +class Ebizmarts_MailChimp_Block_Checkout_Success_DropdownGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups +{ + +} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index b9b8aeca2..7c8d8638a 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -4,19 +4,13 @@ * Checkout subscribe checkbox block renderer * * @category Ebizmarts - * @package Ebizmarts_MageMonkey + * @package Ebizmarts_MailChimp * @author Ebizmarts Team * @license http://opensource.org/licenses/osl-3.0.php */ class Ebizmarts_MailChimp_Block_Checkout_Success_Groups extends Mage_Core_Block_Template { - - protected $_lists = array(); - protected $_info = array(); - protected $_myLists = array(); - protected $_generalList = array(); - protected $_form; - protected $_api; + protected $_currentIntesrest; /** * @var Ebizmarts_MailChimp_Helper_Data */ @@ -30,172 +24,6 @@ public function __construct() $this->storeId = Mage::app()->getStore()->getId(); } - /** - * Get list data from MC - * - * @return array - */ - public function getGeneralList() - { - $storeId = $this->storeId; - $helper = $this->getMailChimpHelper(); - $listId = $helper->getGeneralList($storeId); - - return $listId; - } - - public function getListInterestGroups() - { - $storeId = $this->storeId; - $helper = $this->getMailChimpHelper(); - $return = $helper->getListInterestGroups($storeId); - return $return; - } - - public function getCategoryTitle($category) - { - return $category['title']; - } - - public function renderGroups($category) - { - $object = $this->createObject($category); - - $this->addGroupOptions($category, $object); - - $html = $this->getElementHtml($category, $object); - - return $html; - } - - public function getGroupClass($type) - { - switch ($type) { - case 'radio': - $class = 'Varien_Data_Form_Element_Radios'; - break; - case 'checkboxes': - $class = 'Varien_Data_Form_Element_Checkboxes'; - break; - case 'dropdown': - $class = 'Varien_Data_Form_Element_Select'; - break; - default: - $class = 'Varien_Data_Form_Element_Text'; - break; - } - - return $class; - } - - public function htmlGroupName($category) - { - $storeId = $this->storeId; - $helper = $this->getMailChimpHelper(); - $listId = $helper->getGeneralList($storeId); - $htmlName = "list[{$listId}]"; - $htmlName .= "[{$category['id']}]"; - - if ($category['type'] == 'checkboxes') { - $htmlName .= '[]'; - } - - return $htmlName; - } - - /** - * Form getter/instantiation - * - * @return Varien_Data_Form - */ - public function getForm() - { - if ($this->_form instanceof Varien_Data_Form) { - return $this->_form; - } - $form = new Varien_Data_Form(); - return $form; - } - - /** - * @param $category - * @param $object - */ - protected function addGroupOptions($category, $object) - { - $type = $category['type']; - - if ($type == 'checkboxes' || $type == 'dropdown') { - $options = $this->createOptionArray($category); - - if (isset($category['groups'])) { - foreach ($category['groups'] as $key => $group) { - $options[$key] = $group; - } - - $object->setValues($options); - } - } - } - - /** - * @param $category - * @return mixed - */ - protected function createOptionArray($category) - { - $options = array(); - $type = $category['type']; - - if ($type == 'dropdown') { - $options[''] = $this->__('-- Select Group --'); - } - return $options; - } - - /** - * @param $category - * @param $html - * @return string - */ - protected function addGroupContainer($category, $html) - { - $type = $category['type']; - if ($type != 'checkboxes') { - $html = "
{$html}
"; - } - return $html; - } - - /** - * @param $category - * @return mixed - */ - protected function createObject($category) - { - $type = $category['type']; - $class = $this->getGroupClass($type); - $object = new $class; - $object->setForm($this->getForm()); - $object->setName($this->htmlGroupName($category)); - $object->setHtmlId('interest-group'); - - return $object; - } - - /** - * @param $category - * @param $object - * @return string - */ - protected function getElementHtml($category, $object) - { - $html = $object->getElementHtml(); - $html = $this->addGroupContainer($category, $html); - - return $html; - } - public function getFormUrl() { return $this->getSuccessInterestUrl(); @@ -214,9 +42,8 @@ public function getInterest() $subscriber->loadByEmail($order->getCustomerEmail()); $subscriberId = $subscriber->getSubscriberId(); $customerId = $order->getCustomerId(); - $helper = $this->getMailChimpHelper(); - $interest = $helper->getInterestGroups($customerId, $subscriberId,$order->getStoreId()); + $interest = $helper->getInterestGroups($customerId, $subscriberId, $order->getStoreId()); return $interest; } @@ -256,4 +83,16 @@ protected function getMailChimpHelper() return $this->helper; } + /** + * @return mixed + */ + protected function getCurrentInterest() + { + return $this->_currentIntesrest; + } + + protected function setCurrentInterest($interest) + { + $this->_currentIntesrest = $interest; + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/RadioGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/RadioGroups.php new file mode 100644 index 000000000..f6d8b0901 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/RadioGroups.php @@ -0,0 +1,14 @@ + + * @license http://opensource.org/licenses/osl-3.0.php + */ +class Ebizmarts_MailChimp_Block_Checkout_Success_RadioGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups +{ + +} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/SelectGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/SelectGroups.php new file mode 100644 index 000000000..4c9661204 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/SelectGroups.php @@ -0,0 +1,14 @@ + + * @license http://opensource.org/licenses/osl-3.0.php + */ +class Ebizmarts_MailChimp_Block_Checkout_Success_SelectGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups +{ + +} diff --git a/app/code/community/Ebizmarts/MailChimp/etc/system.xml b/app/code/community/Ebizmarts/MailChimp/etc/system.xml index dbdd1cdd6..d3e959581 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/system.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/system.xml @@ -148,7 +148,6 @@ 1 1 1 - 1 @@ -161,7 +160,6 @@ 1 1 1 - 1 diff --git a/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml b/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml index 513809c8c..489962854 100755 --- a/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml +++ b/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml @@ -55,7 +55,11 @@ + template="ebizmarts/mailchimp/checkout/success/groups.phtml"> + + + + diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/checkbox_groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/checkbox_groups.phtml new file mode 100644 index 000000000..44e17f6a5 --- /dev/null +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/checkbox_groups.phtml @@ -0,0 +1,19 @@ +getCurrentInterest(); +?> +
    + +
  • + + /> + +
  • + +
diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml new file mode 100644 index 000000000..280268ed5 --- /dev/null +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml @@ -0,0 +1,15 @@ +getCurrentInterest(); +?> +
+ +
\ No newline at end of file diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml index c7608ec5e..d52ad6b51 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml @@ -6,62 +6,16 @@ $interest = $block->getInterest(); getMessageBefore() ?>
- + setCurrentInterest($i); ?>
- -
    - -
  • - - /> - -
  • - -
- -
- -
- -
    - -
  • - - /> - -
  • - -
- - + getChild($blockName = "mailchimp.checkout.success.".$i['interest']['type'])->setCurrentInterest($i); + echo $this->getChildHtml($blockName, false);?>
@@ -72,7 +26,7 @@ $interest = $block->getInterest(); diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/radio_groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/radio_groups.phtml new file mode 100644 index 000000000..d4c008f76 --- /dev/null +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/radio_groups.phtml @@ -0,0 +1,19 @@ +getCurrentInterest(); +?> +
    + +
  • + + /> + +
  • + +
From 7a53d65efa84d41e66d0d25d8b5b4d7cabc2163e Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 8 Nov 2018 18:02:36 -0300 Subject: [PATCH 026/113] Fix test. --- .../Block/Checkout/Success/GroupsTest.php | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Checkout/Success/GroupsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Checkout/Success/GroupsTest.php index a59a91e34..47f24a2ab 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Checkout/Success/GroupsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Block/Checkout/Success/GroupsTest.php @@ -29,26 +29,6 @@ public function setUp() $this->_block->setLayout($layout); } - public function testRenderGroups() - { - $category = array(); - - $groupsMock = $this->_groupsMock - ->disableOriginalConstructor() - ->setMethods(array('createObject', 'addGroupOptions', 'getElementHtml')) - ->getMock(); - - $objectMock = $this->getMockBuilder(Varien_Data_Form_Element_Checkboxes::class) - ->disableOriginalConstructor() - ->getMock(); - - $groupsMock->expects($this->once())->method('createObject')->with($category)->willReturn($objectMock); - $groupsMock->expects($this->once())->method('addGroupOptions')->with($category, $objectMock); - $groupsMock->expects($this->once())->method('getElementHtml')->with($category, $objectMock); - - $groupsMock->renderGroups($category); - } - public function testGetInterest() { $interest = array(); From 787c68a0ad8c93c9c49034e014b57fa5d3be80bf Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 8 Nov 2018 18:15:29 -0300 Subject: [PATCH 027/113] Remove white spaces. --- .../MailChimp/Block/Checkout/Success/DropdownGroups.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php index 2ffe62f1e..57e8f4144 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php @@ -10,5 +10,5 @@ */ class Ebizmarts_MailChimp_Block_Checkout_Success_DropdownGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups { - + } From 44f8d9fba770c3913e35e13bc2565b179f17326b Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 9 Nov 2018 10:35:38 -0300 Subject: [PATCH 028/113] Add new line. --- .../ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml index 280268ed5..1769b36eb 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/dropdown_groups.phtml @@ -12,4 +12,4 @@ $i = $this->getCurrentInterest(); - \ No newline at end of file + From c8fc54aa65ccbba30dac86ac988d6180477cdc52 Mon Sep 17 00:00:00 2001 From: keller Date: Fri, 9 Nov 2018 16:49:46 -0300 Subject: [PATCH 029/113] #793 Change logic to not rewrite subscribe() method --- .../Ebizmarts/MailChimp/Model/Observer.php | 18 ++++-- .../Ebizmarts/MailChimp/Model/Subscriber.php | 60 ------------------- .../MailChimp/Model/ObserverTest.php | 32 +++++----- 3 files changed, 28 insertions(+), 82 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 4a41e864c..e55b9595a 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -142,16 +142,18 @@ public function handleSubscriber(Varien_Event_Observer $observer) if ($isEnabled) { $statusChanged = $subscriber->getIsStatusChanged(); $apiSubscriber = $this->makeApiSubscriber(); - if($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1){ - //Override Magento status to always send double opt-in confirmation. - if($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId)) { - $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); - $this->addSuccessIfRequired($helper); + + //Override Magento status to always send double opt-in confirmation. + if ($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId)) { + $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); + $this->addSuccessIfRequired($helper); } $subscriber->setImportMode(true); $this->createEmailCookie($subscriber); + if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { + //Use MailChimp emails if ($statusChanged) { $apiSubscriber->updateSubscriber($subscriber, true); } else { @@ -163,6 +165,12 @@ public function handleSubscriber(Varien_Event_Observer $observer) $apiSubscriber->updateSubscriber($subscriber, true); } } + } else { + //Use Magento emails, setImportMode to false to allow email to be sent, set back to true so subscribe() function doesn't send it again + $subscriber->setImportMode(false); + $subscriber->sendConfirmationRequestEmail(); + $subscriber->setImportMode(true); + } } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php index 4ac84fc6c..1e1026e17 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php @@ -36,64 +36,4 @@ public function sendConfirmationSuccessEmail() return parent::sendConfirmationSuccessEmail(); } } - - //Force double-opt in for registered customers if "Need to Confirm" is enabled - public function subscribe($email) - { - if (Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE)) { - $this->loadByEmail($email); - $customerSession = Mage::getSingleton('customer/session'); - - if (!$this->getId()) { - $this->setSubscriberConfirmCode($this->randomSequence()); - } - - $isConfirmNeed = (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1) ? true : false; - $isOwnSubscribes = false; - $ownerId = Mage::getModel('customer/customer') - ->setWebsiteId(Mage::app()->getStore()->getWebsiteId()) - ->loadByEmail($email) - ->getId(); - $isSubscribeOwnEmail = $customerSession->isLoggedIn() && $ownerId == $customerSession->getId(); - - if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED - || $this->getStatus() == self::STATUS_NOT_ACTIVE - ) { - if ($isConfirmNeed === true) { - $this->setStatus(self::STATUS_NOT_ACTIVE); - } else { - $this->setStatus(self::STATUS_SUBSCRIBED); - } - $this->setSubscriberEmail($email); - } - - if ($isSubscribeOwnEmail) { - $this->setStoreId($customerSession->getCustomer()->getStoreId()); - $this->setCustomerId($customerSession->getCustomerId()); - } else { - $this->setStoreId(Mage::app()->getStore()->getId()); - $this->setCustomerId(0); - } - - $this->setIsStatusChanged(true); - - try { - $this->save(); - if ($isConfirmNeed === true - && $isOwnSubscribes === false - ) { - $this->sendConfirmationRequestEmail(); - } else { - $this->sendConfirmationSuccessEmail(); - } - - return $this->getStatus(); - } catch (Exception $e) { - throw new Exception($e->getMessage()); - } - } else { - return parent::subscribe($email); - } - - } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 8908e058f..bcf6b166b 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -657,13 +657,9 @@ public function testAddColumnToSalesOrderGridCollection() public function testHandleSubscriber($data) { $setImportMode = $data['setImportMode']; + $getStoreId = $data['getStoreId']; $magentoMail = $data['magentoMail']; - $addSuccessIfRequired = $data['addSuccessIfRequired']; - $createEmailCookie = $data['createEmailCookie']; - $isSubscriptionConfirmationEnabled = $data['isSubscriptionConfirmationEnabled']; - $getStatus = $data['getStatus']; - $setStatus = $data['setStatus']; - $updateSubscriber = $data['updateSubscriber']; + $updateSubscriber = $data['updateSubscriber']; $storeId = 1; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) @@ -706,24 +702,28 @@ public function testHandleSubscriber($data) $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); - $subscriberMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + $subscriberMock->expects($this->exactly($getStoreId))->method('getStoreId')->willReturn($storeId); $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); $subscriberMock->expects($this->once())->method('getIsStatusChanged')->willReturn(true); - $subscriberMock->expects($this->exactly($getStatus))->method('getStatus')->willReturn(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); + $subscriberMock->expects($this->once())->method('getStatus')->willReturn(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); - $helperMock->expects($this->exactly($isSubscriptionConfirmationEnabled))->method('isSubscriptionConfirmationEnabled')->with($storeId)->willReturn(true); + $helperMock->expects($this->once())->method('isSubscriptionConfirmationEnabled')->with($storeId)->willReturn(true); $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); - $subscriberMock->expects($this->exactly($setStatus))->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); + $subscriberMock->expects($this->once())->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); - $observerMock->expects($this->exactly($addSuccessIfRequired))->method('addSuccessIfRequired')->with($helperMock); + $observerMock->expects($this->once())->method('addSuccessIfRequired')->with($helperMock); $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); - $subscriberMock->expects($this->exactly($setImportMode))->method('setImportMode')->with(true); + $subscriberMock->expects($this->exactly($setImportMode))->method('setImportMode')->withConsecutive( + array(true), + array(false), + array(true) + ); - $observerMock->expects($this->exactly($createEmailCookie))->method('createEmailCookie')->with($subscriberMock); + $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); $apiSubscriberMock->expects($this->exactly($updateSubscriber))->method('updateSubscriber')->with($subscriberMock, true); @@ -733,10 +733,8 @@ public function testHandleSubscriber($data) public function handleSubscriberDataProvider() { return array( - array(array('magentoMail' => 0, 'setImportMode' => 1, 'addSuccessIfRequired' => 1, 'createEmailCookie' => 1, 'isSubscriptionConfirmationEnabled' => 1, 'getStatus' => 1, - 'setStatus' => 1, 'updateSubscriber' => 1)), - array(array('magentoMail' => 1, 'setImportMode' => 0, 'addSuccessIfRequired' => 0, 'createEmailCookie' => 0, 'isSubscriptionConfirmationEnabled' => 0, 'getStatus' => 0, - 'setStatus' => 0, 'updateSubscriber' => 0)) + array(array('magentoMail' => 0, 'setImportMode' => 1, 'updateSubscriber' => 1, 'getStoreId' => 1)), + array(array('magentoMail' => 1, 'setImportMode' => 3, 'updateSubscriber' => 0, 'getStoreId' => 5)) ); } From cd22233fd11d0ffa3aeef6252567e098176342c4 Mon Sep 17 00:00:00 2001 From: keller Date: Fri, 9 Nov 2018 17:10:25 -0300 Subject: [PATCH 030/113] #793 Fix test --- .../tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index bcf6b166b..658d07ae8 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -661,6 +661,7 @@ public function testHandleSubscriber($data) $magentoMail = $data['magentoMail']; $updateSubscriber = $data['updateSubscriber']; $storeId = 1; + $sendConfirmationRequestEmail = $data['sendConfirmationRequestEmail']; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) ->disableOriginalConstructor() @@ -687,7 +688,7 @@ public function testHandleSubscriber($data) $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() ->setMethods(array('getSubscriberSource', 'getStoreId', 'getIsStatusChanged', 'getStatus', 'setStatus', - 'setImportMode', 'getOrigData', 'getSubscriberStatus')) + 'setImportMode', 'getOrigData', 'getSubscriberStatus', 'sendConfirmationRequestEmail')) ->getMock(); $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) @@ -723,6 +724,8 @@ public function testHandleSubscriber($data) array(true) ); + $subscriberMock->expects($this->exactly($sendConfirmationRequestEmail))->method('sendConfirmationRequestEmail')->willReturn($subscriberMock); + $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); $apiSubscriberMock->expects($this->exactly($updateSubscriber))->method('updateSubscriber')->with($subscriberMock, true); @@ -733,8 +736,8 @@ public function testHandleSubscriber($data) public function handleSubscriberDataProvider() { return array( - array(array('magentoMail' => 0, 'setImportMode' => 1, 'updateSubscriber' => 1, 'getStoreId' => 1)), - array(array('magentoMail' => 1, 'setImportMode' => 3, 'updateSubscriber' => 0, 'getStoreId' => 5)) + array(array('magentoMail' => 0, 'setImportMode' => 1, 'updateSubscriber' => 1, 'getStoreId' => 1, 'sendConfirmationRequestEmail' => 0)), + array(array('magentoMail' => 1, 'setImportMode' => 3, 'updateSubscriber' => 0, 'getStoreId' => 1, 'sendConfirmationRequestEmail' => 1)) ); } From e542a72acd2b96f30ee7d1ba71193fcf7cd805a9 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 3 Dec 2018 13:04:30 -0300 Subject: [PATCH 031/113] improve serialization process for saving objects. --- .../Ebizmarts/MailChimp/Helper/Data.php | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index e2158c9f3..a338be67a 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3254,7 +3254,7 @@ public function getInterestGroups($customerId, $subscriberId, $storeId, $interes $interestGroup = $this->getInterestGroupModel(); $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); if ($interestGroup->getId()) { - $groups = unserialize($interestGroup->getGroupdata()); + $groups = $this->arrayDecode($interestGroup->getGroupdata()); foreach ($groups as $key => $value) { if (isset($interest[$key])) { if (is_array($value)) { @@ -3282,6 +3282,29 @@ public function getInterestGroups($customerId, $subscriberId, $storeId, $interes return $interest; } + + /** + * Format array to save in database. + * + * @param $array + * @return string + */ + protected function arrayEncode($array) + { + return json_encode($array); + } + + /** + * Set database encoded array to normal array + * + * @param $encodedArray + * @return mixed + */ + protected function arrayDecode($encodedArray) + { + return json_decode($encodedArray, true); + } + /** * @param $params * @param $storeId @@ -3316,7 +3339,8 @@ public function saveInterestGroupData($params, $storeId, $customerId = null, $su $interestGroup->setCustomerId($customerId); } if ($groups) { - $interestGroup->setGroupdata(serialize($groups)); + $encodedGroups = $this->arrayEncode($groups); + $interestGroup->setGroupdata($encodedGroups); } //Avoid creating a new entry if no groupData available. (Customer creation) if ($interestGroup->getGroupdata()) { From effaab4583cdeac57d2dc07945c724ccc003c979 Mon Sep 17 00:00:00 2001 From: keller Date: Tue, 4 Dec 2018 10:44:16 -0300 Subject: [PATCH 032/113] closes #832 Optimize deletion of processed webhooks --- .../MailChimp/Model/ProcessWebhook.php | 20 ++++++------------- .../Adminhtml/MailchimpController.php | 1 + 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/ProcessWebhook.php b/app/code/community/Ebizmarts/MailChimp/Model/ProcessWebhook.php index 7a3f26f05..a33f31433 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/ProcessWebhook.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/ProcessWebhook.php @@ -263,20 +263,12 @@ protected function _profile(array $data) public function deleteProcessed() { - $to= Mage::app()->getLocale()->date()->sub(30,Zend_Date::DAY); - $to=$to->toString('yyyy-MM-dd'); - - $collection = Mage::getModel('mailchimp/webhookrequest')->getCollection() - ->addFieldToFilter('processed', 1) - ->addFieldToFilter('fired_at', array( - 'lt'=>$to - )); - $collection->getSelect()->limit(self::BATCH_LIMIT); - - foreach ($collection as $row) { - $row->delete(); - } - + $helper = $this->getHelper(); + $resource = $helper->getCoreResource(); + $connection = $resource->getConnection('core_write'); + $tableName = $resource->getTableName('mailchimp/webhookrequest'); + $where = array("fired_at < NOW() - INTERVAL 30 DAY AND processed = 1"); + $connection->delete($tableName, $where); } } diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 8e07cdb7a..b90129ea7 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -47,6 +47,7 @@ public function createWebhookAction() protected function _isAllowed() { + $acl = null; switch ($this->getRequest()->getActionName()) { case 'resendSubscribers': $acl = 'system/config/mailchimp'; From 29834e6f12b5548829128c98e1d0def8e43a1fa8 Mon Sep 17 00:00:00 2001 From: keller Date: Tue, 4 Dec 2018 11:31:41 -0300 Subject: [PATCH 033/113] #824 Run cronjob once per day instead of once per hour --- app/code/community/Ebizmarts/MailChimp/etc/config.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index 215d327ec..fca3c622e 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -431,7 +431,7 @@ - 0 */1 * * * + 0 0 * * * mailchimp/cron::deleteWebhookRequests From 716355251a6cd4a75818e34968608db6fc07509a Mon Sep 17 00:00:00 2001 From: keller Date: Tue, 4 Dec 2018 15:07:04 -0300 Subject: [PATCH 034/113] closes #834 Add option to send unresized product images to Mailchimp --- .../community/Ebizmarts/MailChimp/Helper/Data.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 454f1326f..5f31152a3 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1329,13 +1329,22 @@ public function getImageUrlById($productId, $magentoStoreId) } else { $curStore = $this->getCurrentStoreId(); $this->setCurrentStore($magentoStoreId); - $upperCaseImage = $this->getImageFunctionName($imageSize); - $imageUrl = $productModel->$upperCaseImage(); + if ($configImageSize == 3){ + $imageUrl = $this->getImageUrl($productModel, $imageSize); + } else { + $upperCaseImage = $this->getImageFunctionName($imageSize); + $imageUrl = $productModel->$upperCaseImage(); + } $this->setCurrentStore($curStore); } return $imageUrl; } + public function getImageUrl($productModel, $imageSize) + { + return (string)$this->_getImageHelper()->init($productModel, $imageSize); + } + /** * Returns imageSize converted to camel case, and concatenates with functionName * From a0ec838b912f5048271444a00fa98df0239717ae Mon Sep 17 00:00:00 2001 From: keller Date: Tue, 4 Dec 2018 15:48:46 -0300 Subject: [PATCH 035/113] #834 Add option to the backend --- app/code/community/Ebizmarts/MailChimp/Helper/Data.php | 3 +++ .../MailChimp/Model/System/Config/Source/ImageSize.php | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 5f31152a3..d70c58fab 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1316,6 +1316,9 @@ public function getImageUrlById($productId, $magentoStoreId) case 2: $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_THUMBNAIL; break; + case 3: + $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_DEFAULT; + break; default: $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_DEFAULT; break; diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/ImageSize.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/ImageSize.php index b1373fef4..98da5f791 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/ImageSize.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/ImageSize.php @@ -12,6 +12,7 @@ class Ebizmarts_MailChimp_Model_System_Config_Source_ImageSize const BASE = 0; const SMALL = 1; const THUMBNAIL = 2; + const ORIGINAL = 3; /** @@ -25,8 +26,8 @@ public function toOptionArray() return array( array('value' => self::BASE, 'label' => $helper->__('Base')), array('value' => self::SMALL, 'label' => $helper->__('Small')), - array('value' => self::THUMBNAIL, 'label' => $helper->__('Thumbnail')) - + array('value' => self::THUMBNAIL, 'label' => $helper->__('Thumbnail')), + array('value' => self::ORIGINAL, 'label' => $helper->__('Original')) ); } } From 7e66354516b54a6a66a64e9e0770b8557b5feb5b Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 4 Dec 2018 17:24:33 -0300 Subject: [PATCH 036/113] Add created at field for groups table. --- ...de-1.1.12.3-1.1.13.php => mysql4-upgrade-1.1.12.4-1.1.13.php} | 1 + 1 file changed, 1 insertion(+) rename app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/{mysql4-upgrade-1.1.12.3-1.1.13.php => mysql4-upgrade-1.1.12.4-1.1.13.php} (90%) diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.3-1.1.13.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.4-1.1.13.php similarity index 90% rename from app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.3-1.1.13.php rename to app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.4-1.1.13.php index 33e0f64f3..0e27c955e 100755 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.3-1.1.13.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.4-1.1.13.php @@ -9,6 +9,7 @@ `customer_id` INT(10) DEFAULT NULL, `subscriber_id` INT(10) DEFAULT NULL, `store_id` SMALLINT (5) NOT NULL DEFAULT 0, + `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP, `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP, `groupdata` TEXT(4096) NOT NULL, PRIMARY KEY (`id`) From 19874cea216c2322f24e0e5c5c6745414e1f3919 Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 4 Dec 2018 17:25:14 -0300 Subject: [PATCH 037/113] Reduce repeated code. --- .../Block/Checkout/Success/CheckboxGroups.php | 14 ----- .../Block/Checkout/Success/DropdownGroups.php | 14 ----- .../Block/Checkout/Success/Groups.php | 13 ----- .../Block/Checkout/Success/RadioGroups.php | 14 ----- .../Block/Checkout/Success/SelectGroups.php | 14 ----- .../Ebizmarts/MailChimp/Block/Group/Type.php | 54 +++++++++++++++++ .../Ebizmarts/MailChimp/Helper/Data.php | 27 +++++---- .../default/layout/ebizmarts/mailchimp.xml | 5 +- .../mailchimp/checkout/success/groups.phtml | 6 +- .../mailchimp/customer/newsletter/index.phtml | 58 +------------------ .../type/checkboxes.phtml} | 4 +- .../type/dropdown.phtml} | 0 .../type/radio.phtml} | 0 13 files changed, 78 insertions(+), 145 deletions(-) delete mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/CheckboxGroups.php delete mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php delete mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/RadioGroups.php delete mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/SelectGroups.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php rename app/design/frontend/base/default/template/ebizmarts/mailchimp/{checkout/success/checkbox_groups.phtml => group/type/checkboxes.phtml} (91%) rename app/design/frontend/base/default/template/ebizmarts/mailchimp/{checkout/success/dropdown_groups.phtml => group/type/dropdown.phtml} (100%) rename app/design/frontend/base/default/template/ebizmarts/mailchimp/{checkout/success/radio_groups.phtml => group/type/radio.phtml} (100%) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/CheckboxGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/CheckboxGroups.php deleted file mode 100644 index cf43d3741..000000000 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/CheckboxGroups.php +++ /dev/null @@ -1,14 +0,0 @@ - - * @license http://opensource.org/licenses/osl-3.0.php - */ -class Ebizmarts_MailChimp_Block_Checkout_Success_CheckboxGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups -{ - -} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php deleted file mode 100644 index 57e8f4144..000000000 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/DropdownGroups.php +++ /dev/null @@ -1,14 +0,0 @@ - - * @license http://opensource.org/licenses/osl-3.0.php - */ -class Ebizmarts_MailChimp_Block_Checkout_Success_DropdownGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups -{ - -} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index 7c8d8638a..a219c71c2 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -82,17 +82,4 @@ protected function getMailChimpHelper() { return $this->helper; } - - /** - * @return mixed - */ - protected function getCurrentInterest() - { - return $this->_currentIntesrest; - } - - protected function setCurrentInterest($interest) - { - $this->_currentIntesrest = $interest; - } } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/RadioGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/RadioGroups.php deleted file mode 100644 index f6d8b0901..000000000 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/RadioGroups.php +++ /dev/null @@ -1,14 +0,0 @@ - - * @license http://opensource.org/licenses/osl-3.0.php - */ -class Ebizmarts_MailChimp_Block_Checkout_Success_RadioGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups -{ - -} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/SelectGroups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/SelectGroups.php deleted file mode 100644 index 4c9661204..000000000 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/SelectGroups.php +++ /dev/null @@ -1,14 +0,0 @@ - - * @license http://opensource.org/licenses/osl-3.0.php - */ -class Ebizmarts_MailChimp_Block_Checkout_Success_SelectGroups extends Ebizmarts_MailChimp_Block_Checkout_Success_Groups -{ - -} diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php b/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php new file mode 100644 index 000000000..14a046660 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php @@ -0,0 +1,54 @@ + + * @license http://opensource.org/licenses/osl-3.0.php + */ +class Ebizmarts_MailChimp_Block_Group_Type extends Mage_Core_Block_Template +{ + protected $_currentInterest; + + public function __construct(array $args = array()) + { + Mage::log(__METHOD__, null, 'ebizmarts.log', true); + Mage::log($args, null, 'ebizmarts.log', true); + if (isset($args['interests'])) { + $this->_currentInterest = $interests = $args['interests']; + $type = $interests['interest']['type']; + $this->setTemplate("ebizmarts/mailchimp/group/type/$type.phtml"); + } + parent::__construct($args); + } + +// protected function _prepareLayout() +// { +// $interests = $this->getCurrentInterest(); +// if ($interests !== null) { +// +// $typeName = $interests['interest']['type'] . 'Groups'; +// $this->setChild($type, $this->getLayout()->createBlock("mailchimp/group_type_$typeName", "mailchimp.group.type.$typeName", array('interests' => $interests))); +// } +// return parent::_prepareLayout(); +// } + +// public function setCurrentInterest($i) +// { +// Mage::log(__METHOD__, null, 'ebizmarts.log', true); +// $this->setTemplate('mailchimp/group/type/'.$i.'_groups.phtml'); +// return $this->_currentInterest = $i; +// } + + /** + * @return mixed + */ + protected function getCurrentInterest() + { + Mage::log(__METHOD__, null, 'ebizmarts.log', true); + Mage::log($this->_currentInterest, null, 'ebizmarts.log', true); + return $this->_currentInterest; + } +} diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index a338be67a..11cf7ed48 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3220,21 +3220,24 @@ public function getInterest($storeId) } $api = $this->getApi($storeId); $listId = $this->getGeneralList($storeId); - $apiInterestCategory = $api->getLists()->getInterestCategory(); - $allInterest = $apiInterestCategory->getAll($listId); - foreach ($allInterest['categories'] as $item) { - if (in_array($item['id'], $interest)) { - $rc[$item['id']]['interest'] = array('id' => $item['id'], 'title' => $item['title'], 'type' => $item['type']); + try { + $apiInterestCategory = $api->getLists()->getInterestCategory(); + $allInterest = $apiInterestCategory->getAll($listId); + foreach ($allInterest['categories'] as $item) { + if (in_array($item['id'], $interest)) { + $rc[$item['id']]['interest'] = array('id' => $item['id'], 'title' => $item['title'], 'type' => $item['type']); + } } - } - $apiInterestCategoryInterest = $apiInterestCategory->getInterests(); - foreach ($interest as $interestId) { - $mailchimpInterest = $apiInterestCategoryInterest->getAll($listId, $interestId); - foreach ($mailchimpInterest['interests'] as $mi) { - $rc[$mi['category_id']]['category'][$mi['display_order']] = array('id' => $mi['id'], 'name' => $mi['name'], 'checked' => false); + $apiInterestCategoryInterest = $apiInterestCategory->getInterests(); + foreach ($interest as $interestId) { + $mailchimpInterest = $apiInterestCategoryInterest->getAll($listId, $interestId); + foreach ($mailchimpInterest['interests'] as $mi) { + $rc[$mi['category_id']]['category'][$mi['display_order']] = array('id' => $mi['id'], 'name' => $mi['name'], 'checked' => false); + } } + } catch (MailChimp_Error $e) { + $this->logError($e->getFriendlyMessage()); } - return $rc; } diff --git a/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml b/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml index 489962854..a4c5d768e 100755 --- a/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml +++ b/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml @@ -40,6 +40,7 @@ + @@ -56,9 +57,7 @@ - - - + diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml index d52ad6b51..a3d013886 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml @@ -6,16 +6,14 @@ $interest = $block->getInterest(); getMessageBefore() ?>
- setCurrentInterest($i); ?> +
- getChild($blockName = "mailchimp.checkout.success.".$i['interest']['type'])->setCurrentInterest($i); - echo $this->getChildHtml($blockName, false);?> + getLayout()->createBlock('mailchimp/group_type', 'mailchimp.group.type', array('interests' => $i))->toHtml(); ?>
diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/customer/newsletter/index.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/customer/newsletter/index.phtml index 19db2efff..cc6068e99 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/customer/newsletter/index.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/customer/newsletter/index.phtml @@ -5,64 +5,12 @@ $interest = $block->getInterest();
    -
  • +
  • - -
      - -
    • - - /> - -
    • - -
    - -
    - - -
    - -
      - -
    • - - /> - -
    • - -
    - - -
- + getLayout()->createBlock('mailchimp/group_type', 'mailchimp.group.type', array('interests' => $i))->toHtml(); ?> - diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/checkbox_groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/checkboxes.phtml similarity index 91% rename from app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/checkbox_groups.phtml rename to app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/checkboxes.phtml index 44e17f6a5..15bb9a80a 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/checkbox_groups.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/checkboxes.phtml @@ -4,8 +4,8 @@ $i = $this->getCurrentInterest(); ?>
    -
  • - + Date: Wed, 5 Dec 2018 11:28:22 -0300 Subject: [PATCH 038/113] #834 Unit tests --- .../Ebizmarts/MailChimp/Helper/DataTest.php | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php index 3e3ce770f..9dedbbae7 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -373,22 +373,32 @@ public function testCreateWebhookIfRequired() $helperMock->createWebhookIfRequired($scopeId, $scope); } - public function testGetImageUrlById() + /** + * @param array $data + * @dataProvider testGetImageUrlByIdDataProvider + */ + + public function testGetImageUrlById($data) { $productId = 1; $magentoStoreId = 1; $defaultStoreId = 0; - $imageSize = 'image'; - $upperCaseImage = 'getImageUrl'; + $imageSize = $data['imageSize']; + $upperCaseImage = $data['method']; + $imageUrl = $data['imageUrl']; + $configImageSize = $data['configImageSize']; + $getImageFunctionName = $data['getImageFunctionName']; + $upperCaseImageTimes = $data['methodTimes']; + $localGetImageUrl = $data['localGetImageUrl']; $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('getProductResourceModel', 'getProductModel', 'getImageSize', 'getCurrentStoreId', 'setCurrentStore', 'getImageFunctionName')) + ->setMethods(array('getProductResourceModel', 'getProductModel', 'getImageSize', 'getCurrentStoreId', 'setCurrentStore', 'getImageFunctionName', 'getImageUrl')) ->getMock(); $productModelMock = $this->getMockBuilder(Mage_Catalog_Model_Product::class) ->disableOriginalConstructor() - ->setMethods(array('setData', 'getImageUrl')) + ->setMethods(array('setData', 'getImageUrl', 'getThumbnailUrl', 'getSmallImageUrl')) ->getMock(); $productResourceModelMock = $this->getMockBuilder(Mage_Catalog_Model_Resource_Product::class) @@ -402,22 +412,40 @@ public function testGetImageUrlById() $helperMock->expects($this->once())->method('getProductResourceModel')->willReturn($productResourceModelMock); $helperMock->expects($this->once())->method('getProductModel')->willReturn($productModelMock); - $helperMock->expects($this->once())->method('getImageSize')->with($magentoStoreId)->willReturn($imageSize); + $helperMock->expects($this->once())->method('getImageSize')->with($magentoStoreId)->willReturn($configImageSize); + $helperMock->expects($this->exactly($localGetImageUrl))->method('getImageUrl')->with($productModelMock, $imageSize)->willReturn($imageUrl); $productResourceModelMock->expects($this->once())->method('getAttributeRawValue')->with($productId, $imageSize, $magentoStoreId)->willReturn($imageModelMock); $productModelMock->expects($this->once())->method('setData')->with($imageSize, $imageModelMock); - $productModelMock->expects($this->once())->method('getImageUrl')->willReturn('ImageUrl'); - + $productModelMock->expects($this->exactly($upperCaseImageTimes))->method($upperCaseImage)->willReturn($imageUrl); $helperMock->expects($this->once())->method('getCurrentStoreId')->willReturn($defaultStoreId); $helperMock->expects($this->exactly(2))->method('setCurrentStore')->withConsecutive(array($magentoStoreId), array($defaultStoreId)); - $helperMock->expects($this->once())->method('getImageFunctionName')->with($imageSize)->willReturn($upperCaseImage); + $helperMock->expects($this->exactly($getImageFunctionName))->method('getImageFunctionName')->with($imageSize)->willReturn($upperCaseImage); $return = $helperMock->getImageUrlById($productId, $magentoStoreId); - $this->assertEquals($return, 'ImageUrl'); + $this->assertEquals($return, $imageUrl); + } + + public function testGetImageUrlByIdDataProvider() + { + return array( + array(array('imageSize' => 'image', 'method' => 'getImageUrl', 'configImageSize' => 0, + 'getImageFunctionName' => 1, 'imageUrl' => 'ImageUrl', 'getImageUrl', 'methodTimes' => 1, + 'localGetImageUrl' => 0)), + array(array('imageSize' => 'small_image', 'method' => 'getSmallImageUrl', 'configImageSize' => 1, + 'getImageFunctionName' => 1, 'imageUrl' => 'SmallImageUrl', 'methodTimes' => 1, + 'localGetImageUrl' => 0)), + array(array('imageSize' => 'thumbnail', 'method' => 'getThumbnailUrl', 'configImageSize' => 2, + 'getImageFunctionName' => 1, 'imageUrl' => 'ThumbnailUrl', 'methodTimes' => 1, + 'localGetImageUrl' => 0)), + array(array('imageSize' => 'image', 'method' => 'getImageUrl', 'configImageSize' => 3, + 'getImageFunctionName' => 0, 'imageUrl' => 'ImageUrl', 'methodTimes' => 0, + 'localGetImageUrl' => 1)) + ); } public function testGetImageFunctionName() From 547df131acca63e55234d46f5bb11b318c93afbd Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Thu, 13 Dec 2018 16:21:53 -0300 Subject: [PATCH 039/113] from delete to post - Ebizmarts_MailChimp_Model_Api_Carts - closes #836 --- .../community/Ebizmarts/MailChimp/Model/Api/Carts.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php index ebd770c9a..e3db8e444 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -133,11 +133,6 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) $modifiedCarts->getSelect()->limit($this->getBatchLimitFromConfig()); foreach ($modifiedCarts as $cart) { $cartId = $cart->getEntityId(); - $allCarts[$this->_counter]['method'] = 'DELETE'; - $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $cartId; - $allCarts[$this->_counter]['operation_id'] = $this->_batchId . '_' . $cartId; - $allCarts[$this->_counter]['body'] = ''; - $this->_counter += 1; /** * @var $customer Mage_Customer_Model_Customer */ @@ -172,8 +167,8 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) $cartJson = $this->_makeCart($cart, $mailchimpStoreId, $magentoStoreId, true); if ($cartJson != "") { - $allCarts[$this->_counter]['method'] = 'POST'; - $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts'; + $allCarts[$this->_counter]['method'] = 'PATCH'; + $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts'.$cartId; $allCarts[$this->_counter]['operation_id'] = $this->_batchId . '_' . $cartId; $allCarts[$this->_counter]['body'] = $cartJson; $this->_counter += 1; From 9b20a98b56f5c990d70973bb2b5802aed04031f5 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 17 Dec 2018 13:29:52 -0300 Subject: [PATCH 040/113] Fixed html before and after for interest groups subscription. --- app/code/community/Ebizmarts/MailChimp/etc/system.xml | 4 ++-- .../default/default/ebizmarts/mailchimp/css/mailchimp.css | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/etc/system.xml b/app/code/community/Ebizmarts/MailChimp/etc/system.xml index 2b0fd8b1e..0c24439de 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/system.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/system.xml @@ -149,7 +149,7 @@ 1 1 - 1 + 1 @@ -161,7 +161,7 @@ 1 1 - 1 + 1 diff --git a/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css b/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css index 235d74f99..f685df7df 100644 --- a/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css +++ b/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css @@ -102,3 +102,7 @@ div.mailchimp-notice { .mailchimp-image { margin: 0 0 4px 10px; } + +textarea#mailchimp_general_interest_before, textarea#mailchimp_general_interest_after { + height: 5em; +} \ No newline at end of file From 9f0786b381ff3c1422595761d6ab1cb39f4f8e55 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 17 Dec 2018 13:40:45 -0300 Subject: [PATCH 041/113] Fixed configuration order. --- app/code/community/Ebizmarts/MailChimp/etc/system.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/etc/system.xml b/app/code/community/Ebizmarts/MailChimp/etc/system.xml index 0c24439de..abf63d04f 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/system.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/system.xml @@ -93,7 +93,7 @@ button mailchimp/adminhtml_system_config_resendSubscribers - 46 + 44 1 1 1 @@ -103,7 +103,7 @@ select mailchimp/system_config_source_list mailchimp/system_config_backend_list - 44 + 45 1 1 1 From c1a4ae5d423a3349acb8c3f6f5ae49635d9718a3 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 17 Dec 2018 14:04:06 -0300 Subject: [PATCH 042/113] Fix test. --- .../Ebizmarts/MailChimp/Helper/DataTest.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php index 19255e72b..5a3bf7ba4 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -1226,14 +1226,17 @@ public function testSaveInterestGroupData() $origCustomerId = 1; $origSubscriberId = 1; $storeId = 1; - $groupData = 'a:2:{s:10:"bc15dbe6a5";a:1:{s:10:"d6b7541ee7";s:10:"d6b7541ee7";}s:10:"2a2f23d671";s:10:"36c250eeff";}'; - $unserializedGroupData = unserialize($groupData); + $encodedGroupData = '{"bc15dbe6a5":{"d6b7541ee7":"d6b7541ee7"},"2a2f23d671":"36c250eeff"}'; + $groupData = array( + 'bc15dbe6a5' => array('d6b7541ee7' => 'd6b7541ee7'), + '2a2f23d671' => '36c250eeff' + ); $currentDateTime = '2018-07-26 12:43:40'; $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() ->setMethods(array('getInterestGroupsIfAvailable', 'isAdmin', 'getCustomerSession', 'getInterestGroupModel', - 'getCurrentDateTime')) + 'getCurrentDateTime', 'arrayEncode')) ->getMock(); $interestGroupMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Interestgroup::class) @@ -1257,7 +1260,7 @@ public function testSaveInterestGroupData() ->setMethods(array('getId')) ->getMock(); - $helperMock->expects($this->once())->method('getInterestGroupsIfAvailable')->with($params)->willReturn($unserializedGroupData); + $helperMock->expects($this->once())->method('getInterestGroupsIfAvailable')->with($params)->willReturn($groupData); $helperMock->expects($this->once())->method('getCustomerSession')->willReturn($customerSessionMock); $helperMock->expects($this->once())->method('isAdmin')->willReturn(false); @@ -1275,8 +1278,11 @@ public function testSaveInterestGroupData() $interestGroupMock->expects($this->once())->method('getCustomerId')->willReturn($origCustomerId); $interestGroupMock->expects($this->once())->method('setSubscriberId')->with($subscriberId)->willReturnSelf(); $interestGroupMock->expects($this->once())->method('setCustomerId')->with($customerId)->willReturnSelf(); - $interestGroupMock->expects($this->once())->method('setGroupdata')->with($groupData)->willReturnSelf(); - $interestGroupMock->expects($this->once())->method('getGroupdata')->willReturn($groupData); + + $helperMock->expects($this->once())->method('arrayEncode')->with($groupData)->willReturn($encodedGroupData); + + $interestGroupMock->expects($this->once())->method('setGroupdata')->with($encodedGroupData)->willReturnSelf(); + $interestGroupMock->expects($this->once())->method('getGroupdata')->willReturn($encodedGroupData); $interestGroupMock->expects($this->once())->method('setStoreId')->with($storeId)->willReturnSelf(); $helperMock->expects($this->once())->method('getCurrentDateTime')->willReturn($currentDateTime); From 216601891eb535fb961e1f1d96cc9dc5e4b6aec5 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 17 Dec 2018 14:13:19 -0300 Subject: [PATCH 043/113] Add new line. --- .../default/default/ebizmarts/mailchimp/css/mailchimp.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css b/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css index f685df7df..151eab694 100644 --- a/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css +++ b/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css @@ -105,4 +105,4 @@ div.mailchimp-notice { textarea#mailchimp_general_interest_before, textarea#mailchimp_general_interest_after { height: 5em; -} \ No newline at end of file +} From 89e78e29aad69e0b4e1d5267f4c43e775671fac8 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 17 Dec 2018 14:57:59 -0300 Subject: [PATCH 044/113] Improved display of groups in checkout. --- .../Block/Checkout/Success/Groups.php | 6 +++-- .../Ebizmarts/MailChimp/Block/Group/Type.php | 22 ------------------- .../Ebizmarts/MailChimp/etc/system.xml | 8 +++---- .../mailchimp/checkout/success/groups.phtml | 6 +++-- .../mailchimp/group/type/checkboxes.phtml | 2 +- .../mailchimp/group/type/radio.phtml | 2 +- .../ebizmarts/mailchimp/css/mailchimp.css | 2 +- 7 files changed, 15 insertions(+), 33 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index a219c71c2..4f083e69c 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -50,13 +50,15 @@ public function getInterest() public function getMessageBefore() { $storeId = $this->storeId; - return $this->getMailChimpHelper()->getCheckoutSuccessHtmlBefore($storeId); + $message = $this->getMailChimpHelper()->getCheckoutSuccessHtmlBefore($storeId); + return $message; } public function getMessageAfter() { $storeId = $this->storeId; - return $this->getMailChimpHelper()->getCheckoutSuccessHtmlAfter($storeId); + $message = $this->getMailChimpHelper()->getCheckoutSuccessHtmlAfter($storeId); + return $message; } /** diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php b/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php index 14a046660..74d746783 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php @@ -14,8 +14,6 @@ class Ebizmarts_MailChimp_Block_Group_Type extends Mage_Core_Block_Template public function __construct(array $args = array()) { - Mage::log(__METHOD__, null, 'ebizmarts.log', true); - Mage::log($args, null, 'ebizmarts.log', true); if (isset($args['interests'])) { $this->_currentInterest = $interests = $args['interests']; $type = $interests['interest']['type']; @@ -24,31 +22,11 @@ public function __construct(array $args = array()) parent::__construct($args); } -// protected function _prepareLayout() -// { -// $interests = $this->getCurrentInterest(); -// if ($interests !== null) { -// -// $typeName = $interests['interest']['type'] . 'Groups'; -// $this->setChild($type, $this->getLayout()->createBlock("mailchimp/group_type_$typeName", "mailchimp.group.type.$typeName", array('interests' => $interests))); -// } -// return parent::_prepareLayout(); -// } - -// public function setCurrentInterest($i) -// { -// Mage::log(__METHOD__, null, 'ebizmarts.log', true); -// $this->setTemplate('mailchimp/group/type/'.$i.'_groups.phtml'); -// return $this->_currentInterest = $i; -// } - /** * @return mixed */ protected function getCurrentInterest() { - Mage::log(__METHOD__, null, 'ebizmarts.log', true); - Mage::log($this->_currentInterest, null, 'ebizmarts.log', true); return $this->_currentInterest; } } diff --git a/app/code/community/Ebizmarts/MailChimp/etc/system.xml b/app/code/community/Ebizmarts/MailChimp/etc/system.xml index abf63d04f..0197a05db 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/system.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/system.xml @@ -140,7 +140,7 @@ 1 1 - + textarea 50 @@ -151,8 +151,8 @@ 1 - - + + textarea 51 @@ -163,7 +163,7 @@ 1 - + select diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml index a3d013886..d33935b47 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/success/groups.phtml @@ -22,8 +22,10 @@ $interest = $block->getInterest();
    diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/checkboxes.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/checkboxes.phtml index 15bb9a80a..9c0bd1924 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/checkboxes.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/checkboxes.phtml @@ -13,7 +13,7 @@ $i = $this->getCurrentInterest(); title="" /> - +
diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/radio.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/radio.phtml index d4c008f76..a01dd058e 100644 --- a/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/radio.phtml +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/group/type/radio.phtml @@ -13,7 +13,7 @@ $i = $this->getCurrentInterest(); title="" /> - + diff --git a/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css b/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css index 151eab694..9c8794e7a 100644 --- a/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css +++ b/skin/adminhtml/default/default/ebizmarts/mailchimp/css/mailchimp.css @@ -103,6 +103,6 @@ div.mailchimp-notice { margin: 0 0 4px 10px; } -textarea#mailchimp_general_interest_before, textarea#mailchimp_general_interest_after { +textarea#mailchimp_general_interest_success_before, textarea#mailchimp_general_interest_success_after { height: 5em; } From 8c2620a903ca381f703638e116af01bb619d81b5 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 17 Dec 2018 15:19:15 -0300 Subject: [PATCH 045/113] Fix Tests. --- .../Ebizmarts/MailChimp/Helper/Data.php | 4 ++-- .../MailChimp/controllers/GroupController.php | 6 ++++-- .../Ebizmarts/MailChimp/Helper/DataTest.php | 12 +++++++++--- .../controllers/GroupControllerTest.php | 18 +++++++++++++++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 741ea39bf..13d16a7db 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3304,7 +3304,7 @@ public function getInterestGroups($customerId, $subscriberId, $storeId, $interes * @param $array * @return string */ - protected function arrayEncode($array) + public function arrayEncode($array) { return json_encode($array); } @@ -3315,7 +3315,7 @@ protected function arrayEncode($array) * @param $encodedArray * @return mixed */ - protected function arrayDecode($encodedArray) + public function arrayDecode($encodedArray) { return json_decode($encodedArray, true); } diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php index 0071bf8b7..c6a67acdc 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/GroupController.php @@ -16,6 +16,7 @@ class Ebizmarts_MailChimp_GroupController extends Mage_Core_Controller_Front_Act { public function indexAction() { + $helper = $this->getHelper(); $order = $this->getSessionLastRealOrder(); $session = $this->getCoreSession(); $interestGroup = $this->getInterestGroupModel(); @@ -34,7 +35,8 @@ public function indexAction() } $subscriberId = $subscriber->getSubscriberId(); $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); - $interestGroup->setGroupdata(serialize($params)); + $encodedGroups = $helper->arrayEncode($params); + $interestGroup->setGroupdata($encodedGroups); $interestGroup->setSubscriberId($subscriberId); $interestGroup->setCustomerId($customerId); $interestGroup->setStoreId($storeId); @@ -45,7 +47,7 @@ public function indexAction() $session->addSuccess($this->__('Thanks for share your interest with us.')); } catch (Exception $e) { - $this->getHelper()->logError($e->getMessage()); + $helper->logError($e->getMessage()); $session->addWarning($this->__('Something went wrong with the interests subscription. Please go to the account subscription menu to subscriber to the interests successfully.')); } $this->_redirect('/'); diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php index 5a3bf7ba4..be2f3164f 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -1193,12 +1193,16 @@ public function testGetInterestGroups() $interestNameTwo = 'Group Two Name'; $displayOrderTwo = 2; $interest = array(1 => array('category' => array($displayOrderOne => array('id' => $interestIdOne, 'name' => $interestNameOne, 'checked' => false), $displayOrderTwo => array('id' => $interestIdTwo, 'name' => $interestNameTwo, 'checked' => false)))); - $groupData = 'a:2:{s:10:"bc15dbe6a5";a:1:{s:10:"d6b7541ee7";s:10:"d6b7541ee7";}s:10:"2a2f23d671";s:10:"36c250eeff";}'; + $encodedGroupData = '{"bc15dbe6a5":{"d6b7541ee7":"d6b7541ee7"},"2a2f23d671":"36c250eeff"}'; + $groupData = array( + 'bc15dbe6a5' => array('d6b7541ee7' => 'd6b7541ee7'), + '2a2f23d671' => '36c250eeff' + ); $expectedResult = $interest; $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('getInterest', 'getInterestGroupModel', 'getLocalInterestCategories')) + ->setMethods(array('getInterest', 'getInterestGroupModel', 'getLocalInterestCategories', 'arrayDecode')) ->getMock(); $interestGroupMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Interestgroup::class) @@ -1211,7 +1215,9 @@ public function testGetInterestGroups() $interestGroupMock->expects($this->once())->method('getByRelatedIdStoreId')->with($customerId, $subscriberId, $storeId)->willReturnSelf(); $interestGroupMock->expects($this->once())->method('getId')->willReturn($interestGroupId); - $interestGroupMock->expects($this->once())->method('getGroupdata')->willReturn($groupData); + $interestGroupMock->expects($this->once())->method('getGroupdata')->willReturn($encodedGroupData); + + $helperMock->expects($this->once())->method('arrayDecode')->with($encodedGroupData)->willReturn($groupData); $result = $helperMock->getInterestGroups($customerId, $subscriberId, $storeId); diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/GroupControllerTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/GroupControllerTest.php index dfa989fa6..1e9f017d9 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/GroupControllerTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/controllers/GroupControllerTest.php @@ -28,8 +28,11 @@ public function testIndexAction() $customerEmail = 'customer@email.com'; $customerFirstName = "First Name"; $customerLastName = "Last Name"; - $groupData = 'a:2:{s:10:"bc15dbe6a5";a:1:{s:10:"d6b7541ee7";s:10:"d6b7541ee7";}s:10:"2a2f23d671";s:10:"36c250eeff";}'; - $params = unserialize($groupData); + $encodedGroupData = '{"bc15dbe6a5":{"d6b7541ee7":"d6b7541ee7"},"2a2f23d671":"36c250eeff"}'; + $params = array( + 'bc15dbe6a5' => array('d6b7541ee7' => 'd6b7541ee7'), + '2a2f23d671' => '36c250eeff' + ); $currentDateTime = '2018-07-26 12:43:40'; $successMessage = 'Thanks for share your interest with us.'; @@ -72,10 +75,16 @@ public function testIndexAction() ->setMethods(array('update')) ->getMock(); + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('arrayEncode')) + ->getMock(); + $groupControllerMock->expects($this->once())->method('getSessionLastRealOrder')->willReturn($orderMock); $groupControllerMock->expects($this->once())->method('getCoreSession')->willReturn($coreSessionMock); $groupControllerMock->expects($this->once())->method('getInterestGroupModel')->willReturn($interestGroupMock); $groupControllerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); + $groupControllerMock->expects($this->once())->method('getHelper')->willReturn($helperMock); $requestMock->expects($this->once())->method('getParams')->willReturn($params); @@ -97,7 +106,10 @@ public function testIndexAction() $subscriberMock->expects($this->once())->method('subscribe')->willReturnSelf(); $interestGroupMock->expects($this->once())->method('getByRelatedIdStoreId')->with($customerId, $subscriberId, $storeId)->willReturnSelf(); - $interestGroupMock->expects($this->once())->method('setGroupdata')->with($groupData)->willReturnSelf(); + + $helperMock->expects($this->once())->method('arrayEncode')->with($params)->willReturn($encodedGroupData); + + $interestGroupMock->expects($this->once())->method('setGroupdata')->with($encodedGroupData)->willReturnSelf(); $interestGroupMock->expects($this->once())->method('setSubscriberId')->with($subscriberId)->willReturnSelf(); $interestGroupMock->expects($this->once())->method('setCustomerId')->with($customerId)->willReturnSelf(); $interestGroupMock->expects($this->once())->method('setStoreId')->with($storeId)->willReturnSelf(); From 252cce610055f6facdd80218431761e187fbced3 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 20 Dec 2018 16:51:56 -0300 Subject: [PATCH 046/113] Minor fixes. --- .../Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php | 2 +- .../Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php | 2 +- app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php | 2 +- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 4 ++-- .../template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index 4f083e69c..3053d292e 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -1,7 +1,7 @@ getEvent()->getSubscriber(); $storeId = $subscriber->getStoreId(); @@ -161,7 +161,7 @@ public function SubscriberSaveBefore(Varien_Event_Observer $observer) * @param Varien_Event_Observer $observer * @throws Mage_Core_Model_Store_Exception */ - public function SubscriberSaveAfter(Varien_Event_Observer $observer) + public function subscriberSaveAfter(Varien_Event_Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); $storeId = $subscriber->getStoreId(); diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml index 71826ddf3..4a08493c4 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml @@ -3,7 +3,7 @@ $interest = $this->getInterest(); ?>
- __('MailChimp Information') ?> + __('Mailchimp Information') ?>
From b0a4d32d9459142e2449ce0c129c06f8c1a432f1 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 20 Dec 2018 17:01:06 -0300 Subject: [PATCH 047/113] Bump version. --- app/code/community/Ebizmarts/MailChimp/etc/config.xml | 4 ++-- ...e-1.1.12.4-1.1.13.php => mysql4-upgrade-1.1.13-1.1.14.php} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/{mysql4-upgrade-1.1.12.4-1.1.13.php => mysql4-upgrade-1.1.13-1.1.14.php} (100%) diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index 2f7fe0d23..e5cc4bbfd 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -3,10 +3,10 @@ - 1.1.13 + 1.1.14 - 1.1.13 + 1.1.14 diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.4-1.1.13.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.13-1.1.14.php similarity index 100% rename from app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.12.4-1.1.13.php rename to app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.13-1.1.14.php From 12cad14385c1eab321246201f37b2dc914fc8f62 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 20 Dec 2018 17:02:40 -0300 Subject: [PATCH 048/113] remove capital letter. --- app/code/community/Ebizmarts/MailChimp/etc/config.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index e5cc4bbfd..95a0d3360 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -31,7 +31,7 @@ mailchimp/observer - SubscriberSaveBefore + subscriberSaveBefore @@ -39,7 +39,7 @@ mailchimp/observer - SubscriberSaveAfter + subscriberSaveAfter From 50f4a5ad6558d48789b4f3734d43cea4cd27beff Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 20 Dec 2018 17:56:39 -0300 Subject: [PATCH 049/113] Check if mailchimp is enabled before loading customer groups. --- .../MailChimp/Model/System/Config/Source/CustomerGroup.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php index 3755e1603..7744760b6 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/CustomerGroup.php @@ -18,7 +18,9 @@ public function __construct() { $helper = $this->makeHelper(); $scopeArray = $helper->getCurrentScope(); - $this->_categories = $helper->getListInterestCategories($scopeArray['scope_id'], $scopeArray['scope']); + if ($helper->isSubscriptionEnabled($scopeArray['scope_id'], $scopeArray['scope'])) { + $this->_categories = $helper->getListInterestCategories($scopeArray['scope_id'], $scopeArray['scope']); + } } /** From dcf54a83bfd93a7cc9c0bc41d8836749cbc7f752 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 21 Dec 2018 15:55:55 -0300 Subject: [PATCH 050/113] closes #836 unitTest --- .../Ebizmarts/MailChimp/Model/Api/Carts.php | 197 ++++++++++++----- .../MailChimp/Model/Api/CartsTest.php | 208 +++++++++++++++++- 2 files changed, 349 insertions(+), 56 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php index e3db8e444..88b4b47c9 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -35,10 +35,10 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId) } $this->_firstDate = $helper->getAbandonedCartFirstDate($magentoStoreId); - $this->_counter = 0; + $this->setCounter(0); $date = $helper->getDateMicrotime(); - $this->_batchId = 'storeid-' . $magentoStoreId . '_' . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . '_' . $date; + $this->setBatchId('storeid-' . $magentoStoreId . '_' . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . '_' . $date); $resendTurn = $helper->getResendTurn($magentoStoreId); if (!$resendTurn) { // get all the carts converted in orders (must be deleted on mailchimp) @@ -58,9 +58,9 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId) */ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) { - $mailchimpTableName = Mage::getSingleton('core/resource')->getTableName('mailchimp/ecommercesyncdata'); + $mailchimpTableName = $this->getMailchimpEcommerceDataTableName(); $allCarts = array(); - $convertedCarts = Mage::getResourceModel('sales/quote_collection'); + $convertedCarts = $this->getQuoteCollection(); // get only the converted quotes $convertedCarts->addFieldToFilter('store_id', array('eq' => $magentoStoreId)); $convertedCarts->addFieldToFilter('is_active', array('eq' => 0)); @@ -81,23 +81,25 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) $allCartsForEmail = $this->_getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); foreach ($allCartsForEmail as $cartForEmail) { $alreadySentCartId = $cartForEmail->getEntityId(); + $counter = $this->getCounter(); if ($alreadySentCartId != $cartId) { - $allCarts[$this->_counter]['method'] = 'DELETE'; - $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $alreadySentCartId; - $allCarts[$this->_counter]['operation_id'] = $this->_batchId . '_' . $alreadySentCartId; - $allCarts[$this->_counter]['body'] = ''; + $allCarts[$counter]['method'] = 'DELETE'; + $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $alreadySentCartId; + $allCarts[$counter]['operation_id'] = $this->getBatchId() . '_' . $alreadySentCartId; + $allCarts[$counter]['body'] = ''; $this->_updateSyncData($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1); - $this->_counter += 1; + $this->setCounter($this->getCounter()+1); } } $allCartsForEmail->clear(); - $allCarts[$this->_counter]['method'] = 'DELETE'; - $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $cartId; - $allCarts[$this->_counter]['operation_id'] = $this->_batchId . '_' . $cartId; - $allCarts[$this->_counter]['body'] = ''; + $counter = $this->getCounter(); + $allCarts[$counter]['method'] = 'DELETE'; + $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $cartId; + $allCarts[$counter]['operation_id'] = $this->getBatchId() . '_' . $cartId; + $allCarts[$counter]['body'] = ''; $this->_updateSyncData($cartId, $mailchimpStoreId, null, null, null, null, 1); - $this->_counter += 1; + $this->setCounter($this->getCounter()+1); } return $allCarts; @@ -108,11 +110,12 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) * @param $magentoStoreId * @return array */ - protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) + public function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) { - $mailchimpTableName = Mage::getSingleton('core/resource')->getTableName('mailchimp/ecommercesyncdata'); + $mailchimpTableName = $this->getMailchimpEcommerceDataTableName(); + $batchId = $this->getBatchId(); $allCarts = array(); - $modifiedCarts = Mage::getResourceModel('sales/quote_collection'); + $modifiedCarts = $this->getQuoteCollection(); // select carts with no orders $modifiedCarts->addFieldToFilter('is_active', array('eq' => 1)); // select carts for the current Magento store id @@ -136,20 +139,21 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) /** * @var $customer Mage_Customer_Model_Customer */ - $customer = Mage::getModel("customer/customer"); - $customer->setWebsiteId(Mage::getModel('core/store')->load($magentoStoreId)->getWebsiteId()); + $customer = $this->getCustomerModel(); + $customer->setWebsiteId($this->getWebSiteIdFromMagentoStoreId($magentoStoreId)); $customer->loadByEmail($cart->getCustomerEmail()); if ($customer->getEmail() != $cart->getCustomerEmail()) { $allCartsForEmail = $this->_getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); foreach ($allCartsForEmail as $cartForEmail) { $alreadySentCartId = $cartForEmail->getEntityId(); + $counter = $this->getCounter(); if ($alreadySentCartId != $cartId) { - $allCarts[$this->_counter]['method'] = 'DELETE'; - $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $alreadySentCartId; - $allCarts[$this->_counter]['operation_id'] = $this->_batchId . '_' . $alreadySentCartId; - $allCarts[$this->_counter]['body'] = ''; + $allCarts[$counter]['method'] = 'DELETE'; + $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $alreadySentCartId; + $allCarts[$counter]['operation_id'] = $batchId . '_' . $alreadySentCartId; + $allCarts[$counter]['body'] = ''; $this->_updateSyncData($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1); - $this->_counter += 1; + $this->setCounter($this->getCounter()+1); } } @@ -161,23 +165,24 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) $this->_updateSyncData($cartId, $mailchimpStoreId); continue; } - + Mage::log($allCarts, null, 'ebizmartsAllCarts.log', true); // send the products that not already sent $allCarts = $this->addProductNotSentData($mailchimpStoreId, $magentoStoreId, $cart, $allCarts); $cartJson = $this->_makeCart($cart, $mailchimpStoreId, $magentoStoreId, true); if ($cartJson != "") { - $allCarts[$this->_counter]['method'] = 'PATCH'; - $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts'.$cartId; - $allCarts[$this->_counter]['operation_id'] = $this->_batchId . '_' . $cartId; - $allCarts[$this->_counter]['body'] = $cartJson; - $this->_counter += 1; - $this->_updateSyncData($cartId, $mailchimpStoreId, null, null, null, null, null, $this->_token); + $counter = $this->getCounter(); + $allCarts[$counter]['method'] = 'PATCH'; + $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts'.$cartId; + $allCarts[$counter]['operation_id'] = $batchId . '_' . $cartId; + $allCarts[$counter]['body'] = $cartJson; + $this->setCounter($this->getCounter()+1); + $this->_updateSyncData($cartId, $mailchimpStoreId, null, null, null, null, null, $this->getToken()); } else { $this->_updateSyncData($cartId, $mailchimpStoreId); } - $this->_token = null; + $this->setToken(null); } return $allCarts; @@ -191,7 +196,7 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) { $helper = $this->getHelper(); $allCarts = array(); - $newCarts = Mage::getResourceModel('sales/quote_collection'); + $newCarts = $this->getQuoteCollection(); $newCarts->addFieldToFilter('is_active', array('eq' => 1)); $newCarts->addFieldToFilter('customer_email', array('notnull' => true)); $newCarts->addFieldToFilter('items_count', array('gt' => 0)); @@ -221,19 +226,20 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) continue; } - $customer = Mage::getModel("customer/customer"); - $customer->setWebsiteId(Mage::getModel('core/store')->load($magentoStoreId)->getWebsiteId()); + $customer = $this->getCustomerModel(); + $customer->setWebsiteId($this->getWebSiteIdFromMagentoStoreId($magentoStoreId)); $customer->loadByEmail($cart->getCustomerEmail()); if ($customer->getEmail() != $cart->getCustomerEmail()) { $allCartsForEmail = $this->_getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); foreach ($allCartsForEmail as $cartForEmail) { + $counter = $this->getCounter(); $alreadySentCartId = $cartForEmail->getEntityId(); - $allCarts[$this->_counter]['method'] = 'DELETE'; - $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $alreadySentCartId; - $allCarts[$this->_counter]['operation_id'] = $this->_batchId . '_' . $alreadySentCartId; - $allCarts[$this->_counter]['body'] = ''; + $allCarts[$counter]['method'] = 'DELETE'; + $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $alreadySentCartId; + $allCarts[$counter]['operation_id'] = $this->getBatchId() . '_' . $alreadySentCartId; + $allCarts[$counter]['body'] = ''; $this->_updateSyncData($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1); - $this->_counter += 1; + $this->setCounter($this->getCounter()+1); } $allCartsForEmail->clear(); @@ -250,17 +256,18 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $cartJson = $this->_makeCart($cart, $mailchimpStoreId, $magentoStoreId); if ($cartJson != "") { - $allCarts[$this->_counter]['method'] = 'POST'; - $allCarts[$this->_counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts'; - $allCarts[$this->_counter]['operation_id'] = $this->_batchId . '_' . $cartId; - $allCarts[$this->_counter]['body'] = $cartJson; - $this->_counter += 1; - $this->_updateSyncData($cartId, $mailchimpStoreId, null, null, null, null, null, $this->_token); + $counter = $this->getCounter(); + $allCarts[$counter]['method'] = 'POST'; + $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts'; + $allCarts[$counter]['operation_id'] = $this->getBatchId() . '_' . $cartId; + $allCarts[$counter]['body'] = $cartJson; + $this->setCounter($this->getCounter()+1); + $this->_updateSyncData($cartId, $mailchimpStoreId, null, null, null, null, null, $this->getToken()); } else { $this->_updateSyncData($cartId, $mailchimpStoreId); } - $this->_token = null; + $this->setToken(null); } return $allCarts; @@ -276,8 +283,8 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) */ protected function _getAllCartsByEmail($email, $mailchimpStoreId, $magentoStoreId) { - $mailchimpTableName = Mage::getSingleton('core/resource')->getTableName('mailchimp/ecommercesyncdata'); - $allCartsForEmail = Mage::getResourceModel('sales/quote_collection'); + $mailchimpTableName = $this->getMailchimpEcommerceDataTableName(); + $allCartsForEmail = $this->getQuoteCollection(); $allCartsForEmail->addFieldToFilter('is_active', array('eq' => 1)); $allCartsForEmail->addFieldToFilter('store_id', array('eq' => $magentoStoreId)); $allCartsForEmail->addFieldToFilter('customer_email', array('eq' => $email)); @@ -383,12 +390,12 @@ protected function _getCheckoutUrl($cart, $isModified) $token = $cart->getMailchimpToken(); } $url = Mage::getModel('core/url')->setStore($cart->getStoreId())->getUrl('', array('_nosid' => true, '_secure' => true)) . 'mailchimp/cart/loadquote?id=' . $cart->getEntityId() . '&token=' . $token; - $this->_token = $token; + $this->setToken($token); return $url; } /** - * @return mixed + * @return int */ protected function getBatchLimitFromConfig() { @@ -542,12 +549,15 @@ public function addProductNotSentData($mailchimpStoreId, $magentoStoreId, $cart, { $helper = $this->getHelper(); $productData = Mage::getModel('mailchimp/api_products')->sendModifiedProduct($cart, $mailchimpStoreId, $magentoStoreId); - $productDataArray = $helper->addEntriesToArray($allCarts, $productData, $this->_counter); + $productDataArray = $helper->addEntriesToArray($allCarts, $productData, $this->getCounter()); $allCarts = $productDataArray[0]; - $this->_counter = $productDataArray[1]; + $this->setCounter($productDataArray[1]); return $allCarts; } + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ protected function getHelper() { return Mage::helper('mailchimp'); @@ -559,7 +569,7 @@ protected function getHelper() */ public function joinMailchimpSyncDataWithoutWhere($newCarts, $mailchimpStoreId) { - $mailchimpTableName = Mage::getSingleton('core/resource')->getTableName('mailchimp/ecommercesyncdata'); + $mailchimpTableName = $this->getMailchimpEcommerceDataTableName(); $newCarts->getSelect()->joinLeft( array('m4m' => $mailchimpTableName), "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' @@ -567,4 +577,83 @@ public function joinMailchimpSyncDataWithoutWhere($newCarts, $mailchimpStoreId) array('m4m.*') ); } + + /** + * @return mixed + */ + public function getMailchimpEcommerceDataTableName() + { + return Mage::getSingleton('core/resource')->getTableName('mailchimp/ecommercesyncdata'); + } + + /** + * @return Mage_Sales_Model_Resource_Quote_Collection + */ + public function getQuoteCollection() + { + return Mage::getResourceModel('sales/quote_collection'); + } + + /** + * @return false|Mage_Core_Model_Abstract + */ + public function getCustomerModel() + { + return Mage::getModel("customer/customer"); + } + + /** + * @param $magentoStoreId + * @return mixed + */ + public function getWebSiteIdFromMagentoStoreId($magentoStoreId) + { + return Mage::getModel('core/store')->load($magentoStoreId)->getWebsiteId(); + } + + /** + * @return int + */ + public function getCounter() + { + return $this->_counter; + } + + /** + * @param $counter + */ + public function setCounter($counter) + { + $this->_counter = $counter; + } + + /** + * @return string + */ + public function getBatchId() + { + return $this->_batchId; + } + + /** + * @param $batchId + */ + public function setBatchId($batchId) + { + $this->_batchId = $batchId; + } + + /** + * @return null + */ + public function getToken() + { + return $this->_token; + } + + public function setToken($token) + { + + $this->_token = $token; + } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 26692bc41..111b3dcb9 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -7,6 +7,9 @@ class Ebizmarts_MailChimp_Model_Api_CartsTest extends PHPUnit_Framework_TestCase */ private $cartsApiMock; + const DATE = '2017-05-18-14-45-54-38849500'; + const BATCH_ID = 'storeid-1_QUO_2017-05-18-14-45-54-38849500'; + public function setUp() { Mage::app('default'); @@ -25,7 +28,13 @@ public function testCreateBatchJson() $magentoStoreId = 1; $batchArray = array(); - $cartsApiMock = $this->cartsApiMock->setMethods(array('getHelper', '_getConvertedQuotes', '_getModifiedQuotes', '_getNewQuotes')) + $cartsApiMock = $this->cartsApiMock->setMethods(array( + 'getHelper', + '_getConvertedQuotes', + '_getModifiedQuotes', + '_getNewQuotes', + 'setBatchId' + )) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) @@ -33,6 +42,7 @@ public function testCreateBatchJson() ->setMethods(array('isAbandonedCartEnabled', 'getAbandonedCartFirstDate', 'getDateMicrotime', 'getResendTurn')) ->getMock(); + $cartsApiMock->expects($this->once())->method('setBatchId')->with(self::BATCH_ID); $cartsApiMock->expects($this->once())->method('getHelper')->willReturn($helperMock); $cartsApiMock->expects($this->once())->method('_getConvertedQuotes')->with($mailchimpStoreId, $magentoStoreId)->willReturn($batchArray); $cartsApiMock->expects($this->once())->method('_getModifiedQuotes')->with($mailchimpStoreId, $magentoStoreId)->willReturn($batchArray); @@ -40,9 +50,203 @@ public function testCreateBatchJson() $helperMock->expects($this->once())->method('isAbandonedCartEnabled')->with($magentoStoreId)->willReturn(true); $helperMock->expects($this->once())->method('getAbandonedCartFirstDate')->with($magentoStoreId)->willReturn('00-00-00 00:00:00'); - $helperMock->expects($this->once())->method('getDateMicrotime')->willReturn('00-00-00 00:00:00'); + $helperMock->expects($this->once())->method('getDateMicrotime')->willReturn(self::DATE); $helperMock->expects($this->once())->method('getResendTurn')->with($magentoStoreId)->willReturn(null); $cartsApiMock->createBatchJson($mailchimpStoreId, $magentoStoreId); } + + public function testGetModifiedQuotes() + { + $mcTableName = 'mailchimp_ecommerce_sync_data'; + $batchLimitFromConfig = 100; + $magentoStoreId = 0; + $webSiteIdFromMagentoStoreId = 0; + $mailchimpStoreId = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; + $customerEmailAddressByCart = 'luciaines@ebizmarts.com'; + $customerEmailAddress = ''; + $counter = 0; + $alreadySentCartId = 2; + $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"luciaines@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; + $cartId = 1; + $customerId = 1; + $stringIsActive = 'is_active'; + $stringStoreId = 'store_id'; + $stringCustomerEmail = 'customer_email'; + $arrayAddFieldToFilterCustomerEmail = array('eq' => $customerEmailAddressByCart); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => $magentoStoreId); + $where = "m4m.mailchimp_sync_deleted = 0 + AND m4m.mailchimp_sync_delta < updated_at"; + $whereGetAllCartsByEmail = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'"; + $arrayTableName = array('m4m' => $mcTableName); + $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' + AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'"; + $m4m = array('m4m.*'); + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.$mailchimpStoreId.'/carts/'. $alreadySentCartId, 'operation_id' => self::BATCH_ID . '_' . $alreadySentCartId, 'body' => '')); + $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; + + $cartsApiMock = $this->cartsApiMock->setMethods( + array('setToken', 'getToken', 'getBatchId', 'getMailchimpEcommerceDataTableName', 'getBatchLimitFromConfig', '_updateSyncData', 'getQuoteCollection', 'getCustomerModel', 'getWebSiteIdFromMagentoStoreId', 'setCounter', 'getCounter', '_makeCart', '_getAllCartsByEmail', 'addProductNotSentData')) + ->getMock(); + + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) + ->getMock(); + + $quoteResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('joinLeft', 'where', 'limit')) + ->getMock(); + + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getToken') + ->willReturn($token); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($quoteResoureceCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn($batchLimitFromConfig); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with($magentoStoreId) + ->willReturn($webSiteIdFromMagentoStoreId); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with($customerEmailAddressByCart,$mailchimpStoreId, $magentoStoreId) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + $counter, + $counter, + $counter, + $counter + ); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1), + array($cartId, $mailchimpStoreId, null, null, null, null, null, $token) + ); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array($counter + 1), + array($counter + 1) + ); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, $mailchimpStoreId, $magentoStoreId, true) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with($mailchimpStoreId, $magentoStoreId, $cartModelMock, $allCarts) + ->willReturn($allCarts); + + $cartModelMock->expects($this->exactly(3)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + $customerEmailAddressByCart, + $customerEmailAddressByCart, + $customerEmailAddressByCart + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn($cartId); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + + $quoteResoureceCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringIsActive, $arrayAddFieldToFilter), + array($stringStoreId, $arrayAddFieldToFilterStoreId) + ); + $quoteResoureceCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + + $quoteResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with($batchLimitFromConfig); + + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with($webSiteIdFromMagentoStoreId); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with($customerEmailAddressByCart); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn($alreadySentCartId); + + $cartsApiMock->_getModifiedQuotes($mailchimpStoreId, $magentoStoreId); + } } From 28d69074a9d0242a6a40ce4afa70baa4e67771eb Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 21 Dec 2018 15:59:49 -0300 Subject: [PATCH 051/113] closes #836 removeLog --- app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php index 88b4b47c9..986c1e667 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -165,7 +165,6 @@ public function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) $this->_updateSyncData($cartId, $mailchimpStoreId); continue; } - Mage::log($allCarts, null, 'ebizmartsAllCarts.log', true); // send the products that not already sent $allCarts = $this->addProductNotSentData($mailchimpStoreId, $magentoStoreId, $cart, $allCarts); From 35ffd2cb8f76fcff27519de0a2d532057d62221e Mon Sep 17 00:00:00 2001 From: keller Date: Fri, 21 Dec 2018 17:28:11 -0300 Subject: [PATCH 052/113] closes #793 Resolve conflicts with develop branch --- .../Ebizmarts/MailChimp/Model/Config.php | 55 ++++--- .../Ebizmarts/MailChimp/Model/Observer.php | 135 +++++++++++++--- .../MailChimp/Model/ObserverTest.php | 148 ++++++++++++++---- 3 files changed, 261 insertions(+), 77 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Config.php b/app/code/community/Ebizmarts/MailChimp/Model/Config.php index 55ab2c402..84d5a99c9 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Config.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Config.php @@ -11,31 +11,35 @@ */ class Ebizmarts_MailChimp_Model_Config { - const GENERAL_ACTIVE = 'mailchimp/general/active'; - const GENERAL_APIKEY = 'mailchimp/general/apikey'; - const GENERAL_OAUTH_WIZARD = 'mailchimp/general/oauth_wizard'; - const GENERAL_ACCOUNT_DETAILS = 'mailchimp/general/account_details'; - const GENERAL_LIST = 'mailchimp/general/list'; - const GENERAL_OLD_LIST = 'mailchimp/general/old_list'; - const GENERAL_LIST_CHANGED_SCOPES = 'mailchimp/general/list_changed_scopes'; - const GENERAL_CHECKOUT_SUBSCRIBE = 'mailchimp/general/checkout_subscribe'; - const GENERAL_MCSTOREID = 'mailchimp/general/storeid'; - const GENERAL_MCISSYNCING = 'mailchimp/general/is_syicing'; - const GENERAL_ECOMMMINSYNCDATEFLAG = 'mailchimp/general/mcminsyncdateflag'; - const GENERAL_SUBMINSYNCDATEFLAG = 'mailchimp/general/subminsyncdateflag'; - const GENERAL_TWO_WAY_SYNC = 'mailchimp/general/webhook_active'; - const GENERAL_UNSUBSCRIBE = 'mailchimp/general/webhook_delete'; - const GENERAL_WEBHOOK_ID = 'mailchimp/general/webhook_id'; - const GENERAL_LOG = 'mailchimp/general/enable_log'; - const GENERAL_ORDER_GRID = 'mailchimp/general/order_grid'; - const GENERAL_MAP_FIELDS = 'mailchimp/general/map_fields'; - const GENERAL_CUSTOM_MAP_FIELDS = 'mailchimp/general/customer_map_fields'; - const GENERAL_MIGRATE_FROM_115 = 'mailchimp/general/migrate_from_115'; - const GENERAL_MIGRATE_FROM_116 = 'mailchimp/general/migrate_from_116'; - const GENERAL_MIGRATE_FROM_1164 = 'mailchimp/general/migrate_from_1164'; - const GENERAL_MIGRATE_LAST_ORDER_ID = 'mailchimp/general/migrate_last_order_id'; - const GENERAL_SUBSCRIBER_AMOUNT = 'mailchimp/general/subscriber_batch_amount'; - const GENERAL_MAGENTO_MAIL = 'mailchimp/general/magento_mail'; + const GENERAL_ACTIVE = 'mailchimp/general/active'; + const GENERAL_APIKEY = 'mailchimp/general/apikey'; + const GENERAL_OAUTH_WIZARD = 'mailchimp/general/oauth_wizard'; + const GENERAL_ACCOUNT_DETAILS = 'mailchimp/general/account_details'; + const GENERAL_LIST = 'mailchimp/general/list'; + const GENERAL_OLD_LIST = 'mailchimp/general/old_list'; + const GENERAL_LIST_CHANGED_SCOPES = 'mailchimp/general/list_changed_scopes'; + const GENERAL_CHECKOUT_SUBSCRIBE = 'mailchimp/general/checkout_subscribe'; + const GENERAL_MCSTOREID = 'mailchimp/general/storeid'; + const GENERAL_MCISSYNCING = 'mailchimp/general/is_syicing'; + const GENERAL_ECOMMMINSYNCDATEFLAG = 'mailchimp/general/mcminsyncdateflag'; + const GENERAL_SUBMINSYNCDATEFLAG = 'mailchimp/general/subminsyncdateflag'; + const GENERAL_TWO_WAY_SYNC = 'mailchimp/general/webhook_active'; + const GENERAL_UNSUBSCRIBE = 'mailchimp/general/webhook_delete'; + const GENERAL_WEBHOOK_ID = 'mailchimp/general/webhook_id'; + const GENERAL_LOG = 'mailchimp/general/enable_log'; + const GENERAL_ORDER_GRID = 'mailchimp/general/order_grid'; + const GENERAL_MAP_FIELDS = 'mailchimp/general/map_fields'; + const GENERAL_CUSTOM_MAP_FIELDS = 'mailchimp/general/customer_map_fields'; + const GENERAL_MIGRATE_FROM_115 = 'mailchimp/general/migrate_from_115'; + const GENERAL_MIGRATE_FROM_116 = 'mailchimp/general/migrate_from_116'; + const GENERAL_MIGRATE_FROM_1164 = 'mailchimp/general/migrate_from_1164'; + const GENERAL_MIGRATE_LAST_ORDER_ID = 'mailchimp/general/migrate_last_order_id'; + const GENERAL_SUBSCRIBER_AMOUNT = 'mailchimp/general/subscriber_batch_amount'; + const GENERAL_TIME_OUT = 'mailchimp/general/connection_timeout'; + const GENERAL_INTEREST_CATEGORIES = 'mailchimp/general/interest_categories'; + const GENERAL_INTEREST_SUCCESS_BEFORE = 'mailchimp/general/interest_success_before'; + const GENERAL_INTEREST_SUCCESS_AFTER = 'mailchimp/general/interest_success_after'; + const GENERAL_MAGENTO_MAIL = 'mailchimp/general/magento_mail'; const ECOMMERCE_ACTIVE = 'mailchimp/ecommerce/active'; const ECOMMERCE_CUSTOMERS_OPTIN = 'mailchimp/ecommerce/customers_optin'; @@ -53,6 +57,7 @@ class Ebizmarts_MailChimp_Model_Config const ECOMMERCE_ORDER_AMOUNT = 'mailchimp/ecommerce/order_batch_amount'; const ECOMMERCE_IMAGE_SIZE = 'mailchimp/ecommerce/image_size'; const ECOMMERCE_SYNC_DATE = 'mailchimp/ecommerce/sync_date'; + const ECOMMERCE_SEND_PROMO = 'mailchimp/ecommerce/send_promo'; const IMAGE_SIZE_DEFAULT = 'image'; const IMAGE_SIZE_SMALL = 'small_image'; diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index e55b9595a..36178c984 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -111,7 +111,7 @@ protected function getCustomerModel() */ public function saveConfig(Varien_Event_Observer $observer) { - $post = Mage::app()->getRequest()->getPost(); + $post = $this->getRequest()->getPost(); $helper = $this->makeHelper(); $scopeArray = $helper->getCurrentScope(); @@ -126,18 +126,20 @@ public function saveConfig(Varien_Event_Observer $observer) } /** - * Handle subscription change (subscribe/unsubscribe) + * Prevent Magento's subscription confirmation email to be sent if MailChimp For Magento is enabled. + * Override Magento's default status if confirmation email is enabled and the customer is logged in. * * @param Varien_Event_Observer $observer * @return Varien_Event_Observer * @throws Mage_Core_Model_Store_Exception */ - public function handleSubscriber(Varien_Event_Observer $observer) + public function subscriberSaveBefore(Varien_Event_Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); + $storeId = $subscriber->getStoreId(); $helper = $this->makeHelper(); + if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { - $storeId = $subscriber->getStoreId(); $isEnabled = $helper->isSubscriptionEnabled($storeId); if ($isEnabled) { $statusChanged = $subscriber->getIsStatusChanged(); @@ -148,9 +150,7 @@ public function handleSubscriber(Varien_Event_Observer $observer) $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $this->addSuccessIfRequired($helper); } - $subscriber->setImportMode(true); - - $this->createEmailCookie($subscriber); + $subscriber->setImportMode(true); if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { //Use MailChimp emails @@ -178,6 +178,34 @@ public function handleSubscriber(Varien_Event_Observer $observer) return $observer; } + /** + * Hnalde Subscriber changes update. + * + * @param Varien_Event_Observer $observer + * @throws Mage_Core_Model_Store_Exception + */ + public function subscriberSaveAfter(Varien_Event_Observer $observer) + { + $subscriber = $observer->getEvent()->getSubscriber(); + $storeId = $subscriber->getStoreId(); + $helper = $this->makeHelper(); + + if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { + $isEnabled = $helper->isSubscriptionEnabled($storeId); + if ($isEnabled) { + $params = $this->getRequest()->getParams(); + $helper->saveInterestGroupData($params, $storeId, null, $subscriber); + + $this->createEmailCookie($subscriber); + + $apiSubscriber = $this->makeApiSubscriber(); + $apiSubscriber->updateSubscriber($subscriber, true); + } + } + + return $observer; + } + /** * Handle subscriber deletion from back end. * @@ -238,40 +266,49 @@ public function alterNewsletterGrid(Varien_Event_Observer $observer) * @param Varien_Event_Observer $observer * @return Varien_Event_Observer */ - public function customerSaveBefore(Varien_Event_Observer $observer) + public function customerSaveAfter(Varien_Event_Observer $observer) { $customer = $observer->getEvent()->getCustomer(); + $origEmail = $customer->getOrigData('email'); + $customerEmail = $customer->getEmail(); $storeId = $customer->getStoreId(); + // if customer was created in admin, use store id selected for Mailchimp. + if (!$storeId) { + $storeId = $customer->getMailchimpStoreView(); + } $helper = $this->makeHelper(); $isEnabled = $helper->isSubscriptionEnabled($storeId); + $params = $this->getRequest()->getParams(); if ($isEnabled) { + $customerId = $customer->getId(); + $subscriberEmail = ($origEmail) ? $origEmail : $customerEmail; + $subscriber = $this->handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId); $apiSubscriber = $this->makeApiSubscriber(); - $origEmail = $customer->getOrigData('email'); - $customerEmail = $customer->getEmail(); if ($origEmail) { // check if customer has changed email address if ($origEmail != $customerEmail) { - $subscriberModel = $this->getSubscriberModel(); - $subscriber = $subscriberModel->loadByEmail($origEmail); if ($subscriber->getId()) { // unsubscribe old email address $apiSubscriber->deleteSubscriber($subscriber); // subscribe new email address + $subscriberModel = $this->getSubscriberModel(); $subscriber = $subscriberModel->loadByCustomer($customer); $subscriber->setSubscriberEmail($customerEmail); // make sure we set the new email address + $subscriber->save(); - $apiSubscriber->updateSubscriber($subscriber, true); } } } - //update subscriber data if a subscriber with the same email address exists - $apiSubscriber->update($customerEmail, $storeId); + //update subscriber data if a subscriber with the same email address exists and was not affected. + if (!$origEmail || $origEmail == $customerEmail) { + $apiSubscriber->update($customerEmail, $storeId); + } if ($helper->isEcomSyncDataEnabled($storeId)) { //update mailchimp ecommerce data for that customer - $this->makeApiCustomer()->update($customer->getId(), $storeId); + $this->makeApiCustomer()->update($customerId, $storeId); } } @@ -854,7 +891,7 @@ public function salesruleDeleteAfter(Varien_Event_Observer $observer) public function secondaryCouponsDelete(Varien_Event_Observer $observer) { $promoCodesApi = $this->makeApiPromoCode(); - $params = Mage::app()->getRequest()->getParams(); + $params = $this->getRequest()->getParams(); if (isset($params['ids']) && isset($params['id'])) { $promoRuleId = $params['id']; $promoCodeIds = $params['ids']; @@ -973,6 +1010,68 @@ protected function createEmailCookie($subscriber) } } + public function addCustomerTab(Varien_Event_Observer $observer) + { + $block = $observer->getEvent()->getBlock(); + $helper = $this->makeHelper(); + // add tab in customer edit page + if ($block instanceof Mage_Adminhtml_Block_Customer_Edit_Tabs) { + $customerId = (int) $this->getRequest()->getParam('id'); + $customer = Mage::getModel('customer/customer')->load($customerId); + $storeId = $customer->getStoreId(); + //If the customer was created in the admin panel use the store view selected for MailChimp. + if (!$storeId) { + $storeId = $customer->getMailchimpStoreView(); + } + if ($helper->getLocalInterestCategories($storeId) && ($this->getRequest()->getActionName() == 'edit' || $this->getRequest()->getParam('type'))) { + $block->addTab('mailchimp', array( + 'label' => $helper->__('MailChimp'), + 'url' => $block->getUrl('adminhtml/mailchimp/index', array('_current' => true)), + 'class' => 'ajax' + )); + } + } + return $observer; + } + + protected function getRequest() + { + return Mage::app()->getRequest(); + } + + /** + * Handle frontend customer interest groups only if is not subscribed and all admin customer groups. + * + * @param $subscriberEmail + * @param $params + * @param $storeId + * @param null $customerId + * @return Mage_Newsletter_Model_Subscriber + * @throws Mage_Core_Model_Store_Exception + */ + protected function handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId = null) + { + $helper = $this->makeHelper(); + $subscriberModel = $this->getSubscriberModel(); + $subscriber = $subscriberModel->loadByEmail($subscriberEmail); + if ($subscriber->getId()) { + $helper->saveInterestGroupData($params, $storeId, $customerId, $subscriber); + } elseif (isset($params['customer_id'])) { + $groups = $helper->getInterestGroupsIfAvailable($params); + if ($groups) { + $helper->saveInterestGroupData($params, $storeId, $customerId); + Mage::getSingleton('adminhtml/session')->addWarning($helper->__('The customer must be subscribed for this change to apply.')); + } + } else { + if (!$helper->isAdmin()) { + //save frontend groupdata when customer is not subscribed. + $helper->saveInterestGroupData($params, $storeId, $customerId); + } + Mage::getSingleton('adminhtml/session')->addError($helper->__('Something went wrong when trying to save the interest group data. The customer must be subscribed for this change to apply.')); + } + return $subscriber; + } + /** * @return mixed */ @@ -1004,6 +1103,6 @@ protected function isCustomerLoggedIn() */ protected function getRequestActionName() { - return Mage::app()->getRequest()->getActionName(); + return $this->getRequest()->getActionName(); } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 658d07ae8..9f6cedde4 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -291,17 +291,20 @@ public function testHandleSubscriberDeletion() $observerMock->handleSubscriberDeletion($eventObserverMock); } - public function testCustomerSaveBefore() + public function testCustomerSaveAfter() { + $adminStoreId = 0; $storeId = 1; $oldEmailAddress = 'oldEmail@example.com'; $newEmailAddress = 'newEmail@example.com'; + $subscriberEmail = ($oldEmailAddress) ? $oldEmailAddress : $newEmailAddress; $subscriberId = 1; $customerId = 1; + $params = array(); $customerMock = $this->getMockBuilder(Mage_Customer_Model_Customer::class) ->disableOriginalConstructor() - ->setMethods(array('getId', 'getOrigData', 'getEmail', 'getStoreId')) + ->setMethods(array('getId', 'getOrigData', 'getEmail', 'getStoreId', 'getMailchimpStoreView')) ->getMock(); $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) @@ -314,9 +317,14 @@ public function testCustomerSaveBefore() ->setMethods(array('getCustomer')) ->getMock(); + $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParams')) + ->getMock(); + $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) ->disableOriginalConstructor() - ->setMethods(array('makeHelper', 'makeApiSubscriber', 'getSubscriberModel', 'makeApiCustomer')) + ->setMethods(array('makeHelper', 'makeApiSubscriber', 'getSubscriberModel', 'makeApiCustomer', 'getRequest', 'handleCustomerGroups')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) @@ -331,7 +339,12 @@ public function testCustomerSaveBefore() $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() - ->setMethods(array('loadByEmail', 'loadByCustomer', 'setSubscriberEmail', 'getId')) + ->setMethods(array('getId')) + ->getMock(); + + $subscriberMockTwo = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByCustomer', 'setSubscriberEmail', 'save')) ->getMock(); $apiCustomerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Customers::class) @@ -343,39 +356,41 @@ public function testCustomerSaveBefore() $eventMock->expects($this->once())->method('getCustomer')->willReturn($customerMock); - $customerMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + $customerMock->expects($this->once())->method('getOrigData')->with('email')->willReturn($oldEmailAddress); + $customerMock->expects($this->once())->method('getEmail')->willReturn($newEmailAddress); + $customerMock->expects($this->once())->method('getStoreId')->willReturn($adminStoreId); + $customerMock->expects($this->once())->method('getMailchimpStoreView')->willReturn($storeId); $observerMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); - $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); + $observerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); - $customerMock->expects($this->once())->method('getOrigData')->with('email')->willReturn($oldEmailAddress); - $customerMock->expects($this->once())->method('getEmail')->willReturn($newEmailAddress); + $requestMock->expects($this->once())->method('getParams')->willReturn($params); - $observerMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); + $customerMock->expects($this->once())->method('getId')->willReturn($customerId); + + $observerMock->expects($this->once())->method('handleCustomerGroups')->with($subscriberEmail, $params, $storeId, $customerId)->willReturn($subscriberMock); + $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); - $subscriberMock->expects($this->once())->method('loadByEmail')->with($oldEmailAddress)->willReturnSelf(); $subscriberMock->expects($this->once())->method('getId')->willReturn($subscriberId); $apiSubscriberMock->expects($this->once())->method('deleteSubscriber')->with($subscriberMock); - $subscriberMock->expects($this->once())->method('loadByCustomer')->with($customerMock)->willReturnSelf(); - $subscriberMock->expects($this->once())->method('setSubscriberEmail')->with($newEmailAddress); + $observerMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMockTwo); - $apiSubscriberMock->expects($this->once())->method('updateSubscriber')->with($subscriberMock, true); - $apiSubscriberMock->expects($this->once())->method('update')->with($newEmailAddress, $storeId); + $subscriberMockTwo->expects($this->once())->method('loadByCustomer')->with($customerMock)->willReturnSelf(); + $subscriberMockTwo->expects($this->once())->method('setSubscriberEmail')->with($newEmailAddress); + $subscriberMockTwo->expects($this->once())->method('save')->willReturnSelf(); $helperMock->expects($this->once())->method('isEcomSyncDataEnabled')->with($storeId)->willReturn(true); $observerMock->expects($this->once())->method('makeApiCustomer')->willReturn($apiCustomerMock); - $customerMock->expects($this->once())->method('getId')->willReturn($customerId); - $apiCustomerMock->expects($this->once())->method('update')->with($customerId, $storeId); - $observerMock->customerSaveBefore($eventObserverMock); + $observerMock->customerSaveAfter($eventObserverMock); } public function testCustomerAddressSaveBefore() @@ -649,17 +664,17 @@ public function testAddColumnToSalesOrderGridCollection() $observerMock->addColumnToSalesOrderGridCollection($eventObserverMock); } + /** * @param array $data - * @dataProvider handleSubscriberDataProvider + * @dataProvider subscriberSaveBeforeDataProvider */ - - public function testHandleSubscriber($data) + public function testSubscriberSaveBefore($data) { $setImportMode = $data['setImportMode']; $getStoreId = $data['getStoreId']; $magentoMail = $data['magentoMail']; - $updateSubscriber = $data['updateSubscriber']; + $subscriberUpdatedAmount = $data['subscriberUpdatedAmount']; $storeId = 1; $sendConfirmationRequestEmail = $data['sendConfirmationRequestEmail']; @@ -670,14 +685,13 @@ public function testHandleSubscriber($data) $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) ->disableOriginalConstructor() - ->setMethods(array('makeHelper', 'getCoreResource', 'getRegistry', 'removeRegistry', 'createEmailCookie', - 'makeApiSubscriber', 'addSuccessIfRequired')) + ->setMethods(array('makeHelper', 'addSuccessIfRequired', 'makeApiSubscriber')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() ->setMethods(array('isSubscriptionEnabled', 'isEcomSyncDataEnabledInAnyScope', - 'isSubscriptionConfirmationEnabled', 'getConfigValueForScope')) + 'isSubscriptionConfirmationEnabled', 'getConfigValueForScope', 'getStoreId')) ->getMock(); $eventMock = $this->getMockBuilder(Varien_Event::class) @@ -687,8 +701,8 @@ public function testHandleSubscriber($data) $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() - ->setMethods(array('getSubscriberSource', 'getStoreId', 'getIsStatusChanged', 'getStatus', 'setStatus', - 'setImportMode', 'getOrigData', 'getSubscriberStatus', 'sendConfirmationRequestEmail')) + ->setMethods(array('getSubscriberSource', 'getIsStatusChanged', 'getStatus', 'setStatus', 'setImportMode', + 'getStoreId', 'sendConfirmationRequestEmail')) ->getMock(); $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) @@ -702,6 +716,7 @@ public function testHandleSubscriber($data) $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); + $subscriberMock->expects($this->once())->method('getStoreId')->willReturn($storeId); $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); $subscriberMock->expects($this->exactly($getStoreId))->method('getStoreId')->willReturn($storeId); @@ -718,6 +733,8 @@ public function testHandleSubscriber($data) $observerMock->expects($this->once())->method('addSuccessIfRequired')->with($helperMock); $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); + $apiSubscriberMock->expects($this->exactly($subscriberUpdatedAmount))->method('updateSubscriber')->with($subscriberMock, true); + $subscriberMock->expects($this->exactly($setImportMode))->method('setImportMode')->withConsecutive( array(true), array(false), @@ -726,21 +743,84 @@ public function testHandleSubscriber($data) $subscriberMock->expects($this->exactly($sendConfirmationRequestEmail))->method('sendConfirmationRequestEmail')->willReturn($subscriberMock); - $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); - - $apiSubscriberMock->expects($this->exactly($updateSubscriber))->method('updateSubscriber')->with($subscriberMock, true); - - $observerMock->handleSubscriber($eventObserverMock); + $observerMock->subscriberSaveBefore($eventObserverMock); } - public function handleSubscriberDataProvider() + public function subscriberSaveBeforeDataProvider() { return array( - array(array('magentoMail' => 0, 'setImportMode' => 1, 'updateSubscriber' => 1, 'getStoreId' => 1, 'sendConfirmationRequestEmail' => 0)), - array(array('magentoMail' => 1, 'setImportMode' => 3, 'updateSubscriber' => 0, 'getStoreId' => 1, 'sendConfirmationRequestEmail' => 1)) + array(array('magentoMail' => 0, 'setImportMode' => 1, 'subscriberUpdatedAmount' => 1, 'getStoreId' => 1, 'sendConfirmationRequestEmail' => 0)), + array(array('magentoMail' => 1, 'setImportMode' => 3, 'subscriberUpdatedAmount' => 0, 'getStoreId' => 1, 'sendConfirmationRequestEmail' => 1)) ); } + public function testSubscriberSaveAfter() + { + $storeId = 1; + $params = array(); + + $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getEvent')) + ->getMock(); + + $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('makeHelper', 'getRequest', 'createEmailCookie', 'makeApiSubscriber')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData')) + ->getMock(); + + $eventMock = $this->getMockBuilder(Varien_Event::class) + ->disableOriginalConstructor() + ->setMethods(array('getSubscriber')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('getSubscriberSource', 'getStoreId')) + ->getMock(); + + $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParams')) + ->getMock(); + + $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) + ->disableOriginalConstructor() + ->setMethods(array('updateSubscriber')) + ->getMock(); + + $eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock); + + $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); + + $subscriberMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $observerMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); + + $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); + + $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); + + $observerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); + + $requestMock->expects($this->once())->method('getParams')->willReturn($params); + + $helperMock->expects($this->once())->method('saveInterestGroupData')->with($params, $storeId, null, $subscriberMock); + + $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); + + $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); + + $apiSubscriberMock->expects($this->once())->method('updateSubscriber')->with($subscriberMock, true); + + $observerMock->subscriberSaveAfter($eventObserverMock); + } + /** * @param array $cookieData * @dataProvider loadCustomerToQuoteDataProvider From 1e6f6b9f5c2e770ec85b0fba7ad9dea492aa5dad Mon Sep 17 00:00:00 2001 From: keller Date: Wed, 26 Dec 2018 10:11:36 -0300 Subject: [PATCH 053/113] closes #793 Fix tests + remove duplicate code --- .../MailChimp/Model/ObserverTest.php | 80 +++---------------- 1 file changed, 10 insertions(+), 70 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 2d18a3ffc..e610077bd 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -670,7 +670,6 @@ public function testAddColumnToSalesOrderGridCollection() * @dataProvider subscriberSaveBeforeDataProvider */ public function testSubscriberSaveBefore($data) - { $setImportMode = $data['setImportMode']; $getStoreId = $data['getStoreId']; @@ -713,30 +712,34 @@ public function testSubscriberSaveBefore($data) $eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock); - $observerMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); - $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); $subscriberMock->expects($this->once())->method('getStoreId')->willReturn($storeId); - $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); - - $subscriberMock->expects($this->exactly($getStoreId))->method('getStoreId')->willReturn($storeId); + $observerMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); $subscriberMock->expects($this->once())->method('getIsStatusChanged')->willReturn(true); + + $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); + $subscriberMock->expects($this->once())->method('getStatus')->willReturn(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); $helperMock->expects($this->once())->method('isSubscriptionConfirmationEnabled')->with($storeId)->willReturn(true); - $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); $subscriberMock->expects($this->once())->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $observerMock->expects($this->once())->method('addSuccessIfRequired')->with($helperMock); + $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); + $apiSubscriberMock->expects($this->exactly($subscriberUpdatedAmount))->method('updateSubscriber')->with($subscriberMock, true); + $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); + + $subscriberMock->expects($this->exactly($getStoreId))->method('getStoreId')->willReturn($storeId); + $subscriberMock->expects($this->exactly($setImportMode))->method('setImportMode')->withConsecutive( array(true), array(false), @@ -756,67 +759,6 @@ public function subscriberSaveBeforeDataProvider() ); } - public function testSubscriberSaveAfter() - { - $storeId = 1; - $params = array(); - - $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) - ->disableOriginalConstructor() - ->setMethods(array('getEvent')) - ->getMock(); - - $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) - ->disableOriginalConstructor() - ->setMethods(array('makeHelper', 'getRequest', 'createEmailCookie', 'makeApiSubscriber')) - ->getMock(); - - $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->disableOriginalConstructor() - ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData')) - ->getMock(); - - $eventMock = $this->getMockBuilder(Varien_Event::class) - ->disableOriginalConstructor() - ->setMethods(array('getSubscriber')) - ->getMock(); - - $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) - ->disableOriginalConstructor() - ->setMethods(array('getSubscriberSource', 'getStoreId')) - ->getMock(); - - $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) - ->disableOriginalConstructor() - ->setMethods(array('getParams')) - ->getMock(); - - $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) - ->disableOriginalConstructor() - ->setMethods(array('updateSubscriber')) - ->getMock(); - - $eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock); - - $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); - - $subscriberMock->expects($this->once())->method('getStoreId')->willReturn($storeId); - - $observerMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); - - $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); - - $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); - - $observerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); - - $requestMock->expects($this->once())->method('getParams')->willReturn($params); - - $helperMock->expects($this->once())->method('saveInterestGroupData')->with($params, $storeId, null, $subscriberMock); - - $observerMock->subscriberSaveBefore($eventObserverMock); - } - public function testSubscriberSaveAfter() { $storeId = 1; @@ -878,8 +820,6 @@ public function testSubscriberSaveAfter() $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); - $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); - $apiSubscriberMock->expects($this->once())->method('updateSubscriber')->with($subscriberMock, true); $observerMock->subscriberSaveAfter($eventObserverMock); From da0c285fa235c62b6caaf577ae6d332cb41daabf Mon Sep 17 00:00:00 2001 From: keller Date: Wed, 26 Dec 2018 10:18:02 -0300 Subject: [PATCH 054/113] closes #793 failed trim_trailing_whitespace fixed --- .../tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index e610077bd..39a00cee2 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -202,6 +202,7 @@ public function testChangeStoreName() $stores[] = $storeMock; $eventObserverMock->expects($this->once())->method('getStore')->willReturn($storeMock); + $storeMock->expects($this->once())->method('getId')->willReturn($storeId); $observerMock->expects($this->once())->method('changeStoreNameIfModuleEnabled')->with($storeId); From 9abca6d19efbf94ca19a0d85e7762badd570ac39 Mon Sep 17 00:00:00 2001 From: keller Date: Wed, 26 Dec 2018 10:24:47 -0300 Subject: [PATCH 055/113] closes #793 failed trim_trailing_whitespace fixed --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index a15bbffdb..1f7707e36 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -204,7 +204,6 @@ public function subscriberSaveAfter(Varien_Event_Observer $observer) } return $observer; - } /** From d36284dc02c1f4264cc00b85069aa967ca01f698 Mon Sep 17 00:00:00 2001 From: keller Date: Wed, 26 Dec 2018 10:35:20 -0300 Subject: [PATCH 056/113] closes #793 failed trim_trailing_whitespace fixed --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 1f7707e36..9ab80118f 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -202,7 +202,7 @@ public function subscriberSaveAfter(Varien_Event_Observer $observer) $apiSubscriber->updateSubscriber($subscriber, true); } } - + return $observer; } From 27ad8d086668724d1e4ed411aeb0c37a018d73d2 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Wed, 26 Dec 2018 13:55:22 -0300 Subject: [PATCH 057/113] Flag parent as modified when child product is modified - closes #848 - Ebizmarts_MailChimp_Model_Api_Products --- .../Ebizmarts/MailChimp/Model/Api/Products.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php index 182b1d5e6..aba388530 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php @@ -327,6 +327,10 @@ protected function _buildProductData($product, $magentoStoreId, $isVariant = tru */ public function update($productId, $mailchimpStoreId) { + $parentIdArray = $this->getAllParentIds($productId); + foreach ($parentIdArray as $parentId) { + $this->_updateSyncData($parentId, $mailchimpStoreId, null, null, 1, null, null, true, false); + } $this->_updateSyncData($productId, $mailchimpStoreId, null, null, 1, null, null, true, false); } @@ -755,13 +759,23 @@ public function getProductCategories($product, $magentoStoreId) protected function getParentId($childId) { $parentId = null; - $parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($childId); + $parentIds = $this->getAllParentIds($childId); if (count($parentIds)) { $parentId = $parentIds[0]; } return $parentId; } + /** + * @param $childId + * @return mixed + */ + protected function getAllParentIds($childId) + { + $parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($childId); + return $parentIds; + } + /** * @param $magentoStoreId * @param $parentId From b5072d36158abaf4bcf52c3eb3b65bbfbb03b86d Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Wed, 26 Dec 2018 13:56:33 -0300 Subject: [PATCH 058/113] unitTest - closes #848 - Ebizmarts_MailChimp_Model_Api_ProductsTest --- .../MailChimp/Model/Api/ProductsTest.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/ProductsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/ProductsTest.php index 91d1bf4a3..c210f3e41 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/ProductsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/ProductsTest.php @@ -620,4 +620,45 @@ public function invokeMethod(&$object, $methodName, array $parameters = array()) return $method->invokeArgs($object, $parameters); } + public function testUpdate() + { + $parentIdArray = array(282, 283, 284, 510, 511, 878, 880, 881); + $productId = 877; + $mailchimpStoreId = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; + + $productsApiMock = $this->productsApiMock + ->setMethods(array('getAllParentIds', '_updateSyncData')) + ->getMock(); + + $productIdArrayMock = $this->getMockBuilder(ArrayObject::class) + ->setMethods(array('getIterator')) + ->getMock(); + + $productsApiMock->expects($this->once()) + ->method('getAllParentIds') + ->with($productId) + ->willReturn($productIdArrayMock); + + + $productIdArrayMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator($parentIdArray)); + + $productsApiMock->expects($this->exactly(9)) + ->method('_updateSyncData') + ->withConsecutive( + array($parentIdArray[0], $mailchimpStoreId, null, null, 1, null, null, true, false), + array($parentIdArray[1], $mailchimpStoreId, null, null, 1, null, null, true, false), + array($parentIdArray[2], $mailchimpStoreId, null, null, 1, null, null, true, false), + array($parentIdArray[3], $mailchimpStoreId, null, null, 1, null, null, true, false), + array($parentIdArray[4], $mailchimpStoreId, null, null, 1, null, null, true, false), + array($parentIdArray[5], $mailchimpStoreId, null, null, 1, null, null, true, false), + array($parentIdArray[6], $mailchimpStoreId, null, null, 1, null, null, true, false), + array($parentIdArray[7], $mailchimpStoreId, null, null, 1, null, null, true, false), + array($productId, $mailchimpStoreId, null, null, 1, null, null, true, false) + ); + + $productsApiMock->update($productId, $mailchimpStoreId); + } + } From 1cb0ecb0a18489a4eaf82906da88942c94378ecb Mon Sep 17 00:00:00 2001 From: keller Date: Wed, 26 Dec 2018 16:42:59 -0300 Subject: [PATCH 059/113] closes #793 Add missing checking in subscribeSaveAfter --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 9ab80118f..a8d2212a8 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -198,8 +198,10 @@ public function subscriberSaveAfter(Varien_Event_Observer $observer) $this->createEmailCookie($subscriber); - $apiSubscriber = $this->makeApiSubscriber(); - $apiSubscriber->updateSubscriber($subscriber, true); + if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { + $apiSubscriber = $this->makeApiSubscriber(); + $apiSubscriber->updateSubscriber($subscriber, true); + } } } From b5ec4d4dee8831684df48c9d1e3c334e67e5048b Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 26 Dec 2018 17:28:41 -0300 Subject: [PATCH 060/113] Confirm email working. --- .../Ebizmarts/MailChimp/Model/Observer.php | 8 ++------ .../Ebizmarts/MailChimp/Model/Subscriber.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index a8d2212a8..835d17d8a 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -165,12 +165,6 @@ public function subscriberSaveBefore(Varien_Event_Observer $observer) $apiSubscriber->updateSubscriber($subscriber, true); } } - } else { - //Use Magento emails, setImportMode to false to allow email to be sent, set back to true so subscribe() function doesn't send it again - $subscriber->setImportMode(false); - $subscriber->sendConfirmationRequestEmail(); - $subscriber->setImportMode(true); - } } } @@ -201,6 +195,8 @@ public function subscriberSaveAfter(Varien_Event_Observer $observer) if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { $apiSubscriber = $this->makeApiSubscriber(); $apiSubscriber->updateSubscriber($subscriber, true); + } else { + $subscriber->setImportMode(false); } } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php index 1e1026e17..efc4ee10c 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Subscriber.php @@ -36,4 +36,17 @@ public function sendConfirmationSuccessEmail() return parent::sendConfirmationSuccessEmail(); } } + + public function confirm($code) + { + if($this->getCode()==$code) { + $this->setStatus(self::STATUS_SUBSCRIBED) + ->setIsStatusChanged(true) + ->setSubscriberSource(Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) + ->save(); + return true; + } + + return false; + } } From b0f74596dcc4b85943ec4fbf3ac6a13c25a6123a Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 26 Dec 2018 17:35:07 -0300 Subject: [PATCH 061/113] Fixed comments. --- .../Ebizmarts/MailChimp/Model/Observer.php | 80 +++++++++---------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 835d17d8a..125cb1a4b 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -126,44 +126,41 @@ public function saveConfig(Varien_Event_Observer $observer) } /** - * Prevent Magento's subscription confirmation email to be sent if MailChimp For Magento is enabled. - * Override Magento's default status if confirmation email is enabled and the customer is logged in. + * Handle confirmation emails and subscription to Mailchimp * * @param Varien_Event_Observer $observer * @return Varien_Event_Observer - * @throws Mage_Core_Model_Store_Exception + * @throws Mage_Core_Exception */ public function subscriberSaveBefore(Varien_Event_Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); $storeId = $subscriber->getStoreId(); $helper = $this->makeHelper(); + $isEnabled = $helper->isSubscriptionEnabled($storeId); - if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { - $isEnabled = $helper->isSubscriptionEnabled($storeId); - if ($isEnabled) { - $statusChanged = $subscriber->getIsStatusChanged(); - $apiSubscriber = $this->makeApiSubscriber(); + if ($isEnabled && $subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { + $statusChanged = $subscriber->getIsStatusChanged(); + $apiSubscriber = $this->makeApiSubscriber(); - //Override Magento status to always send double opt-in confirmation. - if ($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId)) { - $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); - $this->addSuccessIfRequired($helper); - } - $subscriber->setImportMode(true); + //Override Magento status to always send double opt-in confirmation. + if ($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId)) { + $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); + $this->addSuccessIfRequired($helper); + } + $subscriber->setImportMode(true); + + if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { + //Use MailChimp emails + if ($statusChanged) { + $apiSubscriber->updateSubscriber($subscriber, true); + } else { + $origData = $subscriber->getOrigData(); - if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { - //Use MailChimp emails - if ($statusChanged) { + if (is_array($origData) && isset($origData['subscriber_status']) + && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() + ) { $apiSubscriber->updateSubscriber($subscriber, true); - } else { - $origData = $subscriber->getOrigData(); - - if (is_array($origData) && isset($origData['subscriber_status']) - && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() - ) { - $apiSubscriber->updateSubscriber($subscriber, true); - } } } } @@ -173,9 +170,11 @@ public function subscriberSaveBefore(Varien_Event_Observer $observer) } /** - * Hnalde Subscriber changes update. + * Handle interest groups for subscriber and allow Magento email to be sent if configured that way. * * @param Varien_Event_Observer $observer + * @return Varien_Event_Observer + * @throws Mage_Core_Exception * @throws Mage_Core_Model_Store_Exception */ public function subscriberSaveAfter(Varien_Event_Observer $observer) @@ -183,21 +182,19 @@ public function subscriberSaveAfter(Varien_Event_Observer $observer) $subscriber = $observer->getEvent()->getSubscriber(); $storeId = $subscriber->getStoreId(); $helper = $this->makeHelper(); + $isEnabled = $helper->isSubscriptionEnabled($storeId); - if ($subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { - $isEnabled = $helper->isSubscriptionEnabled($storeId); - if ($isEnabled) { - $params = $this->getRequest()->getParams(); - $helper->saveInterestGroupData($params, $storeId, null, $subscriber); + if ($isEnabled && $subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { + $params = $this->getRequest()->getParams(); + $helper->saveInterestGroupData($params, $storeId, null, $subscriber); - $this->createEmailCookie($subscriber); + $this->createEmailCookie($subscriber); - if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { - $apiSubscriber = $this->makeApiSubscriber(); - $apiSubscriber->updateSubscriber($subscriber, true); - } else { - $subscriber->setImportMode(false); - } + if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { + $apiSubscriber = $this->makeApiSubscriber(); + $apiSubscriber->updateSubscriber($subscriber, true); + } else { + $subscriber->setImportMode(false); } } @@ -354,7 +351,7 @@ public function newOrder(Varien_Event_Observer $observer) $email = $order->getCustomerEmail(); $subscriber = $helper->loadListSubscriber($post, $email); if ($subscriber) { - if(!$subscriber->getCustomerId()) { + if (!$subscriber->getCustomerId()) { $subscriber->setSubscriberFirstname($order->getCustomerFirstname()); $subscriber->setSubscriberLastname($order->getCustomerLastname()); } @@ -542,7 +539,6 @@ public function addColumnToSalesOrderGrid(Varien_Event_Observer $observer) } - public function addColumnToSalesOrderGridCollection(Varien_Event_Observer $observer) { @@ -732,7 +728,7 @@ public function productSaveBefore(Varien_Event_Observer $observer) $ecommEnabled = $helper->isEcommerceEnabled($scopeArray['scope_id'], $scopeArray['scope']); if ($ecommEnabled) { - if ($product->getStatus() == self::PRODUCT_IS_DISABLED){ + if ($product->getStatus() == self::PRODUCT_IS_DISABLED) { $apiProduct->updateDisabledProducts($product->getId(), $mailchimpStoreId); } else { $apiProduct->update($product->getId(), $mailchimpStoreId); @@ -1014,7 +1010,7 @@ public function addCustomerTab(Varien_Event_Observer $observer) $helper = $this->makeHelper(); // add tab in customer edit page if ($block instanceof Mage_Adminhtml_Block_Customer_Edit_Tabs) { - $customerId = (int) $this->getRequest()->getParam('id'); + $customerId = (int)$this->getRequest()->getParam('id'); $customer = Mage::getModel('customer/customer')->load($customerId); $storeId = $customer->getStoreId(); //If the customer was created in the admin panel use the store view selected for MailChimp. From 6e7ff42cbea7271fe559086b3f654e99a178359c Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 26 Dec 2018 17:45:23 -0300 Subject: [PATCH 062/113] Fixed test. --- .../app/Ebizmarts/MailChimp/Model/ObserverTest.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 39a00cee2..0c6c6d037 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -702,8 +702,7 @@ public function testSubscriberSaveBefore($data) $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() - ->setMethods(array('getSubscriberSource', 'getIsStatusChanged', 'getStatus', 'setStatus', 'setImportMode', - 'getStoreId', 'sendConfirmationRequestEmail')) + ->setMethods(array('getSubscriberSource', 'getIsStatusChanged', 'getStatus', 'setStatus', 'getStoreId')) ->getMock(); $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) @@ -741,13 +740,6 @@ public function testSubscriberSaveBefore($data) $subscriberMock->expects($this->exactly($getStoreId))->method('getStoreId')->willReturn($storeId); - $subscriberMock->expects($this->exactly($setImportMode))->method('setImportMode')->withConsecutive( - array(true), - array(false), - array(true) - ); - - $subscriberMock->expects($this->exactly($sendConfirmationRequestEmail))->method('sendConfirmationRequestEmail')->willReturn($subscriberMock); $observerMock->subscriberSaveBefore($eventObserverMock); } From d60e2920d908ebd6bc6d16c5283981413bf2a130 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 28 Dec 2018 09:58:12 -0300 Subject: [PATCH 063/113] Code refactor, extract methods. --- .../Ebizmarts/MailChimp/Model/Api/Carts.php | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php index 986c1e667..e1f3473a8 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -56,9 +56,10 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId) * @param $magentoStoreId * @return array */ - protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) + public function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) { $mailchimpTableName = $this->getMailchimpEcommerceDataTableName(); + $batchId = $this->getBatchId(); $allCarts = array(); $convertedCarts = $this->getQuoteCollection(); // get only the converted quotes @@ -85,7 +86,7 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) if ($alreadySentCartId != $cartId) { $allCarts[$counter]['method'] = 'DELETE'; $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $alreadySentCartId; - $allCarts[$counter]['operation_id'] = $this->getBatchId() . '_' . $alreadySentCartId; + $allCarts[$counter]['operation_id'] = $batchId . '_' . $alreadySentCartId; $allCarts[$counter]['body'] = ''; $this->_updateSyncData($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1); $this->setCounter($this->getCounter()+1); @@ -96,7 +97,7 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) $counter = $this->getCounter(); $allCarts[$counter]['method'] = 'DELETE'; $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $cartId; - $allCarts[$counter]['operation_id'] = $this->getBatchId() . '_' . $cartId; + $allCarts[$counter]['operation_id'] = $batchId . '_' . $cartId; $allCarts[$counter]['body'] = ''; $this->_updateSyncData($cartId, $mailchimpStoreId, null, null, null, null, 1); $this->setCounter($this->getCounter()+1); @@ -191,9 +192,10 @@ public function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) * @param $mailchimpStoreId * @return array */ - protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) + public function _getNewQuotes($mailchimpStoreId, $magentoStoreId) { $helper = $this->getHelper(); + $batchId = $this->getBatchId(); $allCarts = array(); $newCarts = $this->getQuoteCollection(); $newCarts->addFieldToFilter('is_active', array('eq' => 1)); @@ -203,8 +205,8 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $newCarts->addFieldToFilter('store_id', array('eq' => $magentoStoreId)); $helper->addResendFilter($newCarts, $magentoStoreId, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); // filter by first date if exists. - if ($this->_firstDate) { - $newCarts->addFieldToFilter('updated_at', array('gt' => $this->_firstDate)); + if ($this->getFirstDate()) { + $newCarts->addFieldToFilter('updated_at', array('gt' => $this->getFirstDate())); } //join with mailchimp_ecommerce_sync_data table to filter by sync data. @@ -216,9 +218,9 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) foreach ($newCarts as $cart) { $cartId = $cart->getEntityId(); - $orderCollection = Mage::getResourceModel('sales/order_collection'); - $orderCollection->addFieldToFilter('main_table.customer_email', array('eq' => $cart->getCustomerEmail())) - ->addFieldToFilter('main_table.updated_at', array('from' => $cart->getUpdatedAt())); + $orderCollection = $this->getOrderCollection(); + $orderCollection->addFieldToFilter('main_table.customer_email', array('eq' => $cart->getCustomerEmail())); + $orderCollection->addFieldToFilter('main_table.updated_at', array('from' => $cart->getUpdatedAt())); //if cart is empty or customer has an order made after the abandonment skip current cart. if (!count($cart->getAllVisibleItems()) || $orderCollection->getSize()) { $this->_updateSyncData($cartId, $mailchimpStoreId); @@ -235,7 +237,7 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $alreadySentCartId = $cartForEmail->getEntityId(); $allCarts[$counter]['method'] = 'DELETE'; $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/' . $alreadySentCartId; - $allCarts[$counter]['operation_id'] = $this->getBatchId() . '_' . $alreadySentCartId; + $allCarts[$counter]['operation_id'] = $batchId . '_' . $alreadySentCartId; $allCarts[$counter]['body'] = ''; $this->_updateSyncData($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1); $this->setCounter($this->getCounter()+1); @@ -258,7 +260,7 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $counter = $this->getCounter(); $allCarts[$counter]['method'] = 'POST'; $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts'; - $allCarts[$counter]['operation_id'] = $this->getBatchId() . '_' . $cartId; + $allCarts[$counter]['operation_id'] = $batchId . '_' . $cartId; $allCarts[$counter]['body'] = $cartJson; $this->setCounter($this->getCounter()+1); $this->_updateSyncData($cartId, $mailchimpStoreId, null, null, null, null, null, $this->getToken()); @@ -655,4 +657,20 @@ public function setToken($token) $this->_token = $token; } + + /** + * @return mixed + */ + protected function getFirstDate() + { + return $this->_firstDate; + } + + /** + * @return Object + */ + protected function getOrderCollection() + { + return Mage::getResourceModel('sales/order_collection'); + } } From 21e5f9cfc594816c860518942b93191c55e65640 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 28 Dec 2018 10:09:23 -0300 Subject: [PATCH 064/113] test for newQuotes of Api carts --- .../MailChimp/Model/Api/CartsTest.php | 360 +++++++++++++++++- 1 file changed, 357 insertions(+), 3 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 111b3dcb9..959367b4d 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -56,11 +56,113 @@ public function testCreateBatchJson() $cartsApiMock->createBatchJson($mailchimpStoreId, $magentoStoreId); } + public function testGetConvertedQuotes () + { + $mailchimpStoreId = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; + $magentoStoreId = 1; + $mailchimpTableName = 'mailchimp_ecommerce_sync_data'; + $batchLimitFromConfig = 100; + $customerEmailAddressByCart = 'luciaines@ebizmarts.com'; + $counter = 0; + $alreadySentCartId = 2; + $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"luciaines@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; + $cartId = 1; + $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; + $stringIsActive = 'is_active'; + $stringStoreId = 'store_id'; + $arrayAddFieldToFilter = array('eq' => 0); + $arrayAddFieldToFilterStoreId = array('eq' => $magentoStoreId); + + + $cartsApiMock = $this->cartsApiMock->setMethods(array( + 'getMailchimpEcommerceDataTableName', + 'getQuoteCollection', + 'getBatchLimitFromConfig', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + '_updateSyncData', + 'setCounter' + )) + ->getMock(); + + $quoteResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('joinLeft', 'where', 'limit')) + ->getMock(); + + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mailchimpTableName); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($quoteResoureceCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn($batchLimitFromConfig); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->willReturn($customerEmailAddressByCart); + $cartsApiMock->expects($this->exactly(2)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + $counter, + $counter + ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1), + array($cartId, $mailchimpStoreId, null, null, null, null, null, $token) + ); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array($counter + 1), + array($counter + 1) + ); + + $quoteResoureceCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringIsActive, $arrayAddFieldToFilter), + array($stringStoreId, $arrayAddFieldToFilterStoreId) + ); + $quoteResoureceCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $quoteResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $cartsApiMock->_getConvertedQuotes($mailchimpStoreId, $magentoStoreId); + } + public function testGetModifiedQuotes() { $mcTableName = 'mailchimp_ecommerce_sync_data'; $batchLimitFromConfig = 100; - $magentoStoreId = 0; + $magentoStoreId = 1; $webSiteIdFromMagentoStoreId = 0; $mailchimpStoreId = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; $customerEmailAddressByCart = 'luciaines@ebizmarts.com'; @@ -72,8 +174,6 @@ public function testGetModifiedQuotes() $customerId = 1; $stringIsActive = 'is_active'; $stringStoreId = 'store_id'; - $stringCustomerEmail = 'customer_email'; - $arrayAddFieldToFilterCustomerEmail = array('eq' => $customerEmailAddressByCart); $arrayAddFieldToFilter = array('eq' => 1); $arrayAddFieldToFilterStoreId = array('eq' => $magentoStoreId); $where = "m4m.mailchimp_sync_deleted = 0 @@ -249,4 +349,258 @@ public function testGetModifiedQuotes() $cartsApiMock->_getModifiedQuotes($mailchimpStoreId, $magentoStoreId); } + + public function testGetNewQuotes() + { + $existFirstDate = '2018-11-30'; + $mailchimpStoreId = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; + $batchLimitFromConfig = 100; + $alreadySentCartId = 2; + $cartId = 1; + $customerId = 1; + $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; + $magentoStoreId = 2; + $webSiteIdFromMagentoStoreId = 0; + $customerEmailAddressByCart = 'luciaines@ebizmarts.com'; + $customerEmailAddress = ''; + $counter = 0; + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.$mailchimpStoreId.'/carts/'. $alreadySentCartId, 'operation_id' => self::BATCH_ID . '_' . $alreadySentCartId, 'body' => '')); + $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"luciaines@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; + $stringIsActive = 'is_active'; + $stringStoreId = 'store_id'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => $magentoStoreId); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => 'luciaines@ebizmarts.com'); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + 'setCounter', + 'addProductNotSentData', + '_makeCart', + 'setToken', + 'getToken', + 'getOrderCollection' + )) + ->getMock(); + + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->getMock(); + + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, $mailchimpStoreId); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn($batchLimitFromConfig); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1), + array($cartId, $mailchimpStoreId, null, null, null, null, null, $token) + ); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with($magentoStoreId) + ->willReturn($webSiteIdFromMagentoStoreId); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with($customerEmailAddressByCart,$mailchimpStoreId, $magentoStoreId) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + $counter, + $counter, + $counter, + $counter + ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array($counter + 1), + array($counter + 1) + ); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with($mailchimpStoreId, $magentoStoreId, $cartModelMock, $allCarts) + ->willReturn($allCarts); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, $mailchimpStoreId, $magentoStoreId) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('getToken') + ->willReturn($token); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, $magentoStoreId, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringIsActive, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array($stringStoreId, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with($batchLimitFromConfig); + + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with($webSiteIdFromMagentoStoreId); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with($customerEmailAddressByCart); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + + $cartModelMock->expects($this->exactly(4)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + $customerEmailAddressByCart, + $customerEmailAddressByCart, + $customerEmailAddressByCart, + $customerEmailAddressByCart + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn($cartId); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn($alreadySentCartId); + + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + + $cartsApiMock->_getNewQuotes($mailchimpStoreId, $magentoStoreId); + } } From 1b3d153f84000c10b51e415b62e313f07d10f64c Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 28 Dec 2018 11:33:01 -0300 Subject: [PATCH 065/113] refactore some variables as constant that were used in two test or more --- .../MailChimp/Model/Api/CartsTest.php | 289 ++++++------------ 1 file changed, 95 insertions(+), 194 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 959367b4d..7a529c17d 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -9,6 +9,16 @@ class Ebizmarts_MailChimp_Model_Api_CartsTest extends PHPUnit_Framework_TestCase const DATE = '2017-05-18-14-45-54-38849500'; const BATCH_ID = 'storeid-1_QUO_2017-05-18-14-45-54-38849500'; + const MAILCHIMP_STORE_ID = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; + const BATCH_LIMIT_FROM_CONFIG = 100; + const MAGENTO_STORE_ID = 1; + const ALREADY_SENT_CART_ID = 2; + const WEB_SITE_ID_FROM_MAGENTO_STORE_ID = 0; + const CUSTOMER_EMAIL_BY_CART = 'test@ebizmarts.com'; + const CART_ID = 1; + const COUNTER = 0; + const STRING_IS_ACTIVE = 'is_active'; + const STRING_STORE_ID = 'store_id'; public function setUp() { @@ -24,8 +34,6 @@ public function tearDown() public function testCreateBatchJson() { - $mailchimpStoreId = 'a1s2d3f4g5h6j7k8l9n0'; - $magentoStoreId = 1; $batchArray = array(); $cartsApiMock = $this->cartsApiMock->setMethods(array( @@ -44,150 +52,53 @@ public function testCreateBatchJson() $cartsApiMock->expects($this->once())->method('setBatchId')->with(self::BATCH_ID); $cartsApiMock->expects($this->once())->method('getHelper')->willReturn($helperMock); - $cartsApiMock->expects($this->once())->method('_getConvertedQuotes')->with($mailchimpStoreId, $magentoStoreId)->willReturn($batchArray); - $cartsApiMock->expects($this->once())->method('_getModifiedQuotes')->with($mailchimpStoreId, $magentoStoreId)->willReturn($batchArray); - $cartsApiMock->expects($this->once())->method('_getNewQuotes')->with($mailchimpStoreId, $magentoStoreId)->willReturn($batchArray); + $cartsApiMock->expects($this->once())->method('_getConvertedQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); + $cartsApiMock->expects($this->once())->method('_getModifiedQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); + $cartsApiMock->expects($this->once())->method('_getNewQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); - $helperMock->expects($this->once())->method('isAbandonedCartEnabled')->with($magentoStoreId)->willReturn(true); - $helperMock->expects($this->once())->method('getAbandonedCartFirstDate')->with($magentoStoreId)->willReturn('00-00-00 00:00:00'); + $helperMock->expects($this->once())->method('isAbandonedCartEnabled')->with(self::MAGENTO_STORE_ID)->willReturn(true); + $helperMock->expects($this->once())->method('getAbandonedCartFirstDate')->with(self::MAGENTO_STORE_ID)->willReturn('00-00-00 00:00:00'); $helperMock->expects($this->once())->method('getDateMicrotime')->willReturn(self::DATE); - $helperMock->expects($this->once())->method('getResendTurn')->with($magentoStoreId)->willReturn(null); + $helperMock->expects($this->once())->method('getResendTurn')->with(self::MAGENTO_STORE_ID)->willReturn(null); - $cartsApiMock->createBatchJson($mailchimpStoreId, $magentoStoreId); - } - - public function testGetConvertedQuotes () - { - $mailchimpStoreId = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; - $magentoStoreId = 1; - $mailchimpTableName = 'mailchimp_ecommerce_sync_data'; - $batchLimitFromConfig = 100; - $customerEmailAddressByCart = 'luciaines@ebizmarts.com'; - $counter = 0; - $alreadySentCartId = 2; - $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"luciaines@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; - $cartId = 1; - $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; - $stringIsActive = 'is_active'; - $stringStoreId = 'store_id'; - $arrayAddFieldToFilter = array('eq' => 0); - $arrayAddFieldToFilterStoreId = array('eq' => $magentoStoreId); - - - $cartsApiMock = $this->cartsApiMock->setMethods(array( - 'getMailchimpEcommerceDataTableName', - 'getQuoteCollection', - 'getBatchLimitFromConfig', - '_getAllCartsByEmail', - 'getCounter', - 'getBatchId', - '_updateSyncData', - 'setCounter' - )) - ->getMock(); - - $quoteResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('joinLeft', 'where', 'limit')) - ->getMock(); - - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail')) - ->getMock(); - - $cartsApiMock->expects($this->once()) - ->method('getMailchimpEcommerceDataTableName') - ->willReturn($mailchimpTableName); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($quoteResoureceCollectionMock); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn($batchLimitFromConfig); - $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->willReturn($customerEmailAddressByCart); - $cartsApiMock->expects($this->exactly(2)) - ->method('getCounter') - ->willReturnOnConsecutiveCalls( - $counter, - $counter - ); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1), - array($cartId, $mailchimpStoreId, null, null, null, null, null, $token) - ); - $cartsApiMock->expects($this->exactly(2)) - ->method('setCounter') - ->withConsecutive( - array($counter + 1), - array($counter + 1) - ); - - $quoteResoureceCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array($stringIsActive, $arrayAddFieldToFilter), - array($stringStoreId, $arrayAddFieldToFilterStoreId) - ); - $quoteResoureceCollectionMock->expects($this->exactly(3)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock, - $varienSelectMock - ); - $quoteResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - - $cartsApiMock->_getConvertedQuotes($mailchimpStoreId, $magentoStoreId); + $cartsApiMock->createBatchJson(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } public function testGetModifiedQuotes() { $mcTableName = 'mailchimp_ecommerce_sync_data'; - $batchLimitFromConfig = 100; - $magentoStoreId = 1; - $webSiteIdFromMagentoStoreId = 0; - $mailchimpStoreId = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; - $customerEmailAddressByCart = 'luciaines@ebizmarts.com'; $customerEmailAddress = ''; - $counter = 0; - $alreadySentCartId = 2; - $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"luciaines@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; - $cartId = 1; + $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"test@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; $customerId = 1; - $stringIsActive = 'is_active'; - $stringStoreId = 'store_id'; $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => $magentoStoreId); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); $where = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_sync_delta < updated_at"; - $whereGetAllCartsByEmail = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'"; + $whereGetAllCartsByEmail = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $arrayTableName = array('m4m' => $mcTableName); $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' - AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'"; + AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $m4m = array('m4m.*'); - $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.$mailchimpStoreId.'/carts/'. $alreadySentCartId, 'operation_id' => self::BATCH_ID . '_' . $alreadySentCartId, 'body' => '')); + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; $cartsApiMock = $this->cartsApiMock->setMethods( - array('setToken', 'getToken', 'getBatchId', 'getMailchimpEcommerceDataTableName', 'getBatchLimitFromConfig', '_updateSyncData', 'getQuoteCollection', 'getCustomerModel', 'getWebSiteIdFromMagentoStoreId', 'setCounter', 'getCounter', '_makeCart', '_getAllCartsByEmail', 'addProductNotSentData')) + array( + 'setToken', + 'getToken', + 'getBatchId', + 'getMailchimpEcommerceDataTableName', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getQuoteCollection', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'setCounter', + 'getCounter', + '_makeCart', + '_getAllCartsByEmail', + 'addProductNotSentData' + )) ->getMock(); $cartModelMock = $this @@ -243,57 +154,57 @@ public function testGetModifiedQuotes() ->willReturn($quoteResoureceCollectionMock); $cartsApiMock->expects($this->once()) ->method('getBatchLimitFromConfig') - ->willReturn($batchLimitFromConfig); + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); $cartsApiMock->expects($this->once()) ->method('getCustomerModel') ->willReturn($customerModelMock); $cartsApiMock->expects($this->once()) ->method('getWebSiteIdFromMagentoStoreId') - ->with($magentoStoreId) - ->willReturn($webSiteIdFromMagentoStoreId); + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) ->method('_getAllCartsByEmail') - ->with($customerEmailAddressByCart,$mailchimpStoreId, $magentoStoreId) + ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(4)) ->method('getCounter') ->willReturnOnConsecutiveCalls( - $counter, - $counter, - $counter, - $counter + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER ); $cartsApiMock->expects($this->exactly(2)) ->method('_updateSyncData') ->withConsecutive( - array($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1), - array($cartId, $mailchimpStoreId, null, null, null, null, null, $token) + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) ); $cartsApiMock->expects($this->exactly(2)) ->method('setCounter') ->withConsecutive( - array($counter + 1), - array($counter + 1) + array(self::COUNTER + 1), + array(self::COUNTER + 1) ); $cartsApiMock->expects($this->once()) ->method('_makeCart') - ->with($cartModelMock, $mailchimpStoreId, $magentoStoreId, true) + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) ->willReturn($cartJson); $cartsApiMock->expects($this->once()) ->method('addProductNotSentData') - ->with($mailchimpStoreId, $magentoStoreId, $cartModelMock, $allCarts) + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) ->willReturn($allCarts); $cartModelMock->expects($this->exactly(3)) ->method('getCustomerEmail') ->willReturnOnConsecutiveCalls( - $customerEmailAddressByCart, - $customerEmailAddressByCart, - $customerEmailAddressByCart + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART ); $cartModelMock->expects($this->once()) ->method('getEntityId') - ->willReturn($cartId); + ->willReturn(self::CART_ID); $cartModelMock->expects($this->once()) ->method('getCustomerId') ->willReturn($customerId); @@ -301,8 +212,8 @@ public function testGetModifiedQuotes() $quoteResoureceCollectionMock->expects($this->exactly(2)) ->method('addFieldToFilter') ->withConsecutive( - array($stringIsActive, $arrayAddFieldToFilter), - array($stringStoreId, $arrayAddFieldToFilterStoreId) + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) ); $quoteResoureceCollectionMock->expects($this->exactly(3)) ->method('getSelect') @@ -324,14 +235,14 @@ public function testGetModifiedQuotes() ->with($where); $varienSelectMock->expects($this->once()) ->method('limit') - ->with($batchLimitFromConfig); + ->with(self::BATCH_LIMIT_FROM_CONFIG); $customerModelMock->expects($this->once()) ->method('setWebsiteId') - ->with($webSiteIdFromMagentoStoreId); + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $customerModelMock->expects($this->once()) ->method('loadByEmail') - ->with($customerEmailAddressByCart); + ->with(self::CUSTOMER_EMAIL_BY_CART); $customerModelMock->expects($this->once()) ->method('getEmail') ->willReturn($customerEmailAddress); @@ -345,29 +256,19 @@ public function testGetModifiedQuotes() $cartByEmailModelMock->expects($this->once()) ->method('getEntityId') - ->willReturn($alreadySentCartId); + ->willReturn(self::ALREADY_SENT_CART_ID); - $cartsApiMock->_getModifiedQuotes($mailchimpStoreId, $magentoStoreId); + $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } public function testGetNewQuotes() { $existFirstDate = '2018-11-30'; - $mailchimpStoreId = '3ade9d9e52e35e9b18d95bdd4d9e9a44'; - $batchLimitFromConfig = 100; - $alreadySentCartId = 2; - $cartId = 1; $customerId = 1; $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; - $magentoStoreId = 2; - $webSiteIdFromMagentoStoreId = 0; - $customerEmailAddressByCart = 'luciaines@ebizmarts.com'; $customerEmailAddress = ''; - $counter = 0; - $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.$mailchimpStoreId.'/carts/'. $alreadySentCartId, 'operation_id' => self::BATCH_ID . '_' . $alreadySentCartId, 'body' => '')); - $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"luciaines@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; - $stringIsActive = 'is_active'; - $stringStoreId = 'store_id'; + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"test@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; $stringCustomerEmail = 'customer_email'; $stringItemsCount = 'items_count'; $stringUpdatedAt = 'updated_at'; @@ -375,11 +276,11 @@ public function testGetNewQuotes() $arrayAddFieldToFilterItemsCount = array('gt' => 0); $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => $magentoStoreId); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); $where = "m4m.mailchimp_sync_delta IS NULL"; $allVisbleItems = array('item'); $sizeOrderCollection = 0; - $addFieldToFilterOrderCollection = array('eq' => 'luciaines@ebizmarts.com'); + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); $stringCustomerEmailMainTable = 'main_table.customer_email'; $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); @@ -467,34 +368,34 @@ public function testGetNewQuotes() ); $cartsApiMock->expects($this->once()) ->method('joinMailchimpSyncDataWithoutWhere') - ->with($newCartsCollectionMock, $mailchimpStoreId); + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); $cartsApiMock->expects($this->once()) ->method('getBatchLimitFromConfig') - ->willReturn($batchLimitFromConfig); + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); $cartsApiMock->expects($this->exactly(2)) ->method('_updateSyncData') ->withConsecutive( - array($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1), - array($cartId, $mailchimpStoreId, null, null, null, null, null, $token) + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) ); $cartsApiMock->expects($this->once()) ->method('getCustomerModel') ->willReturn($customerModelMock); $cartsApiMock->expects($this->once()) ->method('getWebSiteIdFromMagentoStoreId') - ->with($magentoStoreId) - ->willReturn($webSiteIdFromMagentoStoreId); + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) ->method('_getAllCartsByEmail') - ->with($customerEmailAddressByCart,$mailchimpStoreId, $magentoStoreId) + ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(4)) ->method('getCounter') ->willReturnOnConsecutiveCalls( - $counter, - $counter, - $counter, - $counter + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER ); $cartsApiMock->expects($this->once()) ->method('getBatchId') @@ -502,16 +403,16 @@ public function testGetNewQuotes() $cartsApiMock->expects($this->exactly(2)) ->method('setCounter') ->withConsecutive( - array($counter + 1), - array($counter + 1) + array(self::COUNTER + 1), + array(self::COUNTER + 1) ); $cartsApiMock->expects($this->once()) ->method('addProductNotSentData') - ->with($mailchimpStoreId, $magentoStoreId, $cartModelMock, $allCarts) + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) ->willReturn($allCarts); $cartsApiMock->expects($this->once()) ->method('_makeCart') - ->with($cartModelMock, $mailchimpStoreId, $magentoStoreId) + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($cartJson); $cartsApiMock->expects($this->once()) ->method('getToken') @@ -525,15 +426,15 @@ public function testGetNewQuotes() $helperMock->expects($this->once()) ->method('addResendFilter') - ->with($newCartsCollectionMock, $magentoStoreId, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); $newCartsCollectionMock->expects($this->exactly(5)) ->method('addFieldToFilter') ->withConsecutive( - array($stringIsActive, $arrayAddFieldToFilter), + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), array($stringItemsCount, $arrayAddFieldToFilterItemsCount), - array($stringStoreId, $arrayAddFieldToFilterStoreId), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) ); $newCartsCollectionMock->expects($this->exactly(2)) @@ -551,14 +452,14 @@ public function testGetNewQuotes() ->with($where); $varienSelectMock->expects($this->once()) ->method('limit') - ->with($batchLimitFromConfig); + ->with(self::BATCH_LIMIT_FROM_CONFIG); $customerModelMock->expects($this->once()) ->method('setWebsiteId') - ->with($webSiteIdFromMagentoStoreId); + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $customerModelMock->expects($this->once()) ->method('loadByEmail') - ->with($customerEmailAddressByCart); + ->with(self::CUSTOMER_EMAIL_BY_CART); $customerModelMock->expects($this->once()) ->method('getEmail') ->willReturn($customerEmailAddress); @@ -572,14 +473,14 @@ public function testGetNewQuotes() $cartModelMock->expects($this->exactly(4)) ->method('getCustomerEmail') ->willReturnOnConsecutiveCalls( - $customerEmailAddressByCart, - $customerEmailAddressByCart, - $customerEmailAddressByCart, - $customerEmailAddressByCart + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART ); $cartModelMock->expects($this->once()) ->method('getEntityId') - ->willReturn($cartId); + ->willReturn(self::CART_ID); $cartModelMock->expects($this->once()) ->method('getCustomerId') ->willReturn($customerId); @@ -589,7 +490,7 @@ public function testGetNewQuotes() $cartByEmailModelMock->expects($this->once()) ->method('getEntityId') - ->willReturn($alreadySentCartId); + ->willReturn(self::ALREADY_SENT_CART_ID); $orderCollectionMock->expects($this->once()) ->method('getSize') @@ -601,6 +502,6 @@ public function testGetNewQuotes() array($stringUpdated, $addFieldToFilterUpdated) ); - $cartsApiMock->_getNewQuotes($mailchimpStoreId, $magentoStoreId); + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } } From 5a315d30791c18b619272899b3963c9189d0e54f Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 28 Dec 2018 11:34:20 -0300 Subject: [PATCH 066/113] Added the returned type and the description to the refactored methods --- .../Ebizmarts/MailChimp/Model/Api/Carts.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php index e1f3473a8..b6a55e625 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -28,6 +28,7 @@ class Ebizmarts_MailChimp_Model_Api_Carts */ public function createBatchJson($mailchimpStoreId, $magentoStoreId) { + /** @var Ebizmarts_MailChimp_Helper_Data $helper */ $helper = $this->getHelper(); $allCarts = array(); if (!$helper->isAbandonedCartEnabled($magentoStoreId)) { @@ -629,7 +630,7 @@ public function setCounter($counter) } /** - * @return string + * @return Return the batchId for the batchJson of the carts. string */ public function getBatchId() { @@ -645,13 +646,16 @@ public function setBatchId($batchId) } /** - * @return null + * @return MD5 of the cart. string */ public function getToken() { return $this->_token; } + /** + * @param $token + */ public function setToken($token) { @@ -659,7 +663,7 @@ public function setToken($token) } /** - * @return mixed + * @return Returns first date of abandoned cart if exists. string|null */ protected function getFirstDate() { @@ -667,7 +671,7 @@ protected function getFirstDate() } /** - * @return Object + * @return Mage_Sales_Model_Resource_Order_Collection */ protected function getOrderCollection() { From 004cb8ef468886aa6e0d0fe0d723e20afc76a357 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 28 Dec 2018 16:28:21 -0300 Subject: [PATCH 067/113] refactore some variables as constant that were used in two test or more --- .../MailChimp/Model/Api/CartsTest.php | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 7a529c17d..87a70f45c 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -64,6 +64,145 @@ public function testCreateBatchJson() $cartsApiMock->createBatchJson(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } + public function testGetConvertedQuotes () + { + $mailchimpTableName = 'mailchimp_ecommerce_sync_data'; + $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; + $arrayAddFieldToFilter = array('eq' => 0); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_deleted = 0"; + $arrayTableName = array('m4m' => $mailchimpTableName); + $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' + AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + $m4m = array('m4m.*'); + + $cartsApiMock = $this->cartsApiMock->setMethods(array( + 'getMailchimpEcommerceDataTableName', + 'getQuoteCollection', + 'getBatchLimitFromConfig', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + '_updateSyncData', + 'setCounter' + )) + ->getMock(); + + $quoteResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('joinLeft', 'where', 'limit')) + ->getMock(); + + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail')) + ->getMock(); + + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mailchimpTableName); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($quoteResoureceCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER + ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1) + ); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array(self::COUNTER + 1), + array(self::COUNTER + 1) + ); + + $quoteResoureceCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter) + ); + $quoteResoureceCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $quoteResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + + $cartModelMock->expects($this->once()) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls(self::CUSTOMER_EMAIL_BY_CART); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + + $cartsApiMock->_getConvertedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + public function testGetModifiedQuotes() { $mcTableName = 'mailchimp_ecommerce_sync_data'; From aa59c4022c02b70d6364a36a9827faba53fe71d9 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 28 Dec 2018 16:29:51 -0300 Subject: [PATCH 068/113] some modifications for the test _getConvertedQuotes --- app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php index b6a55e625..21f41b15c 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -155,7 +155,7 @@ public function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) $allCarts[$counter]['operation_id'] = $batchId . '_' . $alreadySentCartId; $allCarts[$counter]['body'] = ''; $this->_updateSyncData($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1); - $this->setCounter($this->getCounter()+1); + $this->setCounter($this->getCounter() + 1); } } From c0354ca64f7ca67ef735bbb84d634aab4212606f Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Mon, 7 Jan 2019 13:30:39 -0300 Subject: [PATCH 069/113] coverage 100 percent of the tests for getNewQuotes and getModifiedQuotes --- .../MailChimp/Model/Api/CartsTest.php | 1160 +++++++++++++++-- 1 file changed, 1073 insertions(+), 87 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 87a70f45c..3063ba40d 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -67,7 +67,6 @@ public function testCreateBatchJson() public function testGetConvertedQuotes () { $mailchimpTableName = 'mailchimp_ecommerce_sync_data'; - $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; $arrayAddFieldToFilter = array('eq' => 0); $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); $where = "m4m.mailchimp_sync_deleted = 0"; @@ -213,7 +212,6 @@ public function testGetModifiedQuotes() $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); $where = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_sync_delta < updated_at"; - $whereGetAllCartsByEmail = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $arrayTableName = array('m4m' => $mcTableName); $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; @@ -400,57 +398,33 @@ public function testGetModifiedQuotes() $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } - public function testGetNewQuotes() + public function testGetModifiedQuotesGuestCustomer() { - $existFirstDate = '2018-11-30'; - $customerId = 1; - $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; - $customerEmailAddress = ''; - $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); - $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"test@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $mcTableName = 'mailchimp_ecommerce_sync_data'; + $customerId = ''; + $customerEmailAddress = 'test@ebizmarts.com'; + $stringStoreId = 'store_id'; $arrayAddFieldToFilter = array('eq' => 1); $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array('item'); - $sizeOrderCollection = 0; - $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); - $stringCustomerEmailMainTable = 'main_table.customer_email'; - $stringUpdated = 'main_table.updated_at'; - $addFieldToFilterUpdated = array('from' => ''); + $where = "m4m.mailchimp_sync_deleted = 0 + AND m4m.mailchimp_sync_delta < updated_at"; + $arrayTableName = array('m4m' => $mcTableName); + $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' + AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + $m4m = array('m4m.*'); $cartsApiMock = $this->cartsApiMock->setMethods( array( - 'getHelper', 'getQuoteCollection', - 'getFirstDate', - 'joinMailchimpSyncDataWithoutWhere', 'getBatchLimitFromConfig', '_updateSyncData', 'getCustomerModel', 'getWebSiteIdFromMagentoStoreId', - '_getAllCartsByEmail', - 'getCounter', 'getBatchId', - 'setCounter', - 'addProductNotSentData', - '_makeCart', - 'setToken', - 'getToken', 'getOrderCollection' )) ->getMock(); - $helperMock = $this - ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->setMethods(array('addResendFilter')) - ->getMock(); - $newCartsCollectionMock = $this ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) ->disableOriginalConstructor() @@ -460,7 +434,7 @@ public function testGetNewQuotes() $varienSelectMock = $this ->getMockBuilder(Varien_Db_Select::class) ->disableOriginalConstructor() - ->setMethods(array('where', 'limit')) + ->setMethods(array('where', 'limit', 'joinLeft')) ->getMock(); $customerModelMock = $this @@ -469,54 +443,176 @@ public function testGetNewQuotes() ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) ->getMock(); - $quoteByEmailResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) ->disableOriginalConstructor() - ->setMethods(array('clear', 'getIterator')) + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringStoreId, $arrayAddFieldToFilterStoreId) + ); + $newCartsCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->exactly(2)) + ->method('getEmail') + ->willReturnOnConsecutiveCalls( + $customerEmailAddress, + $customerEmailAddress + ); + + $cartModelMock->expects($this->exactly(3)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + + $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetModifiedQuotesEmptyJson() + { + $mcTableName = 'mailchimp_ecommerce_sync_data'; + $customerEmailAddress = ''; + $cartJson = ''; + $customerId = 1; + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_deleted = 0 + AND m4m.mailchimp_sync_delta < updated_at"; + $whereGetAllCartsByEmail = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + $arrayTableName = array('m4m' => $mcTableName); + $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' + AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + $m4m = array('m4m.*'); + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'setToken', + 'getBatchId', + 'getMailchimpEcommerceDataTableName', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getQuoteCollection', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'setCounter', + 'getCounter', + '_makeCart', + '_getAllCartsByEmail', + 'addProductNotSentData' + )) ->getMock(); $cartModelMock = $this ->getMockBuilder(Mage_Sales_Model_Quote::class) ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) ->getMock(); - $cartByEmailModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) + $quoteResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) ->disableOriginalConstructor() - ->setMethods(array('getEntityId')) + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) ->getMock(); - $orderCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) ->disableOriginalConstructor() - ->setMethods(array('getSize', 'addFieldToFilter')) + ->setMethods(array('joinLeft', 'where', 'limit')) + ->getMock(); + + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) ->getMock(); $cartsApiMock->expects($this->once()) - ->method('getHelper') - ->willReturn($helperMock); + ->method('setToken') + ->with(null); $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getFirstDate') - ->willReturnOnConsecutiveCalls( - $existFirstDate, - $existFirstDate - ); + ->method('getBatchId') + ->willReturn(self::BATCH_ID); $cartsApiMock->expects($this->once()) - ->method('joinMailchimpSyncDataWithoutWhere') - ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($quoteResoureceCollectionMock); $cartsApiMock->expects($this->once()) ->method('getBatchLimitFromConfig') ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) - ); $cartsApiMock->expects($this->once()) ->method('getCustomerModel') ->willReturn($customerModelMock); @@ -528,40 +624,930 @@ public function testGetNewQuotes() ->method('_getAllCartsByEmail') ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(4)) + $cartsApiMock->expects($this->exactly(2)) ->method('getCounter') ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER, self::COUNTER, self::COUNTER ); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); $cartsApiMock->expects($this->exactly(2)) - ->method('setCounter') + ->method('_updateSyncData') ->withConsecutive( - array(self::COUNTER + 1), - array(self::COUNTER + 1) + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID) ); $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); + ->method('setCounter') + ->with(self::COUNTER + 1); $cartsApiMock->expects($this->once()) ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) ->willReturn($cartJson); $cartsApiMock->expects($this->once()) - ->method('getToken') - ->willReturn($token); - $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - $cartsApiMock->expects($this->once()) - ->method('getOrderCollection') - ->willReturn($orderCollectionMock); + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + + $cartModelMock->expects($this->exactly(3)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + + $quoteResoureceCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) + ); + $quoteResoureceCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + + $quoteResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + + $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesNewQuote() + { + $existFirstDate = '2018-11-30'; + $customerId = 1; + $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; + $customerEmailAddress = ''; + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"test@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + 'setCounter', + 'addProductNotSentData', + '_makeCart', + 'setToken', + 'getToken', + 'getOrderCollection' + )) + ->getMock(); + + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->getMock(); + + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) + ); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER + ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array(self::COUNTER + 1), + array(self::COUNTER + 1) + ); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('getToken') + ->willReturn($token); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + + $cartModelMock->expects($this->exactly(4)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesIsOrder() + { + $existFirstDate = '2018-11-30'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 1; + $addFieldToFilterOrderCollection = array('eq' => ''); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getOrderCollection' + )) + ->getMock(); + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getAllVisibleItems')) + ->getMock(); + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + + + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesEmpty() + { + $existFirstDate = '2018-11-30'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array(); + $addFieldToFilterOrderCollection = array('eq' => ''); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getOrderCollection' + )) + ->getMock(); + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getAllVisibleItems')) + ->getMock(); + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + + + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesGuestCustomer() + { + $existFirstDate = '2018-11-30'; + $customerId = ''; + $customerEmailAddress = 'test@ebizmarts.com'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'getBatchId', + 'getOrderCollection' + )) + ->getMock(); + + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->getMock(); + + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->exactly(2)) + ->method('getEmail') + ->willReturnOnConsecutiveCalls( + $customerEmailAddress, + $customerEmailAddress + ); + + $cartModelMock->expects($this->exactly(4)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesEmptyJson() + { + $existFirstDate = '2018-11-30'; + $customerId = 1; + $customerEmailAddress = ''; + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + $cartJson = ''; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + 'setCounter', + 'addProductNotSentData', + '_makeCart', + 'setToken', + 'getOrderCollection' + )) + ->getMock(); + + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->getMock(); + + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID) + ); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER + ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('setCounter') + ->with(self::COUNTER + 1); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); $helperMock->expects($this->once()) ->method('addResendFilter') From f9a2ba6f5fd3a003cb985bebde3d77eec63c3431 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Mon, 7 Jan 2019 13:33:00 -0300 Subject: [PATCH 070/113] removed unused variables from the tests for getNewQuotes and getModifiedQuotes --- .../tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 3063ba40d..df7defbbb 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -537,7 +537,6 @@ public function testGetModifiedQuotesEmptyJson() $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); $where = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_sync_delta < updated_at"; - $whereGetAllCartsByEmail = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $arrayTableName = array('m4m' => $mcTableName); $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; From b3bd6c400cc3fe4f4b7f84e6a8a4ac859048b82f Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 8 Jan 2019 12:54:28 -0300 Subject: [PATCH 071/113] removed repeated code from the tests for getNewQuotes and getModifiedQuotes --- .../MailChimp/Model/Api/CartsTest.php | 2080 ++++++++--------- 1 file changed, 916 insertions(+), 1164 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index df7defbbb..68b8c8f22 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -1,12 +1,10 @@ cartsApiMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Carts::class); } @@ -35,7 +32,6 @@ public function tearDown() public function testCreateBatchJson() { $batchArray = array(); - $cartsApiMock = $this->cartsApiMock->setMethods(array( 'getHelper', '_getConvertedQuotes', @@ -44,264 +40,96 @@ public function testCreateBatchJson() 'setBatchId' )) ->getMock(); - $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() ->setMethods(array('isAbandonedCartEnabled', 'getAbandonedCartFirstDate', 'getDateMicrotime', 'getResendTurn')) ->getMock(); - $cartsApiMock->expects($this->once())->method('setBatchId')->with(self::BATCH_ID); $cartsApiMock->expects($this->once())->method('getHelper')->willReturn($helperMock); $cartsApiMock->expects($this->once())->method('_getConvertedQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); $cartsApiMock->expects($this->once())->method('_getModifiedQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); $cartsApiMock->expects($this->once())->method('_getNewQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); - $helperMock->expects($this->once())->method('isAbandonedCartEnabled')->with(self::MAGENTO_STORE_ID)->willReturn(true); $helperMock->expects($this->once())->method('getAbandonedCartFirstDate')->with(self::MAGENTO_STORE_ID)->willReturn('00-00-00 00:00:00'); $helperMock->expects($this->once())->method('getDateMicrotime')->willReturn(self::DATE); $helperMock->expects($this->once())->method('getResendTurn')->with(self::MAGENTO_STORE_ID)->willReturn(null); - $cartsApiMock->createBatchJson(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } public function testGetConvertedQuotes () - { - $mailchimpTableName = 'mailchimp_ecommerce_sync_data'; - $arrayAddFieldToFilter = array('eq' => 0); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_deleted = 0"; - $arrayTableName = array('m4m' => $mailchimpTableName); - $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' - AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; - $m4m = array('m4m.*'); - - $cartsApiMock = $this->cartsApiMock->setMethods(array( - 'getMailchimpEcommerceDataTableName', - 'getQuoteCollection', - 'getBatchLimitFromConfig', - '_getAllCartsByEmail', - 'getCounter', - 'getBatchId', - '_updateSyncData', - 'setCounter' - )) - ->getMock(); - - $quoteResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('joinLeft', 'where', 'limit')) - ->getMock(); - - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail')) - ->getMock(); - - $quoteByEmailResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('clear', 'getIterator')) - ->getMock(); - - $cartByEmailModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId')) - ->getMock(); - - $cartsApiMock->expects($this->once()) - ->method('getMailchimpEcommerceDataTableName') - ->willReturn($mailchimpTableName); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($quoteResoureceCollectionMock); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(4)) - ->method('getCounter') - ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER, - self::COUNTER, - self::COUNTER - ); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1) - ); - $cartsApiMock->expects($this->exactly(2)) - ->method('setCounter') - ->withConsecutive( - array(self::COUNTER + 1), - array(self::COUNTER + 1) - ); - - $quoteResoureceCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter) - ); - $quoteResoureceCollectionMock->expects($this->exactly(3)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock, - $varienSelectMock - ); - $quoteResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - - $varienSelectMock->expects($this->once()) - ->method('joinLeft') - ->with($arrayTableName, $conditionSelect, $m4m); - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); - - $cartModelMock->expects($this->once()) - ->method('getCustomerEmail') - ->willReturnOnConsecutiveCalls(self::CUSTOMER_EMAIL_BY_CART); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('clear'); - - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); - - $cartByEmailModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::ALREADY_SENT_CART_ID); - - $cartsApiMock->_getConvertedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); - } - - public function testGetModifiedQuotes() { - $mcTableName = 'mailchimp_ecommerce_sync_data'; - $customerEmailAddress = ''; - $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"test@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; - $customerId = 1; - $arrayAddFieldToFilter = array('eq' => 1); + $mailchimpTableName = 'mailchimp_ecommerce_sync_data'; + $arrayAddFieldToFilter = array('eq' => 0); $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_deleted = 0 - AND m4m.mailchimp_sync_delta < updated_at"; - $arrayTableName = array('m4m' => $mcTableName); + $where = "m4m.mailchimp_sync_deleted = 0"; + $arrayTableName = array('m4m' => $mailchimpTableName); $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $m4m = array('m4m.*'); - $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); - $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'setToken', - 'getToken', - 'getBatchId', - 'getMailchimpEcommerceDataTableName', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getQuoteCollection', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - 'setCounter', - 'getCounter', - '_makeCart', - '_getAllCartsByEmail', - 'addProductNotSentData' - )) - ->getMock(); + $cartByEmailModelMock = $this->cartByEmailModelMock(); + + $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); $cartModelMock = $this ->getMockBuilder(Mage_Sales_Model_Quote::class) ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) + ->setMethods(array('getEntityId', 'getCustomerEmail')) ->getMock(); + $cartModelMock->expects($this->once()) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls(self::CUSTOMER_EMAIL_BY_CART); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); - $quoteResoureceCollectionMock = $this + $newCartsCollectionMock = $this ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) ->disableOriginalConstructor() ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) ->getMock(); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter) + ); + $newCartsCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('joinLeft', 'where', 'limit')) - ->getMock(); - - $customerModelMock = $this - ->getMockBuilder(Mage_Customer_Model_Customer::class) - ->disableOriginalConstructor() - ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) - ->getMock(); - - $quoteByEmailResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('clear', 'getIterator')) - ->getMock(); + $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $cartByEmailModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId')) + $cartsApiMock = $this->cartsApiMock->setMethods(array( + 'getMailchimpEcommerceDataTableName', + 'getQuoteCollection', + 'getBatchLimitFromConfig', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + '_updateSyncData', + 'setCounter' + )) ->getMock(); - - $cartsApiMock->expects($this->once()) - ->method('getToken') - ->willReturn($token); - $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); $cartsApiMock->expects($this->once()) ->method('getMailchimpEcommerceDataTableName') - ->willReturn($mcTableName); + ->willReturn($mailchimpTableName); $cartsApiMock->expects($this->once()) ->method('getQuoteCollection') - ->willReturn($quoteResoureceCollectionMock); + ->willReturn($newCartsCollectionMock); $cartsApiMock->expects($this->once()) ->method('getBatchLimitFromConfig') ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(4)) ->method('getCounter') @@ -311,11 +139,14 @@ public function testGetModifiedQuotes() self::COUNTER, self::COUNTER ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); $cartsApiMock->expects($this->exactly(2)) ->method('_updateSyncData') ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1) ); $cartsApiMock->expects($this->exactly(2)) ->method('setCounter') @@ -323,77 +154,40 @@ public function testGetModifiedQuotes() array(self::COUNTER + 1), array(self::COUNTER + 1) ); - $cartsApiMock->expects($this->once()) - ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) - ->willReturn($cartJson); - $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); - $cartModelMock->expects($this->exactly(3)) - ->method('getCustomerEmail') - ->willReturnOnConsecutiveCalls( - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART - ); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getCustomerId') - ->willReturn($customerId); + $cartsApiMock->_getConvertedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } - $quoteResoureceCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) - ); - $quoteResoureceCollectionMock->expects($this->exactly(3)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock, - $varienSelectMock - ); + public function testGetModifiedQuotes() + { + $mcTableName = 'mailchimp_ecommerce_sync_data'; + $customerEmailAddress = ''; + $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"test@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; + $customerId = 1; + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_deleted = 0 + AND m4m.mailchimp_sync_delta < updated_at"; + $arrayTableName = array('m4m' => $mcTableName); + $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' + AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + $m4m = array('m4m.*'); + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; - $quoteResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); + $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $varienSelectMock->expects($this->once()) - ->method('joinLeft') - ->with($arrayTableName, $conditionSelect, $m4m); - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); + $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); - $customerModelMock->expects($this->once()) - ->method('setWebsiteId') - ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $customerModelMock->expects($this->once()) - ->method('loadByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART); - $customerModelMock->expects($this->once()) - ->method('getEmail') - ->willReturn($customerEmailAddress); + $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('clear'); + $newCartsCollectionMock = $this->newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + $cartByEmailModelMock = $this->cartByEmailModelMock(); - $cartByEmailModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::ALREADY_SENT_CART_ID); + $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); + + $cartsApiMock = $this->cartsApiMockModifiedQuotes($token, $mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts); $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -413,119 +207,19 @@ public function testGetModifiedQuotesGuestCustomer() AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $m4m = array('m4m.*'); - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'getQuoteCollection', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - 'getBatchId', - 'getOrderCollection' - )) - ->getMock(); - - $newCartsCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); + $varienSelectMock = $this->modifiedQuotesGuestCustomer($where, $arrayTableName, $conditionSelect, $m4m); - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('where', 'limit', 'joinLeft')) - ->getMock(); + $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $customerModelMock = $this - ->getMockBuilder(Mage_Customer_Model_Customer::class) - ->disableOriginalConstructor() - ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) - ->getMock(); + $newCartsCollectionMock = $this->newCartsModifiedQuotesGuestCustomer($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) - ->getMock(); + $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); + $this->customerModelMockGetEmail($customerModelMock, $customerEmailAddress); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); + $cartsApiMock = $this->cartsApiMockModifiedQuotesGuestCustomer($newCartsCollectionMock, $customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('_updateSyncData') - ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringStoreId, $arrayAddFieldToFilterStoreId) - ); - $newCartsCollectionMock->expects($this->exactly(3)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock, - $varienSelectMock - ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); - $varienSelectMock->expects($this->once()) - ->method('joinLeft') - ->with($arrayTableName, $conditionSelect, $m4m); - - $customerModelMock->expects($this->once()) - ->method('setWebsiteId') - ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $customerModelMock->expects($this->once()) - ->method('loadByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART); - $customerModelMock->expects($this->exactly(2)) - ->method('getEmail') - ->willReturnOnConsecutiveCalls( - $customerEmailAddress, - $customerEmailAddress - ); - - $cartModelMock->expects($this->exactly(3)) - ->method('getCustomerEmail') - ->willReturnOnConsecutiveCalls( - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART - ); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getCustomerId') - ->willReturn($customerId); - - $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); - } + $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } public function testGetModifiedQuotesEmptyJson() { @@ -543,172 +237,19 @@ public function testGetModifiedQuotesEmptyJson() $m4m = array('m4m.*'); $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'setToken', - 'getBatchId', - 'getMailchimpEcommerceDataTableName', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getQuoteCollection', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - 'setCounter', - 'getCounter', - '_makeCart', - '_getAllCartsByEmail', - 'addProductNotSentData' - )) - ->getMock(); - - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) - ->getMock(); - - $quoteResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('joinLeft', 'where', 'limit')) - ->getMock(); - - $customerModelMock = $this - ->getMockBuilder(Mage_Customer_Model_Customer::class) - ->disableOriginalConstructor() - ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) - ->getMock(); - - $quoteByEmailResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('clear', 'getIterator')) - ->getMock(); - - $cartByEmailModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId')) - ->getMock(); - - $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->once()) - ->method('getMailchimpEcommerceDataTableName') - ->willReturn($mcTableName); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($quoteResoureceCollectionMock); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getCounter') - ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER - ); - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID) - ); - $cartsApiMock->expects($this->once()) - ->method('setCounter') - ->with(self::COUNTER + 1); - $cartsApiMock->expects($this->once()) - ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) - ->willReturn($cartJson); - $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); - - $cartModelMock->expects($this->exactly(3)) - ->method('getCustomerEmail') - ->willReturnOnConsecutiveCalls( - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART - ); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getCustomerId') - ->willReturn($customerId); - - $quoteResoureceCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) - ); - $quoteResoureceCollectionMock->expects($this->exactly(3)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock, - $varienSelectMock - ); + $cartByEmailModelMock = $this->cartByEmailModelMock(); - $quoteResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); - $varienSelectMock->expects($this->once()) - ->method('joinLeft') - ->with($arrayTableName, $conditionSelect, $m4m); - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); + $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $customerModelMock->expects($this->once()) - ->method('setWebsiteId') - ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $customerModelMock->expects($this->once()) - ->method('loadByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART); - $customerModelMock->expects($this->once()) - ->method('getEmail') - ->willReturn($customerEmailAddress); + $newCartsCollectionMock = $this->newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('clear'); + $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $cartByEmailModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::ALREADY_SENT_CART_ID); + $cartsApiMock = $this->modifiedQuotesEmptyJson($mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts); $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -737,75 +278,395 @@ public function testGetNewQuotesNewQuote() $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'getHelper', - 'getQuoteCollection', - 'getFirstDate', - 'joinMailchimpSyncDataWithoutWhere', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - '_getAllCartsByEmail', - 'getCounter', - 'getBatchId', - 'setCounter', - 'addProductNotSentData', - '_makeCart', - 'setToken', - 'getToken', - 'getOrderCollection' - )) - ->getMock(); + $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); - $helperMock = $this - ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->setMethods(array('addResendFilter')) - ->getMock(); + $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $newCartsCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); + $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('where', 'limit')) - ->getMock(); + $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $customerModelMock = $this - ->getMockBuilder(Mage_Customer_Model_Customer::class) - ->disableOriginalConstructor() - ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) - ->getMock(); + $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $quoteByEmailResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('clear', 'getIterator')) - ->getMock(); + $cartByEmailModelMock = $this->cartByEmailModelMock(); - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) - ->getMock(); + $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $cartByEmailModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId')) - ->getMock(); + $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); + $cartsApiMock = $this->cartsApiMockNewQuote($helperMock, $newCartsCollectionMock, $existFirstDate, $token, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesIsOrder() + { + $existFirstDate = '2018-11-30'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 1; + $addFieldToFilterOrderCollection = array('eq' => ''); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); + + $varienSelectMock = $this->varienSelectMockNewQuotes($where); + + $cartModelMock = $this->cartModelMockEmptyQuote($allVisbleItems); + + $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); + + $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); + + $cartsApiMock = $this->cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, $existFirstDate, $orderCollectionMock); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesEmpty() + { + $existFirstDate = '2018-11-30'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array(); + $addFieldToFilterOrderCollection = array('eq' => ''); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartModelMock = $this->cartModelMockEmptyQuote($allVisbleItems); + + $varienSelectMock = $this->varienSelectMockNewQuotes($where); + + $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); + + $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); + + $orderCollectionMock = $this->orderCollectionMockEmptyQuotes($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); + + $cartsApiMock = $this->cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, $existFirstDate, $orderCollectionMock); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesGuestCustomer() + { + $existFirstDate = '2018-11-30'; + $customerId = ''; + $customerEmailAddress = 'test@ebizmarts.com'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $varienSelectMock = $this->varienSelectMockNewQuotes($where); + + $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); + + $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); + + $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); + + $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); + + $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); + $this->customerModelMockGetEmail($customerModelMock, $customerEmailAddress); + + $cartsApiMock = $this->cartsApiMockGuestCustomer($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $orderCollectionMock); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesEmptyJson() + { + $existFirstDate = '2018-11-30'; + $customerId = 1; + $customerEmailAddress = ''; + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + $cartJson = ''; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); + + $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); + + $varienSelectMock = $this->varienSelectMockNewQuotes($where); + + $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); + + $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); + + $cartByEmailModelMock = $this->cartByEmailModelMock(); + + $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); + + $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); + + $cartsApiMock = $this->cartsApiMockNewQuotesEmptyJson($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + /** + * @param $customerEmailAddress + * @return mixed + */ + protected function customerModelMockNewQuotes($customerEmailAddress) + { + $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + return $customerModelMock; + } + + /** + * @param $customerId + * @param $allVisbleItems + * @return mixed + */ + protected function cartModelMockNewQuotes($customerId, $allVisbleItems) + { + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->getMock(); + $cartModelMock->expects($this->exactly(4)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + return $cartModelMock; + } + + /** + * @param $where + * @return mixed + */ + protected function varienSelectMockNewQuotes($where) + { + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + return $varienSelectMock; + } + + /** + * @param $arrayAddFieldToFilter + * @param $stringCustomerEmail + * @param $arrayAddFieldToFilterCustomerEmail + * @param $stringItemsCount + * @param $arrayAddFieldToFilterItemsCount + * @param $arrayAddFieldToFilterStoreId + * @param $stringUpdatedAt + * @param $arrayAddFieldToFilterUpdatedAt + * @param $varienSelectMock + * @param $cartModelMock + * @return mixed + */ + protected function newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock) + { + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + return $newCartsCollectionMock; + } + + /** + * @param $newCartsCollectionMock + * @return mixed + */ + protected function helperMockNewQuotes($newCartsCollectionMock) + { + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + return $helperMock; + } + + /** + * @return mixed + */ + protected function cartByEmailModelMock() + { + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + return $cartByEmailModelMock; + } + + /** + * @param $cartByEmailModelMock + * @return mixed + */ + protected function quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock) + { + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + return $quoteByEmailResoureceCollectionMock; + } + + /** + * @param $sizeOrderCollection + * @param $stringCustomerEmailMainTable + * @param $addFieldToFilterOrderCollection + * @param $stringUpdated + * @param $addFieldToFilterUpdated + * @return mixed + */ + protected function orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated) + { $orderCollectionMock = $this ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) ->disableOriginalConstructor() ->setMethods(array('getSize', 'addFieldToFilter')) ->getMock(); + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + return $orderCollectionMock; + } + /** + * @param $helperMock + * @param $newCartsCollectionMock + * @param $existFirstDate + * @param $customerModelMock + * @param $quoteByEmailResoureceCollectionMock + * @param $cartModelMock + * @param $allCarts + * @param $cartJson + * @param $orderCollectionMock + * @return mixed + */ + protected function cartsApiMockNewQuotesEmptyJson($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock) + { + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + 'setCounter', + 'addProductNotSentData', + '_makeCart', + 'setToken', + 'getOrderCollection' + )) + ->getMock(); $cartsApiMock->expects($this->once()) ->method('getHelper') ->willReturn($helperMock); @@ -815,9 +676,9 @@ public function testGetNewQuotesNewQuote() $cartsApiMock->expects($this->exactly(2)) ->method('getFirstDate') ->willReturnOnConsecutiveCalls( - $existFirstDate, - $existFirstDate - ); + $existFirstDate, + $existFirstDate + ); $cartsApiMock->expects($this->once()) ->method('joinMailchimpSyncDataWithoutWhere') ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); @@ -828,7 +689,7 @@ public function testGetNewQuotesNewQuote() ->method('_updateSyncData') ->withConsecutive( array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) + array(self::CART_ID, self::MAILCHIMP_STORE_ID) ); $cartsApiMock->expects($this->once()) ->method('getCustomerModel') @@ -839,25 +700,20 @@ public function testGetNewQuotesNewQuote() ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(4)) + $cartsApiMock->expects($this->exactly(2)) ->method('getCounter') ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER, self::COUNTER, self::COUNTER ); $cartsApiMock->expects($this->once()) ->method('getBatchId') ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->exactly(2)) + $cartsApiMock->expects($this->once()) ->method('setCounter') - ->withConsecutive( - array(self::COUNTER + 1), - array(self::COUNTER + 1) - ); + ->with(self::COUNTER + 1); $cartsApiMock->expects($this->once()) ->method('addProductNotSentData') ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) @@ -865,117 +721,59 @@ public function testGetNewQuotesNewQuote() $cartsApiMock->expects($this->once()) ->method('_makeCart') ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($cartJson); - $cartsApiMock->expects($this->once()) - ->method('getToken') - ->willReturn($token); - $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - $cartsApiMock->expects($this->once()) - ->method('getOrderCollection') - ->willReturn($orderCollectionMock); - - $helperMock->expects($this->once()) - ->method('addResendFilter') - ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); - - $newCartsCollectionMock->expects($this->exactly(5)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), - array($stringItemsCount, $arrayAddFieldToFilterItemsCount), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), - array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) - ); - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock - ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); - - $customerModelMock->expects($this->once()) - ->method('setWebsiteId') - ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $customerModelMock->expects($this->once()) - ->method('loadByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART); - $customerModelMock->expects($this->once()) - ->method('getEmail') - ->willReturn($customerEmailAddress); - - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('clear'); - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); - - $cartModelMock->expects($this->exactly(4)) - ->method('getCustomerEmail') - ->willReturnOnConsecutiveCalls( - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART - ); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getCustomerId') - ->willReturn($customerId); - $cartModelMock->expects($this->once()) - ->method('getAllVisibleItems') - ->willReturn($allVisbleItems); - - $cartByEmailModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::ALREADY_SENT_CART_ID); - - $orderCollectionMock->expects($this->once()) - ->method('getSize') - ->willReturn($sizeOrderCollection); - $orderCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), - array($stringUpdated, $addFieldToFilterUpdated) - ); + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + return $cartsApiMock; + } - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + /** + * @return mixed + */ + protected function customerModelMockSetWebSiteIdLoadByEmail() + { + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + return $customerModelMock; } - public function testGetNewQuotesIsOrder() + /** + * @param $customerModelMock + * @param $customerEmailAddress + */ + protected function customerModelMockGetEmail($customerModelMock, $customerEmailAddress) { - $existFirstDate = '2018-11-30'; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array('item'); - $sizeOrderCollection = 1; - $addFieldToFilterOrderCollection = array('eq' => ''); - $stringCustomerEmailMainTable = 'main_table.customer_email'; - $stringUpdated = 'main_table.updated_at'; - $addFieldToFilterUpdated = array('from' => ''); + $customerModelMock->expects($this->exactly(2)) + ->method('getEmail') + ->willReturnOnConsecutiveCalls( + $customerEmailAddress, + $customerEmailAddress + ); + } + /** + * @param $helperMock + * @param $newCartsCollectionMock + * @param $existFirstDate + * @param $customerModelMock + * @param $orderCollectionMock + * @return mixed + */ + protected function cartsApiMockGuestCustomer($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $orderCollectionMock) + { $cartsApiMock = $this->cartsApiMock->setMethods( array( 'getHelper', @@ -984,34 +782,12 @@ public function testGetNewQuotesIsOrder() 'joinMailchimpSyncDataWithoutWhere', 'getBatchLimitFromConfig', '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'getBatchId', 'getOrderCollection' )) ->getMock(); - $helperMock = $this - ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->setMethods(array('addResendFilter')) - ->getMock(); - $newCartsCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('where', 'limit')) - ->getMock(); - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getAllVisibleItems')) - ->getMock(); - $orderCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('getSize', 'addFieldToFilter')) - ->getMock(); - $cartsApiMock->expects($this->once()) ->method('getHelper') ->willReturn($helperMock); @@ -1033,79 +809,31 @@ public function testGetNewQuotesIsOrder() $cartsApiMock->expects($this->once()) ->method('_updateSyncData') ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); $cartsApiMock->expects($this->once()) ->method('getOrderCollection') ->willReturn($orderCollectionMock); - - - $helperMock->expects($this->once()) - ->method('addResendFilter') - ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); - - $newCartsCollectionMock->expects($this->exactly(5)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), - array($stringItemsCount, $arrayAddFieldToFilterItemsCount), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), - array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) - ); - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock - ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); - - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getAllVisibleItems') - ->willReturn($allVisbleItems); - - $orderCollectionMock->expects($this->once()) - ->method('getSize') - ->willReturn($sizeOrderCollection); - $orderCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), - array($stringUpdated, $addFieldToFilterUpdated) - ); - - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + return $cartsApiMock; } - public function testGetNewQuotesEmpty() + /** + * @param $helperMock + * @param $newCartsCollectionMock + * @param $existFirstDate + * @param $orderCollectionMock + * @return mixed + */ + protected function cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, $existFirstDate, $orderCollectionMock) { - $existFirstDate = '2018-11-30'; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array(); - $addFieldToFilterOrderCollection = array('eq' => ''); - $stringCustomerEmailMainTable = 'main_table.customer_email'; - $stringUpdated = 'main_table.updated_at'; - $addFieldToFilterUpdated = array('from' => ''); - $cartsApiMock = $this->cartsApiMock->setMethods( array( 'getHelper', @@ -1117,31 +845,6 @@ public function testGetNewQuotesEmpty() 'getOrderCollection' )) ->getMock(); - $helperMock = $this - ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->setMethods(array('addResendFilter')) - ->getMock(); - $newCartsCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('where', 'limit')) - ->getMock(); - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getAllVisibleItems')) - ->getMock(); - $orderCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter')) - ->getMock(); - $cartsApiMock->expects($this->once()) ->method('getHelper') ->willReturn($helperMock); @@ -1166,76 +869,67 @@ public function testGetNewQuotesEmpty() $cartsApiMock->expects($this->once()) ->method('getOrderCollection') ->willReturn($orderCollectionMock); + return $cartsApiMock; + } - - $helperMock->expects($this->once()) - ->method('addResendFilter') - ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); - - $newCartsCollectionMock->expects($this->exactly(5)) + /** + * @param $stringCustomerEmailMainTable + * @param $addFieldToFilterOrderCollection + * @param $stringUpdated + * @param $addFieldToFilterUpdated + * @return mixed + */ + protected function orderCollectionMockEmptyQuotes($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated) + { + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter')) + ->getMock(); + $orderCollectionMock->expects($this->exactly(2)) ->method('addFieldToFilter') ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), - array($stringItemsCount, $arrayAddFieldToFilterItemsCount), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), - array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) - ); - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); + return $orderCollectionMock; + } + /** + * @param $allVisbleItems + * @return mixed + */ + protected function cartModelMockEmptyQuote($allVisbleItems) + { + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getAllVisibleItems')) + ->getMock(); $cartModelMock->expects($this->once()) ->method('getEntityId') ->willReturn(self::CART_ID); $cartModelMock->expects($this->once()) ->method('getAllVisibleItems') ->willReturn($allVisbleItems); - - $orderCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), - array($stringUpdated, $addFieldToFilterUpdated) - ); - - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + return $cartModelMock; } - public function testGetNewQuotesGuestCustomer() + /** + * @param $helperMock + * @param $newCartsCollectionMock + * @param $existFirstDate + * @param $token + * @param $customerModelMock + * @param $quoteByEmailResoureceCollectionMock + * @param $cartModelMock + * @param $allCarts + * @param $cartJson + * @param $orderCollectionMock + * @return mixed + */ + protected function cartsApiMockNewQuote($helperMock, $newCartsCollectionMock, $existFirstDate, $token, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock) { - $existFirstDate = '2018-11-30'; - $customerId = ''; - $customerEmailAddress = 'test@ebizmarts.com'; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array('item'); - $sizeOrderCollection = 0; - $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); - $stringCustomerEmailMainTable = 'main_table.customer_email'; - $stringUpdated = 'main_table.updated_at'; - $addFieldToFilterUpdated = array('from' => ''); - $cartsApiMock = $this->cartsApiMock->setMethods( array( 'getHelper', @@ -1246,46 +940,17 @@ public function testGetNewQuotesGuestCustomer() '_updateSyncData', 'getCustomerModel', 'getWebSiteIdFromMagentoStoreId', + '_getAllCartsByEmail', + 'getCounter', 'getBatchId', + 'setCounter', + 'addProductNotSentData', + '_makeCart', + 'setToken', + 'getToken', 'getOrderCollection' )) ->getMock(); - - $helperMock = $this - ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->setMethods(array('addResendFilter')) - ->getMock(); - - $newCartsCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('where', 'limit')) - ->getMock(); - - $customerModelMock = $this - ->getMockBuilder(Mage_Customer_Model_Customer::class) - ->disableOriginalConstructor() - ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) - ->getMock(); - - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) - ->getMock(); - - $orderCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('getSize', 'addFieldToFilter')) - ->getMock(); - $cartsApiMock->expects($this->once()) ->method('getHelper') ->willReturn($helperMock); @@ -1304,9 +969,12 @@ public function testGetNewQuotesGuestCustomer() $cartsApiMock->expects($this->once()) ->method('getBatchLimitFromConfig') ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) + $cartsApiMock->expects($this->exactly(2)) ->method('_updateSyncData') - ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) + ); $cartsApiMock->expects($this->once()) ->method('getCustomerModel') ->willReturn($customerModelMock); @@ -1314,60 +982,87 @@ public function testGetNewQuotesGuestCustomer() ->method('getWebSiteIdFromMagentoStoreId') ->with(self::MAGENTO_STORE_ID) ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER + ); $cartsApiMock->expects($this->once()) ->method('getBatchId') ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array(self::COUNTER + 1), + array(self::COUNTER + 1) + ); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('getToken') + ->willReturn($token); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); $cartsApiMock->expects($this->once()) ->method('getOrderCollection') ->willReturn($orderCollectionMock); + return $cartsApiMock; + } - $helperMock->expects($this->once()) - ->method('addResendFilter') - ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); - - $newCartsCollectionMock->expects($this->exactly(5)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), - array($stringItemsCount, $arrayAddFieldToFilterItemsCount), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), - array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) - ); - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock - ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - + /** + * @param $arrayTableName + * @param $conditionSelect + * @param $m4m + * @param $where + * @return mixed + */ + protected function varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where) + { + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('joinLeft', 'where', 'limit')) + ->getMock(); + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); $varienSelectMock->expects($this->once()) ->method('where') ->with($where); $varienSelectMock->expects($this->once()) ->method('limit') ->with(self::BATCH_LIMIT_FROM_CONFIG); + return $varienSelectMock; + } - $customerModelMock->expects($this->once()) - ->method('setWebsiteId') - ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $customerModelMock->expects($this->once()) - ->method('loadByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART); - $customerModelMock->expects($this->exactly(2)) - ->method('getEmail') - ->willReturnOnConsecutiveCalls( - $customerEmailAddress, - $customerEmailAddress - ); - - $cartModelMock->expects($this->exactly(4)) + /** + * @param $customerId + * @return mixed + */ + protected function cartModelMockModifiedCart($customerId) + { + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) + ->getMock(); + $cartModelMock->expects($this->exactly(3)) ->method('getCustomerEmail') ->willReturnOnConsecutiveCalls( - self::CUSTOMER_EMAIL_BY_CART, self::CUSTOMER_EMAIL_BY_CART, self::CUSTOMER_EMAIL_BY_CART, self::CUSTOMER_EMAIL_BY_CART @@ -1378,138 +1073,86 @@ public function testGetNewQuotesGuestCustomer() $cartModelMock->expects($this->once()) ->method('getCustomerId') ->willReturn($customerId); - $cartModelMock->expects($this->once()) - ->method('getAllVisibleItems') - ->willReturn($allVisbleItems); + return $cartModelMock; + } - $orderCollectionMock->expects($this->once()) - ->method('getSize') - ->willReturn($sizeOrderCollection); - $orderCollectionMock->expects($this->exactly(2)) + /** + * @param $arrayAddFieldToFilter + * @param $arrayAddFieldToFilterStoreId + * @param $varienSelectMock + * @param $cartModelMock + * @return mixed + */ + protected function newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock) + { + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $newCartsCollectionMock->expects($this->exactly(2)) ->method('addFieldToFilter') ->withConsecutive( - array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), - array($stringUpdated, $addFieldToFilterUpdated) + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) ); - - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + $newCartsCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + return $newCartsCollectionMock; } - public function testGetNewQuotesEmptyJson() + /** + * @param $mcTableName + * @param $newCartsCollectionMock + * @param $customerModelMock + * @param $quoteByEmailResoureceCollectionMock + * @param $cartModelMock + * @param $cartJson + * @param $allCarts + * @return mixed + */ + protected function modifiedQuotesEmptyJson($mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts) { - $existFirstDate = '2018-11-30'; - $customerId = 1; - $customerEmailAddress = ''; - $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); - $cartJson = ''; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array('item'); - $sizeOrderCollection = 0; - $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); - $stringCustomerEmailMainTable = 'main_table.customer_email'; - $stringUpdated = 'main_table.updated_at'; - $addFieldToFilterUpdated = array('from' => ''); - $cartsApiMock = $this->cartsApiMock->setMethods( array( - 'getHelper', - 'getQuoteCollection', - 'getFirstDate', - 'joinMailchimpSyncDataWithoutWhere', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - '_getAllCartsByEmail', - 'getCounter', - 'getBatchId', - 'setCounter', - 'addProductNotSentData', - '_makeCart', 'setToken', - 'getOrderCollection' - )) - ->getMock(); - - $helperMock = $this - ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->setMethods(array('addResendFilter')) - ->getMock(); - - $newCartsCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('where', 'limit')) - ->getMock(); - - $customerModelMock = $this - ->getMockBuilder(Mage_Customer_Model_Customer::class) - ->disableOriginalConstructor() - ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) - ->getMock(); - - $quoteByEmailResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('clear', 'getIterator')) - ->getMock(); - - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) - ->getMock(); - - $cartByEmailModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId')) - ->getMock(); - - $orderCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('getSize', 'addFieldToFilter')) + 'getBatchId', + 'getMailchimpEcommerceDataTableName', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getQuoteCollection', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'setCounter', + 'getCounter', + '_makeCart', + '_getAllCartsByEmail', + 'addProductNotSentData' + )) ->getMock(); - $cartsApiMock->expects($this->once()) - ->method('getHelper') - ->willReturn($helperMock); + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); $cartsApiMock->expects($this->once()) ->method('getQuoteCollection') ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getFirstDate') - ->willReturnOnConsecutiveCalls( - $existFirstDate, - $existFirstDate - ); - $cartsApiMock->expects($this->once()) - ->method('joinMailchimpSyncDataWithoutWhere') - ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); $cartsApiMock->expects($this->once()) ->method('getBatchLimitFromConfig') ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID) - ); $cartsApiMock->expects($this->once()) ->method('getCustomerModel') ->willReturn($customerModelMock); @@ -1519,7 +1162,7 @@ public function testGetNewQuotesEmptyJson() ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(2)) ->method('getCounter') @@ -1527,105 +1170,214 @@ public function testGetNewQuotesEmptyJson() self::COUNTER, self::COUNTER ); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID) + ); $cartsApiMock->expects($this->once()) ->method('setCounter') ->with(self::COUNTER + 1); - $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); $cartsApiMock->expects($this->once()) ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) ->willReturn($cartJson); $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - $cartsApiMock->expects($this->once()) - ->method('getOrderCollection') - ->willReturn($orderCollectionMock); + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + return $cartsApiMock; + } - $helperMock->expects($this->once()) - ->method('addResendFilter') - ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + /** + * @param $where + * @param $arrayTableName + * @param $conditionSelect + * @param $m4m + * @return mixed + */ + protected function modifiedQuotesGuestCustomer($where, $arrayTableName, $conditionSelect, $m4m) + { + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit', 'joinLeft')) + ->getMock(); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + return $varienSelectMock; + } - $newCartsCollectionMock->expects($this->exactly(5)) + /** + * @param $arrayAddFieldToFilter + * @param $stringStoreId + * @param $arrayAddFieldToFilterStoreId + * @param $varienSelectMock + * @param $cartModelMock + * @return mixed + */ + protected function newCartsModifiedQuotesGuestCustomer($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock) + { + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $newCartsCollectionMock->expects($this->exactly(2)) ->method('addFieldToFilter') ->withConsecutive( array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), - array($stringItemsCount, $arrayAddFieldToFilterItemsCount), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), - array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + array($stringStoreId, $arrayAddFieldToFilterStoreId) ); - $newCartsCollectionMock->expects($this->exactly(2)) + $newCartsCollectionMock->expects($this->exactly(3)) ->method('getSelect') ->willReturnOnConsecutiveCalls( + $varienSelectMock, $varienSelectMock, $varienSelectMock ); $newCartsCollectionMock->expects($this->once()) ->method('getIterator') ->willReturn(new ArrayIterator(array($cartModelMock))); + return $newCartsCollectionMock; + } - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); - - $customerModelMock->expects($this->once()) - ->method('setWebsiteId') - ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $customerModelMock->expects($this->once()) - ->method('loadByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART); - $customerModelMock->expects($this->once()) - ->method('getEmail') - ->willReturn($customerEmailAddress); - - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('clear'); - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + /** + * @param $newCartsCollectionMock + * @param $customerModelMock + * @return mixed + */ + protected function cartsApiMockModifiedQuotesGuestCustomer($newCartsCollectionMock, $customerModelMock) + { + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getQuoteCollection', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'getBatchId', + 'getOrderCollection' + )) + ->getMock(); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + return $cartsApiMock; + } - $cartModelMock->expects($this->exactly(4)) - ->method('getCustomerEmail') + /** + * @param $token + * @param $mcTableName + * @param $newCartsCollectionMock + * @param $customerModelMock + * @param $quoteByEmailResoureceCollectionMock + * @param $cartModelMock + * @param $cartJson + * @param $allCarts + * @return mixed + */ + protected function cartsApiMockModifiedQuotes($token, $mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts) + { + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'setToken', + 'getToken', + 'getBatchId', + 'getMailchimpEcommerceDataTableName', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getQuoteCollection', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'setCounter', + 'getCounter', + '_makeCart', + '_getAllCartsByEmail', + 'addProductNotSentData' + )) + ->getMock(); + $cartsApiMock->expects($this->once()) + ->method('getToken') + ->willReturn($token); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') ->willReturnOnConsecutiveCalls( - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER ); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getCustomerId') - ->willReturn($customerId); - $cartModelMock->expects($this->once()) - ->method('getAllVisibleItems') - ->willReturn($allVisbleItems); - - $cartByEmailModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::ALREADY_SENT_CART_ID); - - $orderCollectionMock->expects($this->once()) - ->method('getSize') - ->willReturn($sizeOrderCollection); - $orderCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') ->withConsecutive( - array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), - array($stringUpdated, $addFieldToFilterUpdated) + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) ); - - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array(self::COUNTER + 1), + array(self::COUNTER + 1) + ); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + return $cartsApiMock; } -} +} \ No newline at end of file From e2b8a638c8985044ab1747926f29e4fb99aee89d Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 8 Jan 2019 13:02:11 -0300 Subject: [PATCH 072/113] fixed an error in getModifiedQuotes --- .../app/Ebizmarts/MailChimp/Model/Api/CartsTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 68b8c8f22..5e65e78ea 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -216,7 +216,7 @@ public function testGetModifiedQuotesGuestCustomer() $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); $this->customerModelMockGetEmail($customerModelMock, $customerEmailAddress); - $cartsApiMock = $this->cartsApiMockModifiedQuotesGuestCustomer($newCartsCollectionMock, $customerModelMock); + $cartsApiMock = $this->cartsApiMockModifiedQuotesGuestCustomer($mcTableName, $newCartsCollectionMock, $customerModelMock); $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -1251,11 +1251,12 @@ protected function newCartsModifiedQuotesGuestCustomer($arrayAddFieldToFilter, $ } /** + * @param $mcTableName * @param $newCartsCollectionMock * @param $customerModelMock * @return mixed */ - protected function cartsApiMockModifiedQuotesGuestCustomer($newCartsCollectionMock, $customerModelMock) + protected function cartsApiMockModifiedQuotesGuestCustomer($mcTableName, $newCartsCollectionMock, $customerModelMock) { $cartsApiMock = $this->cartsApiMock->setMethods( array( @@ -1265,7 +1266,8 @@ protected function cartsApiMockModifiedQuotesGuestCustomer($newCartsCollectionMo 'getCustomerModel', 'getWebSiteIdFromMagentoStoreId', 'getBatchId', - 'getOrderCollection' + 'getOrderCollection', + 'getMailchimpEcommerceDataTableName' )) ->getMock(); $cartsApiMock->expects($this->once()) @@ -1287,6 +1289,9 @@ protected function cartsApiMockModifiedQuotesGuestCustomer($newCartsCollectionMo $cartsApiMock->expects($this->once()) ->method('getBatchId') ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); return $cartsApiMock; } From 40dc4bea7ada70d3a747e5827d7e04884146eb62 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 8 Jan 2019 13:27:57 -0300 Subject: [PATCH 073/113] added new end of line CartsTest.php --- .../tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 5e65e78ea..fb5ce646e 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -1385,4 +1385,4 @@ protected function cartsApiMockModifiedQuotes($token, $mcTableName, $newCartsCol ->willReturn($allCarts); return $cartsApiMock; } -} \ No newline at end of file +} From 69cbfe2cf5f7ca6663e00b27dc8ed5ccc228728e Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 8 Jan 2019 15:17:54 -0300 Subject: [PATCH 074/113] removed repeated code from the tests for getNewQuotes and getModifiedQuotes --- .../MailChimp/Model/Api/CartsTest.php | 1208 +++++++++-------- 1 file changed, 663 insertions(+), 545 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index fb5ce646e..34cf3a751 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -45,7 +45,7 @@ public function testCreateBatchJson() ->setMethods(array('isAbandonedCartEnabled', 'getAbandonedCartFirstDate', 'getDateMicrotime', 'getResendTurn')) ->getMock(); $cartsApiMock->expects($this->once())->method('setBatchId')->with(self::BATCH_ID); - $cartsApiMock->expects($this->once())->method('getHelper')->willReturn($helperMock); + $this->cartsApiGetHelper($helperMock, $cartsApiMock); $cartsApiMock->expects($this->once())->method('_getConvertedQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); $cartsApiMock->expects($this->once())->method('_getModifiedQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); $cartsApiMock->expects($this->once())->method('_getNewQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); @@ -66,11 +66,8 @@ public function testGetConvertedQuotes () $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $m4m = array('m4m.*'); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); - $cartModelMock = $this ->getMockBuilder(Mage_Sales_Model_Quote::class) ->disableOriginalConstructor() @@ -79,10 +76,7 @@ public function testGetConvertedQuotes () $cartModelMock->expects($this->once()) ->method('getCustomerEmail') ->willReturnOnConsecutiveCalls(self::CUSTOMER_EMAIL_BY_CART); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - + $this->cartModelGetEntityId($cartModelMock); $newCartsCollectionMock = $this ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) ->disableOriginalConstructor() @@ -94,19 +88,9 @@ public function testGetConvertedQuotes () array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter) ); - $newCartsCollectionMock->expects($this->exactly(3)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock, - $varienSelectMock - ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - + $this->newCartsCollectionGetSelectExactlyThree($varienSelectMock, $newCartsCollectionMock); + $this->newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock); $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $cartsApiMock = $this->cartsApiMock->setMethods(array( 'getMailchimpEcommerceDataTableName', 'getQuoteCollection', @@ -121,40 +105,18 @@ public function testGetConvertedQuotes () $cartsApiMock->expects($this->once()) ->method('getMailchimpEcommerceDataTableName') ->willReturn($mailchimpTableName); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(4)) - ->method('getCounter') - ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER, - self::COUNTER, - self::COUNTER - ); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); + $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); + $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); + $this->cartsApiGetCounterExactlyFour($cartsApiMock); + $this->cartsApiGetBatchId($cartsApiMock); $cartsApiMock->expects($this->exactly(2)) ->method('_updateSyncData') ->withConsecutive( array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1) ); - $cartsApiMock->expects($this->exactly(2)) - ->method('setCounter') - ->withConsecutive( - array(self::COUNTER + 1), - array(self::COUNTER + 1) - ); - + $this->cartsApiSetCounterExactlyTwo($cartsApiMock); $cartsApiMock->_getConvertedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -174,21 +136,13 @@ public function testGetModifiedQuotes() $m4m = array('m4m.*'); $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; - $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); - $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $newCartsCollectionMock = $this->newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $cartsApiMock = $this->cartsApiMockModifiedQuotes($token, $mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts); - $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -206,18 +160,12 @@ public function testGetModifiedQuotesGuestCustomer() $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $m4m = array('m4m.*'); - $varienSelectMock = $this->modifiedQuotesGuestCustomer($where, $arrayTableName, $conditionSelect, $m4m); - $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $newCartsCollectionMock = $this->newCartsModifiedQuotesGuestCustomer($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); $this->customerModelMockGetEmail($customerModelMock, $customerEmailAddress); - $cartsApiMock = $this->cartsApiMockModifiedQuotesGuestCustomer($mcTableName, $newCartsCollectionMock, $customerModelMock); - $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -236,21 +184,13 @@ public function testGetModifiedQuotesEmptyJson() AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $m4m = array('m4m.*'); $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); - $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $newCartsCollectionMock = $this->newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $cartsApiMock = $this->modifiedQuotesEmptyJson($mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts); - $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -277,25 +217,15 @@ public function testGetNewQuotesNewQuote() $stringCustomerEmailMainTable = 'main_table.customer_email'; $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); - $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $cartsApiMock = $this->cartsApiMockNewQuote($helperMock, $newCartsCollectionMock, $existFirstDate, $token, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock); - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -317,19 +247,12 @@ public function testGetNewQuotesIsOrder() $stringCustomerEmailMainTable = 'main_table.customer_email'; $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); - $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $cartModelMock = $this->cartModelMockEmptyQuote($allVisbleItems); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $cartsApiMock = $this->cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, $existFirstDate, $orderCollectionMock); - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -350,19 +273,12 @@ public function testGetNewQuotesEmpty() $stringCustomerEmailMainTable = 'main_table.customer_email'; $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); - $cartModelMock = $this->cartModelMockEmptyQuote($allVisbleItems); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $orderCollectionMock = $this->orderCollectionMockEmptyQuotes($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $cartsApiMock = $this->cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, $existFirstDate, $orderCollectionMock); - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -386,22 +302,14 @@ public function testGetNewQuotesGuestCustomer() $stringCustomerEmailMainTable = 'main_table.customer_email'; $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); - $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); $this->customerModelMockGetEmail($customerModelMock, $customerEmailAddress); - $cartsApiMock = $this->cartsApiMockGuestCustomer($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $orderCollectionMock); - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -427,25 +335,15 @@ public function testGetNewQuotesEmptyJson() $stringCustomerEmailMainTable = 'main_table.customer_email'; $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); - $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $cartsApiMock = $this->cartsApiMockNewQuotesEmptyJson($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock); - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -456,9 +354,7 @@ public function testGetNewQuotesEmptyJson() protected function customerModelMockNewQuotes($customerEmailAddress) { $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); - $customerModelMock->expects($this->once()) - ->method('getEmail') - ->willReturn($customerEmailAddress); + $this->customerModelGetEmail($customerEmailAddress, $customerModelMock); return $customerModelMock; } @@ -474,23 +370,10 @@ protected function cartModelMockNewQuotes($customerId, $allVisbleItems) ->disableOriginalConstructor() ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) ->getMock(); - $cartModelMock->expects($this->exactly(4)) - ->method('getCustomerEmail') - ->willReturnOnConsecutiveCalls( - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART - ); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getCustomerId') - ->willReturn($customerId); - $cartModelMock->expects($this->once()) - ->method('getAllVisibleItems') - ->willReturn($allVisbleItems); + $this->cartModelGetCustomerEmailExactlyFour($cartModelMock); + $this->cartModelGetEntityId($cartModelMock); + $this->cartModelGetCustomerId($customerId, $cartModelMock); + $this->cartModelGetAllVisibleItems($allVisbleItems, $cartModelMock); return $cartModelMock; } @@ -505,12 +388,8 @@ protected function varienSelectMockNewQuotes($where) ->disableOriginalConstructor() ->setMethods(array('where', 'limit')) ->getMock(); - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); + $this->varienSelectWhere($where, $varienSelectMock); + $this->varienSelectLimit($varienSelectMock); return $varienSelectMock; } @@ -534,24 +413,9 @@ protected function newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stri ->disableOriginalConstructor() ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) ->getMock(); - $newCartsCollectionMock->expects($this->exactly(5)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), - array($stringItemsCount, $arrayAddFieldToFilterItemsCount), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), - array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) - ); - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock - ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); + $this->newCartsCollectionAddFieldToFilterExactlyFive($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $newCartsCollectionMock); + $this->newCartsCollectionGetSelectExactlyTwo($varienSelectMock, $newCartsCollectionMock); + $this->newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock); return $newCartsCollectionMock; } @@ -565,9 +429,7 @@ protected function helperMockNewQuotes($newCartsCollectionMock) ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->setMethods(array('addResendFilter')) ->getMock(); - $helperMock->expects($this->once()) - ->method('addResendFilter') - ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + $this->helperAddFieldToFilter($newCartsCollectionMock, $helperMock); return $helperMock; } @@ -581,9 +443,7 @@ protected function cartByEmailModelMock() ->disableOriginalConstructor() ->setMethods(array('getEntityId')) ->getMock(); - $cartByEmailModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::ALREADY_SENT_CART_ID); + $this->cartByEmailGetEntityId($cartByEmailModelMock); return $cartByEmailModelMock; } @@ -598,11 +458,8 @@ protected function quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModel ->disableOriginalConstructor() ->setMethods(array('clear', 'getIterator')) ->getMock(); - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('clear'); - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + $this->quoteByEmailClear($quoteByEmailResoureceCollectionMock); + $this->quoteByEmailGetIterator($cartByEmailModelMock, $quoteByEmailResoureceCollectionMock); return $quoteByEmailResoureceCollectionMock; } @@ -621,15 +478,8 @@ protected function orderCollectionMock($sizeOrderCollection, $stringCustomerEmai ->disableOriginalConstructor() ->setMethods(array('getSize', 'addFieldToFilter')) ->getMock(); - $orderCollectionMock->expects($this->once()) - ->method('getSize') - ->willReturn($sizeOrderCollection); - $orderCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), - array($stringUpdated, $addFieldToFilterUpdated) - ); + $this->orderCollectionGetSize($sizeOrderCollection, $orderCollectionMock); + $this->orderCollectionAddFieldToFilterExactlyTwo($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated, $orderCollectionMock); return $orderCollectionMock; } @@ -667,67 +517,22 @@ protected function cartsApiMockNewQuotesEmptyJson($helperMock, $newCartsCollecti 'getOrderCollection' )) ->getMock(); - $cartsApiMock->expects($this->once()) - ->method('getHelper') - ->willReturn($helperMock); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getFirstDate') - ->willReturnOnConsecutiveCalls( - $existFirstDate, - $existFirstDate - ); - $cartsApiMock->expects($this->once()) - ->method('joinMailchimpSyncDataWithoutWhere') - ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID) - ); - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getCounter') - ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER - ); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->once()) - ->method('setCounter') - ->with(self::COUNTER + 1); - $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); - $cartsApiMock->expects($this->once()) - ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($cartJson); - $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - $cartsApiMock->expects($this->once()) - ->method('getOrderCollection') - ->willReturn($orderCollectionMock); + $this->cartsApiGetHelper($helperMock, $cartsApiMock); + $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock); + $this->cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); + $this->cartsApiUpdateSyncDataExactlyTwice($cartsApiMock); + $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); + $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); + $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); + $this->cartsApiGetCounterExactlyTwo($cartsApiMock); + $this->cartsApiGetBatchId($cartsApiMock); + $this->cartsApiSetCounter($cartsApiMock); + $this->cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock); + $this->cartsApiMakeCartExactlyOne($cartModelMock, $cartJson, $cartsApiMock); + $this->cartsApiSetToken($cartsApiMock); + $this->cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock); return $cartsApiMock; } @@ -741,12 +546,8 @@ protected function customerModelMockSetWebSiteIdLoadByEmail() ->disableOriginalConstructor() ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) ->getMock(); - $customerModelMock->expects($this->once()) - ->method('setWebsiteId') - ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $customerModelMock->expects($this->once()) - ->method('loadByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART); + $this->customerModelSetWebsiteId($customerModelMock); + $this->customerModelLoadByEmail($customerModelMock); return $customerModelMock; } @@ -788,40 +589,16 @@ protected function cartsApiMockGuestCustomer($helperMock, $newCartsCollectionMoc 'getOrderCollection' )) ->getMock(); - $cartsApiMock->expects($this->once()) - ->method('getHelper') - ->willReturn($helperMock); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getFirstDate') - ->willReturnOnConsecutiveCalls( - $existFirstDate, - $existFirstDate - ); - $cartsApiMock->expects($this->once()) - ->method('joinMailchimpSyncDataWithoutWhere') - ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('_updateSyncData') - ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->once()) - ->method('getOrderCollection') - ->willReturn($orderCollectionMock); + $this->cartsApiGetHelper($helperMock, $cartsApiMock); + $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock); + $this->cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); + $this->cartsApiUpdateSyncData($cartsApiMock); + $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); + $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); + $this->cartsApiGetBatchId($cartsApiMock); + $this->cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock); return $cartsApiMock; } @@ -845,30 +622,13 @@ protected function cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, 'getOrderCollection' )) ->getMock(); - $cartsApiMock->expects($this->once()) - ->method('getHelper') - ->willReturn($helperMock); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getFirstDate') - ->willReturnOnConsecutiveCalls( - $existFirstDate, - $existFirstDate - ); - $cartsApiMock->expects($this->once()) - ->method('joinMailchimpSyncDataWithoutWhere') - ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('_updateSyncData') - ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getOrderCollection') - ->willReturn($orderCollectionMock); + $this->cartsApiGetHelper($helperMock, $cartsApiMock); + $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock); + $this->cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); + $this->cartsApiUpdateSyncData($cartsApiMock); + $this->cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock); return $cartsApiMock; } @@ -886,12 +646,7 @@ protected function orderCollectionMockEmptyQuotes($stringCustomerEmailMainTable, ->disableOriginalConstructor() ->setMethods(array('addFieldToFilter')) ->getMock(); - $orderCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), - array($stringUpdated, $addFieldToFilterUpdated) - ); + $this->orderCollectionAddFieldToFilterExactlyTwo($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated, $orderCollectionMock); return $orderCollectionMock; } @@ -906,12 +661,8 @@ protected function cartModelMockEmptyQuote($allVisbleItems) ->disableOriginalConstructor() ->setMethods(array('getEntityId', 'getAllVisibleItems')) ->getMock(); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getAllVisibleItems') - ->willReturn($allVisbleItems); + $this->cartModelGetEntityId($cartModelMock); + $this->cartModelGetAllVisibleItems($allVisbleItems, $cartModelMock); return $cartModelMock; } @@ -951,75 +702,23 @@ protected function cartsApiMockNewQuote($helperMock, $newCartsCollectionMock, $e 'getOrderCollection' )) ->getMock(); - $cartsApiMock->expects($this->once()) - ->method('getHelper') - ->willReturn($helperMock); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getFirstDate') - ->willReturnOnConsecutiveCalls( - $existFirstDate, - $existFirstDate - ); - $cartsApiMock->expects($this->once()) - ->method('joinMailchimpSyncDataWithoutWhere') - ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) - ); - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(4)) - ->method('getCounter') - ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER, - self::COUNTER, - self::COUNTER - ); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->exactly(2)) - ->method('setCounter') - ->withConsecutive( - array(self::COUNTER + 1), - array(self::COUNTER + 1) - ); - $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); - $cartsApiMock->expects($this->once()) - ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($cartJson); - $cartsApiMock->expects($this->once()) - ->method('getToken') - ->willReturn($token); - $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - $cartsApiMock->expects($this->once()) - ->method('getOrderCollection') - ->willReturn($orderCollectionMock); + $this->cartsApiGetHelper($helperMock, $cartsApiMock); + $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock); + $this->cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); + $this->cartsApiUpdateSyncDataExactlyTwo($token, $cartsApiMock); + $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); + $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); + $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); + $this->cartsApiGetCounterExactlyFour($cartsApiMock); + $this->cartsApiGetBatchId($cartsApiMock); + $this->cartsApiSetCounterExactlyTwo($cartsApiMock); + $this->cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock); + $this->cartsApiMakeCartExactlyOne($cartModelMock, $cartJson, $cartsApiMock); + $this->cartsApiGetToken($token, $cartsApiMock); + $this->cartsApiSetToken($cartsApiMock); + $this->cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock); return $cartsApiMock; } @@ -1037,15 +736,9 @@ protected function varienSelectMockModifiedCart($arrayTableName, $conditionSelec ->disableOriginalConstructor() ->setMethods(array('joinLeft', 'where', 'limit')) ->getMock(); - $varienSelectMock->expects($this->once()) - ->method('joinLeft') - ->with($arrayTableName, $conditionSelect, $m4m); - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); + $this->varienSelectJoinLeft($arrayTableName, $conditionSelect, $m4m, $varienSelectMock); + $this->varienSelectWhere($where, $varienSelectMock); + $this->varienSelectLimit($varienSelectMock); return $varienSelectMock; } @@ -1060,19 +753,9 @@ protected function cartModelMockModifiedCart($customerId) ->disableOriginalConstructor() ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) ->getMock(); - $cartModelMock->expects($this->exactly(3)) - ->method('getCustomerEmail') - ->willReturnOnConsecutiveCalls( - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART, - self::CUSTOMER_EMAIL_BY_CART - ); - $cartModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::CART_ID); - $cartModelMock->expects($this->once()) - ->method('getCustomerId') - ->willReturn($customerId); + $this->cartModelGetCustomerEmailExactlyThree($cartModelMock); + $this->cartModelGetEntityId($cartModelMock); + $this->cartModelGetCustomerId($customerId, $cartModelMock); return $cartModelMock; } @@ -1090,22 +773,9 @@ protected function newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, ->disableOriginalConstructor() ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) ->getMock(); - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) - ); - $newCartsCollectionMock->expects($this->exactly(3)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock, - $varienSelectMock - ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); + $this->newCartsCollectionAddFieldToFilterExactlyTwo($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $newCartsCollectionMock); + $this->newCartsCollectionGetSelectExactlyThree($varienSelectMock, $newCartsCollectionMock); + $this->newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock); return $newCartsCollectionMock; } @@ -1138,55 +808,19 @@ protected function modifiedQuotesEmptyJson($mcTableName, $newCartsCollectionMock 'addProductNotSentData' )) ->getMock(); - $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->once()) - ->method('getMailchimpEcommerceDataTableName') - ->willReturn($mcTableName); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(2)) - ->method('getCounter') - ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER - ); - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID) - ); - $cartsApiMock->expects($this->once()) - ->method('setCounter') - ->with(self::COUNTER + 1); - $cartsApiMock->expects($this->once()) - ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) - ->willReturn($cartJson); - $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); + $this->cartsApiSetToken($cartsApiMock); + $this->cartsApiGetBatchId($cartsApiMock); + $this->cartsApiGetMailchimpEcommerceDataTableName($mcTableName, $cartsApiMock); + $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); + $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); + $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); + $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); + $this->cartsApiGetCounterExactlyTwo($cartsApiMock); + $this->cartsApiUpdateSyncDataExactlyTwice($cartsApiMock); + $this->cartsApiSetCounter($cartsApiMock); + $this->cartsApiMakeCart($cartModelMock, $cartJson, $cartsApiMock); + $this->cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock); return $cartsApiMock; } @@ -1204,15 +838,9 @@ protected function modifiedQuotesGuestCustomer($where, $arrayTableName, $conditi ->disableOriginalConstructor() ->setMethods(array('where', 'limit', 'joinLeft')) ->getMock(); - $varienSelectMock->expects($this->once()) - ->method('where') - ->with($where); - $varienSelectMock->expects($this->once()) - ->method('limit') - ->with(self::BATCH_LIMIT_FROM_CONFIG); - $varienSelectMock->expects($this->once()) - ->method('joinLeft') - ->with($arrayTableName, $conditionSelect, $m4m); + $this->varienSelectWhere($where, $varienSelectMock); + $this->varienSelectLimit($varienSelectMock); + $this->varienSelectJoinLeft($arrayTableName, $conditionSelect, $m4m, $varienSelectMock); return $varienSelectMock; } @@ -1231,22 +859,9 @@ protected function newCartsModifiedQuotesGuestCustomer($arrayAddFieldToFilter, $ ->disableOriginalConstructor() ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) ->getMock(); - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringStoreId, $arrayAddFieldToFilterStoreId) - ); - $newCartsCollectionMock->expects($this->exactly(3)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock, - $varienSelectMock - ); - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); + $this->newCartsAddFieldToFilterExactlyTwo($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $newCartsCollectionMock); + $this->newCartsCollectionGetSelectExactlyThree($varienSelectMock, $newCartsCollectionMock); + $this->newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock); return $newCartsCollectionMock; } @@ -1270,28 +885,13 @@ protected function cartsApiMockModifiedQuotesGuestCustomer($mcTableName, $newCar 'getMailchimpEcommerceDataTableName' )) ->getMock(); - $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); - $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - $cartsApiMock->expects($this->once()) - ->method('_updateSyncData') - ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - $cartsApiMock->expects($this->once()) - ->method('getMailchimpEcommerceDataTableName') - ->willReturn($mcTableName); + $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); + $this->cartsApiUpdateSyncData($cartsApiMock); + $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); + $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); + $this->cartsApiGetBatchId($cartsApiMock); + $this->cartsApiGetMailchimpEcommerceDataTableName($mcTableName, $cartsApiMock); return $cartsApiMock; } @@ -1326,63 +926,581 @@ protected function cartsApiMockModifiedQuotes($token, $mcTableName, $newCartsCol 'addProductNotSentData' )) ->getMock(); + $this->cartsApiGetToken($token, $cartsApiMock); + $this->cartsApiSetToken($cartsApiMock); + $this->cartsApiGetBatchId($cartsApiMock); + $this->cartsApiGetMailchimpEcommerceDataTableName($mcTableName, $cartsApiMock); + $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); + $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); + $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); + $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); + $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); + $this->cartsApiGetCounterExactlyFour($cartsApiMock); + $this->cartsApiUpdateSyncDataExactlyTwo($token, $cartsApiMock); + $this->cartsApiSetCounterExactlyTwo($cartsApiMock); + $this->cartsApiMakeCart($cartModelMock, $cartJson, $cartsApiMock); + $this->cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock); + return $cartsApiMock; + } + + /** + * @param $token + * @param $cartsApiMock + */ + protected function cartsApiGetToken($token, $cartsApiMock) + { $cartsApiMock->expects($this->once()) ->method('getToken') ->willReturn($token); + } + + /** + * @param $cartModelMock + * @param $allCarts + * @param $cartsApiMock + */ + protected function cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock) + { $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + } + + /** + * @param $cartModelMock + * @param $cartJson + * @param $cartsApiMock + */ + protected function cartsApiMakeCart($cartModelMock, $cartJson, $cartsApiMock) + { $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); + ->method('_makeCart') + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) + ->willReturn($cartJson); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiSetCounterExactlyTwo($cartsApiMock) + { + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array(self::COUNTER + 1), + array(self::COUNTER + 1) + ); + } + + /** + * @param $token + * @param $cartsApiMock + */ + protected function cartsApiUpdateSyncDataExactlyTwo($token, $cartsApiMock) + { + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) + ); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiGetCounterExactlyFour($cartsApiMock) + { + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER + ); + } + + /** + * @param $quoteByEmailResoureceCollectionMock + * @param $cartsApiMock + */ + protected function cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock) + { $cartsApiMock->expects($this->once()) - ->method('getMailchimpEcommerceDataTableName') - ->willReturn($mcTableName); + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock) + { $cartsApiMock->expects($this->once()) - ->method('getQuoteCollection') - ->willReturn($newCartsCollectionMock); + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + } + + /** + * @param $customerModelMock + * @param $cartsApiMock + */ + protected function cartsApiGetCustomerModel($customerModelMock, $cartsApiMock) + { + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiGetBatchLimitFromConfig($cartsApiMock) + { $cartsApiMock->expects($this->once()) ->method('getBatchLimitFromConfig') ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + } + + /** + * @param $newCartsCollectionMock + * @param $cartsApiMock + */ + protected function cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock) + { $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + } + + /** + * @param $mcTableName + * @param $cartsApiMock + */ + protected function cartsApiGetMailchimpEcommerceDataTableName($mcTableName, $cartsApiMock) + { $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiGetBatchId($cartsApiMock) + { $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($quoteByEmailResoureceCollectionMock); - $cartsApiMock->expects($this->exactly(4)) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiSetToken($cartsApiMock) + { + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiUpdateSyncData($cartsApiMock) + { + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + } + + /** + * @param $cartModelMock + * @param $newCartsCollectionMock + */ + protected function newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock) + { + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + } + + /** + * @param $varienSelectMock + * @param $newCartsCollectionMock + */ + protected function newCartsCollectionGetSelectExactlyThree($varienSelectMock, $newCartsCollectionMock) + { + $newCartsCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + } + + /** + * @param $arrayAddFieldToFilter + * @param $stringStoreId + * @param $arrayAddFieldToFilterStoreId + * @param $newCartsCollectionMock + */ + protected function newCartsAddFieldToFilterExactlyTwo($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $newCartsCollectionMock) + { + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringStoreId, $arrayAddFieldToFilterStoreId) + ); + } + + /** + * @param $where + * @param $varienSelectMock + */ + protected function varienSelectWhere($where, $varienSelectMock) + { + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + } + + /** + * @param $varienSelectMock + */ + protected function varienSelectLimit($varienSelectMock) + { + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + } + + /** + * @param $arrayTableName + * @param $conditionSelect + * @param $m4m + * @param $varienSelectMock + */ + protected function varienSelectJoinLeft($arrayTableName, $conditionSelect, $m4m, $varienSelectMock) + { + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiGetCounterExactlyTwo($cartsApiMock) + { + $cartsApiMock->expects($this->exactly(2)) ->method('getCounter') ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER, self::COUNTER, self::COUNTER ); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiSetCounter($cartsApiMock) + { + $cartsApiMock->expects($this->once()) + ->method('setCounter') + ->with(self::COUNTER + 1); + } + + /** + * @param $cartsApiMock + */ + protected function cartsApiUpdateSyncDataExactlyTwice($cartsApiMock) + { $cartsApiMock->expects($this->exactly(2)) ->method('_updateSyncData') ->withConsecutive( array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) + array(self::CART_ID, self::MAILCHIMP_STORE_ID) ); - $cartsApiMock->expects($this->exactly(2)) - ->method('setCounter') + } + + /** + * @param $arrayAddFieldToFilter + * @param $arrayAddFieldToFilterStoreId + * @param $newCartsCollectionMock + */ + protected function newCartsCollectionAddFieldToFilterExactlyTwo($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $newCartsCollectionMock) + { + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') ->withConsecutive( - array(self::COUNTER + 1), - array(self::COUNTER + 1) + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) + ); + } + + /** + * @param $cartModelMock + */ + protected function cartModelGetCustomerEmailExactlyThree($cartModelMock) + { + $cartModelMock->expects($this->exactly(3)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + } + + /** + * @param $cartModelMock + */ + protected function cartModelGetEntityId($cartModelMock) + { + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + } + + /** + * @param $customerId + * @param $cartModelMock + */ + protected function cartModelGetCustomerId($customerId, $cartModelMock) + { + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + } + + /** + * @param $orderCollectionMock + * @param $cartsApiMock + */ + protected function cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock) + { + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + } + + /** + * @param $existFirstDate + * @param $cartsApiMock + */ + protected function cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock) + { + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate ); + } + + /** + * @param $newCartsCollectionMock + * @param $cartsApiMock + */ + protected function cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock) + { + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + } + + /** + * @param $cartModelMock + * @param $cartJson + * @param $cartsApiMock + */ + protected function cartsApiMakeCartExactlyOne($cartModelMock, $cartJson, $cartsApiMock) + { $cartsApiMock->expects($this->once()) ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($cartJson); + } + + /** + * @param $helperMock + * @param $cartsApiMock + */ + protected function cartsApiGetHelper($helperMock, $cartsApiMock) + { $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); - return $cartsApiMock; + ->method('getHelper') + ->willReturn($helperMock); + } + + /** + * @param $allVisbleItems + * @param $cartModelMock + */ + protected function cartModelGetAllVisibleItems($allVisbleItems, $cartModelMock) + { + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + } + + /** + * @param $stringCustomerEmailMainTable + * @param $addFieldToFilterOrderCollection + * @param $stringUpdated + * @param $addFieldToFilterUpdated + * @param $orderCollectionMock + */ + protected function orderCollectionAddFieldToFilterExactlyTwo($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated, $orderCollectionMock) + { + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + } + + /** + * @param $sizeOrderCollection + * @param $orderCollectionMock + */ + protected function orderCollectionGetSize($sizeOrderCollection, $orderCollectionMock) + { + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + } + + /** + * @param $cartByEmailModelMock + * @param $quoteByEmailResoureceCollectionMock + */ + protected function quoteByEmailGetIterator($cartByEmailModelMock, $quoteByEmailResoureceCollectionMock) + { + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + } + + /** + * @param $quoteByEmailResoureceCollectionMock + */ + protected function quoteByEmailClear($quoteByEmailResoureceCollectionMock) + { + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + } + + /** + * @param $cartByEmailModelMock + */ + protected function cartByEmailGetEntityId($cartByEmailModelMock) + { + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + } + + /** + * @param $newCartsCollectionMock + * @param $helperMock + */ + protected function helperAddFieldToFilter($newCartsCollectionMock, $helperMock) + { + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + } + + /** + * @param $varienSelectMock + * @param $newCartsCollectionMock + */ + protected function newCartsCollectionGetSelectExactlyTwo($varienSelectMock, $newCartsCollectionMock) + { + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + } + + /** + * @param $arrayAddFieldToFilter + * @param $stringCustomerEmail + * @param $arrayAddFieldToFilterCustomerEmail + * @param $stringItemsCount + * @param $arrayAddFieldToFilterItemsCount + * @param $arrayAddFieldToFilterStoreId + * @param $stringUpdatedAt + * @param $arrayAddFieldToFilterUpdatedAt + * @param $newCartsCollectionMock + */ + protected function newCartsCollectionAddFieldToFilterExactlyFive($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $newCartsCollectionMock) + { + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + } + + /** + * @param $cartModelMock + */ + protected function cartModelGetCustomerEmailExactlyFour($cartModelMock) + { + $cartModelMock->expects($this->exactly(4)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + } + + /** + * @param $customerEmailAddress + * @param $customerModelMock + */ + protected function customerModelGetEmail($customerEmailAddress, $customerModelMock) + { + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + } + + /** + * @param $customerModelMock + */ + protected function customerModelLoadByEmail($customerModelMock) + { + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + } + + /** + * @param $customerModelMock + */ + protected function customerModelSetWebsiteId($customerModelMock) + { + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); } } From 0aefcb8b41202c5f0f2b3c47766b1bc1b0d46c77 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Wed, 9 Jan 2019 12:10:15 -0300 Subject: [PATCH 075/113] coverage 100 percent of the tests for getNewQuotes and getModifiedQuotes --- .../MailChimp/Model/Api/CartsTest.php | 2520 +++++++++-------- 1 file changed, 1270 insertions(+), 1250 deletions(-) mode change 100644 => 100755 dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php old mode 100644 new mode 100755 index 34cf3a751..0c7c25e7f --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -32,6 +32,7 @@ public function tearDown() public function testCreateBatchJson() { $batchArray = array(); + $cartsApiMock = $this->cartsApiMock->setMethods(array( 'getHelper', '_getConvertedQuotes', @@ -44,8 +45,9 @@ public function testCreateBatchJson() ->disableOriginalConstructor() ->setMethods(array('isAbandonedCartEnabled', 'getAbandonedCartFirstDate', 'getDateMicrotime', 'getResendTurn')) ->getMock(); + $cartsApiMock->expects($this->once())->method('setBatchId')->with(self::BATCH_ID); - $this->cartsApiGetHelper($helperMock, $cartsApiMock); + $cartsApiMock->expects($this->once())->method('getHelper')->willReturn($helperMock); $cartsApiMock->expects($this->once())->method('_getConvertedQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); $cartsApiMock->expects($this->once())->method('_getModifiedQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); $cartsApiMock->expects($this->once())->method('_getNewQuotes')->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID)->willReturn($batchArray); @@ -53,6 +55,7 @@ public function testCreateBatchJson() $helperMock->expects($this->once())->method('getAbandonedCartFirstDate')->with(self::MAGENTO_STORE_ID)->willReturn('00-00-00 00:00:00'); $helperMock->expects($this->once())->method('getDateMicrotime')->willReturn(self::DATE); $helperMock->expects($this->once())->method('getResendTurn')->with(self::MAGENTO_STORE_ID)->willReturn(null); + $cartsApiMock->createBatchJson(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -66,31 +69,7 @@ public function testGetConvertedQuotes () $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; $m4m = array('m4m.*'); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail')) - ->getMock(); - $cartModelMock->expects($this->once()) - ->method('getCustomerEmail') - ->willReturnOnConsecutiveCalls(self::CUSTOMER_EMAIL_BY_CART); - $this->cartModelGetEntityId($cartModelMock); - $newCartsCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter) - ); - $this->newCartsCollectionGetSelectExactlyThree($varienSelectMock, $newCartsCollectionMock); - $this->newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); + $cartsApiMock = $this->cartsApiMock->setMethods(array( 'getMailchimpEcommerceDataTableName', 'getQuoteCollection', @@ -102,21 +81,108 @@ public function testGetConvertedQuotes () 'setCounter' )) ->getMock(); + $quoteResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('joinLeft', 'where', 'limit')) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail')) + ->getMock(); + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + $cartsApiMock->expects($this->once()) ->method('getMailchimpEcommerceDataTableName') ->willReturn($mailchimpTableName); - $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); - $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); - $this->cartsApiGetCounterExactlyFour($cartsApiMock); - $this->cartsApiGetBatchId($cartsApiMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($quoteResoureceCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER + ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); $cartsApiMock->expects($this->exactly(2)) ->method('_updateSyncData') ->withConsecutive( array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1) ); - $this->cartsApiSetCounterExactlyTwo($cartsApiMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array(self::COUNTER + 1), + array(self::COUNTER + 1) + ); + $quoteResoureceCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter) + ); + $quoteResoureceCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $quoteResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $cartModelMock->expects($this->once()) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls(self::CUSTOMER_EMAIL_BY_CART); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + $cartsApiMock->_getConvertedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } @@ -136,190 +202,824 @@ public function testGetModifiedQuotes() $m4m = array('m4m.*'); $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; - $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); - $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $newCartsCollectionMock = $this->newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $cartsApiMock = $this->cartsApiMockModifiedQuotes($token, $mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts); - $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); - } - - public function testGetModifiedQuotesGuestCustomer() - { - $mcTableName = 'mailchimp_ecommerce_sync_data'; - $customerId = ''; - $customerEmailAddress = 'test@ebizmarts.com'; - $stringStoreId = 'store_id'; - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_deleted = 0 - AND m4m.mailchimp_sync_delta < updated_at"; - $arrayTableName = array('m4m' => $mcTableName); - $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' - AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; - $m4m = array('m4m.*'); - $varienSelectMock = $this->modifiedQuotesGuestCustomer($where, $arrayTableName, $conditionSelect, $m4m); - $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $newCartsCollectionMock = $this->newCartsModifiedQuotesGuestCustomer($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); - $this->customerModelMockGetEmail($customerModelMock, $customerEmailAddress); - $cartsApiMock = $this->cartsApiMockModifiedQuotesGuestCustomer($mcTableName, $newCartsCollectionMock, $customerModelMock); - $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); - } - - public function testGetModifiedQuotesEmptyJson() - { - $mcTableName = 'mailchimp_ecommerce_sync_data'; - $customerEmailAddress = ''; - $cartJson = ''; - $customerId = 1; - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_deleted = 0 - AND m4m.mailchimp_sync_delta < updated_at"; - $arrayTableName = array('m4m' => $mcTableName); - $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' - AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; - $m4m = array('m4m.*'); - $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $varienSelectMock = $this->varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where); - $cartModelMock = $this->cartModelMockModifiedCart($customerId); - $newCartsCollectionMock = $this->newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock); - $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $cartsApiMock = $this->modifiedQuotesEmptyJson($mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts); - $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); - } - - public function testGetNewQuotesNewQuote() - { - $existFirstDate = '2018-11-30'; - $customerId = 1; - $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; - $customerEmailAddress = ''; - $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); - $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"test@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array('item'); - $sizeOrderCollection = 0; - $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); - $stringCustomerEmailMainTable = 'main_table.customer_email'; - $stringUpdated = 'main_table.updated_at'; - $addFieldToFilterUpdated = array('from' => ''); - $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $cartsApiMock = $this->cartsApiMockNewQuote($helperMock, $newCartsCollectionMock, $existFirstDate, $token, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock); - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); - } - public function testGetNewQuotesIsOrder() - { - $existFirstDate = '2018-11-30'; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array('item'); - $sizeOrderCollection = 1; - $addFieldToFilterOrderCollection = array('eq' => ''); - $stringCustomerEmailMainTable = 'main_table.customer_email'; - $stringUpdated = 'main_table.updated_at'; - $addFieldToFilterUpdated = array('from' => ''); - $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $cartModelMock = $this->cartModelMockEmptyQuote($allVisbleItems); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $cartsApiMock = $this->cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, $existFirstDate, $orderCollectionMock); - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); - } + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'setToken', + 'getToken', + 'getBatchId', + 'getMailchimpEcommerceDataTableName', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getQuoteCollection', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'setCounter', + 'getCounter', + '_makeCart', + '_getAllCartsByEmail', + 'addProductNotSentData' + )) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) + ->getMock(); + $quoteResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('joinLeft', 'where', 'limit')) + ->getMock(); + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); - public function testGetNewQuotesEmpty() - { - $existFirstDate = '2018-11-30'; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array(); - $addFieldToFilterOrderCollection = array('eq' => ''); - $stringCustomerEmailMainTable = 'main_table.customer_email'; + $cartsApiMock->expects($this->once()) + ->method('getToken') + ->willReturn($token); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($quoteResoureceCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER + ); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) + ); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array(self::COUNTER + 1), + array(self::COUNTER + 1) + ); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + $cartModelMock->expects($this->exactly(3)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + $quoteResoureceCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) + ); + $quoteResoureceCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $quoteResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + + $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetModifiedQuotesGuestCustomer() + { + $mcTableName = 'mailchimp_ecommerce_sync_data'; + $customerId = ''; + $customerEmailAddress = 'test@ebizmarts.com'; + $stringStoreId = 'store_id'; + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_deleted = 0 + AND m4m.mailchimp_sync_delta < updated_at"; + $arrayTableName = array('m4m' => $mcTableName); + $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' + AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + $m4m = array('m4m.*'); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getQuoteCollection', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'getBatchId', + 'getOrderCollection', + 'getMailchimpEcommerceDataTableName' + )) + ->getMock(); + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit', 'joinLeft')) + ->getMock(); + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringStoreId, $arrayAddFieldToFilterStoreId) + ); + $newCartsCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->exactly(2)) + ->method('getEmail') + ->willReturnOnConsecutiveCalls( + $customerEmailAddress, + $customerEmailAddress + ); + $cartModelMock->expects($this->exactly(3)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + + $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetModifiedQuotesEmptyJson() + { + $mcTableName = 'mailchimp_ecommerce_sync_data'; + $customerEmailAddress = ''; + $cartJson = ''; + $customerId = 1; + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_deleted = 0 + AND m4m.mailchimp_sync_delta < updated_at"; + $arrayTableName = array('m4m' => $mcTableName); + $conditionSelect = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' + AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + $m4m = array('m4m.*'); + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'setToken', + 'getBatchId', + 'getMailchimpEcommerceDataTableName', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getQuoteCollection', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + 'setCounter', + 'getCounter', + '_makeCart', + '_getAllCartsByEmail', + 'addProductNotSentData' + )) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) + ->getMock(); + $quoteResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('joinLeft', 'where', 'limit')) + ->getMock(); + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mcTableName); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($quoteResoureceCollectionMock); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER + ); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID) + ); + $cartsApiMock->expects($this->once()) + ->method('setCounter') + ->with(self::COUNTER + 1); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + $cartModelMock->expects($this->exactly(3)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + $quoteResoureceCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) + ); + $quoteResoureceCollectionMock->expects($this->exactly(3)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock, + $varienSelectMock + ); + $quoteResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayTableName, $conditionSelect, $m4m); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + + $cartsApiMock->_getModifiedQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + + public function testGetNewQuotesNewQuote() + { + $existFirstDate = '2018-11-30'; + $customerId = 1; + $token = 'ec4f79b2e4677d2edc5bf78c934e5794'; + $customerEmailAddress = ''; + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + $cartJson = '{"id":"692","customer":{"id":"GUEST-2018-11-30-20-00-07-96938700","email_address":"test@ebizmarts.com","opt_in_status":false,"first_name":"Lucia","last_name":"en el checkout","address":{"address1":"asdf","city":"asd","postal_code":"212312","country":"Tajikistan","country_code":"TJ"}},"campaign_id":"482d28ee12","checkout_url":"http:\/\/f3364930.ngrok.io\/mailchimp\/cart\/loadquote\?id=692&token=ec4f79b2e4677d2edc5bf78c934e5794","currency_code":"USD","order_total":"1700.0000","tax_total":0,"lines":[{"id":"1","product_id":"425","product_variant_id":"310","quantity":5,"price":"1700.0000"}]}'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $stringCustomerEmailMainTable = 'main_table.customer_email'; $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); - $cartModelMock = $this->cartModelMockEmptyQuote($allVisbleItems); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $orderCollectionMock = $this->orderCollectionMockEmptyQuotes($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $cartsApiMock = $this->cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, $existFirstDate, $orderCollectionMock); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + 'setCounter', + 'addProductNotSentData', + '_makeCart', + 'setToken', + 'getToken', + 'getOrderCollection' + )) + ->getMock(); + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->getMock(); + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) + ); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(4)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER, + self::COUNTER, + self::COUNTER + ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->exactly(2)) + ->method('setCounter') + ->withConsecutive( + array(self::COUNTER + 1), + array(self::COUNTER + 1) + ); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); + $cartsApiMock->expects($this->once()) + ->method('_makeCart') + ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($cartJson); + $cartsApiMock->expects($this->once()) + ->method('getToken') + ->willReturn($token); + $cartsApiMock->expects($this->once()) + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); + $cartModelMock->expects($this->exactly(4)) + ->method('getCustomerEmail') + ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART, + self::CUSTOMER_EMAIL_BY_CART + ); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } - public function testGetNewQuotesGuestCustomer() - { - $existFirstDate = '2018-11-30'; - $customerId = ''; - $customerEmailAddress = 'test@ebizmarts.com'; - $stringCustomerEmail = 'customer_email'; - $stringItemsCount = 'items_count'; - $stringUpdatedAt = 'updated_at'; - $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); - $arrayAddFieldToFilterItemsCount = array('gt' => 0); - $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); - $arrayAddFieldToFilter = array('eq' => 1); - $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); - $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array('item'); - $sizeOrderCollection = 0; - $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); - $stringCustomerEmailMainTable = 'main_table.customer_email'; - $stringUpdated = 'main_table.updated_at'; - $addFieldToFilterUpdated = array('from' => ''); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); - $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); - $this->customerModelMockGetEmail($customerModelMock, $customerEmailAddress); - $cartsApiMock = $this->cartsApiMockGuestCustomer($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $orderCollectionMock); + public function testGetNewQuotesIsOrder() + { + $existFirstDate = '2018-11-30'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 1; + $addFieldToFilterOrderCollection = array('eq' => ''); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getOrderCollection' + )) + ->getMock(); + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getAllVisibleItems')) + ->getMock(); + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } - public function testGetNewQuotesEmptyJson() + public function testGetNewQuotesEmpty() { $existFirstDate = '2018-11-30'; - $customerId = 1; - $customerEmailAddress = ''; - $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); - $cartJson = ''; $stringCustomerEmail = 'customer_email'; $stringItemsCount = 'items_count'; $stringUpdatedAt = 'updated_at'; @@ -329,358 +1029,136 @@ public function testGetNewQuotesEmptyJson() $arrayAddFieldToFilter = array('eq' => 1); $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); $where = "m4m.mailchimp_sync_delta IS NULL"; - $allVisbleItems = array('item'); - $sizeOrderCollection = 0; - $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $allVisbleItems = array(); + $addFieldToFilterOrderCollection = array('eq' => ''); $stringCustomerEmailMainTable = 'main_table.customer_email'; $stringUpdated = 'main_table.updated_at'; $addFieldToFilterUpdated = array('from' => ''); - $customerModelMock = $this->customerModelMockNewQuotes($customerEmailAddress); - $cartModelMock = $this->cartModelMockNewQuotes($customerId, $allVisbleItems); - $varienSelectMock = $this->varienSelectMockNewQuotes($where); - $newCartsCollectionMock = $this->newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock); - $helperMock = $this->helperMockNewQuotes($newCartsCollectionMock); - $cartByEmailModelMock = $this->cartByEmailModelMock(); - $quoteByEmailResoureceCollectionMock = $this->quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock); - $orderCollectionMock = $this->orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated); - $cartsApiMock = $this->cartsApiMockNewQuotesEmptyJson($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock); - $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); - } - - /** - * @param $customerEmailAddress - * @return mixed - */ - protected function customerModelMockNewQuotes($customerEmailAddress) - { - $customerModelMock = $this->customerModelMockSetWebSiteIdLoadByEmail(); - $this->customerModelGetEmail($customerEmailAddress, $customerModelMock); - return $customerModelMock; - } - /** - * @param $customerId - * @param $allVisbleItems - * @return mixed - */ - protected function cartModelMockNewQuotes($customerId, $allVisbleItems) - { - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getOrderCollection' + )) ->getMock(); - $this->cartModelGetCustomerEmailExactlyFour($cartModelMock); - $this->cartModelGetEntityId($cartModelMock); - $this->cartModelGetCustomerId($customerId, $cartModelMock); - $this->cartModelGetAllVisibleItems($allVisbleItems, $cartModelMock); - return $cartModelMock; - } - - /** - * @param $where - * @return mixed - */ - protected function varienSelectMockNewQuotes($where) - { - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('where', 'limit')) + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) ->getMock(); - $this->varienSelectWhere($where, $varienSelectMock); - $this->varienSelectLimit($varienSelectMock); - return $varienSelectMock; - } - - /** - * @param $arrayAddFieldToFilter - * @param $stringCustomerEmail - * @param $arrayAddFieldToFilterCustomerEmail - * @param $stringItemsCount - * @param $arrayAddFieldToFilterItemsCount - * @param $arrayAddFieldToFilterStoreId - * @param $stringUpdatedAt - * @param $arrayAddFieldToFilterUpdatedAt - * @param $varienSelectMock - * @param $cartModelMock - * @return mixed - */ - protected function newCartsCollectionMockNewQuotes($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $varienSelectMock, $cartModelMock) - { $newCartsCollectionMock = $this ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) ->disableOriginalConstructor() ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) ->getMock(); - $this->newCartsCollectionAddFieldToFilterExactlyFive($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $newCartsCollectionMock); - $this->newCartsCollectionGetSelectExactlyTwo($varienSelectMock, $newCartsCollectionMock); - $this->newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock); - return $newCartsCollectionMock; - } - - /** - * @param $newCartsCollectionMock - * @return mixed - */ - protected function helperMockNewQuotes($newCartsCollectionMock) - { - $helperMock = $this - ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->setMethods(array('addResendFilter')) - ->getMock(); - $this->helperAddFieldToFilter($newCartsCollectionMock, $helperMock); - return $helperMock; - } - - /** - * @return mixed - */ - protected function cartByEmailModelMock() - { - $cartByEmailModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) ->disableOriginalConstructor() - ->setMethods(array('getEntityId')) + ->setMethods(array('where', 'limit')) ->getMock(); - $this->cartByEmailGetEntityId($cartByEmailModelMock); - return $cartByEmailModelMock; - } - - /** - * @param $cartByEmailModelMock - * @return mixed - */ - protected function quoteByEmailResourceCollectionMockNewQuotes($cartByEmailModelMock) - { - $quoteByEmailResoureceCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) ->disableOriginalConstructor() - ->setMethods(array('clear', 'getIterator')) + ->setMethods(array('getEntityId', 'getAllVisibleItems')) ->getMock(); - $this->quoteByEmailClear($quoteByEmailResoureceCollectionMock); - $this->quoteByEmailGetIterator($cartByEmailModelMock, $quoteByEmailResoureceCollectionMock); - return $quoteByEmailResoureceCollectionMock; - } - - /** - * @param $sizeOrderCollection - * @param $stringCustomerEmailMainTable - * @param $addFieldToFilterOrderCollection - * @param $stringUpdated - * @param $addFieldToFilterUpdated - * @return mixed - */ - protected function orderCollectionMock($sizeOrderCollection, $stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated) - { $orderCollectionMock = $this ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) ->disableOriginalConstructor() - ->setMethods(array('getSize', 'addFieldToFilter')) - ->getMock(); - $this->orderCollectionGetSize($sizeOrderCollection, $orderCollectionMock); - $this->orderCollectionAddFieldToFilterExactlyTwo($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated, $orderCollectionMock); - return $orderCollectionMock; - } - - /** - * @param $helperMock - * @param $newCartsCollectionMock - * @param $existFirstDate - * @param $customerModelMock - * @param $quoteByEmailResoureceCollectionMock - * @param $cartModelMock - * @param $allCarts - * @param $cartJson - * @param $orderCollectionMock - * @return mixed - */ - protected function cartsApiMockNewQuotesEmptyJson($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock) - { - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'getHelper', - 'getQuoteCollection', - 'getFirstDate', - 'joinMailchimpSyncDataWithoutWhere', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - '_getAllCartsByEmail', - 'getCounter', - 'getBatchId', - 'setCounter', - 'addProductNotSentData', - '_makeCart', - 'setToken', - 'getOrderCollection' - )) - ->getMock(); - $this->cartsApiGetHelper($helperMock, $cartsApiMock); - $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock); - $this->cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); - $this->cartsApiUpdateSyncDataExactlyTwice($cartsApiMock); - $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); - $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); - $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); - $this->cartsApiGetCounterExactlyTwo($cartsApiMock); - $this->cartsApiGetBatchId($cartsApiMock); - $this->cartsApiSetCounter($cartsApiMock); - $this->cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock); - $this->cartsApiMakeCartExactlyOne($cartModelMock, $cartJson, $cartsApiMock); - $this->cartsApiSetToken($cartsApiMock); - $this->cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock); - return $cartsApiMock; - } - - /** - * @return mixed - */ - protected function customerModelMockSetWebSiteIdLoadByEmail() - { - $customerModelMock = $this - ->getMockBuilder(Mage_Customer_Model_Customer::class) - ->disableOriginalConstructor() - ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->setMethods(array('addFieldToFilter')) ->getMock(); - $this->customerModelSetWebsiteId($customerModelMock); - $this->customerModelLoadByEmail($customerModelMock); - return $customerModelMock; - } - /** - * @param $customerModelMock - * @param $customerEmailAddress - */ - protected function customerModelMockGetEmail($customerModelMock, $customerEmailAddress) - { - $customerModelMock->expects($this->exactly(2)) - ->method('getEmail') + $cartsApiMock->expects($this->once()) + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') ->willReturnOnConsecutiveCalls( - $customerEmailAddress, - $customerEmailAddress + $existFirstDate, + $existFirstDate + ); + $cartsApiMock->expects($this->once()) + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->once()) + ->method('_updateSyncData') + ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) ); - } - - /** - * @param $helperMock - * @param $newCartsCollectionMock - * @param $existFirstDate - * @param $customerModelMock - * @param $orderCollectionMock - * @return mixed - */ - protected function cartsApiMockGuestCustomer($helperMock, $newCartsCollectionMock, $existFirstDate, $customerModelMock, $orderCollectionMock) - { - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'getHelper', - 'getQuoteCollection', - 'getFirstDate', - 'joinMailchimpSyncDataWithoutWhere', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - 'getBatchId', - 'getOrderCollection' - )) - ->getMock(); - $this->cartsApiGetHelper($helperMock, $cartsApiMock); - $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock); - $this->cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); - $this->cartsApiUpdateSyncData($cartsApiMock); - $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); - $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); - $this->cartsApiGetBatchId($cartsApiMock); - $this->cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock); - return $cartsApiMock; - } - - /** - * @param $helperMock - * @param $newCartsCollectionMock - * @param $existFirstDate - * @param $orderCollectionMock - * @return mixed - */ - protected function cartsApiMockEmptyQuotes($helperMock, $newCartsCollectionMock, $existFirstDate, $orderCollectionMock) - { - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'getHelper', - 'getQuoteCollection', - 'getFirstDate', - 'joinMailchimpSyncDataWithoutWhere', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getOrderCollection' - )) - ->getMock(); - $this->cartsApiGetHelper($helperMock, $cartsApiMock); - $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock); - $this->cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); - $this->cartsApiUpdateSyncData($cartsApiMock); - $this->cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock); - return $cartsApiMock; - } - /** - * @param $stringCustomerEmailMainTable - * @param $addFieldToFilterOrderCollection - * @param $stringUpdated - * @param $addFieldToFilterUpdated - * @return mixed - */ - protected function orderCollectionMockEmptyQuotes($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated) - { - $orderCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) - ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter')) - ->getMock(); - $this->orderCollectionAddFieldToFilterExactlyTwo($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated, $orderCollectionMock); - return $orderCollectionMock; + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } - /** - * @param $allVisbleItems - * @return mixed - */ - protected function cartModelMockEmptyQuote($allVisbleItems) + public function testGetNewQuotesGuestCustomer() { - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getAllVisibleItems')) - ->getMock(); - $this->cartModelGetEntityId($cartModelMock); - $this->cartModelGetAllVisibleItems($allVisbleItems, $cartModelMock); - return $cartModelMock; - } + $existFirstDate = '2018-11-30'; + $customerId = ''; + $customerEmailAddress = 'test@ebizmarts.com'; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); - /** - * @param $helperMock - * @param $newCartsCollectionMock - * @param $existFirstDate - * @param $token - * @param $customerModelMock - * @param $quoteByEmailResoureceCollectionMock - * @param $cartModelMock - * @param $allCarts - * @param $cartJson - * @param $orderCollectionMock - * @return mixed - */ - protected function cartsApiMockNewQuote($helperMock, $newCartsCollectionMock, $existFirstDate, $token, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $allCarts, $cartJson, $orderCollectionMock) - { $cartsApiMock = $this->cartsApiMock->setMethods( array( 'getHelper', @@ -691,762 +1169,290 @@ protected function cartsApiMockNewQuote($helperMock, $newCartsCollectionMock, $e '_updateSyncData', 'getCustomerModel', 'getWebSiteIdFromMagentoStoreId', - '_getAllCartsByEmail', - 'getCounter', 'getBatchId', - 'setCounter', - 'addProductNotSentData', - '_makeCart', - 'setToken', - 'getToken', 'getOrderCollection' )) ->getMock(); - $this->cartsApiGetHelper($helperMock, $cartsApiMock); - $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock); - $this->cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); - $this->cartsApiUpdateSyncDataExactlyTwo($token, $cartsApiMock); - $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); - $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); - $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); - $this->cartsApiGetCounterExactlyFour($cartsApiMock); - $this->cartsApiGetBatchId($cartsApiMock); - $this->cartsApiSetCounterExactlyTwo($cartsApiMock); - $this->cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock); - $this->cartsApiMakeCartExactlyOne($cartModelMock, $cartJson, $cartsApiMock); - $this->cartsApiGetToken($token, $cartsApiMock); - $this->cartsApiSetToken($cartsApiMock); - $this->cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock); - return $cartsApiMock; - } - - /** - * @param $arrayTableName - * @param $conditionSelect - * @param $m4m - * @param $where - * @return mixed - */ - protected function varienSelectMockModifiedCart($arrayTableName, $conditionSelect, $m4m, $where) - { - $varienSelectMock = $this - ->getMockBuilder(Varien_Db_Select::class) - ->disableOriginalConstructor() - ->setMethods(array('joinLeft', 'where', 'limit')) - ->getMock(); - $this->varienSelectJoinLeft($arrayTableName, $conditionSelect, $m4m, $varienSelectMock); - $this->varienSelectWhere($where, $varienSelectMock); - $this->varienSelectLimit($varienSelectMock); - return $varienSelectMock; - } - - /** - * @param $customerId - * @return mixed - */ - protected function cartModelMockModifiedCart($customerId) - { - $cartModelMock = $this - ->getMockBuilder(Mage_Sales_Model_Quote::class) - ->disableOriginalConstructor() - ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId')) + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) ->getMock(); - $this->cartModelGetCustomerEmailExactlyThree($cartModelMock); - $this->cartModelGetEntityId($cartModelMock); - $this->cartModelGetCustomerId($customerId, $cartModelMock); - return $cartModelMock; - } - - /** - * @param $arrayAddFieldToFilter - * @param $arrayAddFieldToFilterStoreId - * @param $varienSelectMock - * @param $cartModelMock - * @return mixed - */ - protected function newCartsCollectionMockModifiedQuotes($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock) - { $newCartsCollectionMock = $this ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) ->disableOriginalConstructor() ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) ->getMock(); - $this->newCartsCollectionAddFieldToFilterExactlyTwo($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $newCartsCollectionMock); - $this->newCartsCollectionGetSelectExactlyThree($varienSelectMock, $newCartsCollectionMock); - $this->newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock); - return $newCartsCollectionMock; - } - - /** - * @param $mcTableName - * @param $newCartsCollectionMock - * @param $customerModelMock - * @param $quoteByEmailResoureceCollectionMock - * @param $cartModelMock - * @param $cartJson - * @param $allCarts - * @return mixed - */ - protected function modifiedQuotesEmptyJson($mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts) - { - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'setToken', - 'getBatchId', - 'getMailchimpEcommerceDataTableName', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getQuoteCollection', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - 'setCounter', - 'getCounter', - '_makeCart', - '_getAllCartsByEmail', - 'addProductNotSentData' - )) - ->getMock(); - $this->cartsApiSetToken($cartsApiMock); - $this->cartsApiGetBatchId($cartsApiMock); - $this->cartsApiGetMailchimpEcommerceDataTableName($mcTableName, $cartsApiMock); - $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); - $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); - $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); - $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); - $this->cartsApiGetCounterExactlyTwo($cartsApiMock); - $this->cartsApiUpdateSyncDataExactlyTwice($cartsApiMock); - $this->cartsApiSetCounter($cartsApiMock); - $this->cartsApiMakeCart($cartModelMock, $cartJson, $cartsApiMock); - $this->cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock); - return $cartsApiMock; - } - - /** - * @param $where - * @param $arrayTableName - * @param $conditionSelect - * @param $m4m - * @return mixed - */ - protected function modifiedQuotesGuestCustomer($where, $arrayTableName, $conditionSelect, $m4m) - { $varienSelectMock = $this ->getMockBuilder(Varien_Db_Select::class) ->disableOriginalConstructor() - ->setMethods(array('where', 'limit', 'joinLeft')) + ->setMethods(array('where', 'limit')) ->getMock(); - $this->varienSelectWhere($where, $varienSelectMock); - $this->varienSelectLimit($varienSelectMock); - $this->varienSelectJoinLeft($arrayTableName, $conditionSelect, $m4m, $varienSelectMock); - return $varienSelectMock; - } - - /** - * @param $arrayAddFieldToFilter - * @param $stringStoreId - * @param $arrayAddFieldToFilterStoreId - * @param $varienSelectMock - * @param $cartModelMock - * @return mixed - */ - protected function newCartsModifiedQuotesGuestCustomer($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $varienSelectMock, $cartModelMock) - { - $newCartsCollectionMock = $this - ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) ->disableOriginalConstructor() - ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) - ->getMock(); - $this->newCartsAddFieldToFilterExactlyTwo($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $newCartsCollectionMock); - $this->newCartsCollectionGetSelectExactlyThree($varienSelectMock, $newCartsCollectionMock); - $this->newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock); - return $newCartsCollectionMock; - } - - /** - * @param $mcTableName - * @param $newCartsCollectionMock - * @param $customerModelMock - * @return mixed - */ - protected function cartsApiMockModifiedQuotesGuestCustomer($mcTableName, $newCartsCollectionMock, $customerModelMock) - { - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'getQuoteCollection', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - 'getBatchId', - 'getOrderCollection', - 'getMailchimpEcommerceDataTableName' - )) + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) ->getMock(); - $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); - $this->cartsApiUpdateSyncData($cartsApiMock); - $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); - $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); - $this->cartsApiGetBatchId($cartsApiMock); - $this->cartsApiGetMailchimpEcommerceDataTableName($mcTableName, $cartsApiMock); - return $cartsApiMock; - } - - /** - * @param $token - * @param $mcTableName - * @param $newCartsCollectionMock - * @param $customerModelMock - * @param $quoteByEmailResoureceCollectionMock - * @param $cartModelMock - * @param $cartJson - * @param $allCarts - * @return mixed - */ - protected function cartsApiMockModifiedQuotes($token, $mcTableName, $newCartsCollectionMock, $customerModelMock, $quoteByEmailResoureceCollectionMock, $cartModelMock, $cartJson, $allCarts) - { - $cartsApiMock = $this->cartsApiMock->setMethods( - array( - 'setToken', - 'getToken', - 'getBatchId', - 'getMailchimpEcommerceDataTableName', - 'getBatchLimitFromConfig', - '_updateSyncData', - 'getQuoteCollection', - 'getCustomerModel', - 'getWebSiteIdFromMagentoStoreId', - 'setCounter', - 'getCounter', - '_makeCart', - '_getAllCartsByEmail', - 'addProductNotSentData' - )) + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->getMock(); + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) ->getMock(); - $this->cartsApiGetToken($token, $cartsApiMock); - $this->cartsApiSetToken($cartsApiMock); - $this->cartsApiGetBatchId($cartsApiMock); - $this->cartsApiGetMailchimpEcommerceDataTableName($mcTableName, $cartsApiMock); - $this->cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock); - $this->cartsApiGetBatchLimitFromConfig($cartsApiMock); - $this->cartsApiGetCustomerModel($customerModelMock, $cartsApiMock); - $this->cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock); - $this->cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock); - $this->cartsApiGetCounterExactlyFour($cartsApiMock); - $this->cartsApiUpdateSyncDataExactlyTwo($token, $cartsApiMock); - $this->cartsApiSetCounterExactlyTwo($cartsApiMock); - $this->cartsApiMakeCart($cartModelMock, $cartJson, $cartsApiMock); - $this->cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock); - return $cartsApiMock; - } - - /** - * @param $token - * @param $cartsApiMock - */ - protected function cartsApiGetToken($token, $cartsApiMock) - { - $cartsApiMock->expects($this->once()) - ->method('getToken') - ->willReturn($token); - } - - /** - * @param $cartModelMock - * @param $allCarts - * @param $cartsApiMock - */ - protected function cartsApiAddProductNotSentData($cartModelMock, $allCarts, $cartsApiMock) - { - $cartsApiMock->expects($this->once()) - ->method('addProductNotSentData') - ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) - ->willReturn($allCarts); - } - - /** - * @param $cartModelMock - * @param $cartJson - * @param $cartsApiMock - */ - protected function cartsApiMakeCart($cartModelMock, $cartJson, $cartsApiMock) - { - $cartsApiMock->expects($this->once()) - ->method('_makeCart') - ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, true) - ->willReturn($cartJson); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiSetCounterExactlyTwo($cartsApiMock) - { - $cartsApiMock->expects($this->exactly(2)) - ->method('setCounter') - ->withConsecutive( - array(self::COUNTER + 1), - array(self::COUNTER + 1) - ); - } - - /** - * @param $token - * @param $cartsApiMock - */ - protected function cartsApiUpdateSyncDataExactlyTwo($token, $cartsApiMock) - { - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, null, $token) - ); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiGetCounterExactlyFour($cartsApiMock) - { - $cartsApiMock->expects($this->exactly(4)) - ->method('getCounter') - ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER, - self::COUNTER, - self::COUNTER - ); - } - - /** - * @param $quoteByEmailResoureceCollectionMock - * @param $cartsApiMock - */ - protected function cartsApiGetAllCartsByEmail($quoteByEmailResoureceCollectionMock, $cartsApiMock) - { - $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) - ->willReturn($quoteByEmailResoureceCollectionMock); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiGetWebSiteFromMagentoStoreId($cartsApiMock) - { - $cartsApiMock->expects($this->once()) - ->method('getWebSiteIdFromMagentoStoreId') - ->with(self::MAGENTO_STORE_ID) - ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); - } - - /** - * @param $customerModelMock - * @param $cartsApiMock - */ - protected function cartsApiGetCustomerModel($customerModelMock, $cartsApiMock) - { - $cartsApiMock->expects($this->once()) - ->method('getCustomerModel') - ->willReturn($customerModelMock); - } - /** - * @param $cartsApiMock - */ - protected function cartsApiGetBatchLimitFromConfig($cartsApiMock) - { $cartsApiMock->expects($this->once()) - ->method('getBatchLimitFromConfig') - ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); - } - - /** - * @param $newCartsCollectionMock - * @param $cartsApiMock - */ - protected function cartsApiGetQuoteCollection($newCartsCollectionMock, $cartsApiMock) - { + ->method('getHelper') + ->willReturn($helperMock); $cartsApiMock->expects($this->once()) ->method('getQuoteCollection') ->willReturn($newCartsCollectionMock); - } - - /** - * @param $mcTableName - * @param $cartsApiMock - */ - protected function cartsApiGetMailchimpEcommerceDataTableName($mcTableName, $cartsApiMock) - { - $cartsApiMock->expects($this->once()) - ->method('getMailchimpEcommerceDataTableName') - ->willReturn($mcTableName); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiGetBatchId($cartsApiMock) - { + $cartsApiMock->expects($this->exactly(2)) + ->method('getFirstDate') + ->willReturnOnConsecutiveCalls( + $existFirstDate, + $existFirstDate + ); $cartsApiMock->expects($this->once()) - ->method('getBatchId') - ->willReturn(self::BATCH_ID); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiSetToken($cartsApiMock) - { + ->method('joinMailchimpSyncDataWithoutWhere') + ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); $cartsApiMock->expects($this->once()) - ->method('setToken') - ->with(null); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiUpdateSyncData($cartsApiMock) - { + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); $cartsApiMock->expects($this->once()) ->method('_updateSyncData') ->with(self::CART_ID, self::MAILCHIMP_STORE_ID); - } - - /** - * @param $cartModelMock - * @param $newCartsCollectionMock - */ - protected function newCartsCollectionGetIterator($cartModelMock, $newCartsCollectionMock) - { - $newCartsCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartModelMock))); - } - - /** - * @param $varienSelectMock - * @param $newCartsCollectionMock - */ - protected function newCartsCollectionGetSelectExactlyThree($varienSelectMock, $newCartsCollectionMock) - { - $newCartsCollectionMock->expects($this->exactly(3)) + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); + $helperMock->expects($this->once()) + ->method('addResendFilter') + ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); + $newCartsCollectionMock->expects($this->exactly(5)) + ->method('addFieldToFilter') + ->withConsecutive( + array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), + array($stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail), + array($stringItemsCount, $arrayAddFieldToFilterItemsCount), + array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), + array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) + ); + $newCartsCollectionMock->expects($this->exactly(2)) ->method('getSelect') ->willReturnOnConsecutiveCalls( - $varienSelectMock, $varienSelectMock, $varienSelectMock ); - } - - /** - * @param $arrayAddFieldToFilter - * @param $stringStoreId - * @param $arrayAddFieldToFilterStoreId - * @param $newCartsCollectionMock - */ - protected function newCartsAddFieldToFilterExactlyTwo($arrayAddFieldToFilter, $stringStoreId, $arrayAddFieldToFilterStoreId, $newCartsCollectionMock) - { - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array($stringStoreId, $arrayAddFieldToFilterStoreId) - ); - } - - /** - * @param $where - * @param $varienSelectMock - */ - protected function varienSelectWhere($where, $varienSelectMock) - { + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); $varienSelectMock->expects($this->once()) ->method('where') ->with($where); - } - - /** - * @param $varienSelectMock - */ - protected function varienSelectLimit($varienSelectMock) - { $varienSelectMock->expects($this->once()) ->method('limit') ->with(self::BATCH_LIMIT_FROM_CONFIG); - } - - /** - * @param $arrayTableName - * @param $conditionSelect - * @param $m4m - * @param $varienSelectMock - */ - protected function varienSelectJoinLeft($arrayTableName, $conditionSelect, $m4m, $varienSelectMock) - { - $varienSelectMock->expects($this->once()) - ->method('joinLeft') - ->with($arrayTableName, $conditionSelect, $m4m); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiGetCounterExactlyTwo($cartsApiMock) - { - $cartsApiMock->expects($this->exactly(2)) - ->method('getCounter') + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->exactly(2)) + ->method('getEmail') ->willReturnOnConsecutiveCalls( - self::COUNTER, - self::COUNTER - ); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiSetCounter($cartsApiMock) - { - $cartsApiMock->expects($this->once()) - ->method('setCounter') - ->with(self::COUNTER + 1); - } - - /** - * @param $cartsApiMock - */ - protected function cartsApiUpdateSyncDataExactlyTwice($cartsApiMock) - { - $cartsApiMock->expects($this->exactly(2)) - ->method('_updateSyncData') - ->withConsecutive( - array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), - array(self::CART_ID, self::MAILCHIMP_STORE_ID) - ); - } - - /** - * @param $arrayAddFieldToFilter - * @param $arrayAddFieldToFilterStoreId - * @param $newCartsCollectionMock - */ - protected function newCartsCollectionAddFieldToFilterExactlyTwo($arrayAddFieldToFilter, $arrayAddFieldToFilterStoreId, $newCartsCollectionMock) - { - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array(self::STRING_IS_ACTIVE, $arrayAddFieldToFilter), - array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId) + $customerEmailAddress, + $customerEmailAddress ); - } - - /** - * @param $cartModelMock - */ - protected function cartModelGetCustomerEmailExactlyThree($cartModelMock) - { - $cartModelMock->expects($this->exactly(3)) + $cartModelMock->expects($this->exactly(4)) ->method('getCustomerEmail') ->willReturnOnConsecutiveCalls( + self::CUSTOMER_EMAIL_BY_CART, self::CUSTOMER_EMAIL_BY_CART, self::CUSTOMER_EMAIL_BY_CART, self::CUSTOMER_EMAIL_BY_CART ); - } - - /** - * @param $cartModelMock - */ - protected function cartModelGetEntityId($cartModelMock) - { $cartModelMock->expects($this->once()) ->method('getEntityId') ->willReturn(self::CART_ID); - } - - /** - * @param $customerId - * @param $cartModelMock - */ - protected function cartModelGetCustomerId($customerId, $cartModelMock) - { $cartModelMock->expects($this->once()) ->method('getCustomerId') ->willReturn($customerId); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); + + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } - /** - * @param $orderCollectionMock - * @param $cartsApiMock - */ - protected function cartApiGetOrderCollection($orderCollectionMock, $cartsApiMock) - { + public function testGetNewQuotesEmptyJson() + { + $existFirstDate = '2018-11-30'; + $customerId = 1; + $customerEmailAddress = ''; + $allCarts = array(array('method' => 'DELETE', 'path' => '/ecommerce/stores/'.self::MAILCHIMP_STORE_ID.'/carts/'. self::ALREADY_SENT_CART_ID, 'operation_id' => self::BATCH_ID . '_' . self::ALREADY_SENT_CART_ID, 'body' => '')); + $cartJson = ''; + $stringCustomerEmail = 'customer_email'; + $stringItemsCount = 'items_count'; + $stringUpdatedAt = 'updated_at'; + $arrayAddFieldToFilterUpdatedAt = array('gt' => $existFirstDate); + $arrayAddFieldToFilterItemsCount = array('gt' => 0); + $arrayAddFieldToFilterCustomerEmail = array('notnull' => true); + $arrayAddFieldToFilter = array('eq' => 1); + $arrayAddFieldToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $where = "m4m.mailchimp_sync_delta IS NULL"; + $allVisbleItems = array('item'); + $sizeOrderCollection = 0; + $addFieldToFilterOrderCollection = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $stringCustomerEmailMainTable = 'main_table.customer_email'; + $stringUpdated = 'main_table.updated_at'; + $addFieldToFilterUpdated = array('from' => ''); + + $cartsApiMock = $this->cartsApiMock->setMethods( + array( + 'getHelper', + 'getQuoteCollection', + 'getFirstDate', + 'joinMailchimpSyncDataWithoutWhere', + 'getBatchLimitFromConfig', + '_updateSyncData', + 'getCustomerModel', + 'getWebSiteIdFromMagentoStoreId', + '_getAllCartsByEmail', + 'getCounter', + 'getBatchId', + 'setCounter', + 'addProductNotSentData', + '_makeCart', + 'setToken', + 'getOrderCollection' + )) + ->getMock(); + $helperMock = $this + ->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('addResendFilter')) + ->getMock(); + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect', 'getIterator')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'limit')) + ->getMock(); + $customerModelMock = $this + ->getMockBuilder(Mage_Customer_Model_Customer::class) + ->disableOriginalConstructor() + ->setMethods(array('setWebsiteId', 'loadByEmail', 'getEmail')) + ->getMock(); + $quoteByEmailResoureceCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('clear', 'getIterator')) + ->getMock(); + $cartModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId', 'getCustomerEmail', 'getCustomerId', 'getAllVisibleItems')) + ->getMock(); + $cartByEmailModelMock = $this + ->getMockBuilder(Mage_Sales_Model_Quote::class) + ->disableOriginalConstructor() + ->setMethods(array('getEntityId')) + ->getMock(); + $orderCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Order_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('getSize', 'addFieldToFilter')) + ->getMock(); + $cartsApiMock->expects($this->once()) - ->method('getOrderCollection') - ->willReturn($orderCollectionMock); - } - - /** - * @param $existFirstDate - * @param $cartsApiMock - */ - protected function cartsApiGetFirstDateExactlyTwo($existFirstDate, $cartsApiMock) - { + ->method('getHelper') + ->willReturn($helperMock); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); $cartsApiMock->expects($this->exactly(2)) ->method('getFirstDate') ->willReturnOnConsecutiveCalls( $existFirstDate, $existFirstDate ); - } - - /** - * @param $newCartsCollectionMock - * @param $cartsApiMock - */ - protected function cartsApiJoinMailchimpSyncDataWithoutWhere($newCartsCollectionMock, $cartsApiMock) - { $cartsApiMock->expects($this->once()) ->method('joinMailchimpSyncDataWithoutWhere') ->with($newCartsCollectionMock, self::MAILCHIMP_STORE_ID); - } - - /** - * @param $cartModelMock - * @param $cartJson - * @param $cartsApiMock - */ - protected function cartsApiMakeCartExactlyOne($cartModelMock, $cartJson, $cartsApiMock) - { + $cartsApiMock->expects($this->once()) + ->method('getBatchLimitFromConfig') + ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); + $cartsApiMock->expects($this->exactly(2)) + ->method('_updateSyncData') + ->withConsecutive( + array(self::ALREADY_SENT_CART_ID, self::MAILCHIMP_STORE_ID, null, null, null, null, 1), + array(self::CART_ID, self::MAILCHIMP_STORE_ID) + ); + $cartsApiMock->expects($this->once()) + ->method('getCustomerModel') + ->willReturn($customerModelMock); + $cartsApiMock->expects($this->once()) + ->method('getWebSiteIdFromMagentoStoreId') + ->with(self::MAGENTO_STORE_ID) + ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->expects($this->once()) + ->method('_getAllCartsByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) + ->willReturn($quoteByEmailResoureceCollectionMock); + $cartsApiMock->expects($this->exactly(2)) + ->method('getCounter') + ->willReturnOnConsecutiveCalls( + self::COUNTER, + self::COUNTER + ); + $cartsApiMock->expects($this->once()) + ->method('getBatchId') + ->willReturn(self::BATCH_ID); + $cartsApiMock->expects($this->once()) + ->method('setCounter') + ->with(self::COUNTER + 1); + $cartsApiMock->expects($this->once()) + ->method('addProductNotSentData') + ->with(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID, $cartModelMock, $allCarts) + ->willReturn($allCarts); $cartsApiMock->expects($this->once()) ->method('_makeCart') ->with($cartModelMock, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($cartJson); - } - - /** - * @param $helperMock - * @param $cartsApiMock - */ - protected function cartsApiGetHelper($helperMock, $cartsApiMock) - { $cartsApiMock->expects($this->once()) - ->method('getHelper') - ->willReturn($helperMock); - } - - /** - * @param $allVisbleItems - * @param $cartModelMock - */ - protected function cartModelGetAllVisibleItems($allVisbleItems, $cartModelMock) - { - $cartModelMock->expects($this->once()) - ->method('getAllVisibleItems') - ->willReturn($allVisbleItems); - } - - /** - * @param $stringCustomerEmailMainTable - * @param $addFieldToFilterOrderCollection - * @param $stringUpdated - * @param $addFieldToFilterUpdated - * @param $orderCollectionMock - */ - protected function orderCollectionAddFieldToFilterExactlyTwo($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection, $stringUpdated, $addFieldToFilterUpdated, $orderCollectionMock) - { - $orderCollectionMock->expects($this->exactly(2)) - ->method('addFieldToFilter') - ->withConsecutive( - array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), - array($stringUpdated, $addFieldToFilterUpdated) - ); - } - - /** - * @param $sizeOrderCollection - * @param $orderCollectionMock - */ - protected function orderCollectionGetSize($sizeOrderCollection, $orderCollectionMock) - { - $orderCollectionMock->expects($this->once()) - ->method('getSize') - ->willReturn($sizeOrderCollection); - } - - /** - * @param $cartByEmailModelMock - * @param $quoteByEmailResoureceCollectionMock - */ - protected function quoteByEmailGetIterator($cartByEmailModelMock, $quoteByEmailResoureceCollectionMock) - { - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('getIterator') - ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); - } - - /** - * @param $quoteByEmailResoureceCollectionMock - */ - protected function quoteByEmailClear($quoteByEmailResoureceCollectionMock) - { - $quoteByEmailResoureceCollectionMock->expects($this->once()) - ->method('clear'); - } - - /** - * @param $cartByEmailModelMock - */ - protected function cartByEmailGetEntityId($cartByEmailModelMock) - { - $cartByEmailModelMock->expects($this->once()) - ->method('getEntityId') - ->willReturn(self::ALREADY_SENT_CART_ID); - } - - /** - * @param $newCartsCollectionMock - * @param $helperMock - */ - protected function helperAddFieldToFilter($newCartsCollectionMock, $helperMock) - { + ->method('setToken') + ->with(null); + $cartsApiMock->expects($this->once()) + ->method('getOrderCollection') + ->willReturn($orderCollectionMock); $helperMock->expects($this->once()) ->method('addResendFilter') ->with($newCartsCollectionMock, self::MAGENTO_STORE_ID, Ebizmarts_MailChimp_Model_Config::IS_QUOTE); - } - - /** - * @param $varienSelectMock - * @param $newCartsCollectionMock - */ - protected function newCartsCollectionGetSelectExactlyTwo($varienSelectMock, $newCartsCollectionMock) - { - $newCartsCollectionMock->expects($this->exactly(2)) - ->method('getSelect') - ->willReturnOnConsecutiveCalls( - $varienSelectMock, - $varienSelectMock - ); - } - - /** - * @param $arrayAddFieldToFilter - * @param $stringCustomerEmail - * @param $arrayAddFieldToFilterCustomerEmail - * @param $stringItemsCount - * @param $arrayAddFieldToFilterItemsCount - * @param $arrayAddFieldToFilterStoreId - * @param $stringUpdatedAt - * @param $arrayAddFieldToFilterUpdatedAt - * @param $newCartsCollectionMock - */ - protected function newCartsCollectionAddFieldToFilterExactlyFive($arrayAddFieldToFilter, $stringCustomerEmail, $arrayAddFieldToFilterCustomerEmail, $stringItemsCount, $arrayAddFieldToFilterItemsCount, $arrayAddFieldToFilterStoreId, $stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt, $newCartsCollectionMock) - { $newCartsCollectionMock->expects($this->exactly(5)) ->method('addFieldToFilter') ->withConsecutive( @@ -1456,13 +1462,35 @@ protected function newCartsCollectionAddFieldToFilterExactlyFive($arrayAddFieldT array(self::STRING_STORE_ID, $arrayAddFieldToFilterStoreId), array($stringUpdatedAt, $arrayAddFieldToFilterUpdatedAt) ); - } - - /** - * @param $cartModelMock - */ - protected function cartModelGetCustomerEmailExactlyFour($cartModelMock) - { + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + $newCartsCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartModelMock))); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + $varienSelectMock->expects($this->once()) + ->method('limit') + ->with(self::BATCH_LIMIT_FROM_CONFIG); + $customerModelMock->expects($this->once()) + ->method('setWebsiteId') + ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $customerModelMock->expects($this->once()) + ->method('loadByEmail') + ->with(self::CUSTOMER_EMAIL_BY_CART); + $customerModelMock->expects($this->once()) + ->method('getEmail') + ->willReturn($customerEmailAddress); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('clear'); + $quoteByEmailResoureceCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn(new ArrayIterator(array($cartByEmailModelMock))); $cartModelMock->expects($this->exactly(4)) ->method('getCustomerEmail') ->willReturnOnConsecutiveCalls( @@ -1471,36 +1499,28 @@ protected function cartModelGetCustomerEmailExactlyFour($cartModelMock) self::CUSTOMER_EMAIL_BY_CART, self::CUSTOMER_EMAIL_BY_CART ); - } - - /** - * @param $customerEmailAddress - * @param $customerModelMock - */ - protected function customerModelGetEmail($customerEmailAddress, $customerModelMock) - { - $customerModelMock->expects($this->once()) - ->method('getEmail') - ->willReturn($customerEmailAddress); - } - - /** - * @param $customerModelMock - */ - protected function customerModelLoadByEmail($customerModelMock) - { - $customerModelMock->expects($this->once()) - ->method('loadByEmail') - ->with(self::CUSTOMER_EMAIL_BY_CART); - } + $cartModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::CART_ID); + $cartModelMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + $cartModelMock->expects($this->once()) + ->method('getAllVisibleItems') + ->willReturn($allVisbleItems); + $cartByEmailModelMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(self::ALREADY_SENT_CART_ID); + $orderCollectionMock->expects($this->once()) + ->method('getSize') + ->willReturn($sizeOrderCollection); + $orderCollectionMock->expects($this->exactly(2)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringCustomerEmailMainTable, $addFieldToFilterOrderCollection), + array($stringUpdated, $addFieldToFilterUpdated) + ); - /** - * @param $customerModelMock - */ - protected function customerModelSetWebsiteId($customerModelMock) - { - $customerModelMock->expects($this->once()) - ->method('setWebsiteId') - ->with(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); + $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } } From 8f2736223877dcf6a9d66550d9eeed4f2ebe0fbc Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 11 Jan 2019 16:58:42 -0300 Subject: [PATCH 076/113] coverage 100 percent of the test testGetAllCartsByEmail --- .../MailChimp/Model/Api/CartsTest.php | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 0c7c25e7f..818cac39d 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -59,6 +59,22 @@ public function testCreateBatchJson() $cartsApiMock->createBatchJson(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } + public function testCreateBatchJsonisAbandonedCartDisabled() + { + $cartsApiMock = $this->cartsApiMock + ->setMethods(array('getHelper')) + ->getMock(); + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('isAbandonedCartEnabled')) + ->getMock(); + + $cartsApiMock->expects($this->once())->method('getHelper')->willReturn($helperMock); + $helperMock->expects($this->once())->method('isAbandonedCartEnabled')->with(self::MAGENTO_STORE_ID)->willReturn(false); + + $cartsApiMock->createBatchJson(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } + public function testGetConvertedQuotes () { $mailchimpTableName = 'mailchimp_ecommerce_sync_data'; @@ -1523,4 +1539,64 @@ public function testGetNewQuotesEmptyJson() $cartsApiMock->_getNewQuotes(self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } + + public function testGetAllCartsByEmail() + { + $mailchimpTableName = 'm4m'; + $stringIsActive = 'is_active'; + $arrayAddToFilterIsActive = array('eq' => 1); + $stringStoreId = 'store_id'; + $arrayAddToFilterStoreId = array('eq' => self::MAGENTO_STORE_ID); + $stringCustomerId = 'customer_email'; + $arrayAddToFilterCustomerId = array('eq' => self::CUSTOMER_EMAIL_BY_CART); + $arrayMailchimpTableName = array('m4m' => $mailchimpTableName); + $condition = "m4m.related_id = main_table.entity_id and m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . "' + AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + $m4m = array('m4m.*'); + $where = "m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_store_id = '" . self::MAILCHIMP_STORE_ID . "'"; + + $cartsApiMock = $this->cartsApiMock + ->setMethods(array('getMailchimpEcommerceDataTableName', 'getQuoteCollection')) + ->getMock(); + $newCartsCollectionMock = $this + ->getMockBuilder(Mage_Sales_Model_Resource_Quote_Collection::class) + ->disableOriginalConstructor() + ->setMethods(array('addFieldToFilter', 'getSelect')) + ->getMock(); + $varienSelectMock = $this + ->getMockBuilder(Varien_Db_Select::class) + ->disableOriginalConstructor() + ->setMethods(array('where', 'joinLeft')) + ->getMock(); + + $cartsApiMock->expects($this->once()) + ->method('getMailchimpEcommerceDataTableName') + ->willReturn($mailchimpTableName); + $cartsApiMock->expects($this->once()) + ->method('getQuoteCollection') + ->willReturn($newCartsCollectionMock); + + $newCartsCollectionMock->expects($this->exactly(3)) + ->method('addFieldToFilter') + ->withConsecutive( + array($stringIsActive, $arrayAddToFilterIsActive), + array($stringStoreId, $arrayAddToFilterStoreId), + array($stringCustomerId, $arrayAddToFilterCustomerId) + ); + $newCartsCollectionMock->expects($this->exactly(2)) + ->method('getSelect') + ->willReturnOnConsecutiveCalls( + $varienSelectMock, + $varienSelectMock + ); + + $varienSelectMock->expects($this->once()) + ->method('joinLeft') + ->with($arrayMailchimpTableName, $condition, $m4m); + $varienSelectMock->expects($this->once()) + ->method('where') + ->with($where); + + $cartsApiMock->_getAllCartsByEmail(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + } } From 306558d2ab009e20989c7ced74968886ae0dcc39 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Wed, 16 Jan 2019 15:47:11 -0300 Subject: [PATCH 077/113] closes #836#844 fixed an error in API carts --- .../Ebizmarts/MailChimp/Model/Api/Carts.php | 75 ++----------------- 1 file changed, 6 insertions(+), 69 deletions(-) mode change 100644 => 100755 app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php old mode 100644 new mode 100755 index 986c1e667..d6ced38e5 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -1,5 +1,4 @@ isAbandonedCartEnabled($magentoStoreId)) { return $allCarts; } - $this->_firstDate = $helper->getAbandonedCartFirstDate($magentoStoreId); $this->setCounter(0); - $date = $helper->getDateMicrotime(); $this->setBatchId('storeid-' . $magentoStoreId . '_' . Ebizmarts_MailChimp_Model_Config::IS_QUOTE . '_' . $date); $resendTurn = $helper->getResendTurn($magentoStoreId); @@ -50,7 +43,6 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId) $allCarts = array_merge($allCarts, $this->_getNewQuotes($mailchimpStoreId, $magentoStoreId)); return $allCarts; } - /** * @param $mailchimpStoreId * @param $magentoStoreId @@ -91,7 +83,6 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) $this->setCounter($this->getCounter()+1); } } - $allCartsForEmail->clear(); $counter = $this->getCounter(); $allCarts[$counter]['method'] = 'DELETE'; @@ -101,10 +92,8 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) $this->_updateSyncData($cartId, $mailchimpStoreId, null, null, null, null, 1); $this->setCounter($this->getCounter()+1); } - return $allCarts; } - /** * @param $mailchimpStoreId * @param $magentoStoreId @@ -156,10 +145,8 @@ public function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) $this->setCounter($this->getCounter()+1); } } - $allCartsForEmail->clear(); } - // avoid carts abandoned as guests when customer email associated to a registered customer. if (!$cart->getCustomerId() && $customer->getEmail() == $cart->getCustomerEmail()) { $this->_updateSyncData($cartId, $mailchimpStoreId); @@ -167,12 +154,11 @@ public function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) } // send the products that not already sent $allCarts = $this->addProductNotSentData($mailchimpStoreId, $magentoStoreId, $cart, $allCarts); - $cartJson = $this->_makeCart($cart, $mailchimpStoreId, $magentoStoreId, true); if ($cartJson != "") { $counter = $this->getCounter(); $allCarts[$counter]['method'] = 'PATCH'; - $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts'.$cartId; + $allCarts[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/carts/'.$cartId; $allCarts[$counter]['operation_id'] = $batchId . '_' . $cartId; $allCarts[$counter]['body'] = $cartJson; $this->setCounter($this->getCounter()+1); @@ -180,13 +166,10 @@ public function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) } else { $this->_updateSyncData($cartId, $mailchimpStoreId); } - $this->setToken(null); } - return $allCarts; } - /** * @param $mailchimpStoreId * @return array @@ -206,14 +189,12 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) if ($this->_firstDate) { $newCarts->addFieldToFilter('updated_at', array('gt' => $this->_firstDate)); } - //join with mailchimp_ecommerce_sync_data table to filter by sync data. $this->joinMailchimpSyncDataWithoutWhere($newCarts, $mailchimpStoreId); // be sure that the quotes are already in mailchimp and not deleted $newCarts->getSelect()->where("m4m.mailchimp_sync_delta IS NULL"); // limit the collection $newCarts->getSelect()->limit($this->getBatchLimitFromConfig()); - foreach ($newCarts as $cart) { $cartId = $cart->getEntityId(); $orderCollection = Mage::getResourceModel('sales/order_collection'); @@ -224,7 +205,6 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $this->_updateSyncData($cartId, $mailchimpStoreId); continue; } - $customer = $this->getCustomerModel(); $customer->setWebsiteId($this->getWebSiteIdFromMagentoStoreId($magentoStoreId)); $customer->loadByEmail($cart->getCustomerEmail()); @@ -240,19 +220,15 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $this->_updateSyncData($alreadySentCartId, $mailchimpStoreId, null, null, null, null, 1); $this->setCounter($this->getCounter()+1); } - $allCartsForEmail->clear(); } - // don't send the carts for guest customers who are registered if (!$cart->getCustomerId() && $customer->getEmail() == $cart->getCustomerEmail()) { $this->_updateSyncData($cartId, $mailchimpStoreId); continue; } - // send the products that not already sent $allCarts = $this->addProductNotSentData($mailchimpStoreId, $magentoStoreId, $cart, $allCarts); - $cartJson = $this->_makeCart($cart, $mailchimpStoreId, $magentoStoreId); if ($cartJson != "") { $counter = $this->getCounter(); @@ -265,13 +241,10 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) } else { $this->_updateSyncData($cartId, $mailchimpStoreId); } - $this->setToken(null); } - return $allCarts; } - /** * Get all existing carts in the current store view for a given email address. * @@ -280,7 +253,7 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) * @param $magentoStoreId * @return object */ - protected function _getAllCartsByEmail($email, $mailchimpStoreId, $magentoStoreId) + public function _getAllCartsByEmail($email, $mailchimpStoreId, $magentoStoreId) { $mailchimpTableName = $this->getMailchimpEcommerceDataTableName(); $allCartsForEmail = $this->getQuoteCollection(); @@ -297,7 +270,6 @@ protected function _getAllCartsByEmail($email, $mailchimpStoreId, $magentoStoreI $allCartsForEmail->getSelect()->where("m4m.mailchimp_sync_deleted = 0 AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'"); return $allCartsForEmail; } - /** * @param $cart * @param $mailchimpStoreId @@ -319,7 +291,6 @@ protected function _makeCart($cart, $mailchimpStoreId, $magentoStoreId, $isModif if ($campaignId) { $oneCart['campaign_id'] = $campaignId; } - $oneCart['checkout_url'] = $this->_getCheckoutUrl($cart, $isModified); $oneCart['currency_code'] = $cart->getQuoteCurrencyCode(); $oneCart['order_total'] = $cart->getGrandTotal(); @@ -333,22 +304,18 @@ protected function _makeCart($cart, $mailchimpStoreId, $magentoStoreId, $isModif if ($item->getProductType() == 'bundle' || $item->getProductType() == 'grouped') { continue; } - if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { $variant = null; if ($item->getOptionByCode('simple_product')) { $variant = $item->getOptionByCode('simple_product')->getProduct(); } - if (!$variant) { continue; } - $variantId = $variant->getId(); } else { $variantId = $item->getProductId(); } - //id can not be 0 so we add 1 to $itemCount before setting the id. $itemCount++; $line['id'] = (string)$itemCount; @@ -358,7 +325,6 @@ protected function _makeCart($cart, $mailchimpStoreId, $magentoStoreId, $isModif $line['price'] = $item->getRowTotal(); $lines[] = $line; } - $jsonData = ""; if ($itemCount) { $oneCart['lines'] = $lines; @@ -370,10 +336,8 @@ protected function _makeCart($cart, $mailchimpStoreId, $magentoStoreId, $isModif $helper->logError("Carts " . $cart->getId() . " json encode failed"); } } - return $jsonData; } - /** * Get URL for the cart. * @@ -392,7 +356,6 @@ protected function _getCheckoutUrl($cart, $isModified) $this->setToken($token); return $url; } - /** * @return int */ @@ -401,7 +364,6 @@ protected function getBatchLimitFromConfig() $helper = $this->getHelper(); return $helper->getCustomerAmountLimit(); } - /** * Get Customer data for the cart. * @@ -420,7 +382,6 @@ protected function _getCustomer($cart, $mailchimpStoreId, $magentoStoreId) $helper->logError($e->getMessage()); return $customer; } - if ($cart->getCustomerId()) { try { $customer = $api->ecommerce->customers->get($mailchimpStoreId, $cart->getCustomerId(), 'email_address'); @@ -446,7 +407,6 @@ protected function _getCustomer($cart, $mailchimpStoreId, $magentoStoreId) } catch (MailChimp_Error $e) { $helper->logError($e->getFriendlyMessage()); } - if (isset($customers['total_items']) && $customers['total_items'] > 0) { $customer = array( 'id' => $customers['customers'][0]['id'], @@ -462,17 +422,14 @@ protected function _getCustomer($cart, $mailchimpStoreId, $magentoStoreId) ); } } - $firstName = $cart->getCustomerFirstname(); if ($firstName) { $customer["first_name"] = $firstName; } - $lastName = $cart->getCustomerLastname(); if ($lastName) { $customer["last_name"] = $lastName; } - $billingAddress = $cart->getBillingAddress(); if ($billingAddress) { $street = $billingAddress->getStreet(); @@ -480,45 +437,35 @@ protected function _getCustomer($cart, $mailchimpStoreId, $magentoStoreId) if ($street[0]) { $address['address1'] = $street[0]; } - if (count($street) > 1) { $address['address1'] = $street[1]; } - if ($billingAddress->getCity()) { $address['city'] = $billingAddress->getCity(); } - if ($billingAddress->getRegion()) { $address['province'] = $billingAddress->getRegion(); } - if ($billingAddress->getRegionCode()) { $address['province_code'] = $billingAddress->getRegionCode(); } - if ($billingAddress->getPostcode()) { $address['postal_code'] = $billingAddress->getPostcode(); } - if ($billingAddress->getCountry()) { $address['country'] = Mage::getModel('directory/country')->loadByCode($billingAddress->getCountry())->getName(); $address['country_code'] = $billingAddress->getCountry(); } - if (count($address)) { $customer['address'] = $address; } } - //company if ($billingAddress->getCompany()) { $customer["company"] = $billingAddress->getCompany(); } - return $customer; } - /** * update product sync data * @@ -536,7 +483,6 @@ protected function _updateSyncData($cartId, $mailchimpStoreId, $syncDelta = null $helper = $this->getHelper(); $helper->saveEcommerceSyncData($cartId, Ebizmarts_MailChimp_Model_Config::IS_QUOTE, $mailchimpStoreId, $syncDelta, $syncError, $syncModified, $syncDeleted, $token, $syncedFlag); } - /** * @param $mailchimpStoreId * @param $magentoStoreId @@ -553,7 +499,6 @@ public function addProductNotSentData($mailchimpStoreId, $magentoStoreId, $cart, $this->setCounter($productDataArray[1]); return $allCarts; } - /** * @return Ebizmarts_MailChimp_Helper_Data */ @@ -561,7 +506,6 @@ protected function getHelper() { return Mage::helper('mailchimp'); } - /** * @param $newCarts * @param $mailchimpStoreId @@ -576,7 +520,6 @@ public function joinMailchimpSyncDataWithoutWhere($newCarts, $mailchimpStoreId) array('m4m.*') ); } - /** * @return mixed */ @@ -584,7 +527,6 @@ public function getMailchimpEcommerceDataTableName() { return Mage::getSingleton('core/resource')->getTableName('mailchimp/ecommercesyncdata'); } - /** * @return Mage_Sales_Model_Resource_Quote_Collection */ @@ -592,7 +534,6 @@ public function getQuoteCollection() { return Mage::getResourceModel('sales/quote_collection'); } - /** * @return false|Mage_Core_Model_Abstract */ @@ -600,7 +541,6 @@ public function getCustomerModel() { return Mage::getModel("customer/customer"); } - /** * @param $magentoStoreId * @return mixed @@ -609,7 +549,6 @@ public function getWebSiteIdFromMagentoStoreId($magentoStoreId) { return Mage::getModel('core/store')->load($magentoStoreId)->getWebsiteId(); } - /** * @return int */ @@ -617,7 +556,6 @@ public function getCounter() { return $this->_counter; } - /** * @param $counter */ @@ -625,7 +563,6 @@ public function setCounter($counter) { $this->_counter = $counter; } - /** * @return string */ @@ -633,7 +570,6 @@ public function getBatchId() { return $this->_batchId; } - /** * @param $batchId */ @@ -641,7 +577,6 @@ public function setBatchId($batchId) { $this->_batchId = $batchId; } - /** * @return null */ @@ -649,10 +584,12 @@ public function getToken() { return $this->_token; } - + /** + * @param $token + */ public function setToken($token) { - $this->_token = $token; } } + From 6bedbe71999f513fa912f284cd962a6b15f55634 Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 16 Jan 2019 16:12:08 -0300 Subject: [PATCH 078/113] Fix database script version update. --- ...upgrade-1.1.13-1.1.14.php => mysql4-upgrade-1.1.14-1.1.15.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/{mysql4-upgrade-1.1.13-1.1.14.php => mysql4-upgrade-1.1.14-1.1.15.php} (100%) diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.13-1.1.14.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.14-1.1.15.php similarity index 100% rename from app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.13-1.1.14.php rename to app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.14-1.1.15.php From 9122beae7c14ef67a9c9251c6fc115f9db6bad35 Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 16 Jan 2019 16:12:57 -0300 Subject: [PATCH 079/113] Bump version for interest groups scripts. --- app/code/community/Ebizmarts/MailChimp/etc/config.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index 95a0d3360..36728e6a1 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -3,10 +3,10 @@ - 1.1.14 + 1.1.15 - 1.1.14 + 1.1.15 From 8c0e241dd7339ebfb84639cf81936c2f023a8799 Mon Sep 17 00:00:00 2001 From: "Pablo S. Benitez" Date: Thu, 17 Jan 2019 15:55:12 -0300 Subject: [PATCH 080/113] param type --- app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php index d6ced38e5..a60aced31 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -585,7 +585,7 @@ public function getToken() return $this->_token; } /** - * @param $token + * @param string $token */ public function setToken($token) { From 07c4d4382aec2fec8dd7ed3e9159733297ca08bb Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 17 Jan 2019 16:04:35 -0300 Subject: [PATCH 081/113] Add new line. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5dbd6330..eae7000f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -439,3 +439,4 @@ \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* + From 78cc3271fa68ba18287744b9b5c386e8ba60a5e1 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 17 Jan 2019 16:05:05 -0300 Subject: [PATCH 082/113] Add new line. --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eae7000f5..e5dbd6330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -439,4 +439,3 @@ \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* - From 35dfb2ca595af5666d57cfe01981bc1910420afa Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 17 Jan 2019 16:05:54 -0300 Subject: [PATCH 083/113] Add new line. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f17876bc..35f31ac04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -452,4 +452,4 @@ ## [1.0.0](https://github.com/mailchimp/mc-magento/tree/1.0.0) (2016-06-06) -\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* \ No newline at end of file +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* From 8a860d1cfc6966c99943d38989d90af8b37aeaf2 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 17 Jan 2019 18:09:12 -0300 Subject: [PATCH 084/113] Change the original option to use the real path in stead of catched. --- .../Ebizmarts/MailChimp/Helper/Data.php | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 7ea66593a..70028597f 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -12,6 +12,10 @@ */ class Ebizmarts_MailChimp_Helper_Data extends Mage_Core_Helper_Abstract { + const DEFAULT_SIZE = '0'; + const SMALL_SIZE = '1'; + const THUMBNAIL_SIZE = '2'; + const ORIGINAL_SIZE = '3'; /** * All MailChimp available language codes @@ -1313,23 +1317,22 @@ public function getImageUrlById($productId, $magentoStoreId) $productModel = $this->getProductModel(); $configImageSize = $this->getImageSize($magentoStoreId); switch ($configImageSize) { - case 0: + case self::DEFAULT_SIZE: $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_DEFAULT; break; - case 1: + case self::SMALL_SIZE: $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_SMALL; break; - case 2: + case self::THUMBNAIL_SIZE: $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_THUMBNAIL; break; - case 3: + case self::ORIGINAL_SIZE: $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_DEFAULT; break; default: $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_DEFAULT; break; } - $productImage = $productResourceModel->getAttributeRawValue($productId, $imageSize, $magentoStoreId); $productModel->setData($imageSize, $productImage); @@ -1338,14 +1341,15 @@ public function getImageUrlById($productId, $magentoStoreId) } else { $curStore = $this->getCurrentStoreId(); $this->setCurrentStore($magentoStoreId); - if ($configImageSize == 3){ - $imageUrl = $this->getImageUrl($productModel, $imageSize); + if ($configImageSize == self::ORIGINAL_SIZE){ + $imageUrl = $this->getOriginalPath($productImage); } else { $upperCaseImage = $this->getImageFunctionName($imageSize); $imageUrl = $productModel->$upperCaseImage(); } $this->setCurrentStore($curStore); } + return $imageUrl; } @@ -3426,4 +3430,13 @@ protected function getCurrentDateTime() { return Mage::getModel('core/date')->date('d-m-Y H:i:s'); } + + /** + * @param $productImage + * @return string + */ + protected function getOriginalPath($productImage) + { + return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $productImage; + } } From e2405b073caa3b990a30eac4ed3815ce261c415b Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 17 Jan 2019 18:13:00 -0300 Subject: [PATCH 085/113] Add comment --- app/code/community/Ebizmarts/MailChimp/Helper/Data.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 70028597f..3647e89eb 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3432,6 +3432,8 @@ protected function getCurrentDateTime() } /** + * Return original path for the imageURL (not the catched one) + * * @param $productImage * @return string */ From 450671a3b9ef302c463c194b0500963226bb8260 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 18 Jan 2019 12:32:45 -0300 Subject: [PATCH 086/113] Fix tests. --- .../Ebizmarts/MailChimp/Helper/Data.php | 15 +++- .../Ebizmarts/MailChimp/Helper/DataTest.php | 79 +++++++++++++------ .../MailChimp/Model/ObserverTest.php | 12 +-- 3 files changed, 76 insertions(+), 30 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 3647e89eb..6bb5591a6 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1344,8 +1344,7 @@ public function getImageUrlById($productId, $magentoStoreId) if ($configImageSize == self::ORIGINAL_SIZE){ $imageUrl = $this->getOriginalPath($productImage); } else { - $upperCaseImage = $this->getImageFunctionName($imageSize); - $imageUrl = $productModel->$upperCaseImage(); + $imageUrl = $this->getImageUrlForSize($imageSize, $productModel); } $this->setCurrentStore($curStore); } @@ -1375,6 +1374,18 @@ public function getImageFunctionName($imageSize) return $functionName; } + /** + * @param $imageSize + * @param $productModel + * @return string + */ + protected function getImageUrlForSize($imageSize, $productModel) + { + $upperCaseImage = (string)$this->getImageFunctionName($imageSize); + $imageUrl = $productModel->$upperCaseImage(); + return $imageUrl; + } + /** * Returns imageSize separated word by word in array * diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php index cc6a3b735..0a9432cd8 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -378,22 +378,19 @@ public function testCreateWebhookIfRequired() * @dataProvider testGetImageUrlByIdDataProvider */ - public function testGetImageUrlById($data) + public function testGetReSizedImageUrlById($data) { $productId = 1; $magentoStoreId = 1; $defaultStoreId = 0; $imageSize = $data['imageSize']; - $upperCaseImage = $data['method']; - $imageUrl = $data['imageUrl']; + $imageUrl = 'http://magento.com/catalog/product/image.jpg'; $configImageSize = $data['configImageSize']; - $getImageFunctionName = $data['getImageFunctionName']; - $upperCaseImageTimes = $data['methodTimes']; - $localGetImageUrl = $data['localGetImageUrl']; $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('getProductResourceModel', 'getProductModel', 'getImageSize', 'getCurrentStoreId', 'setCurrentStore', 'getImageFunctionName', 'getImageUrl')) + ->setMethods(array('getProductResourceModel', 'getProductModel', 'getImageSize', 'getCurrentStoreId', + 'setCurrentStore', 'getImageUrl', 'getImageUrlForSize')) ->getMock(); $productModelMock = $this->getMockBuilder(Mage_Catalog_Model_Product::class) @@ -413,17 +410,15 @@ public function testGetImageUrlById($data) $helperMock->expects($this->once())->method('getProductResourceModel')->willReturn($productResourceModelMock); $helperMock->expects($this->once())->method('getProductModel')->willReturn($productModelMock); $helperMock->expects($this->once())->method('getImageSize')->with($magentoStoreId)->willReturn($configImageSize); - $helperMock->expects($this->exactly($localGetImageUrl))->method('getImageUrl')->with($productModelMock, $imageSize)->willReturn($imageUrl); $productResourceModelMock->expects($this->once())->method('getAttributeRawValue')->with($productId, $imageSize, $magentoStoreId)->willReturn($imageModelMock); $productModelMock->expects($this->once())->method('setData')->with($imageSize, $imageModelMock); - $productModelMock->expects($this->exactly($upperCaseImageTimes))->method($upperCaseImage)->willReturn($imageUrl); - $helperMock->expects($this->once())->method('getCurrentStoreId')->willReturn($defaultStoreId); + $helperMock->expects($this->once())->method('getCurrentStoreId')->willReturn($defaultStoreId); $helperMock->expects($this->exactly(2))->method('setCurrentStore')->withConsecutive(array($magentoStoreId), array($defaultStoreId)); - $helperMock->expects($this->exactly($getImageFunctionName))->method('getImageFunctionName')->with($imageSize)->willReturn($upperCaseImage); + $helperMock->expects($this->once())->method('getImageUrlForSize')->with($imageSize, $productModelMock)->willReturn($imageUrl); $return = $helperMock->getImageUrlById($productId, $magentoStoreId); @@ -433,21 +428,59 @@ public function testGetImageUrlById($data) public function testGetImageUrlByIdDataProvider() { return array( - array(array('imageSize' => 'image', 'method' => 'getImageUrl', 'configImageSize' => 0, - 'getImageFunctionName' => 1, 'imageUrl' => 'ImageUrl', 'getImageUrl', 'methodTimes' => 1, - 'localGetImageUrl' => 0)), - array(array('imageSize' => 'small_image', 'method' => 'getSmallImageUrl', 'configImageSize' => 1, - 'getImageFunctionName' => 1, 'imageUrl' => 'SmallImageUrl', 'methodTimes' => 1, - 'localGetImageUrl' => 0)), - array(array('imageSize' => 'thumbnail', 'method' => 'getThumbnailUrl', 'configImageSize' => 2, - 'getImageFunctionName' => 1, 'imageUrl' => 'ThumbnailUrl', 'methodTimes' => 1, - 'localGetImageUrl' => 0)), - array(array('imageSize' => 'image', 'method' => 'getImageUrl', 'configImageSize' => 3, - 'getImageFunctionName' => 0, 'imageUrl' => 'ImageUrl', 'methodTimes' => 0, - 'localGetImageUrl' => 1)) + array(array('imageSize' => Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_DEFAULT, 'configImageSize' => Ebizmarts_MailChimp_Helper_Data::DEFAULT_SIZE)), + array(array('imageSize' => Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_SMALL, 'configImageSize' => Ebizmarts_MailChimp_Helper_Data::SMALL_SIZE)), + array(array('imageSize' => Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_THUMBNAIL, 'configImageSize' => Ebizmarts_MailChimp_Helper_Data::THUMBNAIL_SIZE)) ); } + public function testGetOriginalImageUrlById() + { + $productId = 1; + $magentoStoreId = 1; + $defaultStoreId = 0; + $imageSize = Ebizmarts_MailChimp_Model_Config::IMAGE_SIZE_DEFAULT; + $imageUrl = 'http://magento.com/catalog/product/image.jpg'; + $configImageSize = Ebizmarts_MailChimp_Helper_Data::ORIGINAL_SIZE; + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getProductResourceModel', 'getProductModel', 'getImageSize', 'getCurrentStoreId', + 'setCurrentStore', 'getImageUrl', 'getOriginalPath')) + ->getMock(); + + $productModelMock = $this->getMockBuilder(Mage_Catalog_Model_Product::class) + ->disableOriginalConstructor() + ->setMethods(array('setData', 'getImageUrl', 'getThumbnailUrl', 'getSmallImageUrl')) + ->getMock(); + + $productResourceModelMock = $this->getMockBuilder(Mage_Catalog_Model_Resource_Product::class) + ->disableOriginalConstructor() + ->setMethods(array('getAttributeRawValue')) + ->getMock(); + + $imageModelMock = $this->getMockBuilder(Mage_Media_Model_Image::class) + ->disableOriginalConstructor() + ->getMock(); + + $helperMock->expects($this->once())->method('getProductResourceModel')->willReturn($productResourceModelMock); + $helperMock->expects($this->once())->method('getProductModel')->willReturn($productModelMock); + $helperMock->expects($this->once())->method('getImageSize')->with($magentoStoreId)->willReturn($configImageSize); + + $productResourceModelMock->expects($this->once())->method('getAttributeRawValue')->with($productId, $imageSize, $magentoStoreId)->willReturn($imageModelMock); + + $productModelMock->expects($this->once())->method('setData')->with($imageSize, $imageModelMock); + + $helperMock->expects($this->once())->method('getCurrentStoreId')->willReturn($defaultStoreId); + $helperMock->expects($this->exactly(2))->method('setCurrentStore')->withConsecutive(array($magentoStoreId), array($defaultStoreId)); + + $helperMock->expects($this->once())->method('getOriginalPath')->with($imageModelMock)->willReturn($imageUrl); + + $return = $helperMock->getImageUrlById($productId, $magentoStoreId); + + $this->assertEquals($return, $imageUrl); + } + public function testGetImageFunctionName() { $imageSize = 'image_size'; diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 0c6c6d037..60628070e 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -672,12 +672,10 @@ public function testAddColumnToSalesOrderGridCollection() */ public function testSubscriberSaveBefore($data) { - $setImportMode = $data['setImportMode']; $getStoreId = $data['getStoreId']; $magentoMail = $data['magentoMail']; $subscriberUpdatedAmount = $data['subscriberUpdatedAmount']; $storeId = 1; - $sendConfirmationRequestEmail = $data['sendConfirmationRequestEmail']; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) ->disableOriginalConstructor() @@ -747,8 +745,8 @@ public function testSubscriberSaveBefore($data) public function subscriberSaveBeforeDataProvider() { return array( - array(array('magentoMail' => 0, 'setImportMode' => 1, 'subscriberUpdatedAmount' => 1, 'getStoreId' => 1, 'sendConfirmationRequestEmail' => 0)), - array(array('magentoMail' => 1, 'setImportMode' => 3, 'subscriberUpdatedAmount' => 0, 'getStoreId' => 1, 'sendConfirmationRequestEmail' => 1)) + array(array('magentoMail' => 0, 'subscriberUpdatedAmount' => 1, 'getStoreId' => 1)), + array(array('magentoMail' => 1, 'subscriberUpdatedAmount' => 0, 'getStoreId' => 1)) ); } @@ -756,6 +754,7 @@ public function testSubscriberSaveAfter() { $storeId = 1; $params = array(); + $magentoMail = 0; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) ->disableOriginalConstructor() @@ -769,7 +768,7 @@ public function testSubscriberSaveAfter() $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData')) + ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData', 'getConfigValueForScope')) ->getMock(); $eventMock = $this->getMockBuilder(Varien_Event::class) @@ -811,6 +810,9 @@ public function testSubscriberSaveAfter() $helperMock->expects($this->once())->method('saveInterestGroupData')->with($params, $storeId, null, $subscriberMock); $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); + + $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); + $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); $apiSubscriberMock->expects($this->once())->method('updateSubscriber')->with($subscriberMock, true); From 61cc409124ea337b4315b7d89ac5133305cc8b19 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 18 Jan 2019 14:39:07 -0300 Subject: [PATCH 087/113] Fix test. --- .../Ebizmarts/MailChimp/Model/Api/Carts.php | 6 ++--- .../MailChimp/Model/Api/CartsTest.php | 22 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php index a4e8adbba..c92435c4a 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php @@ -79,7 +79,7 @@ public function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) foreach ($convertedCarts as $cart) { $cartId = $cart->getEntityId(); // we need to delete all the carts associated with this email - $allCartsForEmail = $this->_getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); + $allCartsForEmail = $this->getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); foreach ($allCartsForEmail as $cartForEmail) { $alreadySentCartId = $cartForEmail->getEntityId(); $counter = $this->getCounter(); @@ -144,7 +144,7 @@ public function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) $customer->setWebsiteId($this->getWebSiteIdFromMagentoStoreId($magentoStoreId)); $customer->loadByEmail($cart->getCustomerEmail()); if ($customer->getEmail() != $cart->getCustomerEmail()) { - $allCartsForEmail = $this->_getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); + $allCartsForEmail = $this->getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); foreach ($allCartsForEmail as $cartForEmail) { $alreadySentCartId = $cartForEmail->getEntityId(); $counter = $this->getCounter(); @@ -231,7 +231,7 @@ public function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $customer->setWebsiteId($this->getWebSiteIdFromMagentoStoreId($magentoStoreId)); $customer->loadByEmail($cart->getCustomerEmail()); if ($customer->getEmail() != $cart->getCustomerEmail()) { - $allCartsForEmail = $this->_getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); + $allCartsForEmail = $this->getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); foreach ($allCartsForEmail as $cartForEmail) { $counter = $this->getCounter(); $alreadySentCartId = $cartForEmail->getEntityId(); diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php index 818cac39d..175219480 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/CartsTest.php @@ -90,7 +90,7 @@ public function testGetConvertedQuotes () 'getMailchimpEcommerceDataTableName', 'getQuoteCollection', 'getBatchLimitFromConfig', - '_getAllCartsByEmail', + 'getAllCartsByEmail', 'getCounter', 'getBatchId', '_updateSyncData', @@ -133,7 +133,7 @@ public function testGetConvertedQuotes () ->method('getBatchLimitFromConfig') ->willReturn(self::BATCH_LIMIT_FROM_CONFIG); $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') + ->method('getAllCartsByEmail') ->with(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(4)) @@ -233,7 +233,7 @@ public function testGetModifiedQuotes() 'setCounter', 'getCounter', '_makeCart', - '_getAllCartsByEmail', + 'getAllCartsByEmail', 'addProductNotSentData' )) ->getMock(); @@ -294,7 +294,7 @@ public function testGetModifiedQuotes() ->with(self::MAGENTO_STORE_ID) ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') + ->method('getAllCartsByEmail') ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(4)) @@ -537,7 +537,7 @@ public function testGetModifiedQuotesEmptyJson() 'setCounter', 'getCounter', '_makeCart', - '_getAllCartsByEmail', + 'getAllCartsByEmail', 'addProductNotSentData' )) ->getMock(); @@ -595,7 +595,7 @@ public function testGetModifiedQuotesEmptyJson() ->with(self::MAGENTO_STORE_ID) ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') + ->method('getAllCartsByEmail') ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(2)) @@ -714,7 +714,7 @@ public function testGetNewQuotesNewQuote() '_updateSyncData', 'getCustomerModel', 'getWebSiteIdFromMagentoStoreId', - '_getAllCartsByEmail', + 'getAllCartsByEmail', 'getCounter', 'getBatchId', 'setCounter', @@ -797,7 +797,7 @@ public function testGetNewQuotesNewQuote() ->with(self::MAGENTO_STORE_ID) ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') + ->method('getAllCartsByEmail') ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(4)) @@ -1355,7 +1355,7 @@ public function testGetNewQuotesEmptyJson() '_updateSyncData', 'getCustomerModel', 'getWebSiteIdFromMagentoStoreId', - '_getAllCartsByEmail', + 'getAllCartsByEmail', 'getCounter', 'getBatchId', 'setCounter', @@ -1437,7 +1437,7 @@ public function testGetNewQuotesEmptyJson() ->with(self::MAGENTO_STORE_ID) ->willReturn(self::WEB_SITE_ID_FROM_MAGENTO_STORE_ID); $cartsApiMock->expects($this->once()) - ->method('_getAllCartsByEmail') + ->method('getAllCartsByEmail') ->with(self::CUSTOMER_EMAIL_BY_CART,self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID) ->willReturn($quoteByEmailResoureceCollectionMock); $cartsApiMock->expects($this->exactly(2)) @@ -1597,6 +1597,6 @@ public function testGetAllCartsByEmail() ->method('where') ->with($where); - $cartsApiMock->_getAllCartsByEmail(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); + $cartsApiMock->getAllCartsByEmail(self::CUSTOMER_EMAIL_BY_CART, self::MAILCHIMP_STORE_ID, self::MAGENTO_STORE_ID); } } From e9fcfcc4f904f73cf45e3be7a1313699657edfe1 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 18 Jan 2019 15:12:58 -0300 Subject: [PATCH 088/113] Fix test. --- .../Ebizmarts/MailChimp/Model/Email/Template.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Email/Template.php b/app/code/community/Ebizmarts/MailChimp/Model/Email/Template.php index 0432cc89c..05f561cd3 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Email/Template.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Email/Template.php @@ -230,7 +230,13 @@ protected function getSendingSetReturnPath() */ protected function getSendersDomains($mail) { - return $mail->senders->domains(); + $mandrillSenders = array(); + try { + $mandrillSenders = $mail->senders->domains(); + } catch (Exception $e) { + Mage::log($e->getMessage(), null, 'Mandrill.log', true); + } + return $mandrillSenders; } /** @@ -240,7 +246,13 @@ protected function getSendersDomains($mail) */ protected function sendMail($email, $mail) { - return $mail->messages->send($email); + $mailSent = false; + try { + $mailSent = $mail->messages->send($email); + } catch (Exception $e) { + Mage::log($e->getMessage(), null, 'Mandrill.log', true); + } + return $mailSent; } /** From 8c55492de538b5a99f7a7454e72f14b946825ec8 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 29 Jan 2019 11:57:01 -0300 Subject: [PATCH 089/113] closes #863 Ebizmarts_MailChimp_Model_Api_Batches --- .../community/Ebizmarts/MailChimp/Model/Api/Batches.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php index 747d53118..d84e7462c 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php @@ -197,13 +197,12 @@ public function handleEcommerceBatches() $helper->handleResendDataBefore(); foreach ($stores as $store) { $storeId = $store->getId(); - if(!$this->_ping($storeId)) { - $helper->logError('Could not connect to MailChimp: Make sure the API Key is correct and there is an internet connection'); - return; - } - if ($helper->isEcomSyncDataEnabled($storeId)) { + if ($helper->isEcomSyncDataEnabled($storeId) && $this->_ping($storeId)) { $this->_getResults($storeId); $this->_sendEcommerceBatch($storeId); + } else if ($helper->isEcomSyncDataEnabled($storeId) && !$this->_ping($storeId)) { + $helper->logError('Could not connect to MailChimp: Make sure the API Key is correct and there is an internet connection'); + return; } } $helper->handleResendDataAfter(); From 7327bfa2fcb6b4cb41263c5a393c46787708b3dc Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 29 Jan 2019 12:15:24 -0300 Subject: [PATCH 090/113] closes #863 Modified the function handleEcommerceBatches for the testHandleEcommerceBatches --- .../community/Ebizmarts/MailChimp/Model/Api/Batches.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php index d84e7462c..a8049c2ab 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php @@ -197,10 +197,12 @@ public function handleEcommerceBatches() $helper->handleResendDataBefore(); foreach ($stores as $store) { $storeId = $store->getId(); - if ($helper->isEcomSyncDataEnabled($storeId) && $this->_ping($storeId)) { + $isEcommerceEnabled = $helper->isEcomSyncDataEnabled($storeId); + $ping = $this->_ping($storeId); + if ($isEcommerceEnabled && $ping) { $this->_getResults($storeId); $this->_sendEcommerceBatch($storeId); - } else if ($helper->isEcomSyncDataEnabled($storeId) && !$this->_ping($storeId)) { + } else if ($isEcommerceEnabled && !$ping) { $helper->logError('Could not connect to MailChimp: Make sure the API Key is correct and there is an internet connection'); return; } From a53192cc136a455505838ab243e71a1a59352909 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 29 Jan 2019 12:16:52 -0300 Subject: [PATCH 091/113] closes #863 fixed the testHandleEcommerceBatches according to the change in the function --- .../tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php index af1dc0ece..f6999e1b3 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php @@ -252,7 +252,7 @@ public function handleEcommerceBatchesDataProvider(){ return array( array(array('mailchimp_api_status' => true, 'isEcomSyncDataEnabled' => 1, '_getResults' => 1, '_sendEcommerceBatch' => 1, 'handleResendDataAfter' => 1, 'addSyncValueToArray' => 1, 'getIterator' => 2, 'getId' => 2)), - array(array('mailchimp_api_status' => false, 'isEcomSyncDataEnabled' => 0, '_getResults' => 0, '_sendEcommerceBatch' => 0, 'handleResendDataAfter' => 0, 'addSyncValueToArray' => 0, + array(array('mailchimp_api_status' => false, 'isEcomSyncDataEnabled' => 1, '_getResults' => 0, '_sendEcommerceBatch' => 0, 'handleResendDataAfter' => 0, 'addSyncValueToArray' => 0, 'getIterator' => 1, 'getId' => 1)) ); } From 3fc82177d5f1eaeed788ff2c9e93045c7bd9fcfc Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 29 Jan 2019 13:55:24 -0300 Subject: [PATCH 092/113] Avoid real time call when MCJs URL is not available. closes #862 --- .../Ebizmarts/MailChimp/Helper/Data.php | 10 +++-- .../Ebizmarts/MailChimp/Model/Api/Stores.php | 11 ++++-- .../Ebizmarts/MailChimp/Helper/DataTest.php | 38 +++++++++++++++++++ .../MailChimp/Model/Api/StoresTest.php | 6 +-- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 6bb5591a6..66d5508cc 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1574,6 +1574,8 @@ protected function getOrderCollectionByCustomerEmail($subscriberEmail) * Return html code for adding the MailChimp javascript. * * @return string + * @throws Mage_Core_Exception + * @throws Mage_Core_Model_Store_Exception */ public function getMCJs() { @@ -1582,14 +1584,14 @@ public function getMCJs() $storeId = $this->getMageApp()->getStore()->getId(); if ($this->isEcomSyncDataEnabled($storeId)) { $currentUrl = $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $storeId); + if ($this->areJsUrlAndListScopesEqual($storeId)) { $url = $currentUrl; } - if (!$url) { - $url = $this->getApiStores()->getMCJsUrl($storeId, 'stores'); + if ($url !== null) { + $script = ''; } - $script = ''; } return $script; } @@ -2952,6 +2954,8 @@ protected function isNotDefaultScope($config) } /** + * Returns true if the js URL belongs to the same scope as where the list has been configured. + * * @param $storeId * @return bool */ diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Stores.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Stores.php index 38350ac13..e6b357804 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Stores.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Stores.php @@ -115,13 +115,13 @@ public function modifyName($name, $scopeId, $scope) } /** - * Returns URL from MailChimp store data + * Retrieve store data and save the MCJs URL for the correct scope in config table. * * @param $scopeId * @param $scope * @return mixed */ - public function getMCJsUrl($scopeId, $scope) + public function retrieveAndSaveMCJsUrlInConfig($scopeId, $scope) { $helper = $this->makeHelper(); try { @@ -133,14 +133,19 @@ public function getMCJsUrl($scopeId, $scope) $configValues = array(array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $url)); $realScope = $helper->getRealScopeForConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, $scopeId, $scope); $helper->saveMailchimpConfig($configValues, $realScope['scope_id'], $realScope['scope']); - return $url; + return true; + } else { + return false; } } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { $helper->logError($e->getMessage()); + return false; } catch (MailChimp_Error $e) { $helper->logError($e->getFriendlyMessage()); + return false; } catch (Exception $e) { $helper->logError($e->getMessage()); + return false; } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php index 0a9432cd8..7da6b70e9 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -1359,4 +1359,42 @@ public function testSaveInterestGroupData() $helperMock->saveInterestGroupData($params, $storeId, null, $subscriberMock); } + + public function testGetMCJs() + { + $storeId = 1; + $jsUrl = 'https://chimpstatic.com/mcjs-connected/js/users/1647ea7abc3f2f3259e2613f9/dffd1d29fea0323354a9caa32.js'; + + $expectedResult = ''; + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getMageApp', 'isEcomSyncDataEnabled', 'getConfigValueForScope', + 'areJsUrlAndListScopesEqual')) + ->getMock(); + + $mageAppMock = $this->getMockBuilder(Mage_Core_Model_App::class) + ->disableOriginalConstructor() + ->setMethods(array('getStore')) + ->getMock(); + + $storeMock = $this->getMockBuilder(Mage_Core_Model_Store::class) + ->disableOriginalConstructor() + ->setMethods(array('getId')) + ->getMock(); + + $helperMock->expects($this->once())->method('getMageApp')->willReturn($mageAppMock); + + $mageAppMock->expects($this->once())->method('getStore')->willReturn($storeMock); + + $storeMock->expects($this->once())->method('getId')->willReturn($storeId); + + $helperMock->expects($this->once())->method('isEcomSyncDataEnabled')->with($storeId)->willReturn(true); + $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $storeId)->willReturn($jsUrl); + $helperMock->expects($this->once())->method('areJsUrlAndListScopesEqual')->with($storeId)->willReturn(true); + + $result = $helperMock->getMCJs(); + + $this->assertEquals($expectedResult, $result); + } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/StoresTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/StoresTest.php index 0ca57937b..23c1f181c 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/StoresTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/StoresTest.php @@ -205,7 +205,7 @@ public function testModifyName() $apiStoresMock->modifyName($name, $scopeId, $scope); } - public function testGetMCJsUrl() + public function testRetrieveAndSaveMCJsUrlInConfig() { $mailChimpStoreId = 'a1s2d3f4g5h6j7k8l9n0'; $scopeId = 1; @@ -239,8 +239,8 @@ public function testGetMCJsUrl() $helperMock->expects($this->once())->method('getRealScopeForConfig')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, $scopeId, $scope)->willReturn($realScope); $helperMock->expects($this->once())->method('saveMailchimpConfig')->with($configValues, $realScope['scope_id'], $realScope['scope']); - $return = $apiStoresMock->getMCJsUrl($scopeId, $scope); + $return = $apiStoresMock->retrieveAndSaveMCJsUrlInConfig($scopeId, $scope); - $this->assertEquals($MCJsUrl, $return); + $this->assertEquals(true, $return); } } From da7ae358ce719bf8b9621fe5180f679cd1d0ea05 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 29 Jan 2019 16:04:54 -0300 Subject: [PATCH 093/113] closes #863 fixed an error in the function handleEcommerceBatches --- .../Ebizmarts/MailChimp/Model/Api/Batches.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php index a8049c2ab..c276fe6b2 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php @@ -197,15 +197,16 @@ public function handleEcommerceBatches() $helper->handleResendDataBefore(); foreach ($stores as $store) { $storeId = $store->getId(); - $isEcommerceEnabled = $helper->isEcomSyncDataEnabled($storeId); - $ping = $this->_ping($storeId); - if ($isEcommerceEnabled && $ping) { - $this->_getResults($storeId); - $this->_sendEcommerceBatch($storeId); - } else if ($isEcommerceEnabled && !$ping) { - $helper->logError('Could not connect to MailChimp: Make sure the API Key is correct and there is an internet connection'); - return; + if ($helper->isEcomSyncDataEnabled($storeId)) { + if ($this->_ping($storeId)) { + $this->_getResults($storeId); + $this->_sendEcommerceBatch($storeId); + } else { + $helper->logError('Could not connect to MailChimp: Make sure the API Key is correct and there is an internet connection'); + return; + } } + } $helper->handleResendDataAfter(); From 6211625cd406c4a1d3b19dde99e6b191ddc3314f Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Wed, 30 Jan 2019 14:28:25 -0300 Subject: [PATCH 094/113] closes #867#770 - check if the module is enabled before execute the code in the function getInterestGroups if the module is disabled the function returns an empty array --- .../Ebizmarts/MailChimp/Helper/Data.php | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 66d5508cc..386f87a2c 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -3294,38 +3294,42 @@ public function getInterest($storeId) */ public function getInterestGroups($customerId, $subscriberId, $storeId, $interest = null) { - if (!$interest) { - $interest = $this->getInterest($storeId); - } - $interestGroup = $this->getInterestGroupModel(); - $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); - if ($interestGroup->getId()) { - $groups = $this->arrayDecode($interestGroup->getGroupdata()); - foreach ($groups as $key => $value) { - if (isset($interest[$key])) { - if (is_array($value)) { - foreach ($value as $groupId) { + if ($this->isSubscriptionEnabled($storeId)) { + if (!$interest) { + $interest = $this->getInterest($storeId); + } + $interestGroup = $this->getInterestGroupModel(); + $interestGroup->getByRelatedIdStoreId($customerId, $subscriberId, $storeId); + if ($interestGroup->getId()) { + $groups = $this->arrayDecode($interestGroup->getGroupdata()); + foreach ($groups as $key => $value) { + if (isset($interest[$key])) { + if (is_array($value)) { + foreach ($value as $groupId) { + foreach ($interest[$key]['category'] as $gkey => $gvalue) { + if ($gvalue['id'] == $groupId) { + $interest[$key]['category'][$gkey]['checked'] = true; + } elseif (!isset($interest[$key]['category'][$gkey]['checked'])) { + $interest[$key]['category'][$gkey]['checked'] = false; + } + } + } + } else { foreach ($interest[$key]['category'] as $gkey => $gvalue) { - if ($gvalue['id'] == $groupId) { + if ($gvalue['id'] == $value) { $interest[$key]['category'][$gkey]['checked'] = true; - } elseif (!isset($interest[$key]['category'][$gkey]['checked'])) { + } else { $interest[$key]['category'][$gkey]['checked'] = false; } } } - } else { - foreach ($interest[$key]['category'] as $gkey => $gvalue) { - if ($gvalue['id'] == $value) { - $interest[$key]['category'][$gkey]['checked'] = true; - } else { - $interest[$key]['category'][$gkey]['checked'] = false; - } - } } } } + return $interest; + } else { + return array(); } - return $interest; } From d916217f5c44904434266f95fa2cea7042a9155b Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Wed, 30 Jan 2019 14:29:32 -0300 Subject: [PATCH 095/113] closes #867#770 - modified test to adapt it to the change in the function getInterestGroups --- .../Ebizmarts/MailChimp/Helper/DataTest.php | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php index 7da6b70e9..672864cd4 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -1263,7 +1263,7 @@ public function testGetInterestGroups() $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('getInterest', 'getInterestGroupModel', 'getLocalInterestCategories', 'arrayDecode')) + ->setMethods(array('getInterest', 'getInterestGroupModel', 'getLocalInterestCategories', 'arrayDecode', 'isSubscriptionEnabled')) ->getMock(); $interestGroupMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Interestgroup::class) @@ -1271,6 +1271,7 @@ public function testGetInterestGroups() ->setMethods(array('getByRelatedIdStoreId', 'getId', 'getGroupdata')) ->getMock(); + $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); $helperMock->expects($this->once())->method('getInterest')->with($storeId)->willReturn($interest); $helperMock->expects($this->once())->method('getInterestGroupModel')->willReturn($interestGroupMock); @@ -1285,6 +1286,25 @@ public function testGetInterestGroups() $this->assertEquals($expectedResult, $result); } + public function testGetInterestGroupsIsSubscriptionDisabled () + { + $customerId = 1; + $subscriberId = 1; + $storeId = 1; + $expectedResult = array(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('isSubscriptionEnabled')) + ->getMock(); + + $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(false); + + $result = $helperMock->getInterestGroups($customerId, $subscriberId, $storeId); + + $this->assertEquals($expectedResult, $result); + } + public function testSaveInterestGroupData() { $params = array(); From 96e9e905ecd2fdace868b143ad6295031c7bf6b7 Mon Sep 17 00:00:00 2001 From: brian-ebizmarts Date: Wed, 30 Jan 2019 14:47:59 -0300 Subject: [PATCH 096/113] Order status fixed spelling error. closes #868 --- app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php index dcf0439fd..f632b4e88 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php @@ -15,9 +15,9 @@ class Ebizmarts_MailChimp_Model_Api_Orders const BATCH_LIMIT = 50; const BATCH_LIMIT_ONLY_ORDERS = 500; const PAID = 'paid'; - const PARTIALLY_PAID = 'parially_paid'; + const PARTIALLY_PAID = 'partially_paid'; const SHIPPED = 'shipped'; - const PARTIALLY_SHIPPED = 'parially_shipped'; + const PARTIALLY_SHIPPED = 'partially_shipped'; const PENDING = 'pending'; const REFUNDED = 'refunded'; const PARTIALLY_REFUNDED = 'partially_refunded'; From c4396578c5e410ddf6e18ed62caf4aee9af94146 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 1 Feb 2019 10:44:49 -0300 Subject: [PATCH 097/113] closes #871 added acl permissions to the action createWebhook --- .../MailChimp/controllers/Adminhtml/MailchimpController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index e3f150fa4..03c464009 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -66,6 +66,9 @@ protected function _isAllowed() case 'resendSubscribers': $acl = 'system/config/mailchimp'; break; + case 'createWebhook': + $acl = 'system/config/mailchimp'; + break; } return Mage::getSingleton('admin/session')->isAllowed($acl); From 05cb3ebb917734ceb26ebd66eecc50c70db0e1dc Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 1 Feb 2019 11:05:30 -0300 Subject: [PATCH 098/113] closes #871 modified the case syntax in the function _isAllowed --- .../MailChimp/controllers/Adminhtml/MailchimpController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 03c464009..08250b0b5 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -64,8 +64,6 @@ protected function _isAllowed() switch ($this->getRequest()->getActionName()) { case 'index': case 'resendSubscribers': - $acl = 'system/config/mailchimp'; - break; case 'createWebhook': $acl = 'system/config/mailchimp'; break; From 7b8a979238fff0d0b65fcc621a6600776f101cbe Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 1 Feb 2019 11:23:41 -0300 Subject: [PATCH 099/113] closes #871 indented the case in the function _isAllowed --- .../controllers/Adminhtml/MailchimpController.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 08250b0b5..7a64dc987 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -62,11 +62,11 @@ protected function _isAllowed() { $acl = null; switch ($this->getRequest()->getActionName()) { - case 'index': - case 'resendSubscribers': - case 'createWebhook': - $acl = 'system/config/mailchimp'; - break; + case 'index': + case 'resendSubscribers': + case 'createWebhook': + $acl = 'system/config/mailchimp'; + break; } return Mage::getSingleton('admin/session')->isAllowed($acl); From 79559c3b3df61f8c13106d2c1d126f6d73522a8c Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 5 Feb 2019 15:16:26 -0300 Subject: [PATCH 100/113] closes #873 modified subscriberSaveAfter and subscriberSaveBefore removed repeated code --- .../Ebizmarts/MailChimp/Model/Observer.php | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 125cb1a4b..4160654a2 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -141,29 +141,13 @@ public function subscriberSaveBefore(Varien_Event_Observer $observer) if ($isEnabled && $subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { $statusChanged = $subscriber->getIsStatusChanged(); - $apiSubscriber = $this->makeApiSubscriber(); //Override Magento status to always send double opt-in confirmation. if ($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId)) { $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $this->addSuccessIfRequired($helper); } - $subscriber->setImportMode(true); - - if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { - //Use MailChimp emails - if ($statusChanged) { - $apiSubscriber->updateSubscriber($subscriber, true); - } else { - $origData = $subscriber->getOrigData(); - - if (is_array($origData) && isset($origData['subscriber_status']) - && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() - ) { - $apiSubscriber->updateSubscriber($subscriber, true); - } - } - } +// $subscriber->setImportMode(true); } return $observer; @@ -190,9 +174,20 @@ public function subscriberSaveAfter(Varien_Event_Observer $observer) $this->createEmailCookie($subscriber); - if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { + if ($helper->isUseMagentoEmailsEnabled($storeId) != 1) { + Mage::log('estoy en subscriberSaveAfter', null, 'ebizmartsSubscriberSaveAfter.log', true); $apiSubscriber = $this->makeApiSubscriber(); - $apiSubscriber->updateSubscriber($subscriber, true); + if ($subscriber->getIsStatusChanged()) { + $apiSubscriber->updateSubscriber($subscriber, true); + } else { + $origData = $subscriber->getOrigData(); + + if (is_array($origData) && isset($origData['subscriber_status']) + && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() + ) { + $apiSubscriber->updateSubscriber($subscriber, true); + } + } } else { $subscriber->setImportMode(false); } @@ -298,6 +293,7 @@ public function customerSaveAfter(Varien_Event_Observer $observer) } //update subscriber data if a subscriber with the same email address exists and was not affected. if (!$origEmail || $origEmail == $customerEmail) { + Mage::log('estoy en customerSaveAfter', null, 'ebizmartsCustomerSaveAfter.log', true); $apiSubscriber->update($customerEmail, $storeId); } From f805a8bbdda027c92c0ad6f9f064a91d1ad7bd2b Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 5 Feb 2019 15:21:42 -0300 Subject: [PATCH 101/113] closes #873 created the function isUseMagentoEmailEnabled to invoke the function getConfigValueForScope instead invoke it directly --- app/code/community/Ebizmarts/MailChimp/Helper/Data.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 386f87a2c..c41dcbe14 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -279,6 +279,16 @@ public function isMailChimpEnabled($scopeId, $scope = null) return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE, $scopeId, $scope); } + /** + * + * @param $scopeId + * @return bool | returns true if useMagentoEmails is enabled + */ + public function isUseMagentoEmailsEnabled ($scopeId) + { + return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $scopeId); + } + /** * Return if module is enabled and list selected for given scope. * From 2d2e503fd44604ca7363e755f7eddd75cd34a5be Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Tue, 5 Feb 2019 16:16:00 -0300 Subject: [PATCH 102/113] closes #873 modified testSubscriberSaveBefore and testSubscriberSaveAfter to adapt it to the changes --- .../MailChimp/Model/ObserverTest.php | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) mode change 100644 => 100755 dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php old mode 100644 new mode 100755 index 60628070e..3fb541e5c --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -673,7 +673,6 @@ public function testAddColumnToSalesOrderGridCollection() public function testSubscriberSaveBefore($data) { $getStoreId = $data['getStoreId']; - $magentoMail = $data['magentoMail']; $subscriberUpdatedAmount = $data['subscriberUpdatedAmount']; $storeId = 1; @@ -684,13 +683,13 @@ public function testSubscriberSaveBefore($data) $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) ->disableOriginalConstructor() - ->setMethods(array('makeHelper', 'addSuccessIfRequired', 'makeApiSubscriber')) + ->setMethods(array('makeHelper', 'addSuccessIfRequired')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() ->setMethods(array('isSubscriptionEnabled', 'isEcomSyncDataEnabledInAnyScope', - 'isSubscriptionConfirmationEnabled', 'getConfigValueForScope', 'getStoreId')) + 'isSubscriptionConfirmationEnabled', 'getStoreId')) ->getMock(); $eventMock = $this->getMockBuilder(Varien_Event::class) @@ -703,11 +702,6 @@ public function testSubscriberSaveBefore($data) ->setMethods(array('getSubscriberSource', 'getIsStatusChanged', 'getStatus', 'setStatus', 'getStoreId')) ->getMock(); - $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) - ->disableOriginalConstructor() - ->setMethods(array('updateSubscriber')) - ->getMock(); - $eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock); $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); @@ -718,22 +712,16 @@ public function testSubscriberSaveBefore($data) $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($storeId)->willReturn(true); - $subscriberMock->expects($this->once())->method('getIsStatusChanged')->willReturn(true); - - $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); - $subscriberMock->expects($this->once())->method('getStatus')->willReturn(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); + $subscriberMock->expects($this->once())->method('getIsStatusChanged')->willReturn(true); + $helperMock->expects($this->once())->method('isSubscriptionConfirmationEnabled')->with($storeId)->willReturn(true); $subscriberMock->expects($this->once())->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $observerMock->expects($this->once())->method('addSuccessIfRequired')->with($helperMock); - $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); - - $apiSubscriberMock->expects($this->exactly($subscriberUpdatedAmount))->method('updateSubscriber')->with($subscriberMock, true); - $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); $subscriberMock->expects($this->exactly($getStoreId))->method('getStoreId')->willReturn($storeId); @@ -754,7 +742,6 @@ public function testSubscriberSaveAfter() { $storeId = 1; $params = array(); - $magentoMail = 0; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) ->disableOriginalConstructor() @@ -768,7 +755,7 @@ public function testSubscriberSaveAfter() $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData', 'getConfigValueForScope')) + ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData', 'isUseMagentoEmailsEnabled')) ->getMock(); $eventMock = $this->getMockBuilder(Varien_Event::class) @@ -778,7 +765,7 @@ public function testSubscriberSaveAfter() $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() - ->setMethods(array('getSubscriberSource', 'getStoreId')) + ->setMethods(array('getSubscriberSource', 'getStoreId', 'getIsStatusChanged')) ->getMock(); $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) @@ -811,7 +798,9 @@ public function testSubscriberSaveAfter() $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); - $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); + $helperMock->expects($this->once())->method('isUseMagentoEmailsEnabled')->with($storeId)->willReturn(false); + + $subscriberMock->expects($this->once())->method('getIsStatusChanged')->willReturn(true); $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); From 28cf0b4fa45435caf54f489c69864901f3fcd52a Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Wed, 6 Feb 2019 13:28:39 -0300 Subject: [PATCH 103/113] closes #874 modified the function SubscriberSaveBefore to not change the status to the new subscribers if the options double opt-in is enabled and UseMagentoEmails too --- .../Ebizmarts/MailChimp/Model/Observer.php | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 125cb1a4b..b2121bede 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -141,29 +141,13 @@ public function subscriberSaveBefore(Varien_Event_Observer $observer) if ($isEnabled && $subscriber->getSubscriberSource() != Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE) { $statusChanged = $subscriber->getIsStatusChanged(); - $apiSubscriber = $this->makeApiSubscriber(); //Override Magento status to always send double opt-in confirmation. - if ($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId)) { + if ($statusChanged && $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED && $helper->isSubscriptionConfirmationEnabled($storeId) && !$helper->isUseMagentoEmailsEnabled($storeId)) { $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $this->addSuccessIfRequired($helper); } - $subscriber->setImportMode(true); - - if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { - //Use MailChimp emails - if ($statusChanged) { - $apiSubscriber->updateSubscriber($subscriber, true); - } else { - $origData = $subscriber->getOrigData(); - if (is_array($origData) && isset($origData['subscriber_status']) - && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() - ) { - $apiSubscriber->updateSubscriber($subscriber, true); - } - } - } } return $observer; @@ -190,12 +174,23 @@ public function subscriberSaveAfter(Varien_Event_Observer $observer) $this->createEmailCookie($subscriber); - if ($helper->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId) != 1) { + if ($helper->isUseMagentoEmailsEnabled($storeId) != 1) { $apiSubscriber = $this->makeApiSubscriber(); - $apiSubscriber->updateSubscriber($subscriber, true); + + if ($subscriber->getIsStatusChanged()) { + $apiSubscriber->updateSubscriber($subscriber, true); + } else { + $origData = $subscriber->getOrigData(); + if (is_array($origData) && isset($origData['subscriber_status']) + && $origData['subscriber_status'] != $subscriber->getSubscriberStatus() + ) { + $apiSubscriber->updateSubscriber($subscriber, true); + } + } } else { $subscriber->setImportMode(false); } + } return $observer; From 04e1760336a7b1c2931e332fadb35ff72885b9b7 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Wed, 6 Feb 2019 13:38:54 -0300 Subject: [PATCH 104/113] closes #873 removed logs --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 4160654a2..64d510b05 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -147,7 +147,7 @@ public function subscriberSaveBefore(Varien_Event_Observer $observer) $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $this->addSuccessIfRequired($helper); } -// $subscriber->setImportMode(true); + } return $observer; @@ -175,7 +175,6 @@ public function subscriberSaveAfter(Varien_Event_Observer $observer) $this->createEmailCookie($subscriber); if ($helper->isUseMagentoEmailsEnabled($storeId) != 1) { - Mage::log('estoy en subscriberSaveAfter', null, 'ebizmartsSubscriberSaveAfter.log', true); $apiSubscriber = $this->makeApiSubscriber(); if ($subscriber->getIsStatusChanged()) { $apiSubscriber->updateSubscriber($subscriber, true); @@ -293,7 +292,6 @@ public function customerSaveAfter(Varien_Event_Observer $observer) } //update subscriber data if a subscriber with the same email address exists and was not affected. if (!$origEmail || $origEmail == $customerEmail) { - Mage::log('estoy en customerSaveAfter', null, 'ebizmartsCustomerSaveAfter.log', true); $apiSubscriber->update($customerEmail, $storeId); } From 147bfeb096ee44838b477ce90e307cc23de47ec5 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Thu, 7 Feb 2019 11:15:02 -0300 Subject: [PATCH 105/113] closes #874 modified tests to adapt it to the change in the function --- .../MailChimp/Model/ObserverTest.php | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 60628070e..0ea6d00d2 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -673,8 +673,6 @@ public function testAddColumnToSalesOrderGridCollection() public function testSubscriberSaveBefore($data) { $getStoreId = $data['getStoreId']; - $magentoMail = $data['magentoMail']; - $subscriberUpdatedAmount = $data['subscriberUpdatedAmount']; $storeId = 1; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) @@ -684,13 +682,13 @@ public function testSubscriberSaveBefore($data) $observerMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) ->disableOriginalConstructor() - ->setMethods(array('makeHelper', 'addSuccessIfRequired', 'makeApiSubscriber')) + ->setMethods(array('makeHelper', 'addSuccessIfRequired')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() ->setMethods(array('isSubscriptionEnabled', 'isEcomSyncDataEnabledInAnyScope', - 'isSubscriptionConfirmationEnabled', 'getConfigValueForScope', 'getStoreId')) + 'isSubscriptionConfirmationEnabled', 'getStoreId', 'isUseMagentoEmailsEnabled')) ->getMock(); $eventMock = $this->getMockBuilder(Varien_Event::class) @@ -703,11 +701,6 @@ public function testSubscriberSaveBefore($data) ->setMethods(array('getSubscriberSource', 'getIsStatusChanged', 'getStatus', 'setStatus', 'getStoreId')) ->getMock(); - $apiSubscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Api_Subscribers::class) - ->disableOriginalConstructor() - ->setMethods(array('updateSubscriber')) - ->getMock(); - $eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock); $eventMock->expects($this->once())->method('getSubscriber')->willReturn($subscriberMock); @@ -720,20 +713,16 @@ public function testSubscriberSaveBefore($data) $subscriberMock->expects($this->once())->method('getIsStatusChanged')->willReturn(true); - $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); - $subscriberMock->expects($this->once())->method('getStatus')->willReturn(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED); $helperMock->expects($this->once())->method('isSubscriptionConfirmationEnabled')->with($storeId)->willReturn(true); + $helperMock->expects($this->once())->method('isUseMagentoEmailsEnabled')->with($storeId)->willReturn(false); + $subscriberMock->expects($this->once())->method('setStatus')->with(Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE); $observerMock->expects($this->once())->method('addSuccessIfRequired')->with($helperMock); - $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); - - $apiSubscriberMock->expects($this->exactly($subscriberUpdatedAmount))->method('updateSubscriber')->with($subscriberMock, true); - $subscriberMock->expects($this->once())->method('getSubscriberSource')->willReturn(null); $subscriberMock->expects($this->exactly($getStoreId))->method('getStoreId')->willReturn($storeId); @@ -754,7 +743,6 @@ public function testSubscriberSaveAfter() { $storeId = 1; $params = array(); - $magentoMail = 0; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) ->disableOriginalConstructor() @@ -768,7 +756,7 @@ public function testSubscriberSaveAfter() $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData', 'getConfigValueForScope')) + ->setMethods(array('isSubscriptionEnabled', 'saveInterestGroupData', 'isUseMagentoEmailsEnabled')) ->getMock(); $eventMock = $this->getMockBuilder(Varien_Event::class) @@ -778,7 +766,7 @@ public function testSubscriberSaveAfter() $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() - ->setMethods(array('getSubscriberSource', 'getStoreId')) + ->setMethods(array('getSubscriberSource', 'getStoreId', 'getIsStatusChanged')) ->getMock(); $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) @@ -811,7 +799,9 @@ public function testSubscriberSaveAfter() $observerMock->expects($this->once())->method('createEmailCookie')->with($subscriberMock); - $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $storeId)->willReturn($magentoMail); + $helperMock->expects($this->once())->method('isUseMagentoEmailsEnabled')->with($storeId)->willReturn(false); + + $subscriberMock->expects($this->once())->method('getIsStatusChanged')->willReturn(true); $observerMock->expects($this->once())->method('makeApiSubscriber')->willReturn($apiSubscriberMock); From 9043947fff067a7c3aac468b01d9ad62e64a8833 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Thu, 7 Feb 2019 13:20:57 -0300 Subject: [PATCH 106/113] closes #874 removed unused variable from testSubscribeSaveAfter --- .../tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 6a6c5d872..7539975e3 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -673,7 +673,6 @@ public function testAddColumnToSalesOrderGridCollection() public function testSubscriberSaveBefore($data) { $getStoreId = $data['getStoreId']; - $subscriberUpdatedAmount = $data['subscriberUpdatedAmount']; $storeId = 1; $eventObserverMock = $this->getMockBuilder(Varien_Event_Observer::class) From afd9d36e637abfa0f1421299cd0ed0f93832f94c Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Thu, 7 Feb 2019 15:48:27 -0300 Subject: [PATCH 107/113] closes #874 removed incorrect error message from the observer --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 7bb8b33a3..048e1f93a 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -1052,11 +1052,8 @@ protected function handleCustomerGroups($subscriberEmail, $params, $storeId, $cu Mage::getSingleton('adminhtml/session')->addWarning($helper->__('The customer must be subscribed for this change to apply.')); } } else { - if (!$helper->isAdmin()) { - //save frontend groupdata when customer is not subscribed. - $helper->saveInterestGroupData($params, $storeId, $customerId); - } - Mage::getSingleton('adminhtml/session')->addError($helper->__('Something went wrong when trying to save the interest group data. The customer must be subscribed for this change to apply.')); + //save frontend groupdata when customer is not subscribed. + $helper->saveInterestGroupData($params, $storeId, $customerId); } return $subscriber; } From 8666054893df21f1a410d0f3d3d027260c9fc85f Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 8 Feb 2019 11:28:11 -0300 Subject: [PATCH 108/113] implemented unitTest of handleCustomerGroups --- .../MailChimp/Model/ObserverTest.php | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 7539975e3..3fa601a3b 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -943,5 +943,119 @@ public function testItemCancel() $mailchimpObserverMock->itemCancel($observerMock); } + + public function testHandleCustomerGroupsIsSubscribed() + { + $params = array(); + $storeId = 1; + $customerId = 15; + $subscriberEmail = 'luciaines+testHandelCustomerGroups@ebizmarts.com'; + + $mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('makeHelper', 'getSubscriberModel')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('saveInterestGroupData')) + ->getMock(); + + $subbscriberModelMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Subscriber::class) + ->setMethods(array('loadByEmail')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Subscriber::class) + ->setMethods(array('getId')) + ->getMock(); + + $mailchimpObserverMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); + $mailchimpObserverMock->expects($this->once())->method('getSubscriberModel')->willReturn($subbscriberModelMock); + + $helperMock->expects($this->once())->method('saveInterestGroupData')->with($params, $storeId, $customerId, $subscriberMock); + + $subbscriberModelMock->expects($this->once())->method('loadByEmail')->with($subscriberEmail)->willReturn($subscriberMock); + + $subscriberMock->expects($this->once())->method('getId')->willReturn(true); + + $mailchimpObserverMock->handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId); + } + + public function testHandleCustomerGroupsIsNotSubcribedFromAdmin() + { + $customerId = 15; + $subscriberEmail = 'luciaines+testHandelCustomerGroups@ebizmarts.com'; + $params = array('form_key' => 'Pm5pxh17N9Z9AINN', 'customer_id' => $customerId, 'group' => array('e939299a7d' => 'e939299a7d', '3dd23446e4' => '3dd23446e4', 'a6c3c332bf' => 'a6c3c332bf')); + $storeId = 1; + $groups = array('d46296f47c' => array ('3dd23446e4' => '3dd23446e4', 'a6c3c332bf' => 'a6c3c332bf')); + + $mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('makeHelper', 'getSubscriberModel', 'getWarningMessageAdminHtmlSession')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('saveInterestGroupData', 'getInterestGroupsIfAvailable')) + ->getMock(); + + $subbscriberModelMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Subscriber::class) + ->setMethods(array('loadByEmail')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Subscriber::class) + ->setMethods(array('getId')) + ->getMock(); + + $mailchimpObserverMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); + $mailchimpObserverMock->expects($this->once())->method('getSubscriberModel')->willReturn($subbscriberModelMock); + $mailchimpObserverMock->expects($this->once())->method('getWarningMessageAdminHtmlSession')->with($helperMock); + + $helperMock->expects($this->once())->method('saveInterestGroupData')->with($params, $storeId, $customerId); + $helperMock->expects($this->once())->method('getInterestGroupsIfAvailable')->with($params)->willReturn($groups); + + $subbscriberModelMock->expects($this->once())->method('loadByEmail')->with($subscriberEmail)->willReturn($subscriberMock); + + $subscriberMock->expects($this->once())->method('getId')->willReturn(false); + + $mailchimpObserverMock->handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId); + } + + public function testHandleCustomerGroupsIsNotSubcribedFromFrontEnd() + { + $params = array(); + $storeId = 1; + $customerId = 15; + $subscriberEmail = 'luciaines+testHandelCustomerGroups@ebizmarts.com'; + + $mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('makeHelper', 'getSubscriberModel')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('saveInterestGroupData')) + ->getMock(); + + $subbscriberModelMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Subscriber::class) + ->setMethods(array('loadByEmail')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Subscriber::class) + ->setMethods(array('getId')) + ->getMock(); + + $mailchimpObserverMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); + $mailchimpObserverMock->expects($this->once())->method('getSubscriberModel')->willReturn($subbscriberModelMock); + + $helperMock->expects($this->once())->method('saveInterestGroupData')->with($params, $storeId, $customerId); + + $subbscriberModelMock->expects($this->once())->method('loadByEmail')->with($subscriberEmail)->willReturn($subscriberMock); + + $subscriberMock->expects($this->once())->method('getId')->willReturn(false); + + $mailchimpObserverMock->handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId); + } } From 90027837c429b0ce5e731326cbd5ebf5a9878a35 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Fri, 8 Feb 2019 11:29:41 -0300 Subject: [PATCH 109/113] modified the function handleCustomerGroups to adapt it to the test --- .../Ebizmarts/MailChimp/Model/Observer.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 048e1f93a..8a31b8781 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -1038,7 +1038,7 @@ protected function getRequest() * @return Mage_Newsletter_Model_Subscriber * @throws Mage_Core_Model_Store_Exception */ - protected function handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId = null) + public function handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId = null) { $helper = $this->makeHelper(); $subscriberModel = $this->getSubscriberModel(); @@ -1049,7 +1049,7 @@ protected function handleCustomerGroups($subscriberEmail, $params, $storeId, $cu $groups = $helper->getInterestGroupsIfAvailable($params); if ($groups) { $helper->saveInterestGroupData($params, $storeId, $customerId); - Mage::getSingleton('adminhtml/session')->addWarning($helper->__('The customer must be subscribed for this change to apply.')); + $this->getWarningMessageAdminHtmlSession($helper); } } else { //save frontend groupdata when customer is not subscribed. @@ -1091,4 +1091,13 @@ protected function getRequestActionName() { return $this->getRequest()->getActionName(); } + + /** + * @param $helper + * @return mixed + */ + protected function getWarningMessageAdminHtmlSession($helper) + { + return Mage::getSingleton('adminhtml/session')->addWarning($helper->__('The customer must be subscribed for this change to apply.')); + } } From f093c0451c04e5f9fa841e9ddb054167a74f0e7a Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Mon, 11 Feb 2019 15:52:45 -0300 Subject: [PATCH 110/113] closes #826 removed the condition addColumnConfig in the if of function addOrderViewMonkey --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 8a31b8781..c7de0cdf1 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -464,10 +464,9 @@ public function addOrderViewMonkey(Varien_Event_Observer $observer) $order = $block->getOrder(); $storeId = $order->getStoreId(); $helper = $this->makeHelper(); - $addColumnConfig = $helper->getMonkeyInGrid($storeId); $ecommEnabled = $helper->isEcomSyncDataEnabled($storeId); - if ($ecommEnabled && $addColumnConfig) { + if ($ecommEnabled) { $transport = $observer->getTransport(); if ($transport) { $html = $transport->getHtml(); From fc306543d24132bf4ef7d5aba7440cc5c66e8e66 Mon Sep 17 00:00:00 2001 From: luciaebizmarts Date: Mon, 11 Feb 2019 15:53:25 -0300 Subject: [PATCH 111/113] closes #826 implemented the test for the function addOrderVidewMonkey --- .../MailChimp/Model/ObserverTest.php | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 3fa601a3b..b5123cddb 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -1057,5 +1057,59 @@ public function testHandleCustomerGroupsIsNotSubcribedFromFrontEnd() $mailchimpObserverMock->handleCustomerGroups($subscriberEmail, $params, $storeId, $customerId); } + + public function testAddOrderViewMonkey() + { + $html = ''; + $storeId = 1; + + $mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->setMethods(array('makeHelper')) + -> getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->setMethods(array('isEcomSyncDataEnabled')) + ->getMock(); + + $observerMock = $this->getMockBuilder(Varien_Event_Observer::class) + ->setMethods(array('getBlock', 'getTransport')) + ->getMock(); + + $blockMock = $this->getMockBuilder(Mage_Core_Block_Abstract::class) + ->setMethods(array('getNameInLayout', 'getOrder', 'getChild')) + ->getMock(); + + $transportMock = $this->getMockBuilder(Varien_Event::class) + ->setMethods(array('getHtml', 'setHtml')) + ->getMock(); + + $orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) + ->setMethods(array('getStoreId')) + ->getMock(); + + $childMock = $this->getMockBuilder(Mage_Core_Helper_String::class) + ->setMethods(array('toHtml')) + ->getMock(); + + $mailchimpObserverMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); + + $helperMock->expects($this->once())->method('isEcomSyncDataEnabled')->with($storeId)->willReturn(true); + + $observerMock->expects($this->once())->method('getBlock')->willReturn($blockMock); + $observerMock->expects($this->once())->method('getTransport')->willReturn($transportMock); + + $blockMock->expects($this->once())->method('getNameInLayout')->willReturn('order_info'); + $blockMock->expects($this->once())->method('getOrder')->willReturn($orderMock); + $blockMock->expects($this->once())->method('getChild')->with('mailchimp.order.info.monkey.block')->willReturn($childMock); + + $transportMock->expects($this->once())->method('getHtml')->willReturn($html); + $transportMock->expects($this->once())->method('setHtml')->with($html); + + $orderMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + + $childMock->expects($this->once())->method('toHtml')->willReturn($html); + + $mailchimpObserverMock->addOrderViewMonkey($observerMock); + } } From 48492cf2ce7833ac10e728979588be85a63d12f2 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 18 Feb 2019 17:25:51 -0300 Subject: [PATCH 112/113] Update changelog. --- CHANGELOG.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35f31ac04..494601e9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,27 @@ # Change Log -## [1.1.14](https://github.com/mailchimp/mc-magento/tree/1.1.14) (2019-01-16) +## [1.1.15](https://github.com/mailchimp/mc-magento/tree/1.1.15) (2019-02-18) + +[Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.14...1.1.15) + +**Implemented enhancements:** + +- Catch exception if mandrill api is not available [\#859](https://github.com/mailchimp/mc-magento/issues/859) +- Add subscription option on order success page [\#770](https://github.com/mailchimp/mc-magento/issues/770) +**Fixed bugs:** + +- Subscribers status doesn't change to subscribed if double opt-in is activated using Magento email through Mandrill [\#874](https://github.com/mailchimp/mc-magento/issues/874) +- Multiple confirmation email from Mailchimp after group subscription [\#873](https://github.com/mailchimp/mc-magento/issues/873) +- Undefined variable: acl [\#871](https://github.com/mailchimp/mc-magento/issues/871) +- Spelling error in order status sent to mailchimp [\#868](https://github.com/mailchimp/mc-magento/issues/868) +- Subscription fails when one store view is disabled with the API key in blank [\#867](https://github.com/mailchimp/mc-magento/issues/867) +- The program fails when set up the extension in one store view and disable another store view leaving the API key in blank [\#863](https://github.com/mailchimp/mc-magento/issues/863) +- Avoid real time calls to Mailchimp API in case it's down [\#862](https://github.com/mailchimp/mc-magento/issues/862) +- If connection ping fails for one store it cancels the entire process [\#846](https://github.com/mailchimp/mc-magento/issues/846) +- 1.1.12 "Display on order grid" also hides Ebizmarts\_MailChimp\_Block\_Adminhtml\_Sales\_Order\_View\_Info\_Monkey [\#826](https://github.com/mailchimp/mc-magento/issues/826) + +## [1.1.14](https://github.com/mailchimp/mc-magento/tree/1.1.14) (2019-01-16) [Full Changelog](https://github.com/mailchimp/mc-magento/compare/1.1.13...1.1.14) **Implemented enhancements:** @@ -11,6 +31,8 @@ **Fixed bugs:** +- Flag parent as modified when child product is modified [\#848](https://github.com/mailchimp/mc-magento/issues/848) +- Change modified abandoned carts sending method from DELETE -\> POST to PATCH [\#836](https://github.com/mailchimp/mc-magento/issues/836) - Orders belonging to deleted stores do not show correct syncing status under "synced to MailChimp" column [\#840](https://github.com/mailchimp/mc-magento/issues/840) ## [1.1.13](https://github.com/mailchimp/mc-magento/tree/1.1.13) (2018-12-11) @@ -18,6 +40,8 @@ **Implemented enhancements:** +- Add option to send unresized product images to Mailchimp [\#834](https://github.com/mailchimp/mc-magento/issues/834) +- Optimize deletion of processed webhooks [\#832](https://github.com/mailchimp/mc-magento/issues/832) - Send subscription confirmation email via Magento [\#793](https://github.com/mailchimp/mc-magento/issues/793) - Order confirmation email is bypassing Aschroder\_SMTPPro [\#673](https://github.com/mailchimp/mc-magento/issues/673) - Add support for List Groups [\#514](https://github.com/mailchimp/mc-magento/issues/514) @@ -452,4 +476,4 @@ ## [1.0.0](https://github.com/mailchimp/mc-magento/tree/1.0.0) (2016-06-06) -\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* \ No newline at end of file From 1c0c61daadf057387e21e76b1cd66d4ba8810b06 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 18 Feb 2019 17:49:05 -0300 Subject: [PATCH 113/113] Add new line. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 494601e9c..7ea2ac227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -476,4 +476,4 @@ ## [1.0.0](https://github.com/mailchimp/mc-magento/tree/1.0.0) (2016-06-06) -\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* \ No newline at end of file +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*