From ac764f7509df7553baef8f4f9b3fd4a18ae5de17 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 11:10:27 -0300 Subject: [PATCH 01/51] added checkbox and functionality to unsubscribe customer in creditmemo creation --- .../Sales/Order/Creditmemo/Unsubscribe.php | 14 +++++++++++ .../Ebizmarts/MailChimp/Model/Observer.php | 24 +++++++++++++++++++ .../Ebizmarts/MailChimp/etc/config.xml | 8 +++++++ .../default/layout/ebizmarts/mailchimp.xml | 5 ++++ .../order/creditmemo/create/unsubscribe.phtml | 4 ++++ 5 files changed, 55 insertions(+) create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php create mode 100644 app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php new file mode 100644 index 000000000..32685759f --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php @@ -0,0 +1,14 @@ +getEvent()->getCreditmemo(); + $order = $creditMemo->getOrder(); + $storeId = $order->getStoreId(); + $helper = $this->makeHelper(); + $mailchimpUnsubscribe = $this->getRequest()->getParam('mailchimp_unsubscribe'); + + if ($mailchimpUnsubscribe == "on") { + $email = $order->getCustomerEmail(); + $subscriberModel = $this->getSubscriberModel(); + $subscriber = $subscriberModel->loadByEmail($email); + $helper->unsubscribeMember($subscriber); + } + + return $observer; + } + /** * Set the products included in the credit memo to be updated on MailChimp on the next cron job run. * diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index 0693aa89c..262029466 100644 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -84,6 +84,14 @@ + + + + mailchimp/observer + createCreditmemo + + + diff --git a/app/design/adminhtml/default/default/layout/ebizmarts/mailchimp.xml b/app/design/adminhtml/default/default/layout/ebizmarts/mailchimp.xml index f8a81e0ec..574ec0414 100644 --- a/app/design/adminhtml/default/default/layout/ebizmarts/mailchimp.xml +++ b/app/design/adminhtml/default/default/layout/ebizmarts/mailchimp.xml @@ -58,6 +58,11 @@ + + + + + diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml new file mode 100644 index 000000000..85f904c8b --- /dev/null +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml @@ -0,0 +1,4 @@ +

+ + +

\ No newline at end of file From 091debeeb5b431b1ca440deabb5210740d073fcf Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 11:32:20 -0300 Subject: [PATCH 02/51] fixed whitespace phpcs error --- 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 5033a501c..77fa65abf 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -779,7 +779,7 @@ public function newCreditMemo(Varien_Event_Observer $observer) /** * If "unsubscribe" checkbox is checked, ubsubscribes the customer. - * + * * @param Varien_Event_Observer $observer * @return Varien_Event_Observer */ From 9b60bc881ec0834a93e5b725af60ad84cf9d39a6 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 14:12:01 -0300 Subject: [PATCH 03/51] test added --- .../MailChimp/Model/ObserverTest.php | 131 ++++++++++++++++++ 1 file changed, 131 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 d79b38299..af41f43cd 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -1771,6 +1771,137 @@ public function testNewCreditMemo() $mailchimpObserverMock->newCreditMemo($observerMock); } + public function testCreateCreditmemoUbsubscribe() + { + $customerEmail = 'customer@mailchimp.com'; + $mailchimpUnsubscribe = 'on'; + + $mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getRequest', 'getSubscriberModel', 'makeHelper')) + ->getMock(); + + $observerMock = $this->getMockBuilder(Varien_Event_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getEvent')) + ->getMock(); + + $eventObserverMock = $this->getMockBuilder(Varien_Event::class) + ->disableOriginalConstructor() + ->setMethods(array('getCreditmemo')) + ->getMock(); + + $creditMemoMock = $this->getMockBuilder(Mage_Sales_Model_Order_Creditmemo::class) + ->disableOriginalConstructor() + ->setMethods(array('getOrder', 'getAllItems')) + ->getMock(); + + $orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) + ->disableOriginalConstructor() + ->setMethods(array('getCustomerEmail')) + ->getMock(); + + $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParam')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByCustomer')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('unsubscribeMember')) + ->getMock(); + + $observerMock->expects($this->once())->method('getEvent')->willReturn($eventObserverMock); + $eventObserverMock->expects($this->once())->method('getCreditmemo')->willReturn($creditMemoMock); + $creditMemoMock->expects($this->once())->method('getOrder')->willReturn($orderMock); + $mailchimpObserverMock->expects($this->once())->method('getRequest')->willReturn($requestMock); + $mailchimpObserverMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); + $requestMock + ->expects($this->once()) + ->method('getParam') + ->with('mailchimp_unsubscribe') + ->willReturn($mailchimpUnsubscribe); + + // Inside $mailchimpUnsubscribe == "on" if: + $orderMock->expects($this->once())->method('getCustomerEmail')->willReturn($customerEmail); + $mailchimpObserverMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); + $subscriberMock + ->expects($this->once()) + ->method('loadByEmail') + ->with($customerEmail) + ->willReturnSelf(); + $helperMock->expects($this->once()) + ->method('unsubscribeMember') + ->with($subscriberMock) + ->willReturnSelf(); + + $mailchimpObserverMock->createCreditmemo($observerMock); + } + + public function testCreateCreditmemo() + { + $customerEmail = 'customer@mailchimp.com'; + $mailchimpUnsubscribe = ''; + + $mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getRequest', 'getSubscriberModel', 'makeHelper')) + ->getMock(); + + $observerMock = $this->getMockBuilder(Varien_Event_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getEvent')) + ->getMock(); + + $eventObserverMock = $this->getMockBuilder(Varien_Event::class) + ->disableOriginalConstructor() + ->setMethods(array('getCreditmemo')) + ->getMock(); + + $creditMemoMock = $this->getMockBuilder(Mage_Sales_Model_Order_Creditmemo::class) + ->disableOriginalConstructor() + ->setMethods(array('getOrder', 'getAllItems')) + ->getMock(); + + $orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) + ->disableOriginalConstructor() + ->setMethods(array('getCustomerEmail')) + ->getMock(); + + $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParam')) + ->getMock(); + + $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByCustomer')) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('unsubscribeMember')) + ->getMock(); + + $observerMock->expects($this->once())->method('getEvent')->willReturn($eventObserverMock); + $eventObserverMock->expects($this->once())->method('getCreditmemo')->willReturn($creditMemoMock); + $creditMemoMock->expects($this->once())->method('getOrder')->willReturn($orderMock); + $mailchimpObserverMock->expects($this->once())->method('getRequest')->willReturn($requestMock); + $mailchimpObserverMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); + $requestMock + ->expects($this->once()) + ->method('getParam') + ->with('mailchimp_unsubscribe') + ->willReturn($mailchimpUnsubscribe); + + $mailchimpObserverMock->createCreditmemo($observerMock); + } + public function testCancelCreditMemo() { $isBundle = false; From 2f1ce97db3aaf021fafd152e67a105306ed9d2aa Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 14:12:21 -0300 Subject: [PATCH 04/51] unused storeId removed --- 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 77fa65abf..00bdda2a9 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -787,7 +787,6 @@ public function createCreditmemo($observer) { $creditMemo = $observer->getEvent()->getCreditmemo(); $order = $creditMemo->getOrder(); - $storeId = $order->getStoreId(); $helper = $this->makeHelper(); $mailchimpUnsubscribe = $this->getRequest()->getParam('mailchimp_unsubscribe'); From ce6eef5f6bf37012d3f6da6539e72bd21f545289 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 14:25:57 -0300 Subject: [PATCH 05/51] test Fixed --- .../tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php | 2 +- 1 file changed, 1 insertion(+), 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 af41f43cd..093c18ac7 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -1808,7 +1808,7 @@ public function testCreateCreditmemoUbsubscribe() $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) ->disableOriginalConstructor() - ->setMethods(array('loadByCustomer')) + ->setMethods(array('loadByEmail')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) From 6af1ec0e03a7165101eb261fc6fbcfa19ad1110a Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 14:59:15 -0300 Subject: [PATCH 06/51] standards fixes --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 2 +- .../tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 00bdda2a9..afd0515ee 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -782,7 +782,7 @@ public function newCreditMemo(Varien_Event_Observer $observer) * * @param Varien_Event_Observer $observer * @return Varien_Event_Observer - */ + */ public function createCreditmemo($observer) { $creditMemo = $observer->getEvent()->getCreditmemo(); 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 093c18ac7..44ffe88d9 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -1827,7 +1827,7 @@ public function testCreateCreditmemoUbsubscribe() ->with('mailchimp_unsubscribe') ->willReturn($mailchimpUnsubscribe); - // Inside $mailchimpUnsubscribe == "on" if: + // Inside mailchimpUnsubscribe if: $orderMock->expects($this->once())->method('getCustomerEmail')->willReturn($customerEmail); $mailchimpObserverMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); $subscriberMock From 821d529c0876da3f850958c34273e2f761950bf8 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 15:06:30 -0300 Subject: [PATCH 07/51] standards fixes --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index afd0515ee..82903eb88 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -782,8 +782,8 @@ public function newCreditMemo(Varien_Event_Observer $observer) * * @param Varien_Event_Observer $observer * @return Varien_Event_Observer - */ - public function createCreditmemo($observer) + */ + public function createCreditmemo($observer) { $creditMemo = $observer->getEvent()->getCreditmemo(); $order = $creditMemo->getOrder(); From 298c3744e8a51108f74dfda96dbc097c38ac3bc3 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 15:13:21 -0300 Subject: [PATCH 08/51] standards fixes --- 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 82903eb88..a6381f141 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -789,7 +789,7 @@ public function createCreditmemo($observer) $order = $creditMemo->getOrder(); $helper = $this->makeHelper(); $mailchimpUnsubscribe = $this->getRequest()->getParam('mailchimp_unsubscribe'); - + if ($mailchimpUnsubscribe == "on") { $email = $order->getCustomerEmail(); $subscriberModel = $this->getSubscriberModel(); From 351621e9ed93f80d2442b19879a8bcbedec60a77 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 15:39:34 -0300 Subject: [PATCH 09/51] test Fixed --- .../mailchimp/sales/order/creditmemo/create/unsubscribe.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml index 85f904c8b..1119a0737 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml @@ -1,4 +1,4 @@

-

\ No newline at end of file +

From 2713d36f587d9656271aa9847ed971eb1287a8b4 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 18 Mar 2020 15:46:19 -0300 Subject: [PATCH 10/51] test Fixed --- .../Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php index 32685759f..cd1e2f090 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php @@ -11,4 +11,4 @@ protected function _toHtml() { return parent::_toHtml(); } -} \ No newline at end of file +} From 64f7d44da7e4d20dcd0bf32fa8ee942a3dfc836c Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Thu, 19 Mar 2020 10:17:24 -0300 Subject: [PATCH 11/51] Request Changes done --- .../Ebizmarts/MailChimp/Model/Observer.php | 14 +- .../MailChimp/Model/ObserverTest.php | 201 +++++++++--------- 2 files changed, 114 insertions(+), 101 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index a6381f141..744ccda64 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -790,7 +790,7 @@ public function createCreditmemo($observer) $helper = $this->makeHelper(); $mailchimpUnsubscribe = $this->getRequest()->getParam('mailchimp_unsubscribe'); - if ($mailchimpUnsubscribe == "on") { + if ($this->isUnsubscribeChecked($mailchimpUnsubscribe)) { $email = $order->getCustomerEmail(); $subscriberModel = $this->getSubscriberModel(); $subscriber = $subscriberModel->loadByEmail($email); @@ -1209,6 +1209,18 @@ public function handleCustomerGroups($subscriberEmail, $params, $storeId, $custo return $subscriber; } + /** + * @return boolean + */ + protected function isUnsubscribeChecked($mailchimpUnsubscribe) + { + if ($mailchimpUnsubscribe === 'on') { + return true; + } + + return false; + } + /** * @return mixed */ 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 44ffe88d9..bc5631fb8 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -2,9 +2,58 @@ class Ebizmarts_MailChimp_Model_ObserverTest extends PHPUnit_Framework_TestCase { + protected $_mailchimpObserverMock; + protected $_observerMock; + protected $_eventObserverMock; + protected $_creditMemoMock; + protected $_orderMock; + protected $_requestMock; + protected $_subscriberMock; + protected $_helperMock; + public function setUp() { Mage::app('default'); + + $this->_mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getRequest', 'getSubscriberModel', 'makeHelper')) + ->getMock(); + + $this->_observerMock = $this->getMockBuilder(Varien_Event_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getEvent')) + ->getMock(); + + $this->_eventObserverMock = $this->getMockBuilder(Varien_Event::class) + ->disableOriginalConstructor() + ->setMethods(array('getCreditmemo')) + ->getMock(); + + $this->_creditMemoMock = $this->getMockBuilder(Mage_Sales_Model_Order_Creditmemo::class) + ->disableOriginalConstructor() + ->setMethods(array('getOrder', 'getAllItems')) + ->getMock(); + + $this->_orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) + ->disableOriginalConstructor() + ->setMethods(array('getCustomerEmail')) + ->getMock(); + + $this->_requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParam')) + ->getMock(); + + $this->_subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByEmail')) + ->getMock(); + + $this->_helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('unsubscribeMember')) + ->getMock(); } public function testProductAttributeUpdateIsUsingCorrectStoreId() @@ -1776,130 +1825,82 @@ public function testCreateCreditmemoUbsubscribe() $customerEmail = 'customer@mailchimp.com'; $mailchimpUnsubscribe = 'on'; - $mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) - ->disableOriginalConstructor() - ->setMethods(array('getRequest', 'getSubscriberModel', 'makeHelper')) - ->getMock(); - - $observerMock = $this->getMockBuilder(Varien_Event_Observer::class) - ->disableOriginalConstructor() - ->setMethods(array('getEvent')) - ->getMock(); - - $eventObserverMock = $this->getMockBuilder(Varien_Event::class) - ->disableOriginalConstructor() - ->setMethods(array('getCreditmemo')) - ->getMock(); - - $creditMemoMock = $this->getMockBuilder(Mage_Sales_Model_Order_Creditmemo::class) - ->disableOriginalConstructor() - ->setMethods(array('getOrder', 'getAllItems')) - ->getMock(); - - $orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) - ->disableOriginalConstructor() - ->setMethods(array('getCustomerEmail')) - ->getMock(); - - $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) - ->disableOriginalConstructor() - ->setMethods(array('getParam')) - ->getMock(); - - $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) - ->disableOriginalConstructor() - ->setMethods(array('loadByEmail')) - ->getMock(); - - $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->disableOriginalConstructor() - ->setMethods(array('unsubscribeMember')) - ->getMock(); - - $observerMock->expects($this->once())->method('getEvent')->willReturn($eventObserverMock); - $eventObserverMock->expects($this->once())->method('getCreditmemo')->willReturn($creditMemoMock); - $creditMemoMock->expects($this->once())->method('getOrder')->willReturn($orderMock); - $mailchimpObserverMock->expects($this->once())->method('getRequest')->willReturn($requestMock); - $mailchimpObserverMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); - $requestMock + $this->_observerMock + ->expects($this->once()) + ->method('getEvent') + ->willReturn($this->_eventObserverMock); + $this->_eventObserverMock + ->expects($this->once()) + ->method('getCreditmemo') + ->willReturn($this->_creditMemoMock); + $this->_creditMemoMock + ->expects($this->once()) + ->method('getOrder') + ->willReturn($this->_orderMock); + $this->_mailchimpObserverMock + ->expects($this->once()) + ->method('getRequest') + ->willReturn($this->_requestMock); + $this->_mailchimpObserverMock + ->expects($this->once()) + ->method('makeHelper') + ->willReturn($this->_helperMock); + $this->_requestMock ->expects($this->once()) ->method('getParam') ->with('mailchimp_unsubscribe') ->willReturn($mailchimpUnsubscribe); // Inside mailchimpUnsubscribe if: - $orderMock->expects($this->once())->method('getCustomerEmail')->willReturn($customerEmail); - $mailchimpObserverMock->expects($this->once())->method('getSubscriberModel')->willReturn($subscriberMock); - $subscriberMock + $this->_orderMock->expects($this->once())->method('getCustomerEmail')->willReturn($customerEmail); + $this->_mailchimpObserverMock + ->expects($this->once()) + ->method('getSubscriberModel') + ->willReturn($this->_subscriberMock); + $this->_subscriberMock ->expects($this->once()) ->method('loadByEmail') ->with($customerEmail) ->willReturnSelf(); - $helperMock->expects($this->once()) + $this->_helperMock->expects($this->once()) ->method('unsubscribeMember') - ->with($subscriberMock) + ->with($this->_subscriberMock) ->willReturnSelf(); - $mailchimpObserverMock->createCreditmemo($observerMock); + $this->_mailchimpObserverMock->createCreditmemo($this->_observerMock); } public function testCreateCreditmemo() { - $customerEmail = 'customer@mailchimp.com'; $mailchimpUnsubscribe = ''; - $mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) - ->disableOriginalConstructor() - ->setMethods(array('getRequest', 'getSubscriberModel', 'makeHelper')) - ->getMock(); - - $observerMock = $this->getMockBuilder(Varien_Event_Observer::class) - ->disableOriginalConstructor() - ->setMethods(array('getEvent')) - ->getMock(); - - $eventObserverMock = $this->getMockBuilder(Varien_Event::class) - ->disableOriginalConstructor() - ->setMethods(array('getCreditmemo')) - ->getMock(); - - $creditMemoMock = $this->getMockBuilder(Mage_Sales_Model_Order_Creditmemo::class) - ->disableOriginalConstructor() - ->setMethods(array('getOrder', 'getAllItems')) - ->getMock(); - - $orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) - ->disableOriginalConstructor() - ->setMethods(array('getCustomerEmail')) - ->getMock(); - - $requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) - ->disableOriginalConstructor() - ->setMethods(array('getParam')) - ->getMock(); - - $subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) - ->disableOriginalConstructor() - ->setMethods(array('loadByCustomer')) - ->getMock(); - - $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->disableOriginalConstructor() - ->setMethods(array('unsubscribeMember')) - ->getMock(); - - $observerMock->expects($this->once())->method('getEvent')->willReturn($eventObserverMock); - $eventObserverMock->expects($this->once())->method('getCreditmemo')->willReturn($creditMemoMock); - $creditMemoMock->expects($this->once())->method('getOrder')->willReturn($orderMock); - $mailchimpObserverMock->expects($this->once())->method('getRequest')->willReturn($requestMock); - $mailchimpObserverMock->expects($this->once())->method('makeHelper')->willReturn($helperMock); - $requestMock + $this->_observerMock + ->expects($this->once()) + ->method('getEvent') + ->willReturn($this->_eventObserverMock); + $this->_eventObserverMock + ->expects($this->once()) + ->method('getCreditmemo') + ->willReturn($this->_creditMemoMock); + $this->_creditMemoMock + ->expects($this->once()) + ->method('getOrder') + ->willReturn($this->_orderMock); + $this->_mailchimpObserverMock + ->expects($this->once()) + ->method('getRequest') + ->willReturn($this->_requestMock); + $this->_mailchimpObserverMock + ->expects($this->once()) + ->method('makeHelper') + ->willReturn($this->_helperMock); + $this->_requestMock ->expects($this->once()) ->method('getParam') ->with('mailchimp_unsubscribe') ->willReturn($mailchimpUnsubscribe); - $mailchimpObserverMock->createCreditmemo($observerMock); + $this->_mailchimpObserverMock->createCreditmemo($this->_observerMock); } public function testCancelCreditMemo() From 2c5ddb1fdebd70fa90303e84b04445452a8900d8 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Thu, 19 Mar 2020 10:22:36 -0300 Subject: [PATCH 12/51] Request Changes done --- .../Ebizmarts/MailChimp/Model/Observer.php | 7 ++++--- .../Ebizmarts/MailChimp/Model/ObserverTest.php | 15 ++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 744ccda64..a585933ea 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -785,13 +785,14 @@ public function newCreditMemo(Varien_Event_Observer $observer) */ public function createCreditmemo($observer) { - $creditMemo = $observer->getEvent()->getCreditmemo(); - $order = $creditMemo->getOrder(); - $helper = $this->makeHelper(); $mailchimpUnsubscribe = $this->getRequest()->getParam('mailchimp_unsubscribe'); if ($this->isUnsubscribeChecked($mailchimpUnsubscribe)) { + $creditMemo = $observer->getEvent()->getCreditmemo(); + $helper = $this->makeHelper(); + $order = $creditMemo->getOrder(); $email = $order->getCustomerEmail(); + $subscriberModel = $this->getSubscriberModel(); $subscriber = $subscriberModel->loadByEmail($email); $helper->unsubscribeMember($subscriber); 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 bc5631fb8..3bca8eec9 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -1875,25 +1875,26 @@ public function testCreateCreditmemo() $mailchimpUnsubscribe = ''; $this->_observerMock - ->expects($this->once()) + ->expects($this->never()) ->method('getEvent') ->willReturn($this->_eventObserverMock); $this->_eventObserverMock - ->expects($this->once()) + ->expects($this->never()) ->method('getCreditmemo') ->willReturn($this->_creditMemoMock); $this->_creditMemoMock - ->expects($this->once()) + ->expects($this->never()) ->method('getOrder') ->willReturn($this->_orderMock); + $this->_mailchimpObserverMock + ->expects($this->never()) + ->method('makeHelper') + ->willReturn($this->_helperMock); + $this->_mailchimpObserverMock ->expects($this->once()) ->method('getRequest') ->willReturn($this->_requestMock); - $this->_mailchimpObserverMock - ->expects($this->once()) - ->method('makeHelper') - ->willReturn($this->_helperMock); $this->_requestMock ->expects($this->once()) ->method('getParam') From c4b24ad89c50dee94fe053d24a9f70ddb43df4be Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Thu, 19 Mar 2020 10:31:22 -0300 Subject: [PATCH 13/51] Request Changes done --- 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 a585933ea..d1efd8b2c 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -792,7 +792,7 @@ public function createCreditmemo($observer) $helper = $this->makeHelper(); $order = $creditMemo->getOrder(); $email = $order->getCustomerEmail(); - + $subscriberModel = $this->getSubscriberModel(); $subscriber = $subscriberModel->loadByEmail($email); $helper->unsubscribeMember($subscriber); From 37a47f228c239bb2b429138d110da9d48b176752 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Thu, 19 Mar 2020 10:54:47 -0300 Subject: [PATCH 14/51] standards fix --- 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 d1efd8b2c..36400ef9a 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -792,7 +792,6 @@ public function createCreditmemo($observer) $helper = $this->makeHelper(); $order = $creditMemo->getOrder(); $email = $order->getCustomerEmail(); - $subscriberModel = $this->getSubscriberModel(); $subscriber = $subscriberModel->loadByEmail($email); $helper->unsubscribeMember($subscriber); From 8a6c6067b489b9b8a9afd6a8aff9bd6b4835e24c Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 23 Mar 2020 08:57:56 -0300 Subject: [PATCH 15/51] Adding Helper and moving methods related to webhooks --- .../Ebizmarts/MailChimp/Helper/Data.php | 234 --------------- .../Ebizmarts/MailChimp/Helper/Webhook.php | 275 ++++++++++++++++++ .../Model/Api/Subscribers/MailchimpTags.php | 26 +- .../Model/System/Config/Backend/Active.php | 11 +- .../Adminhtml/MailchimpController.php | 18 +- .../controllers/WebhookController.php | 19 +- .../mysql4-upgrade-1.1.19-1.1.20.php | 61 ++++ .../MailChimp/Helper/WebhookTest.php | 9 + lib/Ebizmarts/MailChimp/ListsWebhooks.php | 23 ++ 9 files changed, 436 insertions(+), 240 deletions(-) create mode 100644 app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php create mode 100644 app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php create mode 100644 dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index a1a1e5083..b709c2184 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -389,21 +389,6 @@ public function encryptData($data) return Mage::helper('core')->encrypt($data); } - /** - * @param $scopeId - * @param null $scope - * @return mixed - * @throws Mage_Core_Exception - */ - public function getWebhookDeleteAction($scopeId, $scope = null) - { - return $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_UNSUBSCRIBE, - $scopeId, - $scope - ); - } - /** * Get local store_id value of the MC store for given scope. * @@ -704,23 +689,6 @@ public function getTwoWaySyncEnabled($scopeId = 0, $scope = null) ); } - /** - * Get webhook Id. - * - * @param int $scopeId - * @param null $scope - * @return mixed - * @throws Mage_Core_Exception - */ - public function getWebhookId($scopeId = 0, $scope = null) - { - return $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, - $scopeId, - $scope - ); - } - /** * Get if monkey should be displayed in order grid. * @@ -1038,17 +1006,6 @@ public function isErrorLogEnabled() return $logEnabled; } - /** - * @return string - */ - public function getWebhooksKey() - { - $crypt = hash('md5', (string)$this->getConfig()->getNode('global/crypt/key')); - $key = substr($crypt, 0, (strlen($crypt) / 2)); - - return $key; - } - /** * Reset error messages from Products, Subscribers, Customers, Orders, Quotes * and set them to be sent again for given scope. @@ -2756,197 +2713,6 @@ public function loadListCustomer($listId, $email) return $customer; } - /** - * @param $scopeId - * @param string $scope - */ - public function handleWebhookChange($scopeId, $scope = 'stores') - { - $webhookScope = $this->getRealScopeForConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, - $scopeId, - $scope - ); - $listId = $this->getGeneralList($scopeId, $scope); - $this->deleteCurrentWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); - - if ($this->isSubscriptionEnabled($scopeId, $scope)) { - $this->createNewWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); - } - } - - /** - * @param $scopeId - * @param $scope - * @param $listId - * @throws Mage_Core_Exception - */ - protected function deleteCurrentWebhook($scopeId, $scope, $listId) - { - try { - $api = $this->getApi($scopeId, $scope); - $webhookId = $this->getWebhookId($scopeId, $scope); - $apiKey = $this->getApiKey($scopeId, $scope); - - if ($webhookId && $apiKey && $listId) { - try { - $api->getLists()->getWebhooks()->delete($listId, $webhookId); - } catch (MailChimp_Error $e) { - $this->logError($e->getFriendlyMessage()); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - - $this->getConfig() - ->deleteConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, $scope, $scopeId); - } else { - $webhookUrl = $this->getWebhookUrl($scopeId, $scope); - try { - if ($listId) { - $this->_deletedWebhooksByListId($api, $listId, $webhookUrl); - } - } catch (MailChimp_Error $e) { - $this->logError($e->getFriendlyMessage()); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { - $this->logError($e->getMessage()); - } - } - - protected function _deletedWebhooksByListId($api, $listId, $webhookUrl) - { - $webhooks = $api->getLists()->getWebhooks()->getAll($listId); - - foreach ($webhooks['webhooks'] as $webhook) { - if (strpos($webhook['url'], $webhookUrl) !== false) { - $this->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); - } - } - } - - /** - * @param $apiWebhook - * @param $listId - * @param $webhookId - */ - public function deleteWebhookFromList($apiWebhook, $listId, $webhookId) - { - $apiWebhook->delete($listId, $webhookId); - } - - /** - * Returns true on successful creation, or error message if it fails - */ - public function createNewWebhook($scopeId, $scope, $listId) - { - $hookUrl = $this->getWebhookUrl(); - - try { - $api = $this->getApi($scopeId, $scope); - - if ($this->getTwoWaySyncEnabled($scopeId, $scope)) { - $events = array( - 'subscribe' => true, - 'unsubscribe' => true, - 'profile' => false, - 'cleaned' => true, - 'upemail' => true, - 'campaign' => false - ); - $sources = array( - 'user' => true, - 'admin' => true, - 'api' => true - ); - } else { - $events = array( - 'subscribe' => true, - 'unsubscribe' => true, - 'profile' => false, - 'cleaned' => false, - 'upemail' => false, - 'campaign' => false - ); - $sources = array( - 'user' => true, - 'admin' => true, - 'api' => false - ); - } - - try { - $response = $api->getLists()->getWebhooks()->getAll($listId); - $createWebhook = true; - - if (isset($response['total_items']) && $response['total_items'] > 0) { - foreach ($response['webhooks'] as $webhook) { - if ($webhook['url'] == $hookUrl) { - $createWebhook = false; - break; - } - } - } - - if ($createWebhook) { - $newWebhook = $api->getLists()->getWebhooks()->add($listId, $hookUrl, $events, $sources); - $newWebhookId = $newWebhook['id']; - $configValues = array(array(Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, $newWebhookId)); - $this->saveMailchimpConfig($configValues, $scopeId, $scope); - - return true; - } else { - return $this->__('The webhook already exists.'); - } - } catch (MailChimp_Error $e) { - $errorMessage = $e->getFriendlyMessage(); - $this->logError($errorMessage); - $textToCompare = 'The resource submitted could not be validated. ' - . 'For field-specific details, see the \'errors\' array.'; - - if ($e->getMailchimpDetails() == $textToCompare) { - $errorMessage = 'Your store could not be accessed by MailChimp\'s Api. ' - . 'Please confirm the URL: ' . $hookUrl - . ' is accessible externally to allow the webhook creation.'; - $this->logError($errorMessage); - } - - return $this->__($errorMessage); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { - $this->logError($e->getMessage()); - } - } - - /** - * @return string - */ - protected function getWebhookUrl() - { - $store = $this->getMageApp()->getDefaultStoreView(); - $webhooksKey = $this->getWebhooksKey(); - //Generating Webhooks URL - $url = Ebizmarts_MailChimp_Model_ProcessWebhook::WEBHOOKS_PATH; - $hookUrl = $store->getUrl( - $url, - array( - 'wkey' => $webhooksKey, - '_nosid' => true, - '_secure' => true, - ) - ); - - if (false != strstr($hookUrl, '?', true)) { - $hookUrl = strstr($hookUrl, '?', true); - } - - return $hookUrl; - } - /** * @param $path * @param $scopeId diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php new file mode 100644 index 000000000..cd2913a52 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php @@ -0,0 +1,275 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/20/2020 11:14 AM + * @file: Webhook.php + */ +class Ebizmarts_MailChimp_Helper_Data extends Mage_Core_Helper_Abstract +{ + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected $_helper; + + public function __construct() + { + $this->_helper = Mage::getHelper('mailchimp'); + } + + /** + * @param $scopeId + * @param null $scope + * @return mixed + * @throws Mage_Core_Exception + */ + public function getWebhookDeleteAction($scopeId, $scope = null) + { + $helper = $this->getHelper(); + return $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_UNSUBSCRIBE, + $scopeId, + $scope + ); + } + + /** + * Get webhook Id. + * + * @param int $scopeId + * @param null $scope + * @return mixed + * @throws Mage_Core_Exception + */ + public function getWebhookId($scopeId = 0, $scope = null) + { + $helper = $this->getHelper(); + return $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, + $scopeId, + $scope + ); + } + + /** + * @return string + */ + public function getWebhooksKey() + { + $helper = $this->getHelper(); + $crypt = hash('md5', (string)$helper->getConfig()->getNode('global/crypt/key')); + $key = substr($crypt, 0, (strlen($crypt) / 2)); + + return $key; + } + + /** + * @param $scopeId + * @param string $scope + */ + public function handleWebhookChange($scopeId, $scope = 'stores') + { + $helper = $this->getHelper(); + $webhookScope = $helper->getRealScopeForConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, + $scopeId, + $scope + ); + $listId = $helper->getGeneralList($scopeId, $scope); + $helper->deleteCurrentWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); + + if ($helper->isSubscriptionEnabled($scopeId, $scope)) { + $this->createNewWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); + } + } + + /** + * @param $scopeId + * @param $scope + * @param $listId + * @throws Mage_Core_Exception + */ + protected function deleteCurrentWebhook($scopeId, $scope, $listId) + { + $helper = $this->getHelper(); + + try { + $api = $helper->getApi($scopeId, $scope); + $webhookId = $this->getWebhookId($scopeId, $scope); + $apiKey = $helper->getApiKey($scopeId, $scope); + + if ($webhookId && $apiKey && $listId) { + try { + $api->getLists()->getWebhooks()->delete($listId, $webhookId); + } catch (MailChimp_Error $e) { + $helper->logError($e->getFriendlyMessage()); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + + $helper->getConfig() + ->deleteConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, $scope, $scopeId); + } else { + $webhookUrl = $this->getWebhookUrl($scopeId, $scope); + try { + if ($listId) { + $this->_deletedWebhooksByListId($api, $listId, $webhookUrl); + } + } catch (MailChimp_Error $e) { + $helper->logError($e->getFriendlyMessage()); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + } + } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { + $helper->logError($e->getMessage()); + } + } + + protected function _deletedWebhooksByListId($api, $listId, $webhookUrl) + { + $webhooks = $api->getLists()->getWebhooks()->getAll($listId); + + foreach ($webhooks['webhooks'] as $webhook) { + if (strpos($webhook['url'], $webhookUrl) !== false) { + $this->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); + } + } + } + + /** + * @param $apiWebhook + * @param $listId + * @param $webhookId + */ + public function deleteWebhookFromList($apiWebhook, $listId, $webhookId) + { + $apiWebhook->delete($listId, $webhookId); + } + + /** + * Returns true on successful creation, or error message if it fails + */ + public function createNewWebhook($scopeId, $scope, $listId) + { + //TODO: cambiar llamadas de este metodo + $helper = $this->getHelper(); + $hookUrl = $this->getWebhookUrl(); + + try { + $api = $helper->getApi($scopeId, $scope); + + if ($helper->getTwoWaySyncEnabled($scopeId, $scope)) { + $events = array( + 'subscribe' => true, + 'unsubscribe' => true, + 'profile' => true, + 'cleaned' => true, + 'upemail' => true, + 'campaign' => false + ); + $sources = array( + 'user' => true, + 'admin' => true, + 'api' => true + ); + } else { + $events = array( + 'subscribe' => true, + 'unsubscribe' => true, + 'profile' => true, + 'cleaned' => false, + 'upemail' => false, + 'campaign' => false + ); + $sources = array( + 'user' => true, + 'admin' => true, + 'api' => false + ); + } + + try { + $response = $api->getLists()->getWebhooks()->getAll($listId); + $createWebhook = true; + + if (isset($response['total_items']) && $response['total_items'] > 0) { + foreach ($response['webhooks'] as $webhook) { + if ($webhook['url'] == $hookUrl) { + $createWebhook = false; + break; + } + } + } + + if ($createWebhook) { + $newWebhook = $api->getLists()->getWebhooks()->add($listId, $hookUrl, $events, $sources); + $newWebhookId = $newWebhook['id']; + $configValues = array(array(Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, $newWebhookId)); + $helper->saveMailchimpConfig($configValues, $scopeId, $scope); + + return true; + } else { + return $this->__('The webhook already exists.'); + } + } catch (MailChimp_Error $e) { + $errorMessage = $e->getFriendlyMessage(); + $helper->logError($errorMessage); + $textToCompare = 'The resource submitted could not be validated. ' + . 'For field-specific details, see the \'errors\' array.'; + + if ($e->getMailchimpDetails() == $textToCompare) { + $errorMessage = 'Your store could not be accessed by MailChimp\'s Api. ' + . 'Please confirm the URL: ' . $hookUrl + . ' is accessible externally to allow the webhook creation.'; + $helper->logError($errorMessage); + } + + return $helper->__($errorMessage); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { + $helper->logError($e->getMessage()); + } + } + + /** + * @return string + */ + protected function getWebhookUrl() + { + $helper = $this->getHelper(); + $store = $helper->getMageApp()->getDefaultStoreView(); + $webhooksKey = $this->getWebhooksKey(); + //Generating Webhooks URL + $url = Ebizmarts_MailChimp_Model_ProcessWebhook::WEBHOOKS_PATH; + $hookUrl = $store->getUrl( + $url, + array( + 'wkey' => $webhooksKey, + '_nosid' => true, + '_secure' => true, + ) + ); + + if (false != strstr($hookUrl, '?', true)) { + $hookUrl = strstr($hookUrl, '?', true); + } + + return $hookUrl; + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + protected function getHelper() + { + return $this->_helper; + } +} \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php index dc6dc570e..af777e86d 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php @@ -38,6 +38,10 @@ class Ebizmarts_MailChimp_Model_Api_Subscribers_MailchimpTags * @var Ebizmarts_MailChimp_Helper_Date */ protected $_mcDateHelper; + /** + * @var Ebizmarts_MailChimp_Helper_Webhook + */ + protected $_mcWebhookHelper; /** * @var Mage_Sales_Model_Order */ @@ -52,6 +56,7 @@ public function __construct() { $this->setMailChimpHelper(); $this->setMailChimpDateHelper(); + $this->setMailChimpWebhookHelper(); $this->_interestGroupHandle = Mage::getModel('mailchimp/api_subscribers_InterestGroupHandle'); } @@ -272,7 +277,8 @@ public function processMergeFields($data, $subscribe = false) */ protected function _addSubscriberData($subscriber, $fname, $lname, $email, $listId) { - $helper = $this->getHelper(); + $helper = $this->getMailchimpHelper(); + $webhookHelper = $this->getMailchimpWebhookHelper(); $scopeArray = $helper->getFirstScopeFromConfig( Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, $listId @@ -293,7 +299,7 @@ protected function _addSubscriberData($subscriber, $fname, $lname, $email, $list if ($member['status'] == 'subscribed') { $helper->subscribeMember($subscriber); } else if ($member['status'] == 'unsubscribed') { - if (!$helper->getWebhookDeleteAction($subscriber->getStoreId())) { + if (!$webhookHelper->getWebhookDeleteAction($subscriber->getStoreId())) { $helper->unsubscribeMember($subscriber); } } @@ -784,6 +790,22 @@ protected function setMailChimpDateHelper() $this->_mcDateHelper = Mage::helper('mailchimp/date'); } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + public function getMailchimpWebhookHelper() + { + return $this->_mcWebhookHelper; + } + + /** + * @param $mageMCWebhookHelper + */ + protected function setMailChimpWebhookHelper() + { + $this->_mcWebhookHelper = Mage::helper('mailchimp/webhook'); + } + /** * @param $key * @return bool diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/Active.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/Active.php index c69624bc0..1e304d835 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/Active.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/Active.php @@ -16,6 +16,7 @@ class Ebizmarts_MailChimp_Model_System_Config_Backend_Active extends Mage_Core_M protected function _afterSave() { $helper = $this->makeHelper(); + $webhookHelper = $this->makeWebhookHelper(); $scopeId = $this->getScopeId(); $scope = $this->getScope(); $groups = $this->getData('groups'); @@ -32,7 +33,7 @@ protected function _afterSave() if ($this->isValueChanged() && $this->getValue()) { if ($apiKey && $listId) { - $helper->createNewWebhook($scopeId, $scope, $listId); + $webhookHelper->createNewWebhook($scopeId, $scope, $listId); } else { $configValue = array(array(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE, false)); $helper->saveMailchimpConfig($configValue, $scopeId, $scope); @@ -50,6 +51,14 @@ protected function makeHelper() return Mage::helper('mailchimp'); } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function makeWebhookHelper() + { + return Mage::helper('mailchimp/webhook'); + } + /** * @return Mage_Adminhtml_Model_Session */ diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 6b27da40b..8ff6c81cf 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -15,10 +15,12 @@ class Ebizmarts_MailChimp_Adminhtml_MailchimpController extends Mage_Adminhtml_C { protected $_helper; + protected $_webhookHelper; public function preDispatch() { $this->_helper = $this->makeHelper(); + $this->_helper = $this->makeWebhookHelper(); return parent::preDispatch(); } @@ -60,13 +62,14 @@ public function resendSubscribersAction() public function createWebhookAction() { $helper = $this->getHelper(); + $webhookHelper = $this->getWebhookHelper(); $mageApp = $helper->getMageApp(); $request = $mageApp->getRequest(); $scope = $request->getParam('scope'); $scopeId = $request->getParam('scope_id'); $listId = $helper->getGeneralList($scopeId); - $message = $helper->createNewWebhook($scopeId, $scope, $listId); + $message = $webhookHelper->createNewWebhook($scopeId, $scope, $listId); $mageApp->getResponse()->setBody($message); } @@ -204,6 +207,19 @@ protected function getHelper() return $this->_helper; } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function makeWebhookHelper() + { + return Mage::helper('mailchimp/webhook'); + } + + protected function getWebhookHelper() + { + return $this->_webhookHelper; + } + /** * @param $block * @return mixed diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.php b/app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.php index dd743ebdf..4bb67f85a 100755 --- a/app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.php @@ -13,6 +13,7 @@ class Ebizmarts_MailChimp_WebhookController extends Mage_Core_Controller_Front_Action { protected $_mailchimpHelper = null; + protected $_mailchimpWebhookHelper = null; /** * @return Ebizmarts_MailChimp_Helper_Data|Mage_Core_Helper_Abstract @@ -26,6 +27,18 @@ protected function getHelper() return $this->_mailchimpHelper; } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function getWebhookHelper() + { + if (!$this->_mailchimpWebhookHelper) { + $this->_mailchimpWebhookHelper = Mage::helper('mailchimp/webhook'); + } + + return $this->_mailchimpWebhookHelper; + } + /** * Entry point for all webhook operations */ @@ -36,6 +49,7 @@ public function indexAction() $moduleName = $request->getModuleName(); $data = $request->getPost(); $helper = $this->getHelper(); + $webhookHelper = $this->getWebhookHelper(); if ($moduleName == 'monkey') { if (isset($data['data']['list_id'])) { @@ -60,7 +74,7 @@ public function indexAction() return $this; } - $myKey = $helper->getWebhooksKey(); + $myKey = $webhookHelper->getWebhooksKey(); //Validate "wkey" GET parameter if ($myKey == $requestKey) { @@ -84,6 +98,7 @@ public function indexAction() protected function _deleteWebhook($storeId, $listId) { $helper = $this->getHelper(); + $webhookHelper = $this->getWebhookHelper(); try { $api = $helper->getApi($storeId); @@ -97,7 +112,7 @@ protected function _deleteWebhook($storeId, $listId) $webhooks = $api->getLists()->getWebhooks()->getAll($listId); foreach ($webhooks['webhooks'] as $webhook) { if (strpos($webhook['url'], 'monkey/webhook') !== false) { - $helper->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); + $webhookHelper->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); } } } catch (MailChimp_Error $e) { diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php new file mode 100644 index 000000000..a91ecd3a9 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php @@ -0,0 +1,61 @@ +run( + "ALTER TABLE `{$this->getTable('mailchimp_stores')}` + CHANGE COLUMN `apikey` `apikey` VARCHAR(128) NOT NULL;" + ); + + $installer->run( + "TRUNCATE `{$this->getTable('mailchimp_stores')}`;" + ); + + $configDataCollection = Mage::getModel('core/config_data') + ->getCollection() + ->addFieldToFilter('path', 'mailchimp/general/apikey'); + + $mailchimpShards = array('us'); + foreach ($configDataCollection as $data) { + $dbApiKey = $data->getValue(); + foreach ($mailchimpShards as $shard) { + if (strpos($dbApiKey, "-$shard") !== false) { + list($hash, $server) = explode("-$shard", $dbApiKey); + if (is_numeric($server) && strlen($hash) === 32) { + $encryptedApiKey = Mage::helper('core')->encrypt($dbApiKey); + $installer->setConfigData( + 'mailchimp/general/apikey', + $encryptedApiKey, + $data->getScope(), + $data->getScopeId() + ); + } + } + } + } + + /* Mandrill migration */ + $configDataCollection = Mage::getModel('core/config_data') + ->getCollection() + ->addFieldToFilter('path', 'mandrill/general/apikey'); + + foreach ($configDataCollection as $data) { + $dbApiKey = $data->getValue(); + if (strlen($dbApiKey) == 22) { + $encryptedApiKey = Mage::helper('core')->encrypt($dbApiKey); + $installer->setConfigData( + 'mandrill/general/apikey', + $encryptedApiKey, + $data->getScope(), + $data->getScopeId() + ); + } + } +} catch (Exception $e) { + Mage::log($e->getMessage(), null, 'MailChimp_Errors.log', true); +} + + +$installer->endSetup(); diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php new file mode 100644 index 000000000..c1de0035a --- /dev/null +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php @@ -0,0 +1,9 @@ +_master->call('lists/' . $listId . '/webhooks', $_params, Ebizmarts_MailChimp::POST); } + public function edit($listId, $wrbhookId, $url = null, $events = null, $sources = null) + { + $_params = array(); + + if ($url) { + $_params['url'] = $url; + } + + if ($events) { + $_params['events'] = $events; + } + + if ($sources) { + $_params['sources'] = $sources; + } + + return $this->_master->call( + 'lists/' . $listId . '/webhooks/' . $wrbhookId, + $_params, + Ebizmarts_MailChimp::PATCH + ); + } + /** * @param $listId * @return mixed From d67bcaa11f79fbcfb1b9d87d9109f9a526acad47 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 23 Mar 2020 09:40:33 -0300 Subject: [PATCH 16/51] Removed mock instances from setup --- .../MailChimp/Model/ObserverTest.php | 192 +++++++++++------- 1 file changed, 113 insertions(+), 79 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 3bca8eec9..29d545f75 100755 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -2,58 +2,9 @@ class Ebizmarts_MailChimp_Model_ObserverTest extends PHPUnit_Framework_TestCase { - protected $_mailchimpObserverMock; - protected $_observerMock; - protected $_eventObserverMock; - protected $_creditMemoMock; - protected $_orderMock; - protected $_requestMock; - protected $_subscriberMock; - protected $_helperMock; - public function setUp() { Mage::app('default'); - - $this->_mailchimpObserverMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) - ->disableOriginalConstructor() - ->setMethods(array('getRequest', 'getSubscriberModel', 'makeHelper')) - ->getMock(); - - $this->_observerMock = $this->getMockBuilder(Varien_Event_Observer::class) - ->disableOriginalConstructor() - ->setMethods(array('getEvent')) - ->getMock(); - - $this->_eventObserverMock = $this->getMockBuilder(Varien_Event::class) - ->disableOriginalConstructor() - ->setMethods(array('getCreditmemo')) - ->getMock(); - - $this->_creditMemoMock = $this->getMockBuilder(Mage_Sales_Model_Order_Creditmemo::class) - ->disableOriginalConstructor() - ->setMethods(array('getOrder', 'getAllItems')) - ->getMock(); - - $this->_orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) - ->disableOriginalConstructor() - ->setMethods(array('getCustomerEmail')) - ->getMock(); - - $this->_requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) - ->disableOriginalConstructor() - ->setMethods(array('getParam')) - ->getMock(); - - $this->_subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) - ->disableOriginalConstructor() - ->setMethods(array('loadByEmail')) - ->getMock(); - - $this->_helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->disableOriginalConstructor() - ->setMethods(array('unsubscribeMember')) - ->getMock(); } public function testProductAttributeUpdateIsUsingCorrectStoreId() @@ -1825,83 +1776,102 @@ public function testCreateCreditmemoUbsubscribe() $customerEmail = 'customer@mailchimp.com'; $mailchimpUnsubscribe = 'on'; - $this->_observerMock + $mailchimpObserverMock = $this->getMailchimpObserverMock(); + $observerMock = $this->getObserverMock(); + $eventObserverMock = $this->getEventObserverMock(); + $creditMemoMock = $this->getCreditMemoMock(); + $helperMock = $this->getHelperMock(); + $requestMock = $this->getRequestMock(); + + $orderMock = $this->getOrderMock(); + $subscriberMock = $this->getSubscriberMock(); + + $observerMock ->expects($this->once()) ->method('getEvent') - ->willReturn($this->_eventObserverMock); - $this->_eventObserverMock + ->willReturn($eventObserverMock); + $eventObserverMock ->expects($this->once()) ->method('getCreditmemo') - ->willReturn($this->_creditMemoMock); - $this->_creditMemoMock + ->willReturn($creditMemoMock); + $creditMemoMock ->expects($this->once()) ->method('getOrder') - ->willReturn($this->_orderMock); - $this->_mailchimpObserverMock + ->willReturn($orderMock); + $mailchimpObserverMock ->expects($this->once()) ->method('getRequest') - ->willReturn($this->_requestMock); - $this->_mailchimpObserverMock + ->willReturn($requestMock); + $mailchimpObserverMock ->expects($this->once()) ->method('makeHelper') - ->willReturn($this->_helperMock); - $this->_requestMock + ->willReturn($helperMock); + $requestMock ->expects($this->once()) ->method('getParam') ->with('mailchimp_unsubscribe') ->willReturn($mailchimpUnsubscribe); // Inside mailchimpUnsubscribe if: - $this->_orderMock->expects($this->once())->method('getCustomerEmail')->willReturn($customerEmail); - $this->_mailchimpObserverMock + $orderMock->expects($this->once())->method('getCustomerEmail')->willReturn($customerEmail); + $mailchimpObserverMock ->expects($this->once()) ->method('getSubscriberModel') - ->willReturn($this->_subscriberMock); - $this->_subscriberMock + ->willReturn($subscriberMock); + $subscriberMock ->expects($this->once()) ->method('loadByEmail') ->with($customerEmail) ->willReturnSelf(); - $this->_helperMock->expects($this->once()) + $helperMock->expects($this->once()) ->method('unsubscribeMember') - ->with($this->_subscriberMock) + ->with($subscriberMock) ->willReturnSelf(); - $this->_mailchimpObserverMock->createCreditmemo($this->_observerMock); + $mailchimpObserverMock->createCreditmemo($observerMock); } public function testCreateCreditmemo() { $mailchimpUnsubscribe = ''; - $this->_observerMock + $mailchimpObserverMock = $this->getMailchimpObserverMock(); + $observerMock = $this->getObserverMock(); + $eventObserverMock = $this->getEventObserverMock(); + $creditMemoMock = $this->getCreditMemoMock(); + $helperMock = $this->getHelperMock(); + $requestMock = $this->getRequestMock(); + $orderMock = $this->getOrderMock(); + $subscriberMock = $this->getSubscriberMock(); + + $observerMock ->expects($this->never()) ->method('getEvent') - ->willReturn($this->_eventObserverMock); - $this->_eventObserverMock + ->willReturn($eventObserverMock); + $eventObserverMock ->expects($this->never()) ->method('getCreditmemo') - ->willReturn($this->_creditMemoMock); - $this->_creditMemoMock + ->willReturn($creditMemoMock); + $creditMemoMock ->expects($this->never()) ->method('getOrder') - ->willReturn($this->_orderMock); - $this->_mailchimpObserverMock + ->willReturn($orderMock); + $mailchimpObserverMock ->expects($this->never()) ->method('makeHelper') - ->willReturn($this->_helperMock); + ->willReturn($helperMock); - $this->_mailchimpObserverMock + $mailchimpObserverMock ->expects($this->once()) ->method('getRequest') - ->willReturn($this->_requestMock); - $this->_requestMock + ->willReturn($requestMock); + $requestMock ->expects($this->once()) ->method('getParam') ->with('mailchimp_unsubscribe') ->willReturn($mailchimpUnsubscribe); - $this->_mailchimpObserverMock->createCreditmemo($this->_observerMock); + $mailchimpObserverMock->createCreditmemo($observerMock); } public function testCancelCreditMemo() @@ -2041,4 +2011,68 @@ public function testCancelCreditMemo() $mailchimpObserverMock->cancelCreditMemo($observerMock); } + + protected function getMailchimpObserverMock() + { + return $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getRequest', 'getSubscriberModel', 'makeHelper')) + ->getMock(); + } + + protected function getObserverMock() + { + return $this->getMockBuilder(Varien_Event_Observer::class) + ->disableOriginalConstructor() + ->setMethods(array('getEvent')) + ->getMock(); + } + + protected function getEventObserverMock() + { + return $this->_eventObserverMock = $this->getMockBuilder(Varien_Event::class) + ->disableOriginalConstructor() + ->setMethods(array('getCreditmemo')) + ->getMock(); + } + + protected function getCreditMemoMock() + { + return $this->_creditMemoMock = $this->getMockBuilder(Mage_Sales_Model_Order_Creditmemo::class) + ->disableOriginalConstructor() + ->setMethods(array('getOrder', 'getAllItems')) + ->getMock(); + } + + protected function getOrderMock() + { + return $this->_orderMock = $this->getMockBuilder(Mage_Sales_Model_Order::class) + ->disableOriginalConstructor() + ->setMethods(array('getCustomerEmail')) + ->getMock(); + } + + protected function getRequestMock() + { + return $this->_requestMock = $this->getMockBuilder(Mage_Core_Controller_Request_Http::class) + ->disableOriginalConstructor() + ->setMethods(array('getParam')) + ->getMock(); + } + + protected function getSubscriberMock() + { + return $this->_subscriberMock = $this->getMockBuilder(Mage_Newsletter_Model_Subscriber::class) + ->disableOriginalConstructor() + ->setMethods(array('loadByEmail')) + ->getMock(); + } + + protected function getHelperMock() + { + return $this->_helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('unsubscribeMember')) + ->getMock(); + } } From 588ecb7df6794d642392f6ed360030ee5ad75070 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 25 Mar 2020 09:55:51 -0300 Subject: [PATCH 17/51] Added functionality to enable profile updates with webhook --- .../Ebizmarts/MailChimp/Helper/Webhook.php | 4 +- .../mysql4-upgrade-1.1.19-1.1.20.php | 93 +++++++++++-------- .../Ebizmarts/MailChimp/Helper/DataTest.php | 31 ------- .../MailChimp/Helper/WebhookTest.php | 40 +++++++- .../Adminhtml/MailchimpControllerTest.php | 15 ++- lib/Ebizmarts/MailChimp/ListsWebhooks.php | 4 +- 6 files changed, 110 insertions(+), 77 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php index cd2913a52..b7a7b29b7 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php @@ -10,7 +10,7 @@ * @date: 3/20/2020 11:14 AM * @file: Webhook.php */ -class Ebizmarts_MailChimp_Helper_Data extends Mage_Core_Helper_Abstract +class Ebizmarts_MailChimp_Helper_Webhook extends Mage_Core_Helper_Abstract { /** * @var Ebizmarts_MailChimp_Helper_Data @@ -81,7 +81,7 @@ public function handleWebhookChange($scopeId, $scope = 'stores') $scope ); $listId = $helper->getGeneralList($scopeId, $scope); - $helper->deleteCurrentWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); + $this->deleteCurrentWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); if ($helper->isSubscriptionEnabled($scopeId, $scope)) { $this->createNewWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php index a91ecd3a9..8d0b41a20 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php @@ -3,54 +3,71 @@ $installer = $this; try { - /* Mailchimp field change and migration */ - $installer->run( - "ALTER TABLE `{$this->getTable('mailchimp_stores')}` - CHANGE COLUMN `apikey` `apikey` VARCHAR(128) NOT NULL;" - ); - - $installer->run( - "TRUNCATE `{$this->getTable('mailchimp_stores')}`;" - ); + $webhookStores = array(); + /* Check if webhook is created */ $configDataCollection = Mage::getModel('core/config_data') ->getCollection() - ->addFieldToFilter('path', 'mailchimp/general/apikey'); + ->addFieldToFilter('path', 'mailchimp/general/webhook_active') + ->addFieldToFilter('value', '1'); - $mailchimpShards = array('us'); foreach ($configDataCollection as $data) { - $dbApiKey = $data->getValue(); - foreach ($mailchimpShards as $shard) { - if (strpos($dbApiKey, "-$shard") !== false) { - list($hash, $server) = explode("-$shard", $dbApiKey); - if (is_numeric($server) && strlen($hash) === 32) { - $encryptedApiKey = Mage::helper('core')->encrypt($dbApiKey); - $installer->setConfigData( - 'mailchimp/general/apikey', - $encryptedApiKey, - $data->getScope(), - $data->getScopeId() - ); + $webhookStores []= $data->getScopeId(); + } + + /* If webhook is created, edites it and place the new "event" variable */ + if (!empty($webhookStores)) { + $webhookData = array(); + $webhookIds = array(); + $helper = Mage::helper('mailchimp'); + + // Get all the Webhooks ID related to the Stores. + $configDataCollection = Mage::getModel('core/config_data') + ->getCollection() + ->addFieldToFilter('path', 'mailchimp/general/webhook_id') + ->addFieldToFilter('scope_id', array('in' => $webhookStores)); + + foreach ($configDataCollection as $data) { + foreach ($webhookData as $webhook) { + if ($webhook['store_id'] == $data->getScopeId()) { + $webhook ['webhook_id'] = $data->getValue(); } } } - } - /* Mandrill migration */ - $configDataCollection = Mage::getModel('core/config_data') - ->getCollection() - ->addFieldToFilter('path', 'mandrill/general/apikey'); + foreach ($webhookStores as $storeId) { + $listId = $helper->getGeneralList($storeId); + $webhookData []= array('list_id' => $listId, 'store_id' => $storeId); + } - foreach ($configDataCollection as $data) { - $dbApiKey = $data->getValue(); - if (strlen($dbApiKey) == 22) { - $encryptedApiKey = Mage::helper('core')->encrypt($dbApiKey); - $installer->setConfigData( - 'mandrill/general/apikey', - $encryptedApiKey, - $data->getScope(), - $data->getScopeId() - ); + $events = array( + 'subscribe' => true, + 'unsubscribe' => true, + 'profile' => true, + 'cleaned' => true, + 'upemail' => true, + 'campaign' => false + ); + + $sources = array( + 'user' => true, + 'admin' => true, + 'api' => false + ); + + // Edits the webhooks through API. + foreach ($webhookData as $webhook) { + $helper + ->getApi($webhook['store_id']) + ->lists + ->webhooks + ->edit( + $webhook['list_id'], + $webhook['webhook_id'], + null, + $events, + $sources + ); } } } catch (Exception $e) { 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 2a12056dc..76efd6cc2 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -327,37 +327,6 @@ public function testSaveMailChimpConfig() ); } - public function testHandleWebhookChange() - { - $scopeId = 0; - $scope = 'default'; - $realScopeArray = array('scope_id' => 0, 'scope' => 'default'); - $listId = 'a1s2d3f4g5'; - - $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->disableOriginalConstructor() - ->setMethods( - array('getRealScopeForConfig', 'getGeneralList', 'deleteCurrentWebhook', - 'isSubscriptionEnabled', 'createNewWebhook') - ) - ->getMock(); - - $helperMock - ->expects($this->once()) - ->method('getRealScopeForConfig') - ->with(Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, $scopeId, $scope) - ->willReturn($realScopeArray); - $helperMock->expects($this->once())->method('getGeneralList')->with($scopeId, $scope)->willReturn($listId); - $helperMock - ->expects($this->once()) - ->method('deleteCurrentWebhook') - ->with($realScopeArray['scope_id'], $realScopeArray['scope'], $listId); - $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($scopeId, $scope)->willReturn(1); - $helperMock->expects($this->once())->method('createNewWebhook')->with($scopeId, $scope, $listId); - - $helperMock->handleWebhookChange($scopeId, $scope); - } - /** * @param array $data * @dataProvider testGetImageUrlByIdDataProvider diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php index c1de0035a..85c416ca6 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php @@ -1,9 +1,47 @@ 0, 'scope' => 'default'); + $listId = 'a1s2d3f4g5'; + + $webhookHelperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Webhook::class) + ->disableOriginalConstructor() + ->setMethods( + array('deleteCurrentWebhook', 'createNewWebhook', 'getHelper') + ) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods( + array('getRealScopeForConfig', 'getGeneralList', 'isSubscriptionEnabled') + ) + ->getMock(); + + $webhookHelperMock->expects($this->once())->method('getHelper')->willReturn($helperMock); + $helperMock + ->expects($this->once()) + ->method('getRealScopeForConfig') + ->with(Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, $scopeId, $scope) + ->willReturn($realScopeArray); + $helperMock->expects($this->once())->method('getGeneralList')->with($scopeId, $scope)->willReturn($listId); + $webhookHelperMock + ->expects($this->once()) + ->method('deleteCurrentWebhook') + ->with($realScopeArray['scope_id'], $realScopeArray['scope'], $listId); + $helperMock->expects($this->once())->method('isSubscriptionEnabled')->with($scopeId, $scope)->willReturn(1); + $webhookHelperMock->expects($this->once())->method('createNewWebhook')->with($scopeId, $scope, $listId); + + $webhookHelperMock->handleWebhookChange($scopeId, $scope); + } } \ No newline at end of file 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 80f9d53da..de5074fa8 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 @@ -138,12 +138,17 @@ public function testCreateWebhookAction() $mailchimpControllerMock = $this->_mailchimpController ->disableOriginalConstructor() - ->setMethods(array('getHelper')) + ->setMethods(array('getHelper', 'getWebhookHelper')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() - ->setMethods(array('getMageApp', 'createNewWebhook', 'getGeneralList')) + ->setMethods(array('getMageApp', 'getGeneralList')) + ->getMock(); + + $webhookHelperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Webhook::class) + ->disableOriginalConstructor() + ->setMethods(array('createNewWebhook')) ->getMock(); $mageAppMock = $this->getMockBuilder(Mage_Core_Model_App::class) @@ -162,6 +167,10 @@ public function testCreateWebhookAction() ->getMock(); $mailchimpControllerMock->expects($this->once())->method('getHelper')->willReturn($helperMock); + $mailchimpControllerMock + ->expects($this->once()) + ->method('getWebhookHelper') + ->willReturn($webhookHelperMock); $helperMock->expects($this->once())->method('getGeneralList')->with($scopeId)->willReturn($listId); $helperMock->expects($this->once())->method('getMageApp')->willReturn($mageAppMock); @@ -177,7 +186,7 @@ public function testCreateWebhookAction() $scopeId ); - $helperMock + $webhookHelperMock ->expects($this->once()) ->method('createNewWebhook') ->with($scopeId, $scope, $listId) diff --git a/lib/Ebizmarts/MailChimp/ListsWebhooks.php b/lib/Ebizmarts/MailChimp/ListsWebhooks.php index cc6293fd6..e3c906aaa 100644 --- a/lib/Ebizmarts/MailChimp/ListsWebhooks.php +++ b/lib/Ebizmarts/MailChimp/ListsWebhooks.php @@ -39,7 +39,7 @@ public function add($listId, $url = null, $events = null, $sources = null) return $this->_master->call('lists/' . $listId . '/webhooks', $_params, Ebizmarts_MailChimp::POST); } - public function edit($listId, $wrbhookId, $url = null, $events = null, $sources = null) + public function edit($listId, $webhookId, $url = null, $events = null, $sources = null) { $_params = array(); @@ -56,7 +56,7 @@ public function edit($listId, $wrbhookId, $url = null, $events = null, $sources } return $this->_master->call( - 'lists/' . $listId . '/webhooks/' . $wrbhookId, + 'lists/' . $listId . '/webhooks/' . $webhookId, $_params, Ebizmarts_MailChimp::PATCH ); From 4a95e24ebc7c1e120acf505e29315b35bf4d4e1f Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 25 Mar 2020 10:52:41 -0300 Subject: [PATCH 18/51] solving helper related errors --- app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php | 2 +- .../MailChimp/controllers/Adminhtml/MailchimpController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php index b7a7b29b7..85ff44d82 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php @@ -19,7 +19,7 @@ class Ebizmarts_MailChimp_Helper_Webhook extends Mage_Core_Helper_Abstract public function __construct() { - $this->_helper = Mage::getHelper('mailchimp'); + $this->_helper = Mage::helper('mailchimp'); } /** diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 8ff6c81cf..99082c32a 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -20,7 +20,7 @@ class Ebizmarts_MailChimp_Adminhtml_MailchimpController extends Mage_Adminhtml_C public function preDispatch() { $this->_helper = $this->makeHelper(); - $this->_helper = $this->makeWebhookHelper(); + $this->_webhookHelper = $this->makeWebhookHelper(); return parent::preDispatch(); } From c5bb97b1cba9ddad5cb5af863684f6d926be4af8 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Wed, 25 Mar 2020 13:22:14 -0300 Subject: [PATCH 19/51] Fixing phpcs errors --- app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php | 2 +- .../sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php | 8 +------- .../tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php | 2 +- lib/Ebizmarts/MailChimp/ListsWebhooks.php | 8 +++----- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php index 85ff44d82..c3d919935 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php @@ -272,4 +272,4 @@ protected function getHelper() { return $this->_helper; } -} \ No newline at end of file +} diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php index 8d0b41a20..6c96b8141 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php @@ -61,13 +61,7 @@ ->getApi($webhook['store_id']) ->lists ->webhooks - ->edit( - $webhook['list_id'], - $webhook['webhook_id'], - null, - $events, - $sources - ); + ->edit($webhook['list_id'], $webhook['webhook_id'], null, $events, $sources); } } } catch (Exception $e) { diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php index 85c416ca6..5d2afe1e0 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/WebhookTest.php @@ -44,4 +44,4 @@ public function testHandleWebhookChange() $webhookHelperMock->handleWebhookChange($scopeId, $scope); } -} \ No newline at end of file +} diff --git a/lib/Ebizmarts/MailChimp/ListsWebhooks.php b/lib/Ebizmarts/MailChimp/ListsWebhooks.php index e3c906aaa..2df3ef986 100644 --- a/lib/Ebizmarts/MailChimp/ListsWebhooks.php +++ b/lib/Ebizmarts/MailChimp/ListsWebhooks.php @@ -55,11 +55,9 @@ public function edit($listId, $webhookId, $url = null, $events = null, $sources $_params['sources'] = $sources; } - return $this->_master->call( - 'lists/' . $listId . '/webhooks/' . $webhookId, - $_params, - Ebizmarts_MailChimp::PATCH - ); + $urlCall = 'lists/' . $listId . '/webhooks/' . $webhookId; + + return $this->_master->call($urlCall, $_params, Ebizmarts_MailChimp::PATCH); } /** From 18db70c68c13f1837e9dca26d8107fbe7a607dd7 Mon Sep 17 00:00:00 2001 From: santiagoebizmarts Date: Thu, 26 Mar 2020 10:55:50 -0300 Subject: [PATCH 20/51] Travis test --- .../Ebizmarts/MailChimp/Model/System/Config/Backend/List.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php index 3ebcc265f..3f52c4e52 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php @@ -54,6 +54,7 @@ protected function _afterSave() if ($apiKey && $moduleIsActive && $valueChanged) { $helper->handleWebhookChange($scopeId, $scope); + } } From 9844e76474c91c881348f0319d43647027486518 Mon Sep 17 00:00:00 2001 From: santiagoebizmarts Date: Thu, 26 Mar 2020 10:57:43 -0300 Subject: [PATCH 21/51] Travis test --- .../Ebizmarts/MailChimp/Model/System/Config/Backend/List.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php index 3f52c4e52..3ebcc265f 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php @@ -54,7 +54,6 @@ protected function _afterSave() if ($apiKey && $moduleIsActive && $valueChanged) { $helper->handleWebhookChange($scopeId, $scope); - } } From 8ae3f4b2912a9a314f0581905cfebb862c652446 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 30 Mar 2020 10:25:46 -0300 Subject: [PATCH 22/51] Fixed Update and controller. Request Changes Done --- .../Ebizmarts/MailChimp/Helper/Webhook.php | 5 + .../Model/System/Config/Backend/List.php | 11 +- .../Adminhtml/MailchimpController.php | 14 +- .../Ebizmarts/MailChimp/etc/config.xml | 4 +- .../mysql4-upgrade-1.1.19-1.1.20.php | 125 ++++++++++++++---- 5 files changed, 116 insertions(+), 43 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php index c3d919935..6f0bb5599 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php @@ -131,6 +131,11 @@ protected function deleteCurrentWebhook($scopeId, $scope, $listId) } } + /** + * @param $api + * @param $listId + * @param $webhookUrl + */ protected function _deletedWebhooksByListId($api, $listId, $webhookUrl) { $webhooks = $api->getLists()->getWebhooks()->getAll($listId); diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php index 3ebcc265f..4705db344 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php @@ -17,6 +17,7 @@ protected function _afterSave() $groups = $this->getData('groups'); $helper = $this->getMailchimpHelper(); $dateHelper = $this->getMailchimpDateHelper(); + $webhookHelper = $this->getMailchimpWebhookHelper(); $scopeId = $this->getScopeId(); $scope = $this->getScope(); $valueChanged = $this->isValueChanged(); @@ -53,7 +54,7 @@ protected function _afterSave() } if ($apiKey && $moduleIsActive && $valueChanged) { - $helper->handleWebhookChange($scopeId, $scope); + $webhookHelper->handleWebhookChange($scopeId, $scope); } } @@ -73,6 +74,14 @@ protected function getMailchimpDateHelper() return Mage::helper('mailchimp/date'); } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function getMailchimpWebhookHelper() + { + return Mage::helper('mailchimp/webhook'); + } + /** * @return Mage_Adminhtml_Model_Session */ diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 99082c32a..164447d7b 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -19,8 +19,8 @@ class Ebizmarts_MailChimp_Adminhtml_MailchimpController extends Mage_Adminhtml_C public function preDispatch() { - $this->_helper = $this->makeHelper(); - $this->_webhookHelper = $this->makeWebhookHelper(); + $this->_helper = Mage::helper('mailchimp'); + $this->_webhookHelper = Mage::helper('mailchimp/webhook'); return parent::preDispatch(); } @@ -197,11 +197,6 @@ protected function _isAllowed() /** * @return Ebizmarts_MailChimp_Helper_Data */ - protected function makeHelper() - { - return Mage::helper('mailchimp'); - } - protected function getHelper() { return $this->_helper; @@ -210,11 +205,6 @@ protected function getHelper() /** * @return Ebizmarts_MailChimp_Helper_Webhook */ - protected function makeWebhookHelper() - { - return Mage::helper('mailchimp/webhook'); - } - protected function getWebhookHelper() { return $this->_webhookHelper; diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index 0693aa89c..b7dfab680 100644 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -2,10 +2,10 @@ - 1.1.19 + 1.1.20 - 1.1.19 + 1.1.20 diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php index 6c96b8141..0b15fbd5d 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php @@ -3,43 +3,25 @@ $installer = $this; try { - $webhookStores = array(); + $webhookData = array(); /* Check if webhook is created */ $configDataCollection = Mage::getModel('core/config_data') ->getCollection() - ->addFieldToFilter('path', 'mailchimp/general/webhook_active') - ->addFieldToFilter('value', '1'); + ->addFieldToFilter('path', 'mailchimp/general/webhook_id'); foreach ($configDataCollection as $data) { - $webhookStores []= $data->getScopeId(); + $webhookData []= array( + 'webhook_id' => $data->getValue(), + 'scope_id' => $data->getScopeId(), + 'scope' => $data->getScope() + ); } /* If webhook is created, edites it and place the new "event" variable */ - if (!empty($webhookStores)) { - $webhookData = array(); - $webhookIds = array(); + if (!empty($webhookData)) { $helper = Mage::helper('mailchimp'); - // Get all the Webhooks ID related to the Stores. - $configDataCollection = Mage::getModel('core/config_data') - ->getCollection() - ->addFieldToFilter('path', 'mailchimp/general/webhook_id') - ->addFieldToFilter('scope_id', array('in' => $webhookStores)); - - foreach ($configDataCollection as $data) { - foreach ($webhookData as $webhook) { - if ($webhook['store_id'] == $data->getScopeId()) { - $webhook ['webhook_id'] = $data->getValue(); - } - } - } - - foreach ($webhookStores as $storeId) { - $listId = $helper->getGeneralList($storeId); - $webhookData []= array('list_id' => $listId, 'store_id' => $storeId); - } - $events = array( 'subscribe' => true, 'unsubscribe' => true, @@ -55,18 +37,105 @@ 'api' => false ); + // Get all ApiKeys + $apiKeys = array(); + $apiKeyCollection = Mage::getModel('core/config_data') + ->getCollection() + ->addFieldToFilter('path', 'mailchimp/general/apiKey'); + + foreach ($apiKeyCollection as $data) + { + $apiKey = $data->getValue(); + + if ($apiKey !== null) { + $apiKey = $helper->decryptData($apiKey); + } + + $apiKeys []= array( + 'scope' => $data->getScope(), + 'scope_id' => $data->getScopeId(), + 'apikey' => $apiKey + ); + } + + // Get all List Ids + $listIds = array(); + $listCollection = Mage::getModel('core/config_data') + ->getCollection() + ->addFieldToFilter('path', 'mailchimp/general/list'); + + foreach ($listCollection as $data) + { + $listIds []= array( + 'scope' => $data->getScope(), + 'scope_id' => $data->getScopeId(), + 'list' => $data->getValue() + ); + } + // Edits the webhooks through API. foreach ($webhookData as $webhook) { + $listId = retrieveListId($listIds, $webhook['scope'], $webhook['scope_id']); + $apiKey = retrieveApiKey($apiKeys, $webhook['scope'], $webhook['scope_id']); + $helper - ->getApi($webhook['store_id']) + ->getApiByKey($apiKey) ->lists ->webhooks - ->edit($webhook['list_id'], $webhook['webhook_id'], null, $events, $sources); + ->edit($listId, $webhook['webhook_id'], null, $events, $sources); } } } catch (Exception $e) { Mage::log($e->getMessage(), null, 'MailChimp_Errors.log', true); } +function retrieveApiKey($apiKeys, $scope, $scopeId) +{ + $apiKey = searchInData($apiKeys, $scope, $scopeId); + + if ($apiKey === null) { + throw new Exception('API Key not configured at default level.'); + } + + return $apiKey['apikey']; +} + +function retrieveListId($listIds, $scope, $scopeId) +{ + $listId = searchInData($listIds, $scope, $scopeId); + + if ($listId === null) { + throw new Exception('List not configured at default level.'); + } + + return $listId['list']; +} + +function searchInData($searchArray, $scope, $scopeId) +{ + $returnArray = null; + $websiteArray = null; + $defaultArray = null; + + foreach ($searchArray as $data) + { + if ($data['scope'] == $scope && $data['scope_id'] == $scopeId) { + $returnArray = $data; + } elseif ($data['scope'] == 'website' && $data['scope_id'] == '1') { + $websiteArray = $data; + } elseif ($data['scope'] == 'default' && $data['scope_id'] == '1') { + $defaultArray = $data; + } + } + + if ($returnArray === null) { + $returnArray = $websiteArray; + } elseif ($returnArray === null) { + $returnArray = $defaultArray; + } + + return $returnArray; +} + $installer->endSetup(); From 129e6d2afbc3a1706ac62a19cf4c225909ef03e4 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 30 Mar 2020 11:24:33 -0300 Subject: [PATCH 23/51] Reverted version change --- 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 b7dfab680..0693aa89c 100644 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -2,10 +2,10 @@ - 1.1.20 + 1.1.19 - 1.1.20 + 1.1.19 From 903f07ebe3b1f0cf541974a30832c94203256a13 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Fri, 3 Apr 2020 14:17:50 -0300 Subject: [PATCH 24/51] Migration to 1.1.21 added, functionality to add profile updates to webhook --- .../Ebizmarts/MailChimp/Helper/Data.php | 91 ++++++++++- .../Ebizmarts/MailChimp/Model/Config.php | 1 + .../mysql4-upgrade-1.1.19-1.1.20.php | 141 ------------------ .../mysql4-upgrade-1.1.20-1.1.21.php | 32 ++++ 4 files changed, 123 insertions(+), 142 deletions(-) delete mode 100644 app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php create mode 100644 app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index b709c2184..e4bb0d9f4 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1986,6 +1986,11 @@ public function handleMigrationUpdates() 0, 'default' ); + $migrateFrom1120 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 0, + 'default' + ); if ($migrateFrom115) { $this->_migrateFrom115($initialTime); @@ -1993,6 +1998,8 @@ public function handleMigrationUpdates() $this->_migrateFrom116($initialTime); } elseif ($migrateFrom1164 && !$dateHelper->timePassed($initialTime)) { $this->_migrateFrom1164($initialTime); + } elseif ($migrateFrom1120 && !$dateHelper->timePassed($initialTime)) { + $this->_migrateFrom1120($initialTime); } } @@ -2520,7 +2527,13 @@ public function migrationFinished() 'default' ); - if (!$migrateFrom115 && !$migrateFrom116 && !$migrateFrom1164) { + $migrateFrom1120 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 0, + 'default' + ); + + if (!$migrateFrom115 && !$migrateFrom116 && !$migrateFrom1164 && !$migrateFrom1120) { $migrationFinished = true; } @@ -2578,6 +2591,78 @@ protected function delete1164MigrationConfigData() ); } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function getWebhookHelper() + { + return Mage::helper('mailchimp/webhook'); + } + + /** + * Migrate data from version 1.1.21. + * + * @param $initialTime + */ + protected function _migrateFrom1120($initialTime) + { + $dateHelper = $this->getDateHelper(); + $webhookHelper = $this->getWebhookHelper(); + + if (!$dateHelper->timePassed($initialTime)) { + // Get all stores data. + $stores = $this->getMageApp()->getStores(); + + $events = array( + 'subscribe' => true, + 'unsubscribe' => true, + 'profile' => true, + 'cleaned' => true, + 'upemail' => true, + 'campaign' => false + ); + + $sources = array( + 'user' => true, + 'admin' => true, + 'api' => false + ); + + foreach ($stores as $storeId => $store) { + // Gets the ListId and WebhookId for the iterated store. + $listId = $this->getGeneralList($scopeId, $scope); + $webhookId = $webhookHelper->getWebhookId($scopeId, $scope); + + // Edits the webhook with the new $event array. + $this + ->getApi($storeId, $store) + ->getLists() + ->getWebhooks() + ->edit($listId, $webhookId, null, $events, $sources); + + if ($dateHelper->timePassed($initialTime)) { + $finished = false; + break; + } + } + + $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => false, '1120' => true); + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Delete config data for migration from 1.1.21. + */ + protected function delete1120MigrationConfigData() + { + $this->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 'default', + 0 + ); + } + /** * @param $arrayMigrationConfigData */ @@ -2595,6 +2680,10 @@ public function handleDeleteMigrationConfigData($arrayMigrationConfigData) if ($migrationConfigData == '1164' && $value) { $this->delete1164MigrationConfigData(); } + + if ($migrationConfigData == '1120' && $value) { + $this->delete1120MigrationConfigData(); + } } $this->getConfig()->cleanCache(); diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Config.php b/app/code/community/Ebizmarts/MailChimp/Model/Config.php index 4bb82041b..67f822764 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Config.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Config.php @@ -32,6 +32,7 @@ class Ebizmarts_MailChimp_Model_Config 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_FROM_1120 = 'mailchimp/general/migrate_from_1120'; 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'; diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php deleted file mode 100644 index 0b15fbd5d..000000000 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.19-1.1.20.php +++ /dev/null @@ -1,141 +0,0 @@ -getCollection() - ->addFieldToFilter('path', 'mailchimp/general/webhook_id'); - - foreach ($configDataCollection as $data) { - $webhookData []= array( - 'webhook_id' => $data->getValue(), - 'scope_id' => $data->getScopeId(), - 'scope' => $data->getScope() - ); - } - - /* If webhook is created, edites it and place the new "event" variable */ - if (!empty($webhookData)) { - $helper = Mage::helper('mailchimp'); - - $events = array( - 'subscribe' => true, - 'unsubscribe' => true, - 'profile' => true, - 'cleaned' => true, - 'upemail' => true, - 'campaign' => false - ); - - $sources = array( - 'user' => true, - 'admin' => true, - 'api' => false - ); - - // Get all ApiKeys - $apiKeys = array(); - $apiKeyCollection = Mage::getModel('core/config_data') - ->getCollection() - ->addFieldToFilter('path', 'mailchimp/general/apiKey'); - - foreach ($apiKeyCollection as $data) - { - $apiKey = $data->getValue(); - - if ($apiKey !== null) { - $apiKey = $helper->decryptData($apiKey); - } - - $apiKeys []= array( - 'scope' => $data->getScope(), - 'scope_id' => $data->getScopeId(), - 'apikey' => $apiKey - ); - } - - // Get all List Ids - $listIds = array(); - $listCollection = Mage::getModel('core/config_data') - ->getCollection() - ->addFieldToFilter('path', 'mailchimp/general/list'); - - foreach ($listCollection as $data) - { - $listIds []= array( - 'scope' => $data->getScope(), - 'scope_id' => $data->getScopeId(), - 'list' => $data->getValue() - ); - } - - // Edits the webhooks through API. - foreach ($webhookData as $webhook) { - $listId = retrieveListId($listIds, $webhook['scope'], $webhook['scope_id']); - $apiKey = retrieveApiKey($apiKeys, $webhook['scope'], $webhook['scope_id']); - - $helper - ->getApiByKey($apiKey) - ->lists - ->webhooks - ->edit($listId, $webhook['webhook_id'], null, $events, $sources); - } - } -} catch (Exception $e) { - Mage::log($e->getMessage(), null, 'MailChimp_Errors.log', true); -} - -function retrieveApiKey($apiKeys, $scope, $scopeId) -{ - $apiKey = searchInData($apiKeys, $scope, $scopeId); - - if ($apiKey === null) { - throw new Exception('API Key not configured at default level.'); - } - - return $apiKey['apikey']; -} - -function retrieveListId($listIds, $scope, $scopeId) -{ - $listId = searchInData($listIds, $scope, $scopeId); - - if ($listId === null) { - throw new Exception('List not configured at default level.'); - } - - return $listId['list']; -} - -function searchInData($searchArray, $scope, $scopeId) -{ - $returnArray = null; - $websiteArray = null; - $defaultArray = null; - - foreach ($searchArray as $data) - { - if ($data['scope'] == $scope && $data['scope_id'] == $scopeId) { - $returnArray = $data; - } elseif ($data['scope'] == 'website' && $data['scope_id'] == '1') { - $websiteArray = $data; - } elseif ($data['scope'] == 'default' && $data['scope_id'] == '1') { - $defaultArray = $data; - } - } - - if ($returnArray === null) { - $returnArray = $websiteArray; - } elseif ($returnArray === null) { - $returnArray = $defaultArray; - } - - return $returnArray; -} - - -$installer->endSetup(); diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php new file mode 100644 index 000000000..7732bf699 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php @@ -0,0 +1,32 @@ +getCollection() + ->addFieldToFilter('path', 'mailchimp/general/webhook_id'); + + /* If webhook is created, edites it and place the new "event" variable */ + if ($configDataCollection->getSize()) { + Mage::log('Setting constant', null, 'giorello.log', true); + // Sets the migration flag to edit webhooks. + Mage::helper('mailchimp') + ->saveMailChimpConfig( + array( + array( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 1) + ), + 0, + 'default' + ); + } +} catch (Exception $e) { + Mage::log($e->getMessage(), null, 'MailChimp_Errors.log', true); +} + +$installer->endSetup(); From 605dc4188f5a505f8b314465635817db39ec7f9c Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 6 Apr 2020 10:03:05 -0300 Subject: [PATCH 25/51] Migration Helper added --- .../System/Config/Fieldset/Mailchimp/Hint.php | 10 +- .../Ebizmarts/MailChimp/Helper/Data.php | 692 ---------------- .../Ebizmarts/MailChimp/Helper/Migration.php | 752 ++++++++++++++++++ .../Ebizmarts/MailChimp/Model/Cron.php | 14 +- .../Model/System/Config/Source/Account.php | 17 +- .../mysql4-upgrade-1.1.20-1.1.21.php | 1 - 6 files changed, 789 insertions(+), 697 deletions(-) create mode 100644 app/code/community/Ebizmarts/MailChimp/Helper/Migration.php diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Fieldset/Mailchimp/Hint.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Fieldset/Mailchimp/Hint.php index 136b88fbb..4f889db49 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Fieldset/Mailchimp/Hint.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Fieldset/Mailchimp/Hint.php @@ -38,7 +38,7 @@ public function getMailChimpVersion() */ public function getMigrationFinishedStatus() { - return $this->makeHelper()->migrationFinished(); + return $this->makeMigrationHelper()->migrationFinished(); } /** @@ -101,4 +101,12 @@ protected function makeHelper() { return Mage::helper('mailchimp'); } + + /** + * @return Ebizmarts_MailChimp_Helper_Migration + */ + protected function makeMigrationHelper() + { + return Mage::helper('mailchimp/migration'); + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index e4bb0d9f4..0f4528cdd 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1963,469 +1963,6 @@ public function retrieveAndSaveMCJsUrlInConfig($scopeId, $scope = 'stores') return $mcJsUrlSaved; } - /** - * Handle data migration for versions that require it. - */ - public function handleMigrationUpdates() - { - $dateHelper = $this->getDateHelper(); - - $initialTime = $dateHelper->getTimestamp(); - $migrateFrom115 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, - 0, - 'default' - ); - $migrateFrom116 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, - 0, - 'default' - ); - $migrateFrom1164 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, - 0, - 'default' - ); - $migrateFrom1120 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, - 0, - 'default' - ); - - if ($migrateFrom115) { - $this->_migrateFrom115($initialTime); - } elseif ($migrateFrom116 && !$dateHelper->timePassed($initialTime)) { - $this->_migrateFrom116($initialTime); - } elseif ($migrateFrom1164 && !$dateHelper->timePassed($initialTime)) { - $this->_migrateFrom1164($initialTime); - } elseif ($migrateFrom1120 && !$dateHelper->timePassed($initialTime)) { - $this->_migrateFrom1120($initialTime); - } - } - - /** - * Migrate data from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $initialTime - * @throws Mage_Core_Exception - */ - protected function _migrateFrom115($initialTime) - { - $dateHelper = $this->getDateHelper(); - $arrayMigrationConfigData = array('115' => true, '116' => false, '1164' => false); - //migrate data from older version to the new schemma - if ($this->isEcommerceEnabled(0)) { - $mailchimpStoreId = $this->getMCStoreId(0); - - //migrate customers - $this->_migrateCustomersFrom115($mailchimpStoreId, $initialTime); - - if (!$dateHelper->timePassed($initialTime)) { - //migrate products - $this->_migrateProductsFrom115($mailchimpStoreId, $initialTime); - - if (!$dateHelper->timePassed($initialTime)) { - //migrate orders - $this->_migrateOrdersFrom115($mailchimpStoreId, $initialTime); - - if (!$dateHelper->timePassed($initialTime)) { - //migrate carts - $finished = $this->_migrateCartsFrom115($mailchimpStoreId, $initialTime); - - if ($finished) { - $this->_migrateFrom115dropColumn($arrayMigrationConfigData); - } - } - } - } - } else { - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - } - } - - protected function _migrateFrom115dropColumn($arrayMigrationConfigData) - { - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - - //Remove attributes no longer used - $setup = Mage::getResourceModel('catalog/setup', 'catalog_setup'); - - try { - $setup->removeAttribute('catalog_product', 'mailchimp_sync_delta'); - $setup->removeAttribute('catalog_product', 'mailchimp_sync_error'); - $setup->removeAttribute('catalog_product', 'mailchimp_sync_modified'); - $setup->removeAttribute('customer', 'mailchimp_sync_delta'); - $setup->removeAttribute('customer', 'mailchimp_sync_error'); - $setup->removeAttribute('customer', 'mailchimp_sync_modified'); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - - $coreResource = $this->getCoreResource(); - - try { - $quoteTable = $coreResource->getTableName('sales/quote'); - $connectionQuote = $setup->getConnection(); - $connectionQuote->dropColumn($quoteTable, 'mailchimp_sync_delta'); - $connectionQuote->dropColumn($quoteTable, 'mailchimp_sync_error'); - $connectionQuote->dropColumn($quoteTable, 'mailchimp_deleted'); - $connectionQuote->dropColumn($quoteTable, 'mailchimp_token'); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - - try { - $orderTable = $coreResource->getTableName('sales/order'); - $connectionOrder = $setup->getConnection(); - $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_delta'); - $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_error'); - $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_modified'); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - - /** - * Migrate Customers from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $mailchimpStoreId - * @param $initialTime - * @throws Mage_Core_Exception - */ - protected function _migrateCustomersFrom115($mailchimpStoreId, $initialTime) - { - try { - $entityType = Mage::getSingleton('eav/config')->getEntityType('customer'); - $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'mailchimp_sync_delta'); - - if ($attribute->getId()) { - $mailchimpTableName = $this->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); - $customerCollection = Mage::getResourceModel('customer/customer_collection'); - $customerCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); - $customerCollection->getSelect()->joinLeft( - array('m4m' => $mailchimpTableName), - "m4m.related_id = e.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER - . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", - array('m4m.*') - ); - $customerCollection->getSelect()->where( - "m4m.mailchimp_sync_delta IS null" - ); - $this->_makeForCollectionItem( - $customerCollection, - $mailchimpStoreId, - $initialTime, - function ($customer, $mailchimpStoreId) { - $customerId = $customer->getEntityId(); - $customerObject = Mage::getModel('customer/customer')->load($customerId); - $syncError = null; - $syncModified = null; - $syncDelta = $customerObject->getMailchimpSyncDelta(); - - if ($customer->getMailchimpSyncError()) { - $syncError = $customer->getMailchimpSyncError(); - } - - if ($customer->getMailchimpSyncModified()) { - $syncModified = $customer->getMailchimpSyncModified(); - } - - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); - $ecommerceSyncData->saveEcommerceSyncData( - $customerId, - Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER, - $mailchimpStoreId, - $syncDelta, - $syncError, - $syncModified - ); - } - ); - } - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - - /** - * Migrate Products from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $mailchimpStoreId - * @param $initialTime - * @throws Mage_Core_Exception - */ - protected function _migrateProductsFrom115($mailchimpStoreId, $initialTime) - { - try { - $entityType = Mage_Catalog_Model_Product::ENTITY; - $attributeCode = 'mailchimp_sync_delta'; - $attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entityType, $attributeCode); - - if ($attribute->getId()) { - $mailchimpTableName = $this->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); - $productCollection = Mage::getResourceModel('catalog/product_collection'); - $productCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); - $productCollection->getSelect()->joinLeft( - array('m4m' => $mailchimpTableName), - "m4m.related_id = e.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_PRODUCT - . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", - array('m4m.*') - ); - $productCollection->getSelect()->where("m4m.mailchimp_sync_delta IS null"); - $this->_makeForCollectionItem( - $productCollection, - $mailchimpStoreId, - $initialTime, - function ($product, $mailchimpStoreId) { - $productId = $product->getEntityId(); - $_resource = Mage::getResourceSingleton('catalog/product'); - $syncDelta = $_resource->getAttributeRawValue( - $productId, - 'mailchimp_sync_delta', - $this->getMageApp()->getStore() - ); - $syncError = null; - $syncModified = null; - - if ($product->getMailchimpSyncError()) { - $syncError = $product->getMailchimpSyncError(); - } - - if ($product->getMailchimpSyncModified()) { - $syncModified = $product->getMailchimpSyncModified(); - } - - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); - $ecommerceSyncData->saveEcommerceSyncData( - $productId, - Ebizmarts_MailChimp_Model_Config::IS_PRODUCT, - $mailchimpStoreId, - $syncDelta, - $syncError, - $syncModified - ); - } - ); - } - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - - /** - * Migrate Orders from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $mailchimpStoreId - * @param $initialTime - * @throws Mage_Core_Exception - */ - protected function _migrateOrdersFrom115($mailchimpStoreId, $initialTime) - { - try { - $resource = $this->getCoreResource(); - $readConnection = $resource->getConnection('core_read'); - $tableName = $resource->getTableName('sales/order'); - $orderFields = $readConnection->describeTable($tableName); - - if (isset($orderFields['mailchimp_sync_delta'])) { - $mailchimpTableName = $resource->getTableName('mailchimp/ecommercesyncdata'); - $orderCollection = Mage::getResourceModel('sales/order_collection'); - - $orderCollection->getSelect()->joinLeft( - array('m4m' => $mailchimpTableName), - "m4m.related_id = main_table.entity_id AND m4m.type = '" - . Ebizmarts_MailChimp_Model_Config::IS_ORDER . - "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", - array('m4m.*') - ); - $orderCollection->getSelect() - ->where( - "m4m.mailchimp_sync_delta IS NULL AND main_table.mailchimp_sync_delta > '0000-00-00 00:00:00'" - ); - $this->_makeForCollectionItem( - $orderCollection, - $mailchimpStoreId, - $initialTime, - function ($order, $mailchimpStoreId) { - $orderId = $order->getEntityId(); - $syncError = null; - $syncModified = null; - $orderObject = $this->getSalesOrderModel()->load($orderId); - $syncDelta = $orderObject->getMailchimpSyncDelta(); - - if ($order->getMailchimpSyncError()) { - $syncError = $order->getMailchimpSyncError(); - } - - if ($order->getMailchimpSyncModified()) { - $syncModified = $order->getMailchimpSyncModified(); - } - - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); - $ecommerceSyncData->saveEcommerceSyncData( - $orderId, - Ebizmarts_MailChimp_Model_Config::IS_ORDER, - $mailchimpStoreId, - $syncDelta, - $syncError, - $syncModified - ); - } - ); - } - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - - /** - * Migrate Carts from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $mailchimpStoreId - * @param $initialTime - * @return bool - * @throws Mage_Core_Exception - */ - protected function _migrateCartsFrom115($mailchimpStoreId, $initialTime) - { - try { - $resource = $this->getCoreResource(); - $readConnection = $resource->getConnection('core_read'); - $tableName = $resource->getTableName('sales/quote'); - $quoteFields = $readConnection->describeTable($tableName); - - if (isset($quoteFields['mailchimp_sync_delta'])) { - $mailchimpTableName = $resource->getTableName('mailchimp/ecommercesyncdata'); - $quoteCollection = Mage::getResourceModel('sales/quote_collection'); - $quoteCollection->getSelect()->joinLeft( - array('m4m' => $mailchimpTableName), - "m4m.related_id = main_table.entity_id AND m4m.type = '" - . Ebizmarts_MailChimp_Model_Config::IS_QUOTE - . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", - array('m4m.*') - ); - // be sure that the quotes are already in mailchimp and not deleted - $quoteCollection->getSelect() - ->where( - "m4m.mailchimp_sync_delta IS NULL AND main_table.mailchimp_sync_delta > '0000-00-00 00:00:00'" - ); - $finished = $this->_makeForCollectionItem( - $quoteCollection, - $mailchimpStoreId, - $initialTime, - function ($quote, $mailchimpStoreId) { - $quoteId = $quote->getEntityId(); - $syncError = null; - $syncDeleted = null; - $token = null; - $quoteObject = $this->getSalesOrderModel()->load($quoteId); - $syncDelta = $quoteObject->getMailchimpSyncDelta(); - - if ($quote->getMailchimpSyncError()) { - $syncError = $quote->getMailchimpSyncError(); - } - - if ($quote->getMailchimpSyncDeleted()) { - $syncDeleted = $quote->getMailchimpSyncDeleted(); - } - - if ($quote->getMailchimpToken()) { - $token = $quote->getMailchimpToken(); - } - - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); - $ecommerceSyncData->saveEcommerceSyncData( - $quoteId, - Ebizmarts_MailChimp_Model_Config::IS_QUOTE, - $mailchimpStoreId, - $syncDelta, - $syncError, - null, - $syncDeleted, - $token - ); - } - ); - } else { - $finished = true; - } - - return $finished; - } catch (Exception $e) { - $this->logError( - $this->__( - 'Unexpected error happened during migration from version 1.1.5 to 1.1.6.' - . 'Please contact our support at ' - ) . 'mailchimp@ebizmarts-desk.zendesk.com' - . $this->__(' See error details below.') - ); - $this->logError($e->getMessage()); - - return false; - } - } - - /** - * Delete config data for migration from 1.1.5. - */ - protected function delete115MigrationConfigData() - { - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, - 'default', - 0 - ); - } - - /** - * Helper function for data migration from version 1.1.5. - * - * @param $collection - * @param $mailchimpStoreId - * @param $initialTime - * @param Closure $callback - * @return bool - */ - protected function _makeForCollectionItem($collection, $mailchimpStoreId, $initialTime, Closure $callback) - { - $dateHelper = $this->getDateHelper(); - $finished = false; - - if (!$collection->getSize()) { - $finished = true; - } - - $collection->setPageSize(100); - - $pages = $collection->getLastPageNumber(); - $currentPage = 1; - - do { - $collection->setCurPage($currentPage); - $this->_loadItemCollection($collection); - - foreach ($collection as $collectionItem) { - $callback($collectionItem, $mailchimpStoreId); - } - - $currentPage++; - // clear collection, - // if not done, the same page will be loaded each loop - // - will also free memory - $collection->clear(); - - if ($dateHelper->timePassed($initialTime)) { - break; - } - - if ($currentPage == $pages) { - $finished = true; - } - } while ($currentPage <= $pages); - - return $finished; - } - /** * @param $collection */ @@ -2434,23 +1971,6 @@ protected function _loadItemCollection($collection) $collection->load(); } - /** - * Migrate data from version 1.1.6. - * - * @param $initialTime - */ - protected function _migrateFrom116($initialTime) - { - $this->_setIsSyncingIfFinishedInAllStores(true); - $finished = $this->_migrateOrdersFrom116($initialTime); - - if ($finished) { - $this->_setIsSyncingIfFinishedInAllStores(false); - $arrayMigrationConfigData = array('115' => false, '116' => true, '1164' => false); - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - } - } - /** * Modify is_syncing value if initial sync finished for all stores. * @@ -2469,128 +1989,6 @@ protected function _setIsSyncingIfFinishedInAllStores($syncValue) } } - /** - * Update Order ids to the Increment id in MailChimp. - * - * @param $initialTime - * @return bool - */ - protected function _migrateOrdersFrom116($initialTime) - { - $dateHelper = $this->getDateHelper(); - $finished = false; - - if (!$dateHelper->timePassed($initialTime)) { - $finished = true; - $stores = $this->getMageApp()->getStores(); - - foreach ($stores as $storeId => $store) { - if ($this->isEcomSyncDataEnabled($storeId)) { - Mage::getModel('mailchimp/api_batches')->replaceAllOrders($initialTime, $storeId); - } - - if ($dateHelper->timePassed($initialTime)) { - $finished = false; - break; - } - } - } - - return $finished; - } - - /** - * Return if migration has finished checking the config values. - * - * @return bool - * @throws Mage_Core_Exception - */ - public function migrationFinished() - { - $migrationFinished = false; - - $migrateFrom115 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, - 0, - 'default' - ); - - $migrateFrom116 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, - 0, - 'default' - ); - - $migrateFrom1164 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, - 0, - 'default' - ); - - $migrateFrom1120 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, - 0, - 'default' - ); - - if (!$migrateFrom115 && !$migrateFrom116 && !$migrateFrom1164 && !$migrateFrom1120) { - $migrationFinished = true; - } - - return $migrationFinished; - } - - /** - * Delete config data for migration from 1.1.6. - */ - public function delete116MigrationConfigData() - { - $stores = $this->getMageApp()->getStores(); - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, - 'default', - 0 - ); - - foreach ($stores as $storeId => $store) { - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_LAST_ORDER_ID, - 'stores', - $storeId - ); - } - } - - /** - * Migrate data from version 1.1.6.4. - * - * @param $initialTime - */ - protected function _migrateFrom1164($initialTime) - { - $dateHelper = $this->getDateHelper(); - - if (!$dateHelper->timePassed($initialTime)) { - $writeConnection = $this->getCoreResource()->getConnection('core_write'); - $resource = Mage::getResourceModel('mailchimp/ecommercesyncdata'); - $writeConnection->update($resource->getMainTable(), array('batch_id' => '1'), "batch_id = 0"); - $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => true); - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - } - } - - /** - * Delete config data for migration from 1.1.6.4. - */ - protected function delete1164MigrationConfigData() - { - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, - 'default', - 0 - ); - } - /** * @return Ebizmarts_MailChimp_Helper_Webhook */ @@ -2599,96 +1997,6 @@ protected function getWebhookHelper() return Mage::helper('mailchimp/webhook'); } - /** - * Migrate data from version 1.1.21. - * - * @param $initialTime - */ - protected function _migrateFrom1120($initialTime) - { - $dateHelper = $this->getDateHelper(); - $webhookHelper = $this->getWebhookHelper(); - - if (!$dateHelper->timePassed($initialTime)) { - // Get all stores data. - $stores = $this->getMageApp()->getStores(); - - $events = array( - 'subscribe' => true, - 'unsubscribe' => true, - 'profile' => true, - 'cleaned' => true, - 'upemail' => true, - 'campaign' => false - ); - - $sources = array( - 'user' => true, - 'admin' => true, - 'api' => false - ); - - foreach ($stores as $storeId => $store) { - // Gets the ListId and WebhookId for the iterated store. - $listId = $this->getGeneralList($scopeId, $scope); - $webhookId = $webhookHelper->getWebhookId($scopeId, $scope); - - // Edits the webhook with the new $event array. - $this - ->getApi($storeId, $store) - ->getLists() - ->getWebhooks() - ->edit($listId, $webhookId, null, $events, $sources); - - if ($dateHelper->timePassed($initialTime)) { - $finished = false; - break; - } - } - - $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => false, '1120' => true); - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - } - } - - /** - * Delete config data for migration from 1.1.21. - */ - protected function delete1120MigrationConfigData() - { - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, - 'default', - 0 - ); - } - - /** - * @param $arrayMigrationConfigData - */ - public function handleDeleteMigrationConfigData($arrayMigrationConfigData) - { - foreach ($arrayMigrationConfigData as $migrationConfigData => $value) { - if ($migrationConfigData == '115' && $value) { - $this->delete115MigrationConfigData(); - } - - if ($migrationConfigData == '116' && $value) { - $this->delete116MigrationConfigData(); - } - - if ($migrationConfigData == '1164' && $value) { - $this->delete1164MigrationConfigData(); - } - - if ($migrationConfigData == '1120' && $value) { - $this->delete1120MigrationConfigData(); - } - } - - $this->getConfig()->cleanCache(); - } - /** * Return the list of magento store IDs configured to synchronise to * the given mailchimp list ID. diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php b/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php new file mode 100644 index 000000000..b2db68a89 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php @@ -0,0 +1,752 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/20/2020 11:14 AM + * @file: Webhook.php + */ +class Ebizmarts_MailChimp_Helper_Webhook extends Mage_Core_Helper_Abstract +{ + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected $_helper; + + /** + * @var Ebizmarts_MailChimp_Helper_Date + */ + protected $_dateHelper; + + /** + * @var Ebizmarts_MailChimp_Helper_Webhook + */ + protected $_webhookHelper; + + public function __construct() + { + $this->_helper = Mage::helper('mailchimp'); + $this->_dateHelper = Mage::helper('mailchimp/date'); + $this->_webhookHelper = Mage::helper('mailchimp/webhook'); + } + + /** + * Handle data migration for versions that require it. + */ + public function handleMigrationUpdates() + { + $dateHelper = $this->getDateHelper(); + + $initialTime = $dateHelper->getTimestamp(); + $migrateFrom115 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, + 0, + 'default' + ); + $migrateFrom116 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, + 0, + 'default' + ); + $migrateFrom1164 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, + 0, + 'default' + ); + $migrateFrom1120 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 0, + 'default' + ); + + if ($migrateFrom115) { + $this->_migrateFrom115($initialTime); + } elseif ($migrateFrom116 && !$dateHelper->timePassed($initialTime)) { + $this->_migrateFrom116($initialTime); + } elseif ($migrateFrom1164 && !$dateHelper->timePassed($initialTime)) { + $this->_migrateFrom1164($initialTime); + } elseif ($migrateFrom1120 && !$dateHelper->timePassed($initialTime)) { + $this->_migrateFrom1120($initialTime); + } + } + + /** + * Migrate data from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $initialTime + * @throws Mage_Core_Exception + */ + protected function _migrateFrom115($initialTime) + { + $dateHelper = $this->getDateHelper(); + $arrayMigrationConfigData = array('115' => true, '116' => false, '1164' => false); + //migrate data from older version to the new schemma + if ($this->isEcommerceEnabled(0)) { + $mailchimpStoreId = $this->getMCStoreId(0); + + //migrate customers + $this->_migrateCustomersFrom115($mailchimpStoreId, $initialTime); + + if (!$dateHelper->timePassed($initialTime)) { + //migrate products + $this->_migrateProductsFrom115($mailchimpStoreId, $initialTime); + + if (!$dateHelper->timePassed($initialTime)) { + //migrate orders + $this->_migrateOrdersFrom115($mailchimpStoreId, $initialTime); + + if (!$dateHelper->timePassed($initialTime)) { + //migrate carts + $finished = $this->_migrateCartsFrom115($mailchimpStoreId, $initialTime); + + if ($finished) { + $this->_migrateFrom115dropColumn($arrayMigrationConfigData); + } + } + } + } + } else { + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Helper function for data migration from version 1.1.5. + * + * @param $collection + * @param $mailchimpStoreId + * @param $initialTime + * @param Closure $callback + * @return bool + */ + protected function _makeForCollectionItem($collection, $mailchimpStoreId, $initialTime, Closure $callback) + { + $dateHelper = $this->getDateHelper(); + $finished = false; + + if (!$collection->getSize()) { + $finished = true; + } + + $collection->setPageSize(100); + + $pages = $collection->getLastPageNumber(); + $currentPage = 1; + + do { + $collection->setCurPage($currentPage); + $this->_loadItemCollection($collection); + + foreach ($collection as $collectionItem) { + $callback($collectionItem, $mailchimpStoreId); + } + + $currentPage++; + // clear collection, + // if not done, the same page will be loaded each loop + // - will also free memory + $collection->clear(); + + if ($dateHelper->timePassed($initialTime)) { + break; + } + + if ($currentPage == $pages) { + $finished = true; + } + } while ($currentPage <= $pages); + + return $finished; + } + + protected function _migrateFrom115dropColumn($arrayMigrationConfigData) + { + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + + //Remove attributes no longer used + $setup = Mage::getResourceModel('catalog/setup', 'catalog_setup'); + + try { + $setup->removeAttribute('catalog_product', 'mailchimp_sync_delta'); + $setup->removeAttribute('catalog_product', 'mailchimp_sync_error'); + $setup->removeAttribute('catalog_product', 'mailchimp_sync_modified'); + $setup->removeAttribute('customer', 'mailchimp_sync_delta'); + $setup->removeAttribute('customer', 'mailchimp_sync_error'); + $setup->removeAttribute('customer', 'mailchimp_sync_modified'); + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + + $coreResource = $this->getCoreResource(); + + try { + $quoteTable = $coreResource->getTableName('sales/quote'); + $connectionQuote = $setup->getConnection(); + $connectionQuote->dropColumn($quoteTable, 'mailchimp_sync_delta'); + $connectionQuote->dropColumn($quoteTable, 'mailchimp_sync_error'); + $connectionQuote->dropColumn($quoteTable, 'mailchimp_deleted'); + $connectionQuote->dropColumn($quoteTable, 'mailchimp_token'); + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + + try { + $orderTable = $coreResource->getTableName('sales/order'); + $connectionOrder = $setup->getConnection(); + $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_delta'); + $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_error'); + $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_modified'); + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + } + + /** + * Migrate Customers from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $mailchimpStoreId + * @param $initialTime + * @throws Mage_Core_Exception + */ + protected function _migrateCustomersFrom115($mailchimpStoreId, $initialTime) + { + try { + $entityType = Mage::getSingleton('eav/config')->getEntityType('customer'); + $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'mailchimp_sync_delta'); + + if ($attribute->getId()) { + $mailchimpTableName = $this->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); + $customerCollection = Mage::getResourceModel('customer/customer_collection'); + $customerCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); + $customerCollection->getSelect()->joinLeft( + array('m4m' => $mailchimpTableName), + "m4m.related_id = e.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER + . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", + array('m4m.*') + ); + $customerCollection->getSelect()->where( + "m4m.mailchimp_sync_delta IS null" + ); + $this->_makeForCollectionItem( + $customerCollection, + $mailchimpStoreId, + $initialTime, + function ($customer, $mailchimpStoreId) { + $customerId = $customer->getEntityId(); + $customerObject = Mage::getModel('customer/customer')->load($customerId); + $syncError = null; + $syncModified = null; + $syncDelta = $customerObject->getMailchimpSyncDelta(); + + if ($customer->getMailchimpSyncError()) { + $syncError = $customer->getMailchimpSyncError(); + } + + if ($customer->getMailchimpSyncModified()) { + $syncModified = $customer->getMailchimpSyncModified(); + } + + $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData->saveEcommerceSyncData( + $customerId, + Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER, + $mailchimpStoreId, + $syncDelta, + $syncError, + $syncModified + ); + } + ); + } + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + } + + /** + * Migrate Products from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $mailchimpStoreId + * @param $initialTime + * @throws Mage_Core_Exception + */ + protected function _migrateProductsFrom115($mailchimpStoreId, $initialTime) + { + try { + $entityType = Mage_Catalog_Model_Product::ENTITY; + $attributeCode = 'mailchimp_sync_delta'; + $attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entityType, $attributeCode); + + if ($attribute->getId()) { + $mailchimpTableName = $this->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); + $productCollection = Mage::getResourceModel('catalog/product_collection'); + $productCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); + $productCollection->getSelect()->joinLeft( + array('m4m' => $mailchimpTableName), + "m4m.related_id = e.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_PRODUCT + . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", + array('m4m.*') + ); + $productCollection->getSelect()->where("m4m.mailchimp_sync_delta IS null"); + $this->_makeForCollectionItem( + $productCollection, + $mailchimpStoreId, + $initialTime, + function ($product, $mailchimpStoreId) { + $productId = $product->getEntityId(); + $_resource = Mage::getResourceSingleton('catalog/product'); + $syncDelta = $_resource->getAttributeRawValue( + $productId, + 'mailchimp_sync_delta', + $this->getMageApp()->getStore() + ); + $syncError = null; + $syncModified = null; + + if ($product->getMailchimpSyncError()) { + $syncError = $product->getMailchimpSyncError(); + } + + if ($product->getMailchimpSyncModified()) { + $syncModified = $product->getMailchimpSyncModified(); + } + + $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData->saveEcommerceSyncData( + $productId, + Ebizmarts_MailChimp_Model_Config::IS_PRODUCT, + $mailchimpStoreId, + $syncDelta, + $syncError, + $syncModified + ); + } + ); + } + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + } + + /** + * Migrate Orders from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $mailchimpStoreId + * @param $initialTime + * @throws Mage_Core_Exception + */ + protected function _migrateOrdersFrom115($mailchimpStoreId, $initialTime) + { + try { + $resource = $this->getCoreResource(); + $readConnection = $resource->getConnection('core_read'); + $tableName = $resource->getTableName('sales/order'); + $orderFields = $readConnection->describeTable($tableName); + + if (isset($orderFields['mailchimp_sync_delta'])) { + $mailchimpTableName = $resource->getTableName('mailchimp/ecommercesyncdata'); + $orderCollection = Mage::getResourceModel('sales/order_collection'); + + $orderCollection->getSelect()->joinLeft( + array('m4m' => $mailchimpTableName), + "m4m.related_id = main_table.entity_id AND m4m.type = '" + . Ebizmarts_MailChimp_Model_Config::IS_ORDER . + "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", + array('m4m.*') + ); + $orderCollection->getSelect() + ->where( + "m4m.mailchimp_sync_delta IS NULL AND main_table.mailchimp_sync_delta > '0000-00-00 00:00:00'" + ); + $this->_makeForCollectionItem( + $orderCollection, + $mailchimpStoreId, + $initialTime, + function ($order, $mailchimpStoreId) { + $orderId = $order->getEntityId(); + $syncError = null; + $syncModified = null; + $orderObject = $this->getSalesOrderModel()->load($orderId); + $syncDelta = $orderObject->getMailchimpSyncDelta(); + + if ($order->getMailchimpSyncError()) { + $syncError = $order->getMailchimpSyncError(); + } + + if ($order->getMailchimpSyncModified()) { + $syncModified = $order->getMailchimpSyncModified(); + } + + $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData->saveEcommerceSyncData( + $orderId, + Ebizmarts_MailChimp_Model_Config::IS_ORDER, + $mailchimpStoreId, + $syncDelta, + $syncError, + $syncModified + ); + } + ); + } + } catch (Exception $e) { + $this->logError($e->getMessage()); + } + } + + /** + * Migrate Carts from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $mailchimpStoreId + * @param $initialTime + * @return bool + * @throws Mage_Core_Exception + */ + protected function _migrateCartsFrom115($mailchimpStoreId, $initialTime) + { + try { + $resource = $this->getCoreResource(); + $readConnection = $resource->getConnection('core_read'); + $tableName = $resource->getTableName('sales/quote'); + $quoteFields = $readConnection->describeTable($tableName); + + if (isset($quoteFields['mailchimp_sync_delta'])) { + $mailchimpTableName = $resource->getTableName('mailchimp/ecommercesyncdata'); + $quoteCollection = Mage::getResourceModel('sales/quote_collection'); + $quoteCollection->getSelect()->joinLeft( + array('m4m' => $mailchimpTableName), + "m4m.related_id = main_table.entity_id AND m4m.type = '" + . Ebizmarts_MailChimp_Model_Config::IS_QUOTE + . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", + array('m4m.*') + ); + // be sure that the quotes are already in mailchimp and not deleted + $quoteCollection->getSelect() + ->where( + "m4m.mailchimp_sync_delta IS NULL AND main_table.mailchimp_sync_delta > '0000-00-00 00:00:00'" + ); + $finished = $this->_makeForCollectionItem( + $quoteCollection, + $mailchimpStoreId, + $initialTime, + function ($quote, $mailchimpStoreId) { + $quoteId = $quote->getEntityId(); + $syncError = null; + $syncDeleted = null; + $token = null; + $quoteObject = $this->getSalesOrderModel()->load($quoteId); + $syncDelta = $quoteObject->getMailchimpSyncDelta(); + + if ($quote->getMailchimpSyncError()) { + $syncError = $quote->getMailchimpSyncError(); + } + + if ($quote->getMailchimpSyncDeleted()) { + $syncDeleted = $quote->getMailchimpSyncDeleted(); + } + + if ($quote->getMailchimpToken()) { + $token = $quote->getMailchimpToken(); + } + + $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData->saveEcommerceSyncData( + $quoteId, + Ebizmarts_MailChimp_Model_Config::IS_QUOTE, + $mailchimpStoreId, + $syncDelta, + $syncError, + null, + $syncDeleted, + $token + ); + } + ); + } else { + $finished = true; + } + + return $finished; + } catch (Exception $e) { + $this->logError( + $this->__( + 'Unexpected error happened during migration from version 1.1.5 to 1.1.6.' + . 'Please contact our support at ' + ) . 'mailchimp@ebizmarts-desk.zendesk.com' + . $this->__(' See error details below.') + ); + $this->logError($e->getMessage()); + + return false; + } + } + + /** + * Delete config data for migration from 1.1.5. + */ + protected function delete115MigrationConfigData() + { + $this->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, + 'default', + 0 + ); + } + + /** + * Migrate data from version 1.1.6. + * + * @param $initialTime + */ + protected function _migrateFrom116($initialTime) + { + $this->_setIsSyncingIfFinishedInAllStores(true); + $finished = $this->_migrateOrdersFrom116($initialTime); + + if ($finished) { + $this->_setIsSyncingIfFinishedInAllStores(false); + $arrayMigrationConfigData = array('115' => false, '116' => true, '1164' => false); + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Update Order ids to the Increment id in MailChimp. + * + * @param $initialTime + * @return bool + */ + protected function _migrateOrdersFrom116($initialTime) + { + $dateHelper = $this->getDateHelper(); + $finished = false; + + if (!$dateHelper->timePassed($initialTime)) { + $finished = true; + $stores = $this->getMageApp()->getStores(); + + foreach ($stores as $storeId => $store) { + if ($this->isEcomSyncDataEnabled($storeId)) { + Mage::getModel('mailchimp/api_batches')->replaceAllOrders($initialTime, $storeId); + } + + if ($dateHelper->timePassed($initialTime)) { + $finished = false; + break; + } + } + } + + return $finished; + } + + /** + * Return if migration has finished checking the config values. + * + * @return bool + * @throws Mage_Core_Exception + */ + public function migrationFinished() + { + $migrationFinished = false; + + $migrateFrom115 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, + 0, + 'default' + ); + + $migrateFrom116 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, + 0, + 'default' + ); + + $migrateFrom1164 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, + 0, + 'default' + ); + + $migrateFrom1120 = $this->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 0, + 'default' + ); + + if (!$migrateFrom115 && !$migrateFrom116 && !$migrateFrom1164 && !$migrateFrom1120) { + $migrationFinished = true; + } + + return $migrationFinished; + } + + /** + * Delete config data for migration from 1.1.6. + */ + public function delete116MigrationConfigData() + { + $stores = $this->getMageApp()->getStores(); + $this->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, + 'default', + 0 + ); + + foreach ($stores as $storeId => $store) { + $this->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_LAST_ORDER_ID, + 'stores', + $storeId + ); + } + } + + /** + * Migrate data from version 1.1.6.4. + * + * @param $initialTime + */ + protected function _migrateFrom1164($initialTime) + { + $dateHelper = $this->getDateHelper(); + + if (!$dateHelper->timePassed($initialTime)) { + $writeConnection = $this->getCoreResource()->getConnection('core_write'); + $resource = Mage::getResourceModel('mailchimp/ecommercesyncdata'); + $writeConnection->update($resource->getMainTable(), array('batch_id' => '1'), "batch_id = 0"); + $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => true); + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Delete config data for migration from 1.1.6.4. + */ + protected function delete1164MigrationConfigData() + { + $this->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, + 'default', + 0 + ); + } + + /** + * Migrate data from version 1.1.21. + * + * @param $initialTime + */ + protected function _migrateFrom1120($initialTime) + { + $dateHelper = $this->getDateHelper(); + $webhookHelper = $this->getWebhookHelper(); + + if (!$dateHelper->timePassed($initialTime)) { + // Get all stores data. + $stores = $this->getMageApp()->getStores(); + + $events = array( + 'subscribe' => true, + 'unsubscribe' => true, + 'profile' => true, + 'cleaned' => true, + 'upemail' => true, + 'campaign' => false + ); + + $sources = array( + 'user' => true, + 'admin' => true, + 'api' => false + ); + + foreach ($stores as $storeId => $store) { + // Gets the ListId and WebhookId for the iterated store. + $listId = $this->getGeneralList($scopeId, $scope); + $webhookId = $webhookHelper->getWebhookId($scopeId, $scope); + + // Edits the webhook with the new $event array. + $this + ->getApi($storeId, $store) + ->getLists() + ->getWebhooks() + ->edit($listId, $webhookId, null, $events, $sources); + + if ($dateHelper->timePassed($initialTime)) { + $finished = false; + break; + } + } + + $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => false, '1120' => true); + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Delete config data for migration from 1.1.21. + */ + protected function delete1120MigrationConfigData() + { + $this->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 'default', + 0 + ); + } + + /** + * @param $arrayMigrationConfigData + */ + public function handleDeleteMigrationConfigData($arrayMigrationConfigData) + { + foreach ($arrayMigrationConfigData as $migrationConfigData => $value) { + if ($migrationConfigData == '115' && $value) { + $this->delete115MigrationConfigData(); + } + + if ($migrationConfigData == '116' && $value) { + $this->delete116MigrationConfigData(); + } + + if ($migrationConfigData == '1164' && $value) { + $this->delete1164MigrationConfigData(); + } + + if ($migrationConfigData == '1120' && $value) { + $this->delete1120MigrationConfigData(); + } + } + + $this->getConfig()->cleanCache(); + } + + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected function _getHelper() + { + return $this->_helper; + } + + /** + * @var Ebizmarts_MailChimp_Helper_Date + */ + protected function _getDateHelper() + { + return $this->_dateHelper; + } + + /** + * @var Ebizmarts_MailChimp_Helper_Webhook + */ + protected function _getWebhookHelper() + { + return $this->_webhookHelper; + } +} \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Cron.php b/app/code/community/Ebizmarts/MailChimp/Model/Cron.php index bc1c0f657..1205ee7a8 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Cron.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Cron.php @@ -16,18 +16,23 @@ class Ebizmarts_MailChimp_Model_Cron * @var Ebizmarts_MailChimp_Helper_Data */ protected $_mailChimpHelper; + /** + * @var Ebizmarts_MailChimp_Helper_Migration + */ + protected $_mailChimpMigrationHelper; public function __construct() { $this->_mailChimpHelper = Mage::helper('mailchimp'); + $this->_mailChimpMigrationHelper = Mage::helper('mailchimp/migration'); } public function syncEcommerceBatchData() { - if ($this->getHelper()->migrationFinished()) { + if ($this->getMigrationHelper()->migrationFinished()) { Mage::getModel('mailchimp/api_batches')->handleEcommerceBatches(); } else { - $this->getHelper()->handleMigrationUpdates(); + $this->getMigrationHelper()->handleMigrationUpdates(); } } @@ -55,4 +60,9 @@ protected function getHelper() { return $this->_mailChimpHelper; } + + protected function getMigrationHelper() + { + return $this->_mailChimpMigrationHelper; + } } 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 18c539e5e..3f362701c 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 @@ -26,6 +26,11 @@ class Ebizmarts_MailChimp_Model_System_Config_Source_Account */ protected $_helper; + /** + * @var Ebizmarts_MailChimp_Helper_Migration + */ + protected $_migrationHelper; + const USERNAME_KEY = 0; const TOTAL_ACCOUNT_SUB_KEY = 1; const TOTAL_LIST_SUB_KEY = 2; @@ -56,6 +61,7 @@ public function __construct($params) { $mcStore = null; $helper = $this->_helper = $this->makeHelper(); + $migrationHelper = $this->makeMigrationHelper(); $scopeArray = $helper->getCurrentScope(); $apiKey = (empty($params)) ? $helper->getApiKey($scopeArray['scope_id'], $scopeArray['scope']) @@ -138,6 +144,7 @@ public function __construct($params) public function toOptionArray() { $helper = $this->_helper; + $migrationHelper = $this->_migrationHelper; $scopeArray = $helper->getCurrentScope(); if (is_array($this->_accountDetails)) { $totalAccountSubscribersText = $helper->__('Total Account Subscribers:'); @@ -208,7 +215,7 @@ public function toOptionArray() ); } - if (!$helper->migrationFinished() + if (!$migrationHelper->migrationFinished() && $helper->isEcommerceEnabled($scopeArray['scope_id'], $scopeArray['scope']) ) { $storeMigrationText = $helper->__( @@ -239,6 +246,14 @@ protected function makeHelper() return Mage::helper('mailchimp'); } + /** + * @return Ebizmarts_MailChimp_Helper_Migration + */ + protected function makeMigrationHelper() + { + return Mage::helper('mailchimp/migration'); + } + /** * @param $mailchimpStoreId * @return string diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php index 7732bf699..a5ed4c9a3 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php @@ -12,7 +12,6 @@ /* If webhook is created, edites it and place the new "event" variable */ if ($configDataCollection->getSize()) { - Mage::log('Setting constant', null, 'giorello.log', true); // Sets the migration flag to edit webhooks. Mage::helper('mailchimp') ->saveMailChimpConfig( From 4b6e077e69a86481a943fc86a04177ad269831f6 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 6 Apr 2020 13:35:43 -0300 Subject: [PATCH 26/51] Tests added, class name fixed, data helper references fixed --- .../Ebizmarts/MailChimp/Helper/Migration.php | 126 +++++++++++------- .../Ebizmarts/MailChimp/Helper/DataTest.php | 29 ---- .../MailChimp/Helper/MigrationTest.php | 44 ++++++ 3 files changed, 119 insertions(+), 80 deletions(-) create mode 100644 dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/MigrationTest.php diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php b/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php index b2db68a89..159ab978c 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php @@ -10,7 +10,7 @@ * @date: 3/20/2020 11:14 AM * @file: Webhook.php */ -class Ebizmarts_MailChimp_Helper_Webhook extends Mage_Core_Helper_Abstract +class Ebizmarts_MailChimp_Helper_Migration extends Mage_Core_Helper_Abstract { /** * @var Ebizmarts_MailChimp_Helper_Data @@ -39,25 +39,26 @@ public function __construct() */ public function handleMigrationUpdates() { + $helper = $this->getHelper(); $dateHelper = $this->getDateHelper(); $initialTime = $dateHelper->getTimestamp(); - $migrateFrom115 = $this->getConfigValueForScope( + $migrateFrom115 = $helper->getConfigValueForScope( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, 0, 'default' ); - $migrateFrom116 = $this->getConfigValueForScope( + $migrateFrom116 = $helper->getConfigValueForScope( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, 0, 'default' ); - $migrateFrom1164 = $this->getConfigValueForScope( + $migrateFrom1164 = $helper->getConfigValueForScope( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, 0, 'default' ); - $migrateFrom1120 = $this->getConfigValueForScope( + $migrateFrom1120 = $helper->getConfigValueForScope( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, 0, 'default' @@ -82,10 +83,11 @@ public function handleMigrationUpdates() */ protected function _migrateFrom115($initialTime) { + $helper = $this->getHelper(); $dateHelper = $this->getDateHelper(); $arrayMigrationConfigData = array('115' => true, '116' => false, '1164' => false); //migrate data from older version to the new schemma - if ($this->isEcommerceEnabled(0)) { + if ($helper->isEcommerceEnabled(0)) { $mailchimpStoreId = $this->getMCStoreId(0); //migrate customers @@ -125,6 +127,7 @@ protected function _migrateFrom115($initialTime) */ protected function _makeForCollectionItem($collection, $mailchimpStoreId, $initialTime, Closure $callback) { + $helper = $this->getHelper(); $dateHelper = $this->getDateHelper(); $finished = false; @@ -139,7 +142,7 @@ protected function _makeForCollectionItem($collection, $mailchimpStoreId, $initi do { $collection->setCurPage($currentPage); - $this->_loadItemCollection($collection); + $helper->_loadItemCollection($collection); foreach ($collection as $collectionItem) { $callback($collectionItem, $mailchimpStoreId); @@ -165,6 +168,7 @@ protected function _makeForCollectionItem($collection, $mailchimpStoreId, $initi protected function _migrateFrom115dropColumn($arrayMigrationConfigData) { + $helper = $this->getHelper(); $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); //Remove attributes no longer used @@ -178,10 +182,10 @@ protected function _migrateFrom115dropColumn($arrayMigrationConfigData) $setup->removeAttribute('customer', 'mailchimp_sync_error'); $setup->removeAttribute('customer', 'mailchimp_sync_modified'); } catch (Exception $e) { - $this->logError($e->getMessage()); + $helper->logError($e->getMessage()); } - $coreResource = $this->getCoreResource(); + $coreResource = $helper->getCoreResource(); try { $quoteTable = $coreResource->getTableName('sales/quote'); @@ -191,7 +195,7 @@ protected function _migrateFrom115dropColumn($arrayMigrationConfigData) $connectionQuote->dropColumn($quoteTable, 'mailchimp_deleted'); $connectionQuote->dropColumn($quoteTable, 'mailchimp_token'); } catch (Exception $e) { - $this->logError($e->getMessage()); + $helper->logError($e->getMessage()); } try { @@ -201,7 +205,7 @@ protected function _migrateFrom115dropColumn($arrayMigrationConfigData) $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_error'); $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_modified'); } catch (Exception $e) { - $this->logError($e->getMessage()); + $helper->logError($e->getMessage()); } } @@ -214,12 +218,14 @@ protected function _migrateFrom115dropColumn($arrayMigrationConfigData) */ protected function _migrateCustomersFrom115($mailchimpStoreId, $initialTime) { + $helper = $this->getHelper(); + try { $entityType = Mage::getSingleton('eav/config')->getEntityType('customer'); $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'mailchimp_sync_delta'); if ($attribute->getId()) { - $mailchimpTableName = $this->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); + $mailchimpTableName = $helper->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); $customerCollection = Mage::getResourceModel('customer/customer_collection'); $customerCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); $customerCollection->getSelect()->joinLeft( @@ -250,7 +256,7 @@ function ($customer, $mailchimpStoreId) { $syncModified = $customer->getMailchimpSyncModified(); } - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData = $helper->getMailchimpEcommerceSyncDataModel(); $ecommerceSyncData->saveEcommerceSyncData( $customerId, Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER, @@ -263,7 +269,7 @@ function ($customer, $mailchimpStoreId) { ); } } catch (Exception $e) { - $this->logError($e->getMessage()); + $helper->logError($e->getMessage()); } } @@ -276,13 +282,15 @@ function ($customer, $mailchimpStoreId) { */ protected function _migrateProductsFrom115($mailchimpStoreId, $initialTime) { + $helper = $this->getHelper(); + try { $entityType = Mage_Catalog_Model_Product::ENTITY; $attributeCode = 'mailchimp_sync_delta'; $attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entityType, $attributeCode); if ($attribute->getId()) { - $mailchimpTableName = $this->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); + $mailchimpTableName = $helper->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); $productCollection = Mage::getResourceModel('catalog/product_collection'); $productCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); $productCollection->getSelect()->joinLeft( @@ -302,7 +310,7 @@ function ($product, $mailchimpStoreId) { $syncDelta = $_resource->getAttributeRawValue( $productId, 'mailchimp_sync_delta', - $this->getMageApp()->getStore() + $helper->getMageApp()->getStore() ); $syncError = null; $syncModified = null; @@ -315,7 +323,7 @@ function ($product, $mailchimpStoreId) { $syncModified = $product->getMailchimpSyncModified(); } - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData = $helper->getMailchimpEcommerceSyncDataModel(); $ecommerceSyncData->saveEcommerceSyncData( $productId, Ebizmarts_MailChimp_Model_Config::IS_PRODUCT, @@ -328,7 +336,7 @@ function ($product, $mailchimpStoreId) { ); } } catch (Exception $e) { - $this->logError($e->getMessage()); + $helper->logError($e->getMessage()); } } @@ -341,8 +349,10 @@ function ($product, $mailchimpStoreId) { */ protected function _migrateOrdersFrom115($mailchimpStoreId, $initialTime) { + $helper = $this->getHelper(); + try { - $resource = $this->getCoreResource(); + $resource = $helper->getCoreResource(); $readConnection = $resource->getConnection('core_read'); $tableName = $resource->getTableName('sales/order'); $orderFields = $readConnection->describeTable($tableName); @@ -370,7 +380,7 @@ function ($order, $mailchimpStoreId) { $orderId = $order->getEntityId(); $syncError = null; $syncModified = null; - $orderObject = $this->getSalesOrderModel()->load($orderId); + $orderObject = $helper->getSalesOrderModel()->load($orderId); $syncDelta = $orderObject->getMailchimpSyncDelta(); if ($order->getMailchimpSyncError()) { @@ -381,7 +391,7 @@ function ($order, $mailchimpStoreId) { $syncModified = $order->getMailchimpSyncModified(); } - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData = $helper->getMailchimpEcommerceSyncDataModel(); $ecommerceSyncData->saveEcommerceSyncData( $orderId, Ebizmarts_MailChimp_Model_Config::IS_ORDER, @@ -394,7 +404,7 @@ function ($order, $mailchimpStoreId) { ); } } catch (Exception $e) { - $this->logError($e->getMessage()); + $helper->logError($e->getMessage()); } } @@ -408,8 +418,10 @@ function ($order, $mailchimpStoreId) { */ protected function _migrateCartsFrom115($mailchimpStoreId, $initialTime) { + $helper = $this->getHelper(); + try { - $resource = $this->getCoreResource(); + $resource = $helper->getCoreResource(); $readConnection = $resource->getConnection('core_read'); $tableName = $resource->getTableName('sales/quote'); $quoteFields = $readConnection->describeTable($tableName); @@ -438,7 +450,7 @@ function ($quote, $mailchimpStoreId) { $syncError = null; $syncDeleted = null; $token = null; - $quoteObject = $this->getSalesOrderModel()->load($quoteId); + $quoteObject = $helper->getSalesOrderModel()->load($quoteId); $syncDelta = $quoteObject->getMailchimpSyncDelta(); if ($quote->getMailchimpSyncError()) { @@ -453,7 +465,7 @@ function ($quote, $mailchimpStoreId) { $token = $quote->getMailchimpToken(); } - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData = $helper->getMailchimpEcommerceSyncDataModel(); $ecommerceSyncData->saveEcommerceSyncData( $quoteId, Ebizmarts_MailChimp_Model_Config::IS_QUOTE, @@ -472,14 +484,14 @@ function ($quote, $mailchimpStoreId) { return $finished; } catch (Exception $e) { - $this->logError( - $this->__( + $helper->logError( + $helper->__( 'Unexpected error happened during migration from version 1.1.5 to 1.1.6.' . 'Please contact our support at ' ) . 'mailchimp@ebizmarts-desk.zendesk.com' - . $this->__(' See error details below.') + . $helper->__(' See error details below.') ); - $this->logError($e->getMessage()); + $helper->logError($e->getMessage()); return false; } @@ -490,7 +502,8 @@ function ($quote, $mailchimpStoreId) { */ protected function delete115MigrationConfigData() { - $this->getConfig()->deleteConfig( + $helper = $this->getHelper(); + $helper->getConfig()->deleteConfig( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, 'default', 0 @@ -504,11 +517,12 @@ protected function delete115MigrationConfigData() */ protected function _migrateFrom116($initialTime) { - $this->_setIsSyncingIfFinishedInAllStores(true); + $helper = $this->getHelper(); + $helper->_setIsSyncingIfFinishedInAllStores(true); $finished = $this->_migrateOrdersFrom116($initialTime); if ($finished) { - $this->_setIsSyncingIfFinishedInAllStores(false); + $helper->_setIsSyncingIfFinishedInAllStores(false); $arrayMigrationConfigData = array('115' => false, '116' => true, '1164' => false); $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); } @@ -522,15 +536,16 @@ protected function _migrateFrom116($initialTime) */ protected function _migrateOrdersFrom116($initialTime) { + $helper = $this->getHelper(); $dateHelper = $this->getDateHelper(); $finished = false; if (!$dateHelper->timePassed($initialTime)) { $finished = true; - $stores = $this->getMageApp()->getStores(); + $stores = $helper->getMageApp()->getStores(); foreach ($stores as $storeId => $store) { - if ($this->isEcomSyncDataEnabled($storeId)) { + if ($helper->isEcomSyncDataEnabled($storeId)) { Mage::getModel('mailchimp/api_batches')->replaceAllOrders($initialTime, $storeId); } @@ -552,27 +567,28 @@ protected function _migrateOrdersFrom116($initialTime) */ public function migrationFinished() { + $helper = $this->getHelper(); $migrationFinished = false; - $migrateFrom115 = $this->getConfigValueForScope( + $migrateFrom115 = $helper->getConfigValueForScope( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, 0, 'default' ); - $migrateFrom116 = $this->getConfigValueForScope( + $migrateFrom116 = $helper->getConfigValueForScope( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, 0, 'default' ); - $migrateFrom1164 = $this->getConfigValueForScope( + $migrateFrom1164 = $helper->getConfigValueForScope( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, 0, 'default' ); - $migrateFrom1120 = $this->getConfigValueForScope( + $migrateFrom1120 = $helper->getConfigValueForScope( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, 0, 'default' @@ -590,15 +606,16 @@ public function migrationFinished() */ public function delete116MigrationConfigData() { - $stores = $this->getMageApp()->getStores(); - $this->getConfig()->deleteConfig( + $helper = $this->getHelper(); + $stores = $helper->getMageApp()->getStores(); + $helper->getConfig()->deleteConfig( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, 'default', 0 ); foreach ($stores as $storeId => $store) { - $this->getConfig()->deleteConfig( + $helper->getConfig()->deleteConfig( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_LAST_ORDER_ID, 'stores', $storeId @@ -613,10 +630,11 @@ public function delete116MigrationConfigData() */ protected function _migrateFrom1164($initialTime) { + $helper = $this->getHelper(); $dateHelper = $this->getDateHelper(); if (!$dateHelper->timePassed($initialTime)) { - $writeConnection = $this->getCoreResource()->getConnection('core_write'); + $writeConnection = $helper->getCoreResource()->getConnection('core_write'); $resource = Mage::getResourceModel('mailchimp/ecommercesyncdata'); $writeConnection->update($resource->getMainTable(), array('batch_id' => '1'), "batch_id = 0"); $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => true); @@ -629,7 +647,9 @@ protected function _migrateFrom1164($initialTime) */ protected function delete1164MigrationConfigData() { - $this->getConfig()->deleteConfig( + $helper = $this->getHelper(); + + $helper->getConfig()->deleteConfig( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, 'default', 0 @@ -643,12 +663,13 @@ protected function delete1164MigrationConfigData() */ protected function _migrateFrom1120($initialTime) { + $helper = $this->getHelper(); $dateHelper = $this->getDateHelper(); $webhookHelper = $this->getWebhookHelper(); if (!$dateHelper->timePassed($initialTime)) { // Get all stores data. - $stores = $this->getMageApp()->getStores(); + $stores = $helper->getMageApp()->getStores(); $events = array( 'subscribe' => true, @@ -667,11 +688,11 @@ protected function _migrateFrom1120($initialTime) foreach ($stores as $storeId => $store) { // Gets the ListId and WebhookId for the iterated store. - $listId = $this->getGeneralList($scopeId, $scope); + $listId = $helper->getGeneralList($scopeId, $scope); $webhookId = $webhookHelper->getWebhookId($scopeId, $scope); // Edits the webhook with the new $event array. - $this + $helper ->getApi($storeId, $store) ->getLists() ->getWebhooks() @@ -693,7 +714,9 @@ protected function _migrateFrom1120($initialTime) */ protected function delete1120MigrationConfigData() { - $this->getConfig()->deleteConfig( + $helper = $this->getHelper(); + + $helper->getConfig()->deleteConfig( Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, 'default', 0 @@ -705,6 +728,7 @@ protected function delete1120MigrationConfigData() */ public function handleDeleteMigrationConfigData($arrayMigrationConfigData) { + $helper = $this->getHelper(); foreach ($arrayMigrationConfigData as $migrationConfigData => $value) { if ($migrationConfigData == '115' && $value) { $this->delete115MigrationConfigData(); @@ -723,13 +747,13 @@ public function handleDeleteMigrationConfigData($arrayMigrationConfigData) } } - $this->getConfig()->cleanCache(); + $helper->getConfig()->cleanCache(); } /** * @var Ebizmarts_MailChimp_Helper_Data */ - protected function _getHelper() + protected function getHelper() { return $this->_helper; } @@ -737,7 +761,7 @@ protected function _getHelper() /** * @var Ebizmarts_MailChimp_Helper_Date */ - protected function _getDateHelper() + protected function getDateHelper() { return $this->_dateHelper; } @@ -745,7 +769,7 @@ protected function _getDateHelper() /** * @var Ebizmarts_MailChimp_Helper_Webhook */ - protected function _getWebhookHelper() + protected function getWebhookHelper() { return $this->_webhookHelper; } 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 76efd6cc2..e6c5636bc 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -1672,35 +1672,6 @@ public function testGetAllApiKeys() $helperMock->getAllApiKeys(); } - public function testHandleDeleteMigrationConfigData() - { - $arrayMigrationConfigData = array('115' => true, '116' => true, '1164' => true); - - $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) - ->disableOriginalConstructor() - ->setMethods( - array( - 'delete115MigrationConfigData', 'delete116MigrationConfigData', - 'delete1164MigrationConfigData', 'getConfig' - ) - ) - ->getMock(); - - $modelConfigMock = $this->getMockBuilder(Mage_Core_Model_Config::class) - ->disableOriginalConstructor() - ->setMethods(array('cleanCache')) - ->getMock(); - - $helperMock->expects($this->once())->method('delete115MigrationConfigData'); - $helperMock->expects($this->once())->method('delete116MigrationConfigData'); - $helperMock->expects($this->once())->method('delete1164MigrationConfigData'); - $helperMock->expects($this->once())->method('getConfig')->willReturn($modelConfigMock); - - $modelConfigMock->expects($this->once())->method('cleanCache'); - - $helperMock->handleDeleteMigrationConfigData($arrayMigrationConfigData); - } - public function testGetSessionLastRealOrderM19() { $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/MigrationTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/MigrationTest.php new file mode 100644 index 000000000..b7d773101 --- /dev/null +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/MigrationTest.php @@ -0,0 +1,44 @@ + true, '116' => true, '1164' => true); + + $migrationHelperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Migration::class) + ->disableOriginalConstructor() + ->setMethods( + array( + 'delete115MigrationConfigData', 'delete116MigrationConfigData', + 'delete1164MigrationConfigData', 'getHelper' + ) + ) + ->getMock(); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getConfig')) + ->getMock(); + + $modelConfigMock = $this->getMockBuilder(Mage_Core_Model_Config::class) + ->disableOriginalConstructor() + ->setMethods(array('cleanCache')) + ->getMock(); + + $migrationHelperMock->expects($this->once())->method('getHelper')->willReturn($helperMock); + $migrationHelperMock->expects($this->once())->method('delete115MigrationConfigData'); + $migrationHelperMock->expects($this->once())->method('delete116MigrationConfigData'); + $migrationHelperMock->expects($this->once())->method('delete1164MigrationConfigData'); + $helperMock->expects($this->once())->method('getConfig')->willReturn($modelConfigMock); + + $modelConfigMock->expects($this->once())->method('cleanCache'); + + $migrationHelperMock->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } +} From 5362b8ba22713e19e8888b43efeb9c99d2b9c162 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 6 Apr 2020 14:27:31 -0300 Subject: [PATCH 27/51] Solvin PHPCS errors --- .../Ebizmarts/MailChimp/Helper/Migration.php | 4 ++-- .../mysql4-upgrade-1.1.20-1.1.21.php | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php b/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php index 159ab978c..c3592fb1d 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php @@ -679,7 +679,7 @@ protected function _migrateFrom1120($initialTime) 'upemail' => true, 'campaign' => false ); - + $sources = array( 'user' => true, 'admin' => true, @@ -773,4 +773,4 @@ protected function getWebhookHelper() { return $this->_webhookHelper; } -} \ No newline at end of file +} diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php index a5ed4c9a3..f7078e2af 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php @@ -14,15 +14,15 @@ if ($configDataCollection->getSize()) { // Sets the migration flag to edit webhooks. Mage::helper('mailchimp') - ->saveMailChimpConfig( - array( + ->saveMailChimpConfig( array( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, - 1) - ), - 0, - 'default' - ); + array( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 1) + ), + 0, + 'default' + ); } } catch (Exception $e) { Mage::log($e->getMessage(), null, 'MailChimp_Errors.log', true); From 7f6e942a7756184f94a71e8b3c9c28774d8b329b Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 6 Apr 2020 14:35:44 -0300 Subject: [PATCH 28/51] Solvin PHPCS errors --- .../sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php index f7078e2af..f895cba0a 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php @@ -22,7 +22,7 @@ ), 0, 'default' - ); + ); } } catch (Exception $e) { Mage::log($e->getMessage(), null, 'MailChimp_Errors.log', true); From 512762394c6c7ede3c829cb0c65aa539b02b84f3 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Tue, 7 Apr 2020 09:01:54 -0300 Subject: [PATCH 29/51] Solved undefined var error --- .../Ebizmarts/MailChimp/Model/System/Config/Source/Account.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3f362701c..3be9278b2 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 @@ -61,7 +61,7 @@ public function __construct($params) { $mcStore = null; $helper = $this->_helper = $this->makeHelper(); - $migrationHelper = $this->makeMigrationHelper(); + $migrationHelper = $this->_migrationHelper = $this->makeMigrationHelper(); $scopeArray = $helper->getCurrentScope(); $apiKey = (empty($params)) ? $helper->getApiKey($scopeArray['scope_id'], $scopeArray['scope']) From f07fea22412c8dff5fc8691e6b4910d65e5afd22 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Tue, 7 Apr 2020 10:00:45 -0300 Subject: [PATCH 30/51] Added doccomment to webhoook edit in Lib --- lib/Ebizmarts/MailChimp/ListsWebhooks.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/Ebizmarts/MailChimp/ListsWebhooks.php b/lib/Ebizmarts/MailChimp/ListsWebhooks.php index 2df3ef986..cf38f1cd8 100644 --- a/lib/Ebizmarts/MailChimp/ListsWebhooks.php +++ b/lib/Ebizmarts/MailChimp/ListsWebhooks.php @@ -39,6 +39,15 @@ public function add($listId, $url = null, $events = null, $sources = null) return $this->_master->call('lists/' . $listId . '/webhooks', $_params, Ebizmarts_MailChimp::POST); } + /** + * @param $listId The unique id for the list. + * @param $webhookId The unique id for the webhook. + * @param null $url Email address for a subscriber. + * @param null $events The events that can trigger the webhook and whether they are enabled. + * @param null $sources The possible sources of any events that can trigger a webhook and + * whether they are enabled. + * @return mixed + */ public function edit($listId, $webhookId, $url = null, $events = null, $sources = null) { $_params = array(); From 0f64151080f5f441814d3b7647591fe333e6aaf3 Mon Sep 17 00:00:00 2001 From: Javier Giorello Date: Mon, 4 May 2020 12:21:29 -0300 Subject: [PATCH 31/51] Replaced quoteEscape for mcEscapeQuote method --- .../Adminhtml/Customer/Edit/Tab/Mailchimp.php | 8 +++ .../Sales/Order/View/Info/Monkey.php | 2 +- .../System/Config/Form/Field/Mapfields.php | 8 +++ .../MailChimp/Block/Checkout/Subscribe.php | 8 +++ .../Block/Checkout/Success/Groups.php | 16 ++--- .../Block/Customer/Newsletter/Index.php | 16 ++--- .../Ebizmarts/MailChimp/Block/Group/Type.php | 11 +++ .../MailChimp/Block/Popup/Emailcatcher.php | 7 ++ .../Ebizmarts/MailChimp/Helper/Data.php | 9 +++ .../mailchimp/customer/tab/mailchimp.phtml | 2 +- .../mailchimp/sales/order/view/monkey.phtml | 6 +- .../config/form/field/array_dropdown.phtml | 72 +++++++++---------- .../mailchimp/checkout/subscribe.phtml | 4 +- .../mailchimp/checkout/success/groups.phtml | 8 +-- .../mailchimp/customer/newsletter/index.phtml | 4 +- .../mailchimp/group/type/checkboxes.phtml | 4 +- .../mailchimp/group/type/dropdown.phtml | 2 +- .../mailchimp/group/type/radio.phtml | 4 +- .../mailchimp/popup/emailcatcher.phtml | 10 +-- dev/tests/mailchimp/autoload.php | 2 +- 20 files changed, 127 insertions(+), 76 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 60f1fef4a..137cc2078 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 @@ -51,6 +51,14 @@ public function getInterest() return $interest; } + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return $this->_helper; + } + /** * @return Mage_Newsletter_Model_Subscriber */ diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/View/Info/Monkey.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/View/Info/Monkey.php index c4032cffb..6449a6f56 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/View/Info/Monkey.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/View/Info/Monkey.php @@ -69,7 +69,7 @@ public function getCampaignName() /** * @return Ebizmarts_MailChimp_Helper_Data */ - protected function getMailChimpHelper() + public function getMailChimpHelper() { return Mage::helper('mailchimp'); } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Form/Field/Mapfields.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Form/Field/Mapfields.php index bd6951a45..5da085070 100755 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Form/Field/Mapfields.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Form/Field/Mapfields.php @@ -69,6 +69,14 @@ public function __construct() ksort($this->_customerAttributes); } + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return $this->_helper; + } + /** * @param string $columnName * @return string diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php index b7a733e63..3c65e661d 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php @@ -30,6 +30,14 @@ public function __construct() $this->_storeId = Mage::app()->getStore()->getId(); } + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return $this->_helper; + } + /** * Render block HTML * 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 2a0d38ccd..dd1c00b43 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -81,6 +81,14 @@ public function getMessageAfter() return $message; } + /** + * @return Ebizmarts_MailChimp_Helper_Data|Mage_Core_Helper_Abstract + */ + public function getMailChimpHelper() + { + return $this->_helper; + } + /** * @return false|Mage_Core_Model_Abstract */ @@ -101,12 +109,4 @@ protected function getSessionLastRealOrder() { return $this->getMailChimpHelper()->getSessionLastRealOrder(); } - - /** - * @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 90632c4c9..198c5f19d 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php @@ -57,21 +57,21 @@ public function getInterest() } /** - * Retrieve email from Customer object in session - * - * @return string Email address + * @return Ebizmarts_MailChimp_Helper_Data */ - protected function _getEmail() + public function getMailChimpHelper() { - return $this->getCustomerSession()->getCustomer()->getEmail(); + return $this->_helper; } /** - * @return Ebizmarts_MailChimp_Helper_Data + * Retrieve email from Customer object in session + * + * @return string Email address */ - protected function getMailChimpHelper() + protected function _getEmail() { - return $this->_helper; + return $this->getCustomerSession()->getCustomer()->getEmail(); } /** diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php b/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php index b31819dd2..6eec27d7f 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php @@ -11,9 +11,12 @@ class Ebizmarts_MailChimp_Block_Group_Type extends Mage_Core_Block_Template { protected $_currentInterest; + protected $_helper; public function __construct(array $args = array()) { + $this->_helper = Mage::helper('mailchimp'); + if (isset($args['interests'])) { $this->_currentInterest = $interests = $args['interests']; $type = $interests['interest']['type']; @@ -23,6 +26,14 @@ public function __construct(array $args = array()) parent::__construct($args); } + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return $this->_helper; + } + /** * @return mixed */ diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Popup/Emailcatcher.php b/app/code/community/Ebizmarts/MailChimp/Block/Popup/Emailcatcher.php index 93deaf83b..4d693dcbc 100755 --- a/app/code/community/Ebizmarts/MailChimp/Block/Popup/Emailcatcher.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Popup/Emailcatcher.php @@ -12,6 +12,13 @@ */ class Ebizmarts_MailChimp_Block_Popup_Emailcatcher extends Mage_Core_Block_Template { + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return Mage::helper('mailchimp'); + } protected function _canCancel() { diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 0f4528cdd..eac8baec2 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1174,6 +1174,15 @@ public function saveLastItemsSent($scopeId, $scope, $filters = null) } } + /** + * @param $data + * @return string + */ + public function mcEscapeQuote($data) + { + return htmlspecialchars($data, ENT_QUOTES, null, false); + } + /** * @param $scopeId * @param $scope 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 8276f3d8f..b24fb7c2f 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 @@ -4,7 +4,7 @@ $interest = $this->getInterest();
- quoteEscape($this->__('Mailchimp Information')) ?> + gethelper()->mcEscapeQuote($this->__('Mailchimp Information')) ?>
diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/view/monkey.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/view/monkey.phtml index b1dde8c54..55bff2b38 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/view/monkey.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/view/monkey.phtml @@ -1,7 +1,7 @@ isReferred()) : ?>
-

quoteEscape($this->__('MailChimp')) ?>

+

getMailChimpHelper()->mcEscapeQuote($this->__('MailChimp')) ?>

@@ -16,7 +16,7 @@ isDataAvailable()): ?> - quoteEscape( + getMailChimpHelper()->mcEscapeQuote( $this->__("Yaay! Recovered by Mailchimp's campaign.") . "
" . $this->__("Name") . @@ -27,7 +27,7 @@ ); ?>
- quoteEscape( + getMailChimpHelper()->mcEscapeQuote( $this->__("The campaign does not belong to the audience configured for ") . $this->getStoreCodeFromOrder() . $this->__(" view.") diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/system/config/form/field/array_dropdown.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/system/config/form/field/array_dropdown.phtml index b9b7622c4..12dd4a308 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/system/config/form/field/array_dropdown.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/system/config/form/field/array_dropdown.phtml @@ -14,28 +14,28 @@ if (!$this->_addAfter) { $colspan = $colspan > 1 ? 'colspan="' . $colspan . '"' : ''; ?> -
+
- + _columns as $columnName => $column): ?> - + - + - - + -
quoteEscape($column['label']) ?>gethelper()->mcEscapeQuote($column['label']) ?> quoteEscape($colspan) ?>>gethelper()->mcEscapeQuote($colspan) ?>>
+
quoteEscape($colspan) ?>> + gethelper()->mcEscapeQuote($colspan) ?>>
- +
-
+