Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckContactUsFormObserverTest.php
Go to the documentation of this file.
1 <?php
7 
11 class CheckContactUsFormObserverTest extends \PHPUnit\Framework\TestCase
12 {
17 
21  protected $helperMock;
22 
26  protected $actionFlagMock;
27 
28  /*
29  * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
30  */
32 
36  protected $redirectMock;
37 
42 
47 
51  protected $sessionMock;
52 
56  protected $captchaMock;
57 
61  protected $dataPersistorMock;
62 
63  protected function setUp()
64  {
65  $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
66 
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();
75 
76  $this->checkContactUsFormObserver = $this->objectManagerHelper->getObject(
77  \Magento\Captcha\Observer\CheckContactUsFormObserver::class,
78  [
79  'helper' => $this->helperMock,
80  'actionFlag' => $this->actionFlagMock,
81  'messageManager' => $this->messageManagerMock,
82  'redirect' => $this->redirectMock,
83  'captchaStringResolver' => $this->captchaStringResolverMock
84  ]
85  );
86  $this->objectManagerHelper->setBackwardCompatibleProperty(
87  $this->checkContactUsFormObserver,
88  'dataPersistor',
89  $this->dataPersistorMock
90  );
91 
92  $this->captchaMock = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
93  }
94 
96  {
97  $formId = 'contact_us';
98  $captchaValue = 'some-value';
99 
100  $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
101  $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
102  $request->expects($this->any())
103  ->method('getPost')
104  ->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)
105  ->willReturn([$formId => $captchaValue]);
106  $controller->expects($this->any())->method('getRequest')->willReturn($request);
107  $this->captchaMock->expects($this->any())->method('isRequired')->willReturn(true);
108  $this->captchaMock->expects($this->once())
109  ->method('isCorrect')
110  ->with($captchaValue)
111  ->willReturn(true);
112  $this->captchaStringResolverMock->expects($this->once())
113  ->method('resolve')
114  ->with($request, $formId)
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');
120 
121  $this->checkContactUsFormObserver->execute(
122  new \Magento\Framework\Event\Observer(['controller_action' => $controller])
123  );
124  }
125 
127  {
128  $formId = 'contact_us';
129  $captchaValue = 'some-value';
130  $warningMessage = 'Incorrect CAPTCHA.';
131  $redirectRoutePath = 'contact/index/index';
132  $redirectUrl = 'http://magento.com/contacts/';
133  $postData = ['name' => 'Some Name'];
134 
135  $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
136  $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
137  $request->expects($this->any())
138  ->method('getPost')
139  ->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)
140  ->willReturn([$formId => $captchaValue]);
141  $request->expects($this->once())
142  ->method('getPostValue')
143  ->willReturn($postData);
144 
145  $this->redirectMock->expects($this->once())
146  ->method('redirect')
147  ->with($response, $redirectRoutePath, [])
148  ->willReturn($redirectUrl);
149 
150  $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
151  $controller->expects($this->any())->method('getRequest')->willReturn($request);
152  $controller->expects($this->any())->method('getResponse')->willReturn($response);
153  $this->captchaMock->expects($this->any())->method('isRequired')->willReturn(true);
154  $this->captchaMock->expects($this->once())
155  ->method('isCorrect')
156  ->with($captchaValue)
157  ->willReturn(false);
158  $this->captchaStringResolverMock->expects($this->once())
159  ->method('resolve')
160  ->with($request, $formId)
161  ->willReturn($captchaValue);
162  $this->helperMock->expects($this->any())
163  ->method('getCaptcha')
164  ->with($formId)
165  ->willReturn($this->captchaMock);
166  $this->messageManagerMock->expects($this->once())->method('addError')->with($warningMessage);
167  $this->actionFlagMock->expects($this->once())
168  ->method('set')
169  ->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
170  $this->dataPersistorMock->expects($this->once())
171  ->method('set')
172  ->with($formId, $postData);
173 
174  $this->checkContactUsFormObserver->execute(
175  new \Magento\Framework\Event\Observer(['controller_action' => $controller])
176  );
177  }
178 
180  {
181  $this->helperMock->expects($this->any())
182  ->method('getCaptcha')
183  ->with('contact_us')
184  ->willReturn($this->captchaMock);
185  $this->captchaMock->expects($this->any())->method('isRequired')->willReturn(false);
186  $this->captchaMock->expects($this->never())->method('isCorrect');
187 
188  $this->checkContactUsFormObserver->execute(new \Magento\Framework\Event\Observer());
189  }
190 }
$response
Definition: 404.php:11
$controller
Definition: info.phtml:14