From 65000d81b388a7218ae9c9ec5c12bcb00d932c3c Mon Sep 17 00:00:00 2001 From: Sergio Baixauli Date: Sun, 5 Nov 2017 12:13:25 +0100 Subject: [PATCH 1/5] Fix newsletter subscriptions between stores MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Include store_id in query result in order to ensure that the action (subscribe/unsubscribe) it’s done in the correct store. --- .../Model/ResourceModel/Subscriber.php | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Newsletter/Model/ResourceModel/Subscriber.php b/app/code/Magento/Newsletter/Model/ResourceModel/Subscriber.php index c72ae42031001..c7ce4b2f2f11b 100644 --- a/app/code/Magento/Newsletter/Model/ResourceModel/Subscriber.php +++ b/app/code/Magento/Newsletter/Model/ResourceModel/Subscriber.php @@ -118,17 +118,37 @@ public function loadByEmail($subscriberEmail) */ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer) { - $select = $this->connection->select()->from($this->getMainTable())->where('customer_id=:customer_id'); - - $result = $this->connection->fetchRow($select, ['customer_id' => $customer->getId()]); + $select = $this->connection + ->select() + ->from($this->getMainTable()) + ->where('customer_id=:customer_id and store_id=:store_id'); + + $result = $this->connection + ->fetchRow( + $select, + [ + 'customer_id' => $customer->getId(), + 'store_id' => $customer->getStoreId() + ] + ); if ($result) { return $result; } - $select = $this->connection->select()->from($this->getMainTable())->where('subscriber_email=:subscriber_email'); - - $result = $this->connection->fetchRow($select, ['subscriber_email' => $customer->getEmail()]); + $select = $this->connection + ->select() + ->from($this->getMainTable()) + ->where('subscriber_email=:subscriber_email and store_id=:store_id'); + + $result = $this->connection + ->fetchRow( + $select, + [ + 'subscriber_email' => $customer->getEmail(), + 'store_id' => $customer->getStoreId() + ] + ); if ($result) { return $result; From 81725aa3bc34d1f60dacfdf13d04975c51c751e8 Mon Sep 17 00:00:00 2001 From: Sergio Baixauli Date: Sun, 5 Nov 2017 14:08:53 +0100 Subject: [PATCH 2/5] Fix customer store_id mapping --- app/code/Magento/Newsletter/Model/Subscriber.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Newsletter/Model/Subscriber.php b/app/code/Magento/Newsletter/Model/Subscriber.php index 9e021a21d23b3..d6d109de76b2a 100644 --- a/app/code/Magento/Newsletter/Model/Subscriber.php +++ b/app/code/Magento/Newsletter/Model/Subscriber.php @@ -349,6 +349,7 @@ public function loadByCustomerId($customerId) { try { $customerData = $this->customerRepository->getById($customerId); + $customerData->setStoreId($this->_storeManager->getStore()->getId()); $data = $this->getResource()->loadByCustomerData($customerData); $this->addData($data); if (!empty($data) && $customerData->getId() && !$this->getCustomerId()) { From 19fe0dfa6fbacc4ba30a2b33741def32dfb79275 Mon Sep 17 00:00:00 2001 From: David Manners Date: Fri, 10 Nov 2017 10:54:53 +0000 Subject: [PATCH 3/5] Fix the Newsletter subscriber unit test so that it mocks the store manager correctly --- .../Magento/Newsletter/Test/Unit/Model/SubscriberTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php b/app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php index 7716f4744a922..5a4032dc4dffd 100644 --- a/app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php +++ b/app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php @@ -187,6 +187,12 @@ public function testUpdateSubscription() $customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id'); $customerDataMock->expects($this->once())->method('getEmail')->willReturn('email'); + $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class) + ->disableOriginalConstructor() + ->setMethods(['getId']) + ->getMock(); + $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel); + $this->assertEquals($this->subscriber, $this->subscriber->updateSubscription($customerId)); } From 2e2f66b7ce51aa17673c4fc9c4558fbf5911e35a Mon Sep 17 00:00:00 2001 From: David Manners Date: Fri, 10 Nov 2017 11:13:52 +0000 Subject: [PATCH 4/5] Update the store id attached to the second customer when dealing with new newsletter subscription mapping --- .../testsuite/Magento/Newsletter/_files/subscribers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Newsletter/_files/subscribers.php b/dev/tests/integration/testsuite/Magento/Newsletter/_files/subscribers.php index 152d65681c54f..84e72979cc4c5 100644 --- a/dev/tests/integration/testsuite/Magento/Newsletter/_files/subscribers.php +++ b/dev/tests/integration/testsuite/Magento/Newsletter/_files/subscribers.php @@ -28,7 +28,7 @@ $subscriber = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() ->create(\Magento\Newsletter\Model\Subscriber::class); -$subscriber->setStoreId($otherStore) +$subscriber->setStoreId($currentStore) // Intentionally setting ID to 0 instead of 2 to test fallback mechanism in Subscriber model ->setCustomerId(0) ->setSubscriberEmail('customer_two@example.com') From 194ba57c450a030889015cc36466d4f6cadf616a Mon Sep 17 00:00:00 2001 From: David Manners Date: Fri, 10 Nov 2017 11:50:41 +0000 Subject: [PATCH 5/5] Setup the store id attached to the customer on the admin index save test --- .../Magento/Customer/Controller/Adminhtml/IndexTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/IndexTest.php b/dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/IndexTest.php index 769120127329e..dca24e92f0040 100644 --- a/dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/IndexTest.php +++ b/dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/IndexTest.php @@ -347,6 +347,7 @@ public function testSaveActionExistingCustomerUnsubscribeNewsletter() 'email' => 'customer@example.com', 'firstname' => 'test firstname', 'lastname' => 'test lastname', + 'sendemail_store_id' => 1 ], 'subscription' => '0' ];