65 $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
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);
72 $this->sessionMock = $this->createPartialMock(\
Magento\Framework\Session\SessionManager::class, [
'addError']);
73 $this->dataPersistorMock = $this->getMockBuilder(\
Magento\Framework\
App\Request\DataPersistorInterface::class)
74 ->getMockForAbstractClass();
76 $this->checkContactUsFormObserver = $this->objectManagerHelper->getObject(
77 \
Magento\Captcha\Observer\CheckContactUsFormObserver::class,
79 'helper' => $this->helperMock,
80 'actionFlag' => $this->actionFlagMock,
81 'messageManager' => $this->messageManagerMock,
82 'redirect' => $this->redirectMock,
83 'captchaStringResolver' => $this->captchaStringResolverMock
86 $this->objectManagerHelper->setBackwardCompatibleProperty(
87 $this->checkContactUsFormObserver,
89 $this->dataPersistorMock
92 $this->captchaMock = $this->createMock(\
Magento\Captcha\Model\DefaultModel::class);
97 $formId =
'contact_us';
98 $captchaValue =
'some-value';
104 ->with(\
Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE,
null)
105 ->willReturn([$formId => $captchaValue]);
107 $this->captchaMock->expects($this->any())->method(
'isRequired')->willReturn(
true);
108 $this->captchaMock->expects($this->once())
109 ->method(
'isCorrect')
110 ->with($captchaValue)
112 $this->captchaStringResolverMock->expects($this->once())
115 ->willReturn($captchaValue);
116 $this->helperMock->expects($this->any())
117 ->method(
'getCaptcha')
118 ->with($formId)->willReturn($this->captchaMock);
119 $this->sessionMock->expects($this->never())->method(
'addError');
121 $this->checkContactUsFormObserver->execute(
128 $formId =
'contact_us';
129 $captchaValue =
'some-value';
130 $warningMessage =
'Incorrect CAPTCHA.';
131 $redirectRoutePath =
'contact/index/index';
132 $redirectUrl =
'http://magento.com/contacts/';
139 ->with(\
Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE,
null)
140 ->willReturn([$formId => $captchaValue]);
142 ->method(
'getPostValue')
145 $this->redirectMock->expects($this->once())
147 ->with(
$response, $redirectRoutePath, [])
148 ->willReturn($redirectUrl);
153 $this->captchaMock->expects($this->any())->method(
'isRequired')->willReturn(
true);
154 $this->captchaMock->expects($this->once())
155 ->method(
'isCorrect')
156 ->with($captchaValue)
158 $this->captchaStringResolverMock->expects($this->once())
161 ->willReturn($captchaValue);
162 $this->helperMock->expects($this->any())
163 ->method(
'getCaptcha')
165 ->willReturn($this->captchaMock);
166 $this->messageManagerMock->expects($this->once())->method(
'addError')->with($warningMessage);
167 $this->actionFlagMock->expects($this->once())
169 ->with(
'', \
Magento\Framework\
App\Action\Action::FLAG_NO_DISPATCH,
true);
170 $this->dataPersistorMock->expects($this->once())
174 $this->checkContactUsFormObserver->execute(
181 $this->helperMock->expects($this->any())
182 ->method(
'getCaptcha')
184 ->willReturn($this->captchaMock);
185 $this->captchaMock->expects($this->any())->method(
'isRequired')->willReturn(
false);
186 $this->captchaMock->expects($this->never())->method(
'isCorrect');
188 $this->checkContactUsFormObserver->execute(
new \
Magento\Framework\Event\Observer());