Skip to content

Commit e500c3d

Browse files
committed
Move additional dependencies from private getters to constructor - Magento_Captcha
1 parent 3a2bb6f commit e500c3d

File tree

2 files changed

+110
-103
lines changed

2 files changed

+110
-103
lines changed

app/code/Magento/Captcha/Observer/CheckContactUsFormObserver.php

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,41 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Captcha\Observer;
79

10+
use Magento\Captcha\Helper\Data;
11+
use Magento\Framework\App\Action\Action;
12+
use Magento\Framework\App\ActionFlag;
13+
use Magento\Framework\App\Response\RedirectInterface;
14+
use Magento\Framework\Event\Observer;
815
use Magento\Framework\Event\ObserverInterface;
916
use Magento\Framework\App\Request\DataPersistorInterface;
10-
use Magento\Framework\App\ObjectManager;
17+
use Magento\Framework\Message\ManagerInterface;
1118

1219
/**
13-
* Class CheckContactUsFormObserver
20+
* Check captcha on contact us form submit observer.
1421
*/
1522
class CheckContactUsFormObserver implements ObserverInterface
1623
{
1724
/**
18-
* @var \Magento\Captcha\Helper\Data
25+
* @var Data
1926
*/
2027
protected $_helper;
2128

2229
/**
23-
* @var \Magento\Framework\App\ActionFlag
30+
* @var ActionFlag
2431
*/
2532
protected $_actionFlag;
2633

2734
/**
28-
* @var \Magento\Framework\Message\ManagerInterface
35+
* @var ManagerInterface
2936
*/
3037
protected $messageManager;
3138

3239
/**
33-
* @var \Magento\Framework\App\Response\RedirectInterface
40+
* @var RedirectInterface
3441
*/
3542
protected $redirect;
3643

@@ -45,60 +52,48 @@ class CheckContactUsFormObserver implements ObserverInterface
4552
private $dataPersistor;
4653

4754
/**
48-
* @param \Magento\Captcha\Helper\Data $helper
49-
* @param \Magento\Framework\App\ActionFlag $actionFlag
50-
* @param \Magento\Framework\Message\ManagerInterface $messageManager
51-
* @param \Magento\Framework\App\Response\RedirectInterface $redirect
55+
* @param Data $helper
56+
* @param ActionFlag $actionFlag
57+
* @param ManagerInterface $messageManager
58+
* @param RedirectInterface $redirect
5259
* @param CaptchaStringResolver $captchaStringResolver
60+
* @param DataPersistorInterface $dataPersistor
5361
*/
5462
public function __construct(
55-
\Magento\Captcha\Helper\Data $helper,
56-
\Magento\Framework\App\ActionFlag $actionFlag,
57-
\Magento\Framework\Message\ManagerInterface $messageManager,
58-
\Magento\Framework\App\Response\RedirectInterface $redirect,
59-
CaptchaStringResolver $captchaStringResolver
63+
Data $helper,
64+
ActionFlag $actionFlag,
65+
ManagerInterface $messageManager,
66+
RedirectInterface $redirect,
67+
CaptchaStringResolver $captchaStringResolver,
68+
DataPersistorInterface $dataPersistor
6069
) {
6170
$this->_helper = $helper;
6271
$this->_actionFlag = $actionFlag;
6372
$this->messageManager = $messageManager;
6473
$this->redirect = $redirect;
6574
$this->captchaStringResolver = $captchaStringResolver;
75+
$this->dataPersistor = $dataPersistor;
6676
}
6777

6878
/**
6979
* Check CAPTCHA on Contact Us page
7080
*
71-
* @param \Magento\Framework\Event\Observer $observer
81+
* @param Observer $observer
7282
* @return void
7383
*/
74-
public function execute(\Magento\Framework\Event\Observer $observer)
84+
public function execute(Observer $observer)
7585
{
7686
$formId = 'contact_us';
7787
$captcha = $this->_helper->getCaptcha($formId);
7888
if ($captcha->isRequired()) {
79-
/** @var \Magento\Framework\App\Action\Action $controller */
89+
/** @var Action $controller */
8090
$controller = $observer->getControllerAction();
8191
if (!$captcha->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
8292
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA.'));
83-
$this->getDataPersistor()->set($formId, $controller->getRequest()->getPostValue());
84-
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
93+
$this->dataPersistor->set($formId, $controller->getRequest()->getPostValue());
94+
$this->_actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
8595
$this->redirect->redirect($controller->getResponse(), 'contact/index/index');
8696
}
8797
}
8898
}
89-
90-
/**
91-
* Get Data Persistor
92-
*
93-
* @return DataPersistorInterface
94-
*/
95-
private function getDataPersistor()
96-
{
97-
if ($this->dataPersistor === null) {
98-
$this->dataPersistor = ObjectManager::getInstance()
99-
->get(DataPersistorInterface::class);
100-
}
101-
102-
return $this->dataPersistor;
103-
}
10499
}

app/code/Magento/Captcha/Test/Unit/Observer/CheckContactUsFormObserverTest.php

Lines changed: 80 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,109 +5,122 @@
55
*/
66
namespace Magento\Captcha\Test\Unit\Observer;
77

8+
use Magento\Captcha\Helper\Data;
9+
use Magento\Captcha\Model\DefaultModel;
10+
use Magento\Captcha\Observer\CaptchaStringResolver;
11+
use Magento\Captcha\Observer\CheckContactUsFormObserver;
12+
use Magento\Framework\App\Action\Action;
13+
use Magento\Framework\App\ActionFlag;
14+
use Magento\Framework\App\Request\DataPersistorInterface;
15+
use Magento\Framework\App\Request\Http;
16+
use Magento\Framework\App\Response\RedirectInterface;
17+
use Magento\Framework\Event\Observer;
18+
use Magento\Framework\Message\ManagerInterface;
19+
use Magento\Framework\Session\SessionManager;
20+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
21+
use PHPUnit\Framework\MockObject\MockObject;
22+
use PHPUnit\Framework\TestCase;
23+
824
/**
25+
* Test class for \Magento\Captcha\Observer\CheckContactUsFormObserver
26+
*
927
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1028
*/
11-
class CheckContactUsFormObserverTest extends \PHPUnit\Framework\TestCase
29+
class CheckContactUsFormObserverTest extends TestCase
1230
{
1331
/**
14-
* @var \Magento\Captcha\Observer\CheckContactUsFormObserver
32+
* @var ObjectManager
1533
*/
16-
protected $checkContactUsFormObserver;
34+
private $objectManagerHelper;
1735

1836
/**
19-
* @var \Magento\Captcha\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
37+
* @var CheckContactUsFormObserver
2038
*/
21-
protected $helperMock;
39+
private $checkContactUsFormObserver;
2240

2341
/**
24-
* @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
42+
* @var Data|MockObject
2543
*/
26-
protected $actionFlagMock;
44+
private $helperMock;
2745

28-
/*
29-
* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
46+
/**
47+
* @var ActionFlag|MockObject
3048
*/
31-
protected $messageManagerMock;
49+
private $actionFlagMock;
3250

3351
/**
34-
* @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
52+
* @var ManagerInterface|MockObject
3553
*/
36-
protected $redirectMock;
54+
private $messageManagerMock;
3755

3856
/**
39-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
57+
* @var RedirectInterface|MockObject
4058
*/
41-
protected $objectManagerHelper;
59+
private $redirectMock;
4260

4361
/**
44-
* @var \Magento\Captcha\Observer\CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject
62+
* @var CaptchaStringResolver|MockObject
4563
*/
46-
protected $captchaStringResolverMock;
64+
private $captchaStringResolverMock;
4765

4866
/**
49-
* @var \Magento\Framework\Session\SessionManager|\PHPUnit_Framework_MockObject_MockObject
67+
* @var DataPersistorInterface|MockObject
5068
*/
51-
protected $sessionMock;
69+
private $dataPersistorMock;
5270

5371
/**
54-
* @var \Magento\Captcha\Model\DefaultModel|\PHPUnit_Framework_MockObject_MockObject
72+
* @var SessionManager|MockObject
5573
*/
56-
protected $captchaMock;
74+
private $sessionMock;
5775

5876
/**
59-
* @var \Magento\Framework\App\Request\DataPersistorInterface|\PHPUnit_Framework_MockObject_MockObject
77+
* @var DefaultModel|MockObject
6078
*/
61-
protected $dataPersistorMock;
79+
private $captchaMock;
6280

6381
protected function setUp()
6482
{
65-
$this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
83+
$this->objectManagerHelper = new ObjectManager($this);
84+
85+
$this->helperMock = $this->createMock(Data::class);
86+
$this->actionFlagMock = $this->createMock(ActionFlag::class);
87+
$this->messageManagerMock = $this->createMock(ManagerInterface::class);
88+
$this->redirectMock = $this->createMock(RedirectInterface::class);
89+
$this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class);
90+
$this->dataPersistorMock = $this->getMockBuilder(DataPersistorInterface::class)
91+
->getMockForAbstractClass();
6692

67-
$this->helperMock = $this->createMock(\Magento\Captcha\Helper\Data::class);
68-
$this->actionFlagMock = $this->createMock(\Magento\Framework\App\ActionFlag::class);
69-
$this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
70-
$this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
71-
$this->captchaStringResolverMock = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
7293
$this->sessionMock = $this->createPartialMock(
73-
\Magento\Framework\Session\SessionManager::class,
94+
SessionManager::class,
7495
['addErrorMessage']
7596
);
76-
$this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
77-
->getMockForAbstractClass();
97+
$this->captchaMock = $this->createMock(DefaultModel::class);
7898

7999
$this->checkContactUsFormObserver = $this->objectManagerHelper->getObject(
80-
\Magento\Captcha\Observer\CheckContactUsFormObserver::class,
100+
CheckContactUsFormObserver::class,
81101
[
82102
'helper' => $this->helperMock,
83103
'actionFlag' => $this->actionFlagMock,
84104
'messageManager' => $this->messageManagerMock,
85105
'redirect' => $this->redirectMock,
86-
'captchaStringResolver' => $this->captchaStringResolverMock
106+
'captchaStringResolver' => $this->captchaStringResolverMock,
107+
'dataPersistor' => $this->dataPersistorMock
87108
]
88109
);
89-
$this->objectManagerHelper->setBackwardCompatibleProperty(
90-
$this->checkContactUsFormObserver,
91-
'dataPersistor',
92-
$this->dataPersistorMock
93-
);
94-
95-
$this->captchaMock = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
96110
}
97111

98112
public function testCheckContactUsFormWhenCaptchaIsRequiredAndValid()
99113
{
100114
$formId = 'contact_us';
101115
$captchaValue = 'some-value';
102116

103-
$controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
104-
$request = $this->createMock(\Magento\Framework\App\Request\Http::class);
105-
$request->expects($this->any())
106-
->method('getPost')
107-
->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)
117+
$controller = $this->createMock(Action::class);
118+
$request = $this->createMock(Http::class);
119+
$request->method('getPost')
120+
->with(Data::INPUT_NAME_FIELD_VALUE, null)
108121
->willReturn([$formId => $captchaValue]);
109-
$controller->expects($this->any())->method('getRequest')->willReturn($request);
110-
$this->captchaMock->expects($this->any())->method('isRequired')->willReturn(true);
122+
$controller->method('getRequest')->willReturn($request);
123+
$this->captchaMock->method('isRequired')->willReturn(true);
111124
$this->captchaMock->expects($this->once())
112125
->method('isCorrect')
113126
->with($captchaValue)
@@ -116,13 +129,13 @@ public function testCheckContactUsFormWhenCaptchaIsRequiredAndValid()
116129
->method('resolve')
117130
->with($request, $formId)
118131
->willReturn($captchaValue);
119-
$this->helperMock->expects($this->any())
120-
->method('getCaptcha')
121-
->with($formId)->willReturn($this->captchaMock);
132+
$this->helperMock->method('getCaptcha')
133+
->with($formId)
134+
->willReturn($this->captchaMock);
122135
$this->sessionMock->expects($this->never())->method('addErrorMessage');
123136

124137
$this->checkContactUsFormObserver->execute(
125-
new \Magento\Framework\Event\Observer(['controller_action' => $controller])
138+
new Observer(['controller_action' => $controller])
126139
);
127140
}
128141

@@ -135,11 +148,10 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
135148
$redirectUrl = 'http://magento.com/contacts/';
136149
$postData = ['name' => 'Some Name'];
137150

138-
$request = $this->createMock(\Magento\Framework\App\Request\Http::class);
151+
$request = $this->createMock(Http::class);
139152
$response = $this->createMock(\Magento\Framework\App\Response\Http::class);
140-
$request->expects($this->any())
141-
->method('getPost')
142-
->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)
153+
$request->method('getPost')
154+
->with(Data::INPUT_NAME_FIELD_VALUE, null)
143155
->willReturn([$formId => $captchaValue]);
144156
$request->expects($this->once())
145157
->method('getPostValue')
@@ -150,10 +162,10 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
150162
->with($response, $redirectRoutePath, [])
151163
->willReturn($redirectUrl);
152164

153-
$controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
154-
$controller->expects($this->any())->method('getRequest')->willReturn($request);
155-
$controller->expects($this->any())->method('getResponse')->willReturn($response);
156-
$this->captchaMock->expects($this->any())->method('isRequired')->willReturn(true);
165+
$controller = $this->createMock(Action::class);
166+
$controller->method('getRequest')->willReturn($request);
167+
$controller->method('getResponse')->willReturn($response);
168+
$this->captchaMock->method('isRequired')->willReturn(true);
157169
$this->captchaMock->expects($this->once())
158170
->method('isCorrect')
159171
->with($captchaValue)
@@ -162,32 +174,32 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
162174
->method('resolve')
163175
->with($request, $formId)
164176
->willReturn($captchaValue);
165-
$this->helperMock->expects($this->any())
166-
->method('getCaptcha')
177+
$this->helperMock->method('getCaptcha')
167178
->with($formId)
168179
->willReturn($this->captchaMock);
169-
$this->messageManagerMock->expects($this->once())->method('addErrorMessage')->with($warningMessage);
180+
$this->messageManagerMock->expects($this->once())
181+
->method('addErrorMessage')
182+
->with($warningMessage);
170183
$this->actionFlagMock->expects($this->once())
171184
->method('set')
172-
->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
185+
->with('', Action::FLAG_NO_DISPATCH, true);
173186
$this->dataPersistorMock->expects($this->once())
174187
->method('set')
175188
->with($formId, $postData);
176189

177190
$this->checkContactUsFormObserver->execute(
178-
new \Magento\Framework\Event\Observer(['controller_action' => $controller])
191+
new Observer(['controller_action' => $controller])
179192
);
180193
}
181194

182195
public function testCheckContactUsFormDoesNotCheckCaptchaWhenItIsNotRequired()
183196
{
184-
$this->helperMock->expects($this->any())
185-
->method('getCaptcha')
197+
$this->helperMock->method('getCaptcha')
186198
->with('contact_us')
187199
->willReturn($this->captchaMock);
188-
$this->captchaMock->expects($this->any())->method('isRequired')->willReturn(false);
200+
$this->captchaMock->method('isRequired')->willReturn(false);
189201
$this->captchaMock->expects($this->never())->method('isCorrect');
190202

191-
$this->checkContactUsFormObserver->execute(new \Magento\Framework\Event\Observer());
203+
$this->checkContactUsFormObserver->execute(new Observer());
192204
}
193205
}

0 commit comments

Comments
 (0)