Skip to content

Commit bc9f2c0

Browse files
author
Oleksandr Iegorov
committed
Merge branches '2.1.13-PR-0.1' and 'MAGETWO-66793' of https://github.com/magento-tango/magento2ce into 2.1.13-PR-0.1
2 parents 4550944 + b9dc20c commit bc9f2c0

File tree

5 files changed

+146
-26
lines changed

5 files changed

+146
-26
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/Save.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function execute()
210210
$this->dataObjectHelper->populateWithArray(
211211
$customer,
212212
$customerData,
213-
'\Magento\Customer\Api\Data\CustomerInterface'
213+
\Magento\Customer\Api\Data\CustomerInterface::class
214214
);
215215
$addresses = [];
216216
foreach ($addressesData as $addressData) {
@@ -224,7 +224,7 @@ public function execute()
224224
$this->dataObjectHelper->populateWithArray(
225225
$addressDataObject,
226226
$addressData,
227-
'\Magento\Customer\Api\Data\AddressInterface'
227+
\Magento\Customer\Api\Data\AddressInterface::class
228228
);
229229
$addresses[] = $addressDataObject;
230230
}
@@ -253,7 +253,7 @@ public function execute()
253253
$isSubscribed = $this->getRequest()->getPost('subscription');
254254
}
255255
if ($isSubscribed !== null) {
256-
if ($isSubscribed !== 'false') {
256+
if ($isSubscribed !== '0') {
257257
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
258258
} else {
259259
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);

app/code/Magento/Customer/Test/Unit/Controller/Adminhtml/Index/SaveTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function testExecuteWithExistentCustomer()
280280
{
281281
$customerId = 22;
282282
$addressId = 11;
283-
$subscription = 'true';
283+
$subscription = '1';
284284
$postValue = [
285285
'customer' => [
286286
'entity_id' => $customerId,
@@ -610,7 +610,7 @@ public function testExecuteWithNewCustomer()
610610
{
611611
$customerId = 22;
612612
$addressId = 11;
613-
$subscription = 'false';
613+
$subscription = '0';
614614
$postValue = [
615615
'customer' => [
616616
'coolness' => false,
@@ -879,7 +879,7 @@ public function testExecuteWithNewCustomer()
879879
*/
880880
public function testExecuteWithNewCustomerAndValidationException()
881881
{
882-
$subscription = 'false';
882+
$subscription = '0';
883883
$postValue = [
884884
'customer' => [
885885
'coolness' => false,
@@ -1022,7 +1022,7 @@ public function testExecuteWithNewCustomerAndValidationException()
10221022
*/
10231023
public function testExecuteWithNewCustomerAndLocalizedException()
10241024
{
1025-
$subscription = 'false';
1025+
$subscription = '0';
10261026
$postValue = [
10271027
'customer' => [
10281028
'coolness' => false,
@@ -1165,7 +1165,7 @@ public function testExecuteWithNewCustomerAndLocalizedException()
11651165
*/
11661166
public function testExecuteWithNewCustomerAndException()
11671167
{
1168-
$subscription = 'false';
1168+
$subscription = '0';
11691169
$postValue = [
11701170
'customer' => [
11711171
'coolness' => false,

app/code/Magento/Newsletter/Model/Subscriber.php

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
use Magento\Customer\Api\AccountManagementInterface;
99
use Magento\Customer\Api\CustomerRepositoryInterface;
10-
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Exception\MailException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\Stdlib\DateTime\DateTime;
1214

1315
/**
1416
* Subscriber model
1517
*
16-
* @method \Magento\Newsletter\Model\ResourceModel\Subscriber _getResource()
17-
* @method \Magento\Newsletter\Model\ResourceModel\Subscriber getResource()
1818
* @method int getStoreId()
1919
* @method $this setStoreId(int $value)
2020
* @method string getChangeStatusAt()
@@ -93,6 +93,13 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
9393
*/
9494
protected $_customerSession;
9595

96+
/**
97+
* Date
98+
*
99+
* @var DateTime
100+
*/
101+
private $dateTime;
102+
96103
/**
97104
* Store manager
98105
*
@@ -133,9 +140,10 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
133140
* @param CustomerRepositoryInterface $customerRepository
134141
* @param AccountManagementInterface $customerAccountManagement
135142
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
136-
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
137-
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
143+
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
144+
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
138145
* @param array $data
146+
* @param DateTime|null $dateTime
139147
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
140148
*/
141149
public function __construct(
@@ -151,7 +159,8 @@ public function __construct(
151159
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
152160
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
153161
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
154-
array $data = []
162+
array $data = [],
163+
DateTime $dateTime = null
155164
) {
156165
$this->_newsletterData = $newsletterData;
157166
$this->_scopeConfig = $scopeConfig;
@@ -161,6 +170,7 @@ public function __construct(
161170
$this->customerRepository = $customerRepository;
162171
$this->customerAccountManagement = $customerAccountManagement;
163172
$this->inlineTranslation = $inlineTranslation;
173+
$this->dateTime = $dateTime ?: ObjectManager::getInstance()->get(DateTime::class);
164174
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
165175
}
166176

@@ -263,7 +273,6 @@ public function setStatus($value)
263273
* @param boolean $scope
264274
* @return $this
265275
*/
266-
267276
public function setMessagesScope($scope)
268277
{
269278
$this->getResource()->setMessagesScope($scope);
@@ -349,6 +358,7 @@ public function loadByCustomerId($customerId)
349358
{
350359
try {
351360
$customerData = $this->customerRepository->getById($customerId);
361+
$customerData->setStoreId($this->_storeManager->getStore()->getId());
352362
$data = $this->getResource()->loadByCustomerData($customerData);
353363
$this->addData($data);
354364
if (!empty($data) && $customerData->getId() && !$this->getCustomerId()) {
@@ -446,6 +456,7 @@ public function subscribe($email)
446456
$this->setStatusChanged(true);
447457

448458
try {
459+
/* Save model before sending out email */
449460
$this->save();
450461
if ($isConfirmNeed === true
451462
&& $isOwnSubscribes === false
@@ -544,15 +555,13 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
544555
}
545556

546557
$sendInformationEmail = false;
547-
$status = self::STATUS_SUBSCRIBED;
548-
if ($subscribe) {
549-
if (AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED
550-
== $this->customerAccountManagement->getConfirmationStatus($customerId)
551-
) {
552-
$status = self::STATUS_UNCONFIRMED;
553-
}
554-
} else {
555-
$status = self::STATUS_UNSUBSCRIBED;
558+
$isConfirmNeed = 1 === (int)$this->_scopeConfig->getValue(
559+
self::XML_PATH_CONFIRMATION_FLAG,
560+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
561+
);
562+
$status = $this->getSubscriptionStatus($subscribe, $isConfirmNeed, $customerId);
563+
if ($status == self::STATUS_SUBSCRIBED && $customerData->getConfirmation() === null) {
564+
$sendInformationEmail = true;
556565
}
557566
/**
558567
* If subscription status has been changed then send email to the customer
@@ -584,7 +593,9 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
584593
$sendSubscription = $sendInformationEmail;
585594
if ($sendSubscription === null xor $sendSubscription) {
586595
try {
587-
if ($this->isStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
596+
if ($isConfirmNeed) {
597+
$this->sendConfirmationRequestEmail();
598+
} elseif ($this->isStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
588599
$this->sendUnsubscriptionEmail();
589600
} elseif ($this->isStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
590601
$this->sendConfirmationSuccessEmail();
@@ -597,6 +608,32 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
597608
return $this;
598609
}
599610

611+
/**
612+
* Get subscription status
613+
*
614+
* @param bool $subscribe
615+
* @param bool $isConfirmNeed
616+
* @param string $customerId
617+
* @return int
618+
*/
619+
private function getSubscriptionStatus($subscribe, $isConfirmNeed, $customerId)
620+
{
621+
$status = self::STATUS_SUBSCRIBED;
622+
if ($subscribe) {
623+
if (AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED
624+
== $this->customerAccountManagement->getConfirmationStatus($customerId)
625+
) {
626+
$status = self::STATUS_UNCONFIRMED;
627+
} elseif ($isConfirmNeed) {
628+
$status = self::STATUS_NOT_ACTIVE;
629+
}
630+
} elseif (($this->getStatus() != self::STATUS_UNCONFIRMED)) {
631+
$status = self::STATUS_UNSUBSCRIBED;
632+
}
633+
634+
return $status;
635+
}
636+
600637
/**
601638
* Confirms subscriber newsletter
602639
*
@@ -798,4 +835,18 @@ public function getSubscriberFullName()
798835
}
799836
return $name;
800837
}
838+
839+
/**
840+
* Set date of last changed status
841+
*
842+
* @return $this
843+
*/
844+
public function beforeSave()
845+
{
846+
parent::beforeSave();
847+
if ($this->dataHasChangedFor('subscriber_status')) {
848+
$this->setChangeStatusAt($this->dateTime->gmtDate());
849+
}
850+
return $this;
851+
}
801852
}

app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Newsletter\Test\Unit\Model;
77

8+
/**
9+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10+
*/
811
class SubscriberTest extends \PHPUnit_Framework_TestCase
912
{
1013
/**
@@ -202,6 +205,12 @@ public function testUpdateSubscription()
202205
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
203206
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
204207

208+
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
209+
->disableOriginalConstructor()
210+
->setMethods(['getId'])
211+
->getMock();
212+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
213+
205214
$this->assertEquals($this->subscriber, $this->subscriber->updateSubscription($customerId));
206215
}
207216

@@ -257,6 +266,66 @@ public function testSubscribeCustomerById()
257266
$this->subscriber->subscribeCustomerById($customerId);
258267
}
259268

269+
public function testSubscribeCustomerById1()
270+
{
271+
$customerId = 1;
272+
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
273+
->getMock();
274+
$this->customerRepository->expects($this->atLeastOnce())
275+
->method('getById')
276+
->with($customerId)->willReturn($customerDataMock);
277+
$this->resource->expects($this->atLeastOnce())
278+
->method('loadByCustomerData')
279+
->with($customerDataMock)
280+
->willReturn(
281+
[
282+
'subscriber_id' => 1,
283+
'subscriber_status' => 3
284+
]
285+
);
286+
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
287+
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
288+
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
289+
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
290+
$this->sendEmailCheck();
291+
$this->customerAccountManagement->expects($this->once())
292+
->method('getConfirmationStatus')
293+
->willReturn(\Magento\Customer\Api\AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED);
294+
$this->scopeConfig->expects($this->atLeastOnce())->method('getValue')->with()->willReturn(true);
295+
296+
$this->subscriber->subscribeCustomerById($customerId);
297+
$this->assertEquals(\Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->getStatus());
298+
}
299+
300+
public function testSubscribeCustomerByIdAfterConfirmation()
301+
{
302+
$customerId = 1;
303+
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
304+
->getMock();
305+
$this->customerRepository->expects($this->atLeastOnce())
306+
->method('getById')
307+
->with($customerId)->willReturn($customerDataMock);
308+
$this->resource->expects($this->atLeastOnce())
309+
->method('loadByCustomerData')
310+
->with($customerDataMock)
311+
->willReturn(
312+
[
313+
'subscriber_id' => 1,
314+
'subscriber_status' => 4
315+
]
316+
);
317+
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
318+
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
319+
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
320+
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
321+
$this->sendEmailCheck();
322+
$this->customerAccountManagement->expects($this->never())->method('getConfirmationStatus');
323+
$this->scopeConfig->expects($this->atLeastOnce())->method('getValue')->with()->willReturn(true);
324+
325+
$this->subscriber->updateSubscription($customerId);
326+
$this->assertEquals(\Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED, $this->subscriber->getStatus());
327+
}
328+
260329
public function testUnsubscribe()
261330
{
262331
$this->resource->expects($this->once())->method('save')->willReturnSelf();

dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/IndexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public function testSaveActionExistingCustomerUnsubscribeNewsletter()
347347
'firstname' => 'test firstname',
348348
'lastname' => 'test lastname',
349349
],
350-
'subscription' => 'false'
350+
'subscription' => '0'
351351
];
352352
$this->getRequest()->setPostValue($post);
353353
$this->getRequest()->setParam('id', 1);

0 commit comments

Comments
 (0)