diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index c72f7539d..f3f233191 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -848,7 +848,7 @@ public function deleteStore($scopeId, $scope) } } - protected function saveLastItemsSent($scopeId, $scope) + public function saveLastItemsSent($scopeId, $scope) { $isSyncing = $this->getMCIsSyncing($scopeId, $scope); if ($isSyncing != 1) { @@ -876,10 +876,12 @@ protected function getLastCustomerSent($scopeId, $scope) $mcStoreId = $this->getMCStoreId($scopeId, $scope); $syncDataCollection = Mage::getModel('mailchimp/ecommercesyncdata')->getCollection() ->addFieldToFilter('mailchimp_store_id', array('eq' => $mcStoreId)) - ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER)); + ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER)) + ->setOrder('related_id', 'DESC') + ->getSelect()->limit(1); if ($syncDataCollection->getSize()) { - $customerSyncData = $syncDataCollection->getLastItem(); + $customerSyncData = $syncDataCollection->getFirstItem(); $lastCustomerSent = $customerSyncData->getRelatedId(); } @@ -892,10 +894,12 @@ protected function getLastProductSent($scopeId, $scope) $mcStoreId = $this->getMCStoreId($scopeId, $scope); $syncDataCollection = Mage::getModel('mailchimp/ecommercesyncdata')->getCollection() ->addFieldToFilter('mailchimp_store_id', array('eq' => $mcStoreId)) - ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_PRODUCT)); + ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_PRODUCT)) + ->setOrder('related_id', 'DESC') + ->getSelect()->limit(1); if ($syncDataCollection->getSize()) { - $productSyncData = $syncDataCollection->getLastItem(); + $productSyncData = $syncDataCollection->getFirstItem(); $lastProductSent = $productSyncData->getRelatedId(); } @@ -908,10 +912,12 @@ protected function getLastOrderSent($scopeId, $scope) $mcStoreId = $this->getMCStoreId($scopeId, $scope); $syncDataCollection = Mage::getModel('mailchimp/ecommercesyncdata')->getCollection() ->addFieldToFilter('mailchimp_store_id', array('eq' => $mcStoreId)) - ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_ORDER)); + ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_ORDER)) + ->setOrder('related_id', 'DESC') + ->getSelect()->limit(1); if ($syncDataCollection->getSize()) { - $orderSyncData = $syncDataCollection->getLastItem(); + $orderSyncData = $syncDataCollection->getFirstItem(); $lastOrderSent = $orderSyncData->getRelatedId(); } @@ -924,10 +930,12 @@ protected function getLastCartSent($scopeId, $scope) $mcStoreId = $this->getMCStoreId($scopeId, $scope); $syncDataCollection = Mage::getModel('mailchimp/ecommercesyncdata')->getCollection() ->addFieldToFilter('mailchimp_store_id', array('eq' => $mcStoreId)) - ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_QUOTE)); + ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_QUOTE)) + ->setOrder('related_id', 'DESC') + ->getSelect()->limit(1); if ($syncDataCollection->getSize()) { - $cartSyncData = $syncDataCollection->getLastItem(); + $cartSyncData = $syncDataCollection->getFirstItem(); $lastCartSent = $cartSyncData->getRelatedId(); } return $lastCartSent; @@ -939,10 +947,12 @@ protected function getLastPromoCodeSent($scopeId, $scope) $mcStoreId = $this->getMCStoreId($scopeId, $scope); $syncDataCollection = Mage::getResourceModel('mailchimp/ecommercesyncdata_collection') ->addFieldToFilter('mailchimp_store_id', array('eq' => $mcStoreId)) - ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_PROMO_CODE)); + ->addFieldToFilter('type', array('eq' => Ebizmarts_MailChimp_Model_Config::IS_PROMO_CODE)) + ->setOrder('related_id', 'DESC') + ->getSelect()->limit(1); if ($syncDataCollection->getSize()) { - $promoCodeSyncData = $syncDataCollection->getLastItem(); + $promoCodeSyncData = $syncDataCollection->getFirstItem(); $lastPromoCodeSent = $promoCodeSyncData->getRelatedId(); } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php index c1890c31d..e926f0a7b 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php @@ -205,7 +205,6 @@ public function handleEcommerceBatches() $syncedDateArray = array(); foreach ($stores as $store) { $storeId = $store->getId(); - $this->handleResetIfNecessary($storeId); $syncedDateArray = $this->addSyncValueToArray($storeId, $syncedDateArray); } $this->handleSyncingValue($syncedDateArray); @@ -778,22 +777,6 @@ public function handleSyncingValue($syncedDateArray) } } - /** - * @param $storeId - */ - protected function handleResetIfNecessary($storeId) - { - $helper = $this->getHelper(); - if ($helper->getIsReset($storeId)) { - $scopeToReset = $helper->getMailChimpScopeByStoreId($storeId); - if ($scopeToReset) { - $helper->resetMCEcommerceData($scopeToReset['scope_id'], $scopeToReset['scope'], true); - $configValue = array(array(Ebizmarts_MailChimp_Model_Config::GENERAL_MCSTORE_RESETED, 0)); - $helper->saveMailchimpConfig($configValue, $scopeToReset['scope_id'], $scopeToReset['scope']); - } - } - } - /** * @return string */ 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 2958cee9a..b21453ddd 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -211,7 +211,7 @@ public function testGetDateSyncFinishByStoreId() ->getMock(); $helperMock->expects($this->once())->method('getMCStoreId')->with($scopeId, $scope)->willReturn($mailchimpStoreId); - $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE."_$mailchimpStoreId", $scopeId, $scope); + $helperMock->expects($this->once())->method('getConfigValueForScope')->with(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE . "_$mailchimpStoreId", $scopeId, $scope); $helperMock->getDateSyncFinishByStoreId($scopeId, $scope); } @@ -441,7 +441,8 @@ public function testGetImageFunctionName() } - public function testSetImageSizeVarToArray(){ + public function testSetImageSizeVarToArray() + { $imageSize = 'image_size'; $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) @@ -772,10 +773,10 @@ public function testGetMCStoreNameForDefault() $helperMock->expects($this->exactly(2))->method('getConfigValueForScope')->withConsecutive( array(Mage_Core_Model_Store::XML_PATH_STORE_STORE_NAME, $scopeId, $scope), array('web/unsecure/base_url', 0) - )->willReturnOnConsecutiveCalls( + )->willReturnOnConsecutiveCalls( '', $storeName - ); + ); $result = $helperMock->getMCStoreName($scopeId, $scope); @@ -931,4 +932,40 @@ public function testResetCampaign() $helperMock->resetCampaign($scopeId, $scope); } + + public function testSaveLastItemsSent() + { + $scope = 'stores'; + $scopeId = 1; + $customerLastId = 10; + $productLastId = 10; + $orderLastId = 10; + $cartLastId = 10; + $promoCodeLastId = 10; + $configValues = array( + array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_CUSTOMER_LAST_ID, $customerLastId), + array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_PRODUCT_LAST_ID, $productLastId), + array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_ORDER_LAST_ID, $orderLastId), + array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_CART_LAST_ID, $cartLastId), + array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_PCD_LAST_ID, $promoCodeLastId), + array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_RESEND_ENABLED, 1), + array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_RESEND_TURN, 1) + ); + + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('getMCIsSyncing', 'getLastCustomerSent', 'getLastProductSent', 'getLastOrderSent', + 'getLastCartSent', 'getLastPromoCodeSent', 'saveMailchimpConfig')) + ->getMock(); + + $helperMock->expects($this->once())->method('getMCIsSyncing')->with($scopeId, $scope)->willReturn(false); + $helperMock->expects($this->once())->method('getLastCustomerSent')->with($scopeId, $scope)->willReturn($customerLastId); + $helperMock->expects($this->once())->method('getLastProductSent')->with($scopeId, $scope)->willReturn($productLastId); + $helperMock->expects($this->once())->method('getLastOrderSent')->with($scopeId, $scope)->willReturn($orderLastId); + $helperMock->expects($this->once())->method('getLastCartSent')->with($scopeId, $scope)->willReturn($cartLastId); + $helperMock->expects($this->once())->method('getLastPromoCodeSent')->with($scopeId, $scope)->willReturn($promoCodeLastId); + $helperMock->expects($this->once())->method('saveMailchimpConfig')->with($configValues, $scopeId, $scope); + + $helperMock->saveLastItemsSent($scopeId, $scope); + } } diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php index 210abd78d..32e24d9b2 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/Api/BatchesTest.php @@ -174,7 +174,7 @@ public function testHandleEcommerceBatches() $storeId = 1; $apiBatchesMock = $this->apiBatchesMock ->disableOriginalConstructor() - ->setMethods(array('getHelper', '_getResults', '_sendEcommerceBatch', 'handleResetIfNecessary', 'addSyncValueToArray', 'handleSyncingValue', 'getStores')) + ->setMethods(array('getHelper', '_getResults', '_sendEcommerceBatch', 'addSyncValueToArray', 'handleSyncingValue', 'getStores')) ->getMock(); $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) @@ -206,7 +206,6 @@ public function testHandleEcommerceBatches() $apiBatchesMock->expects($this->once())->method('getStores')->willReturn($storeArrayMock); $apiBatchesMock->expects($this->once())->method('_getResults')->with($storeId); $apiBatchesMock->expects($this->once())->method('_sendEcommerceBatch')->with($storeId); - $apiBatchesMock->expects($this->once())->method('handleResetIfNecessary')->with($storeId); $apiBatchesMock->expects($this->once())->method('addSyncValueToArray')->with($storeId, $syncedArray)->willReturn($syncedArray); $apiBatchesMock->handleEcommerceBatches();