Skip to content

Commit 4a32270

Browse files
committed
Merge branch 'ACP2E-35' of https://github.com/magento-l3/magento2ce into PR-2022-02-15-CE2
2 parents 9ab950d + 2ea64f8 commit 4a32270

File tree

2 files changed

+41
-15
lines changed
  • app/code/Magento/Newsletter/Controller/Subscriber
  • dev/tests/integration/testsuite/Magento/Newsletter/Controller/Subscriber

2 files changed

+41
-15
lines changed

app/code/Magento/Newsletter/Controller/Subscriber/NewAction.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Newsletter\Controller\Subscriber;
88

99
use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
1011
use Magento\Customer\Model\Session;
1112
use Magento\Customer\Model\Url as CustomerUrl;
1213
use Magento\Framework\App\Action\Context;
@@ -47,6 +48,11 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
4748
*/
4849
private $subscriptionManager;
4950

51+
/**
52+
* @var CustomerRepositoryInterface
53+
*/
54+
private $customerRepository;
55+
5056
/**
5157
* Initialize dependencies.
5258
*
@@ -57,7 +63,8 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
5763
* @param CustomerUrl $customerUrl
5864
* @param CustomerAccountManagement $customerAccountManagement
5965
* @param SubscriptionManagerInterface $subscriptionManager
60-
* @param EmailValidator $emailValidator
66+
* @param EmailValidator|null $emailValidator
67+
* @param CustomerRepositoryInterface|null $customerRepository
6168
*/
6269
public function __construct(
6370
Context $context,
@@ -67,11 +74,14 @@ public function __construct(
6774
CustomerUrl $customerUrl,
6875
CustomerAccountManagement $customerAccountManagement,
6976
SubscriptionManagerInterface $subscriptionManager,
70-
EmailValidator $emailValidator = null
77+
EmailValidator $emailValidator = null,
78+
CustomerRepositoryInterface $customerRepository = null
7179
) {
7280
$this->customerAccountManagement = $customerAccountManagement;
7381
$this->subscriptionManager = $subscriptionManager;
7482
$this->emailValidator = $emailValidator ?: ObjectManager::getInstance()->get(EmailValidator::class);
83+
$this->customerRepository = $customerRepository ?: ObjectManager::getInstance()
84+
->get(CustomerRepositoryInterface::class);
7585
parent::__construct(
7686
$context,
7787
$subscriberFactory,
@@ -165,7 +175,8 @@ public function execute()
165175
}
166176

167177
$storeId = (int)$this->_storeManager->getStore()->getId();
168-
$currentCustomerId = $this->getSessionCustomerId($email);
178+
$currentCustomerId = $this->getCustomerId($email, $websiteId);
179+
169180
$subscriber = $currentCustomerId
170181
? $this->subscriptionManager->subscribeCustomer($currentCustomerId, $storeId)
171182
: $this->subscriptionManager->subscribe($email, $storeId);
@@ -182,28 +193,26 @@ public function execute()
182193
}
183194
/** @var Redirect $redirect */
184195
$redirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
196+
// phpcs:ignore Magento2.Legacy.ObsoleteResponse
185197
$redirectUrl = $this->_redirect->getRedirectUrl();
186198
return $redirect->setUrl($redirectUrl);
187199
}
188200

189201
/**
190-
* Get customer id from session if he is owner of the email
202+
* Check if customer with provided email exists and return its id
191203
*
192204
* @param string $email
205+
* @param int $websiteId
193206
* @return int|null
194207
*/
195-
private function getSessionCustomerId(string $email): ?int
208+
private function getCustomerId(string $email, int $websiteId): ?int
196209
{
197-
if (!$this->_customerSession->isLoggedIn()) {
210+
try {
211+
$customer = $this->customerRepository->get($email, $websiteId);
212+
return (int)$customer->getId();
213+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
198214
return null;
199215
}
200-
201-
$customer = $this->_customerSession->getCustomerDataObject();
202-
if ($customer->getEmail() !== $email) {
203-
return null;
204-
}
205-
206-
return (int)$this->_customerSession->getId();
207216
}
208217

209218
/**

dev/tests/integration/testsuite/Magento/Newsletter/Controller/Subscriber/NewActionTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\App\Request\Http as HttpRequest;
1414
use Magento\Newsletter\Model\ResourceModel\Subscriber as SubscriberResource;
1515
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory;
16+
use Magento\Newsletter\Model\ResourceModel\Subscriber\Grid\Collection as GridCollection;
1617
use Magento\TestFramework\TestCase\AbstractController;
1718
use Laminas\Stdlib\Parameters;
1819

@@ -107,17 +108,33 @@ public function subscribersDataProvider(): array
107108

108109
/**
109110
* @magentoDataFixture Magento/Customer/_files/new_customer.php
111+
* @dataProvider emailAndStatusDataProvider
110112
*
111113
* @return void
112114
*/
113-
public function testNewActionUsedEmail(): void
115+
public function testNewActionUsedEmail($email, $subscriptionType): void
114116
{
115-
$this->prepareRequest('new_customer@example.com');
117+
$this->prepareRequest($email);
116118
$this->dispatch('newsletter/subscriber/new');
117119

120+
/** @var GridCollection $gridCollection */
121+
$gridCollection = $this->_objectManager->create(GridCollection::class);
122+
$item = $gridCollection->getFirstItem();
123+
self::assertEquals($subscriptionType, (int)$item->getType());
118124
$this->performAsserts('Thank you for your subscription.');
119125
}
120126

127+
/**
128+
* @return array
129+
*/
130+
public function emailAndStatusDataProvider()
131+
{
132+
return [
133+
'customer' => ['new_customer@example.com', 2],
134+
'not_a_customer' => ['not_a_customer@gmail.com', 1],
135+
];
136+
}
137+
121138
/**
122139
* @magentoDataFixture Magento/Customer/_files/new_customer.php
123140
*

0 commit comments

Comments
 (0)