Skip to content

Commit 0f56de1

Browse files
author
Alexander Akimov
authored
Merge pull request #1740 from magento-chaika/PR_in_2.1
[Chaika] bugfixes in 2.1
2 parents a91df24 + a80915c commit 0f56de1

File tree

2 files changed

+210
-42
lines changed

2 files changed

+210
-42
lines changed

app/code/Magento/Customer/Controller/Account/Confirm.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
78
namespace Magento\Customer\Controller\Account;
89

10+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
11+
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
912
use Magento\Customer\Model\Url;
1013
use Magento\Framework\App\Action\Context;
1114
use Magento\Customer\Model\Session;
1215
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
use Magento\Framework\App\ObjectManager;
1317
use Magento\Store\Model\StoreManagerInterface;
1418
use Magento\Customer\Api\AccountManagementInterface;
1519
use Magento\Customer\Api\CustomerRepositoryInterface;
@@ -32,10 +36,10 @@ class Confirm extends \Magento\Customer\Controller\AbstractAccount
3236
/** @var StoreManagerInterface */
3337
protected $storeManager;
3438

35-
/** @var AccountManagementInterface */
39+
/** @var AccountManagementInterface */
3640
protected $customerAccountManagement;
3741

38-
/** @var CustomerRepositoryInterface */
42+
/** @var CustomerRepositoryInterface */
3943
protected $customerRepository;
4044

4145
/** @var Address */
@@ -49,6 +53,16 @@ class Confirm extends \Magento\Customer\Controller\AbstractAccount
4953
*/
5054
protected $session;
5155

56+
/**
57+
* @var CookieMetadataFactory
58+
*/
59+
private $cookieMetadataFactory;
60+
61+
/**
62+
* @var PhpCookieManager
63+
*/
64+
private $cookieMetadataManager;
65+
5266
/**
5367
* @param Context $context
5468
* @param Session $customerSession
@@ -58,6 +72,9 @@ class Confirm extends \Magento\Customer\Controller\AbstractAccount
5872
* @param CustomerRepositoryInterface $customerRepository
5973
* @param Address $addressHelper
6074
* @param UrlFactory $urlFactory
75+
* @param CookieMetadataFactory $cookieMetadataFactory
76+
* @param PhpCookieManager $cookieMetadataManager
77+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6178
*/
6279
public function __construct(
6380
Context $context,
@@ -67,7 +84,9 @@ public function __construct(
6784
AccountManagementInterface $customerAccountManagement,
6885
CustomerRepositoryInterface $customerRepository,
6986
Address $addressHelper,
70-
UrlFactory $urlFactory
87+
UrlFactory $urlFactory,
88+
CookieMetadataFactory $cookieMetadataFactory = null,
89+
PhpCookieManager $cookieMetadataManager = null
7190
) {
7291
$this->session = $customerSession;
7392
$this->scopeConfig = $scopeConfig;
@@ -76,6 +95,11 @@ public function __construct(
7695
$this->customerRepository = $customerRepository;
7796
$this->addressHelper = $addressHelper;
7897
$this->urlModel = $urlFactory->create();
98+
$this->cookieMetadataFactory = $cookieMetadataFactory ?: ObjectManager::getInstance()
99+
->get(CookieMetadataFactory::class);
100+
$this->cookieMetadataManager = $cookieMetadataManager ?: ObjectManager::getInstance()
101+
->get(PhpCookieManager::class);
102+
79103
parent::__construct($context);
80104
}
81105

@@ -100,6 +124,8 @@ public function execute()
100124
throw new \Exception(__('Bad request.'));
101125
}
102126

127+
//clean customer cookies
128+
$this->cleanCookie('mage-cache-sessid');
103129
// log in and send greeting email
104130
$customerEmail = $this->customerRepository->getById($customerId)->getEmail();
105131
$customer = $this->customerAccountManagement->activate($customerEmail, $key);
@@ -121,7 +147,7 @@ public function execute()
121147
/**
122148
* Retrieve success message
123149
*
124-
* @return string
150+
* @return \Magento\Framework\Phrase
125151
*/
126152
protected function getSuccessMessage()
127153
{
@@ -166,4 +192,21 @@ protected function getSuccessRedirect()
166192
}
167193
return $this->_redirect->success($backUrl ? $backUrl : $successUrl);
168194
}
195+
196+
/**
197+
* Clean cookie by name.
198+
*
199+
* @param String $cookieName
200+
* @return void
201+
*/
202+
private function cleanCookie($cookieName)
203+
{
204+
if ($this->cookieMetadataManager->getCookie($cookieName)) {
205+
$this->cookieMetadataManager->deleteCookie(
206+
$cookieName,
207+
$this->cookieMetadataFactory->createCookieMetadata()
208+
->setPath('/')
209+
);
210+
}
211+
}
169212
}

app/code/Magento/Customer/Test/Unit/Controller/Account/ConfirmTest.php

Lines changed: 163 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,27 @@
1010

1111
use Magento\Customer\Helper\Address;
1212
use Magento\Customer\Model\Url;
13+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
14+
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1316
use Magento\Store\Model\ScopeInterface;
17+
use Magento\Customer\Controller\Account\Confirm;
18+
use Magento\Customer\Model\Session;
19+
use Magento\Framework\App\RequestInterface;
20+
use Magento\Framework\App\Response\Http;
21+
use Magento\Framework\App\ViewInterface;
22+
use Magento\Framework\App\Response\RedirectInterface;
23+
use Magento\Framework\UrlFactory;
24+
use Magento\Customer\Api\AccountManagementInterface;
25+
use Magento\Customer\Api\Data\CustomerInterface;
26+
use Magento\Customer\Api\CustomerRepositoryInterface;
27+
use Magento\Framework\Message\Manager;
28+
use Magento\Store\Model\StoreManager;
29+
use Magento\Store\Model\Store;
30+
use Magento\Framework\Controller\Result\Redirect;
31+
use Magento\Framework\Controller\ResultFactory;
32+
use Magento\Framework\App\Config\ScopeConfigInterface;
33+
use Magento\Framework\App\Action\Context;
1434

1535
/**
1636
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -98,51 +118,53 @@ class ConfirmTest extends \PHPUnit_Framework_TestCase
98118
*/
99119
protected $redirectResultMock;
100120

121+
/** @var PhpCookieManager | \PHPUnit_Framework_MockObject_MockObject */
122+
private $cookieMetadataManager;
123+
124+
/** @var CookieMetadataFactory | \PHPUnit_Framework_MockObject_MockObject */
125+
private $cookieMetadataFactory;
126+
101127
protected function setUp()
102128
{
103-
$this->customerSessionMock = $this->getMock('\Magento\Customer\Model\Session', [], [], '', false);
104-
$this->requestMock = $this->getMock('Magento\Framework\App\RequestInterface', [], [], '', false);
105-
$this->responseMock = $this->getMock(
106-
'Magento\Framework\App\Response\Http', ['setRedirect', '__wakeup'], [], '', false
107-
);
108-
$viewMock = $this->getMock('Magento\Framework\App\ViewInterface');
109-
$this->redirectMock = $this->getMock('Magento\Framework\App\Response\RedirectInterface');
110-
111-
$this->urlMock = $this->getMock('Magento\Framework\Url', [], [], '', false);
112-
$urlFactoryMock = $this->getMock('Magento\Framework\UrlFactory', [], [], '', false);
129+
$this->customerSessionMock = $this->createDefaultMock(Session::class);
130+
$this->requestMock = $this->createDefaultMock(RequestInterface::class);
131+
$this->responseMock = $this->getMockBuilder(Http::class)
132+
->disableOriginalConstructor()
133+
->setMethods(['setRedirect', '__wakeup'])
134+
->getMock();
135+
$viewMock = $this->createDefaultMock(ViewInterface::class);
136+
$this->redirectMock = $this->createDefaultMock(RedirectInterface::class);
137+
138+
$this->urlMock = $this->createDefaultMock(\Magento\Framework\Url::class);
139+
$urlFactoryMock = $this->createDefaultMock(UrlFactory::class);
113140
$urlFactoryMock->expects($this->any())
114141
->method('create')
115142
->will($this->returnValue($this->urlMock));
116143

117-
$this->customerAccountManagementMock =
118-
$this->getMockForAbstractClass('Magento\Customer\Api\AccountManagementInterface');
119-
$this->customerDataMock = $this->getMock(
120-
'Magento\Customer\Api\Data\CustomerInterface', [], [], '', false
121-
);
144+
$this->customerAccountManagementMock = $this->getMockBuilder(AccountManagementInterface::class)
145+
->getMockForAbstractClass();
146+
$this->customerDataMock = $this->createDefaultMock(CustomerInterface::class);
122147

123-
$this->customerRepositoryMock =
124-
$this->getMockForAbstractClass('Magento\Customer\Api\CustomerRepositoryInterface');
125-
126-
$this->messageManagerMock = $this->getMock('Magento\Framework\Message\Manager', [], [], '', false);
127-
$this->addressHelperMock = $this->getMock('Magento\Customer\Helper\Address', [], [], '', false);
128-
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManager', [], [], '', false);
129-
$this->storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
130-
$this->redirectResultMock = $this->getMock('Magento\Framework\Controller\Result\Redirect', [], [], '', false);
131-
132-
$resultFactoryMock = $this->getMock(
133-
'Magento\Framework\Controller\ResultFactory',
134-
['create'],
135-
[],
136-
'',
137-
false
138-
);
148+
$this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
149+
->getMockForAbstractClass();
150+
151+
$this->messageManagerMock = $this->createDefaultMock(Manager::class);
152+
$this->addressHelperMock = $this->createDefaultMock(Address::class);
153+
$this->storeManagerMock = $this->createDefaultMock(StoreManager::class);
154+
$this->storeMock = $this->createDefaultMock(Store::class);
155+
$this->redirectResultMock = $this->createDefaultMock(Redirect::class);
156+
157+
$resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
158+
->disableOriginalConstructor()
159+
->setMethods(['create'])
160+
->getMock();
139161
$resultFactoryMock->expects($this->once())
140162
->method('create')
141-
->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
163+
->with(ResultFactory::TYPE_REDIRECT)
142164
->willReturn($this->redirectResultMock);
143165

144-
$this->scopeConfigMock = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface');
145-
$this->contextMock = $this->getMock('Magento\Framework\App\Action\Context', [], [], '', false);
166+
$this->scopeConfigMock = $this->createDefaultMock(ScopeConfigInterface::class);
167+
$this->contextMock = $this->createDefaultMock(Context::class);
146168
$this->contextMock->expects($this->any())
147169
->method('getRequest')
148170
->willReturn($this->requestMock);
@@ -162,10 +184,31 @@ protected function setUp()
162184
->method('getResultFactory')
163185
->willReturn($resultFactoryMock);
164186

165-
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
187+
$this->cookieMetadataFactory = $this->getMockBuilder(CookieMetadataFactory::class)
188+
->disableOriginalConstructor()
189+
->setMethods(['setPath', 'createCookieMetadata'])
190+
->getMock();
166191

192+
$cookieMetaData = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
193+
->disableOriginalConstructor()
194+
->getMock();
195+
196+
$cookieMetaData->expects($this->any())
197+
->method('setPath')
198+
->will($this->returnSelf());
199+
200+
$this->cookieMetadataFactory->expects($this->any())
201+
->method('createCookieMetadata')
202+
->will($this->returnValue($cookieMetaData));
203+
204+
/** @var PhpCookieManager | \PHPUnit_Framework_MockObject_MockObject $cookieMetadataManager */
205+
$this->cookieMetadataManager = $this->getMockBuilder(PhpCookieManager::class)
206+
->disableOriginalConstructor()
207+
->getMock();
208+
209+
$objectManagerHelper = new ObjectManager($this);
167210
$this->model = $objectManagerHelper->getObject(
168-
'Magento\Customer\Controller\Account\Confirm',
211+
Confirm::class,
169212
[
170213
'context' => $this->contextMock,
171214
'customerSession' => $this->customerSessionMock,
@@ -175,6 +218,8 @@ protected function setUp()
175218
'customerRepository' => $this->customerRepositoryMock,
176219
'addressHelper' => $this->addressHelperMock,
177220
'urlFactory' => $urlFactoryMock,
221+
'cookieMetadataFactory' => $this->cookieMetadataFactory,
222+
'cookieMetadataManager' => $this->cookieMetadataManager,
178223
]
179224
);
180225
}
@@ -190,7 +235,7 @@ public function testIsLoggedIn()
190235
->with('*/*/')
191236
->willReturnSelf();
192237

193-
$this->assertInstanceOf('Magento\Framework\Controller\Result\Redirect', $this->model->execute());
238+
$this->assertInstanceOf(Redirect::class, $this->model->execute());
194239
}
195240

196241
/**
@@ -232,7 +277,7 @@ public function testNoCustomerIdInRequest($customerId, $key)
232277
->with($this->equalTo($testUrl))
233278
->willReturnSelf();
234279

235-
$this->assertInstanceOf('Magento\Framework\Controller\Result\Redirect', $this->model->execute());
280+
$this->assertInstanceOf(Redirect::class, $this->model->execute());
236281
}
237282

238283
/**
@@ -442,4 +487,84 @@ public function getSuccessRedirectDataProvider()
442487
],
443488
];
444489
}
490+
491+
/**
492+
* @param string $className
493+
* @return \PHPUnit_Framework_MockObject_MockObject
494+
*/
495+
protected function createDefaultMock($className)
496+
{
497+
return $this->getMockBuilder($className)
498+
->disableOriginalConstructor()
499+
->getMock();
500+
}
501+
502+
/**
503+
* Tests cookie cleaning
504+
*
505+
* @dataProvider dataProviderClean
506+
* @param mixed $cookieValue
507+
* @param \PHPUnit_Framework_MockObject_Matcher_Invocation $deleteMatcher
508+
*/
509+
public function testClean($cookieValue, \PHPUnit_Framework_MockObject_Matcher_Invocation $deleteMatcher)
510+
{
511+
$this->customerSessionMock->expects($this->once())
512+
->method('isLoggedIn')
513+
->will($this->returnValue(false));
514+
515+
$this->requestMock->expects($this->any())
516+
->method('getParam')
517+
->willReturnMap([
518+
['id', false, 'id'],
519+
['key', false, 'key'],
520+
]);
521+
522+
$this->customerDataMock->expects($this->once())
523+
->method('getEmail')
524+
->will($this->returnValue('email@exmple.com'));
525+
526+
$this->customerRepositoryMock->expects($this->any())
527+
->method('getById')
528+
->will($this->returnValue($this->customerDataMock));
529+
530+
$this->customerAccountManagementMock->expects($this->once())
531+
->method('activate');
532+
533+
$this->storeMock->expects($this->any())
534+
->method('getFrontendName')
535+
->will($this->returnValue('frontend'));
536+
$this->storeManagerMock->expects($this->any())
537+
->method('getStore')
538+
->will($this->returnValue($this->storeMock));
539+
540+
$this->cookieMetadataManager->expects($this->once())
541+
->method('getCookie')
542+
->will($this->returnValue($cookieValue));
543+
544+
$this->cookieMetadataManager->expects($deleteMatcher)
545+
->method('deleteCookie')
546+
->will($this->returnValue(null));
547+
548+
/** @noinspection PhpUnhandledExceptionInspection */
549+
$this->model->execute();
550+
}
551+
552+
/**
553+
* Provides data for testing clean method
554+
*
555+
* @return array
556+
*/
557+
public function dataProviderClean()
558+
{
559+
return [
560+
'clean-cookie' => [
561+
'testValue',
562+
$this->once()
563+
],
564+
'no-clean' => [
565+
null,
566+
$this->never()
567+
],
568+
];
569+
}
445570
}

0 commit comments

Comments
 (0)