From 8b1a2972b3f0ddd4713d00055b8d60f737a72ce4 Mon Sep 17 00:00:00 2001 From: Magento EngCom Team Date: Tue, 23 Jan 2018 11:00:47 -0600 Subject: [PATCH] :arrow_double_up: Forwardport of magento/magento2#12035 to 2.3-develop branch Applied pull request patch https://github.com/magento/magento2/pull/12035.patch (created by @sbaixauli) based on commit(s): 1. 65000d81b388a7218ae9c9ec5c12bcb00d932c3c 2. 81725aa3bc34d1f60dacfdf13d04975c51c751e8 3. 19fe0dfa6fbacc4ba30a2b33741def32dfb79275 4. 2e2f66b7ce51aa17673c4fc9c4558fbf5911e35a 5. 194ba57c450a030889015cc36466d4f6cadf616a Fixed GitHub Issues in 2.3-develop branch: - magento/magento2#10014: Newsletter subscriptions status not isolated between multi stores (reported by @mikelevy300) --- .../Model/ResourceModel/Subscriber.php | 32 +++++++++++++++---- .../Magento/Newsletter/Model/Subscriber.php | 1 + .../Test/Unit/Model/SubscriberTest.php | 6 ++++ .../Controller/Adminhtml/IndexTest.php | 1 + .../Magento/Newsletter/_files/subscribers.php | 2 +- 5 files changed, 35 insertions(+), 7 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; diff --git a/app/code/Magento/Newsletter/Model/Subscriber.php b/app/code/Magento/Newsletter/Model/Subscriber.php index 595c728117749..f6bb0ef2ad041 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()) { 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)); } 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' ]; 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')