Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfirmTest.php
Go to the documentation of this file.
1 <?php
8 
13 
18 class ConfirmTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $model;
24 
28  protected $requestMock;
29 
33  protected $responseMock;
34 
39 
43  protected $redirectMock;
44 
48  protected $urlMock;
49 
54 
59 
63  protected $customerDataMock;
64 
69 
73  protected $addressHelperMock;
74 
78  protected $storeManagerMock;
79 
83  protected $storeMock;
84 
88  protected $scopeConfigMock;
89 
93  protected $contextMock;
94 
99 
100  protected function setUp()
101  {
102  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
103  $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
104  $this->responseMock = $this->createPartialMock(
105  \Magento\Framework\App\Response\Http::class,
106  ['setRedirect', '__wakeup']
107  );
108  $viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
109  $this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
110 
111  $this->urlMock = $this->createMock(\Magento\Framework\Url::class);
112  $urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
113  $urlFactoryMock->expects($this->any())
114  ->method('create')
115  ->will($this->returnValue($this->urlMock));
116 
117  $this->customerAccountManagementMock =
118  $this->getMockForAbstractClass(\Magento\Customer\Api\AccountManagementInterface::class);
119  $this->customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
120 
121  $this->customerRepositoryMock =
122  $this->getMockForAbstractClass(\Magento\Customer\Api\CustomerRepositoryInterface::class);
123 
124  $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
125  $this->addressHelperMock = $this->createMock(\Magento\Customer\Helper\Address::class);
126  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
127  $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
128  $this->redirectResultMock = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
129 
130  $resultFactoryMock = $this->createPartialMock(\Magento\Framework\Controller\ResultFactory::class, ['create']);
131  $resultFactoryMock->expects($this->once())
132  ->method('create')
133  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
134  ->willReturn($this->redirectResultMock);
135 
136  $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
137  $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
138  $this->contextMock->expects($this->any())
139  ->method('getRequest')
140  ->willReturn($this->requestMock);
141  $this->contextMock->expects($this->any())
142  ->method('getResponse')
143  ->willReturn($this->responseMock);
144  $this->contextMock->expects($this->any())
145  ->method('getRedirect')
146  ->willReturn($this->redirectMock);
147  $this->contextMock->expects($this->any())
148  ->method('getView')
149  ->willReturn($viewMock);
150  $this->contextMock->expects($this->any())
151  ->method('getMessageManager')
152  ->willReturn($this->messageManagerMock);
153  $this->contextMock->expects($this->any())
154  ->method('getResultFactory')
155  ->willReturn($resultFactoryMock);
156 
157  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
158 
159  $this->model = $objectManagerHelper->getObject(
160  \Magento\Customer\Controller\Account\Confirm::class,
161  [
162  'context' => $this->contextMock,
163  'customerSession' => $this->customerSessionMock,
164  'scopeConfig' => $this->scopeConfigMock,
165  'storeManager' => $this->storeManagerMock,
166  'customerAccountManagement' => $this->customerAccountManagementMock,
167  'customerRepository' => $this->customerRepositoryMock,
168  'addressHelper' => $this->addressHelperMock,
169  'urlFactory' => $urlFactoryMock,
170  ]
171  );
172  }
173 
174  public function testIsLoggedIn()
175  {
176  $this->customerSessionMock->expects($this->once())
177  ->method('isLoggedIn')
178  ->will($this->returnValue(true));
179 
180  $this->redirectResultMock->expects($this->once())
181  ->method('setPath')
182  ->with('*/*/')
183  ->willReturnSelf();
184 
185  $this->assertInstanceOf(\Magento\Framework\Controller\Result\Redirect::class, $this->model->execute());
186  }
187 
191  public function testNoCustomerIdInRequest($customerId, $key)
192  {
193  $this->customerSessionMock->expects($this->once())
194  ->method('isLoggedIn')
195  ->will($this->returnValue(false));
196 
197  $this->requestMock->expects($this->at(0))
198  ->method('getParam')
199  ->with($this->equalTo('id'), false)
200  ->will($this->returnValue($customerId));
201  $this->requestMock->expects($this->at(1))
202  ->method('getParam')
203  ->with($this->equalTo('key'), false)
204  ->will($this->returnValue($key));
205 
206  $exception = new \Exception('Bad request.');
207  $this->messageManagerMock->expects($this->once())
208  ->method('addException')
209  ->with($this->equalTo($exception), $this->equalTo('There was an error confirming the account'));
210 
211  $testUrl = 'http://example.com';
212  $this->urlMock->expects($this->once())
213  ->method('getUrl')
214  ->with($this->equalTo('*/*/index'), ['_secure' => true])
215  ->will($this->returnValue($testUrl));
216 
217  $this->redirectMock->expects($this->once())
218  ->method('error')
219  ->with($this->equalTo($testUrl))
220  ->will($this->returnValue($testUrl));
221 
222  $this->redirectResultMock->expects($this->once())
223  ->method('setUrl')
224  ->with($this->equalTo($testUrl))
225  ->willReturnSelf();
226 
227  $this->assertInstanceOf(\Magento\Framework\Controller\Result\Redirect::class, $this->model->execute());
228  }
229 
233  public function getParametersDataProvider()
234  {
235  return [
236  [true, false],
237  [false, true],
238  ];
239  }
240 
250  public function testSuccessMessage($customerId, $key, $vatValidationEnabled, $addressType, $successMessage)
251  {
252  $this->customerSessionMock->expects($this->once())
253  ->method('isLoggedIn')
254  ->will($this->returnValue(false));
255 
256  $this->requestMock->expects($this->any())
257  ->method('getParam')
258  ->willReturnMap([
259  ['id', false, $customerId],
260  ['key', false, $key],
261  ]);
262 
263  $this->customerRepositoryMock->expects($this->any())
264  ->method('getById')
265  ->with($customerId)
266  ->will($this->returnValue($this->customerDataMock));
267 
269  $this->customerDataMock->expects($this->once())
270  ->method('getEmail')
271  ->will($this->returnValue($email));
272 
273  $this->customerAccountManagementMock->expects($this->once())
274  ->method('activate')
275  ->with($this->equalTo($email), $this->equalTo($key))
276  ->will($this->returnValue($this->customerDataMock));
277 
278  $this->customerSessionMock->expects($this->any())
279  ->method('setCustomerDataAsLoggedIn')
280  ->with($this->equalTo($this->customerDataMock))
281  ->willReturnSelf();
282 
283  $this->messageManagerMock->expects($this->any())
284  ->method('addSuccess')
285  ->with($this->stringContains($successMessage))
286  ->willReturnSelf();
287 
288  $this->addressHelperMock->expects($this->once())
289  ->method('isVatValidationEnabled')
290  ->will($this->returnValue($vatValidationEnabled));
291  $this->addressHelperMock->expects($this->any())
292  ->method('getTaxCalculationAddressType')
293  ->will($this->returnValue($addressType));
294 
295  $this->storeMock->expects($this->any())
296  ->method('getFrontendName')
297  ->will($this->returnValue('frontend'));
298  $this->storeManagerMock->expects($this->any())
299  ->method('getStore')
300  ->will($this->returnValue($this->storeMock));
301 
302  $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
303  ->disableOriginalConstructor()
304  ->getMock();
305  $cookieMetadataManager->expects($this->once())
306  ->method('getCookie')
307  ->with('mage-cache-sessid')
308  ->willReturn(true);
309  $cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
310  ->disableOriginalConstructor()
311  ->getMock();
312  $cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
313  ->disableOriginalConstructor()
314  ->getMock();
315  $cookieMetadataFactory->expects($this->once())
316  ->method('createCookieMetadata')
317  ->willReturn($cookieMetadata);
318  $cookieMetadata->expects($this->once())
319  ->method('setPath')
320  ->with('/');
321  $cookieMetadataManager->expects($this->once())
322  ->method('deleteCookie')
323  ->with('mage-cache-sessid', $cookieMetadata);
324 
325  $refClass = new \ReflectionClass(Confirm::class);
326  $cookieMetadataManagerProperty = $refClass->getProperty('cookieMetadataManager');
327  $cookieMetadataManagerProperty->setAccessible(true);
328  $cookieMetadataManagerProperty->setValue($this->model, $cookieMetadataManager);
329 
330  $cookieMetadataFactoryProperty = $refClass->getProperty('cookieMetadataFactory');
331  $cookieMetadataFactoryProperty->setAccessible(true);
332  $cookieMetadataFactoryProperty->setValue($this->model, $cookieMetadataFactory);
333 
334  $this->model->execute();
335  }
336 
341  {
342  return [
343  [1, 1, false, null, __('Thank you for registering with')],
344  [1, 1, true, Address::TYPE_BILLING, __('enter your billing address for proper VAT calculation')],
345  [1, 1, true, Address::TYPE_SHIPPING, __('enter your shipping address for proper VAT calculation')],
346  ];
347  }
348 
360  public function testSuccessRedirect(
361  $customerId,
362  $key,
363  $backUrl,
364  $successUrl,
365  $resultUrl,
366  $isSetFlag,
367  $successMessage
368  ) {
369  $this->customerSessionMock->expects($this->once())
370  ->method('isLoggedIn')
371  ->will($this->returnValue(false));
372 
373  $this->requestMock->expects($this->any())
374  ->method('getParam')
375  ->willReturnMap([
376  ['id', false, $customerId],
377  ['key', false, $key],
378  ['back_url', false, $backUrl],
379  ]);
380 
381  $this->customerRepositoryMock->expects($this->any())
382  ->method('getById')
383  ->with($customerId)
384  ->will($this->returnValue($this->customerDataMock));
385 
387  $this->customerDataMock->expects($this->once())
388  ->method('getEmail')
389  ->will($this->returnValue($email));
390 
391  $this->customerAccountManagementMock->expects($this->once())
392  ->method('activate')
393  ->with($this->equalTo($email), $this->equalTo($key))
394  ->will($this->returnValue($this->customerDataMock));
395 
396  $this->customerSessionMock->expects($this->any())
397  ->method('setCustomerDataAsLoggedIn')
398  ->with($this->equalTo($this->customerDataMock))
399  ->willReturnSelf();
400 
401  $this->messageManagerMock->expects($this->any())
402  ->method('addSuccess')
403  ->with($this->stringContains($successMessage))
404  ->willReturnSelf();
405 
406  $this->storeMock->expects($this->any())
407  ->method('getFrontendName')
408  ->will($this->returnValue('frontend'));
409  $this->storeManagerMock->expects($this->any())
410  ->method('getStore')
411  ->will($this->returnValue($this->storeMock));
412 
413  $this->urlMock->expects($this->any())
414  ->method('getUrl')
415  ->with($this->equalTo('*/*/index'), ['_secure' => true])
416  ->will($this->returnValue($successUrl));
417 
418  $this->redirectMock->expects($this->once())
419  ->method('success')
420  ->with($this->equalTo($resultUrl))
421  ->willReturn($resultUrl);
422 
423  $this->scopeConfigMock->expects($this->any())
424  ->method('isSetFlag')
425  ->with(
428  )
429  ->willReturn($isSetFlag);
430 
431  $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
432  ->disableOriginalConstructor()
433  ->getMock();
434  $cookieMetadataManager->expects($this->once())
435  ->method('getCookie')
436  ->with('mage-cache-sessid')
437  ->willReturn(false);
438 
439  $refClass = new \ReflectionClass(Confirm::class);
440  $refProperty = $refClass->getProperty('cookieMetadataManager');
441  $refProperty->setAccessible(true);
442  $refProperty->setValue($this->model, $cookieMetadataManager);
443 
444  $this->model->execute();
445  }
446 
451  {
452  return [
453  [
454  1,
455  1,
456  'http://example.com/back',
457  null,
458  'http://example.com/back',
459  true,
460  __('Thank you for registering with'),
461  ],
462  [
463  1,
464  1,
465  null,
466  'http://example.com/success',
467  'http://example.com/success',
468  true,
469  __('Thank you for registering with'),
470  ],
471  [
472  1,
473  1,
474  null,
475  'http://example.com/success',
476  'http://example.com/success',
477  false,
478  __('Thank you for registering with'),
479  ],
480  ];
481  }
482 }
$email
Definition: details.phtml:13
__()
Definition: __.php:13
testSuccessMessage($customerId, $key, $vatValidationEnabled, $addressType, $successMessage)
const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
Definition: Url.php:27
testSuccessRedirect( $customerId, $key, $backUrl, $successUrl, $resultUrl, $isSetFlag, $successMessage)