Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreatePostTest.php
Go to the documentation of this file.
1 <?php
9 
14 
19 class CreatePostTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $model;
25 
30 
34  protected $customerUrl;
35 
39  protected $registration;
40 
44  protected $redirectMock;
45 
50 
54  protected $accountManagement;
55 
59  protected $responseMock;
60 
64  protected $requestMock;
65 
69  protected $urlMock;
70 
75 
79  protected $customerMock;
80 
85 
90 
94  protected $scopeConfigMock;
95 
99  protected $storeManagerMock;
100 
104  protected $storeMock;
105 
110 
114  protected $subscriberMock;
115 
120 
125 
130 
134  protected function setUp()
135  {
136  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
141  $this->markTestSkipped('Cannot be unit tested with the auto generated builder dependencies');
142  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
143  $this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
144  $this->responseMock = $this->createMock(\Magento\Framework\Webapi\Response::class);
145  $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
146 
147  $this->urlMock = $this->createMock(\Magento\Framework\Url::class);
148  $urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
149  $urlFactoryMock->expects($this->once())
150  ->method('create')
151  ->will($this->returnValue($this->urlMock));
152 
153  $this->customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
154  $this->customerDetailsMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
155  $this->customerDetailsFactoryMock = $this->createMock(
156  \Magento\Customer\Api\Data\CustomerInterfaceFactory::class
157  );
158 
159  $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
160  $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
161 
162  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
163  $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
164 
165  $this->customerRepository = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
166  $this->accountManagement = $this->createMock(\Magento\Customer\Api\AccountManagementInterface::class);
167  $this->addressHelperMock = $this->createMock(\Magento\Customer\Helper\Address::class);
168  $formFactoryMock = $this->createMock(\Magento\Customer\Model\Metadata\FormFactory::class);
169 
170  $this->subscriberMock = $this->createMock(\Magento\Newsletter\Model\Subscriber::class);
171  $subscriberFactoryMock = $this->createPartialMock(
172  \Magento\Newsletter\Model\SubscriberFactory::class,
173  ['create']
174  );
175  $subscriberFactoryMock->expects($this->any())
176  ->method('create')
177  ->will($this->returnValue($this->subscriberMock));
178 
179  $regionFactoryMock = $this->createMock(\Magento\Customer\Api\Data\RegionInterfaceFactory::class);
180  $addressFactoryMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterfaceFactory::class);
181  $this->customerUrl = $this->createMock(\Magento\Customer\Model\Url::class);
182  $this->registration = $this->createMock(\Magento\Customer\Model\Registration::class);
183  $escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
184  $this->customerExtractorMock = $this->createMock(\Magento\Customer\Model\CustomerExtractor::class);
185  $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
186 
187  $eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
188 
189  $this->resultRedirectFactoryMock = $this->getMockBuilder(
190  \Magento\Framework\Controller\Result\RedirectFactory::class
191  )
192  ->setMethods(['create'])
193  ->disableOriginalConstructor()
194  ->getMock();
195  $this->resultRedirectFactoryMock->expects($this->any())
196  ->method('create')
197  ->willReturn($this->redirectMock);
198 
199  $contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
200  $contextMock->expects($this->any())
201  ->method('getRequest')
202  ->willReturn($this->requestMock);
203  $contextMock->expects($this->any())
204  ->method('getResponse')
205  ->willReturn($this->responseMock);
206  $contextMock->expects($this->any())
207  ->method('getRedirect')
208  ->willReturn($this->redirectMock);
209  $contextMock->expects($this->any())
210  ->method('getMessageManager')
211  ->willReturn($this->messageManagerMock);
212  $contextMock->expects($this->any())
213  ->method('getEventManager')
214  ->willReturn($eventManagerMock);
215  $contextMock->expects($this->any())
216  ->method('getResultRedirectFactory')
217  ->willReturn($this->resultRedirectFactoryMock);
218 
219  $this->model = $objectManager->getObject(
220  \Magento\Customer\Controller\Account\CreatePost::class,
221  [
222  'context' => $contextMock,
223  'customerSession' => $this->customerSessionMock,
224  'scopeConfig' => $this->scopeConfigMock,
225  'storeManager' => $this->storeManagerMock,
226  'accountManagement' => $this->accountManagement,
227  'addressHelper' => $this->addressHelperMock,
228  'urlFactory' => $urlFactoryMock,
229  'formFactory' => $formFactoryMock,
230  'subscriberFactory' => $subscriberFactoryMock,
231  'regionDataFactory' => $regionFactoryMock,
232  'addressDataFactory' => $addressFactoryMock,
233  'customerDetailsFactory' => $this->customerDetailsFactoryMock,
234  'customerUrl' => $this->customerUrl,
235  'registration' => $this->registration,
236  'escape' => $escaperMock,
237  'customerExtractor' => $this->customerExtractorMock,
238  'dataObjectHelper' => $this->dataObjectHelperMock,
239  ]
240  );
241  }
242 
247  {
248  $this->customerSessionMock->expects($this->once())
249  ->method('isLoggedIn')
250  ->will($this->returnValue(false));
251 
252  $this->registration->expects($this->once())
253  ->method('isAllowed')
254  ->will($this->returnValue(false));
255 
256  $this->redirectMock->expects($this->once())
257  ->method('redirect')
258  ->with($this->responseMock, '*/*/', [])
259  ->will($this->returnValue(false));
260 
261  $this->customerRepository->expects($this->never())
262  ->method('save');
263 
264  $this->model->execute();
265  }
266 
267  public function testRegenerateIdOnExecution()
268  {
269  $this->customerSessionMock->expects($this->once())
270  ->method('regenerateId');
271  $this->customerSessionMock->expects($this->once())
272  ->method('isLoggedIn')
273  ->will($this->returnValue(false));
274 
275  $this->registration->expects($this->once())
276  ->method('isAllowed')
277  ->will($this->returnValue(true));
278  $this->requestMock->expects($this->once())
279  ->method('isPost')
280  ->will($this->returnValue(true));
281 
282  $this->customerExtractorMock->expects($this->once())
283  ->method('extract')
284  ->willReturn($this->customerMock);
285  $this->accountManagement->expects($this->once())
286  ->method('createAccount')
287  ->willReturn($this->customerMock);
288  $this->storeManagerMock->expects($this->once())
289  ->method('getStore')
290  ->willReturn($this->storeMock);
291  $this->model->execute();
292  }
293 
305  public function testSuccessMessage(
306  $customerId,
308  $password,
309  $confirmationStatus,
310  $vatValidationEnabled,
311  $addressType,
312  $successMessage
313  ) {
314  $this->customerSessionMock->expects($this->once())
315  ->method('isLoggedIn')
316  ->will($this->returnValue(false));
317 
318  $this->registration->expects($this->once())
319  ->method('isAllowed')
320  ->will($this->returnValue(true));
321  $this->customerUrl->expects($this->once())
322  ->method('getEmailConfirmationUrl')
323  ->will($this->returnValue($customerEmail));
324 
325  $this->customerSessionMock->expects($this->once())
326  ->method('regenerateId');
327 
328  $this->customerMock->expects($this->any())
329  ->method('getId')
330  ->will($this->returnValue($customerId));
331  $this->customerMock->expects($this->any())
332  ->method('getEmail')
333  ->will($this->returnValue($customerEmail));
334 
335  $this->customerExtractorMock->expects($this->any())
336  ->method('extract')
337  ->with($this->equalTo('customer_account_create'), $this->equalTo($this->requestMock))
338  ->will($this->returnValue($this->customerMock));
339 
340  $this->requestMock->expects($this->once())
341  ->method('isPost')
342  ->will($this->returnValue(true));
343  $this->requestMock->expects($this->any())
344  ->method('getPost')
345  ->will($this->returnValue(false));
346 
347  $this->requestMock->expects($this->any())
348  ->method('getParam')
349  ->willReturnMap([
350  ['password', null, $password],
351  ['password_confirmation', null, $password],
352  ['is_subscribed', false, true],
353  ]);
354 
355  $this->customerMock->expects($this->once())
356  ->method('setAddresses')
357  ->with($this->equalTo([]))
358  ->will($this->returnSelf());
359 
360  $this->accountManagement->expects($this->once())
361  ->method('createAccount')
362  ->with($this->equalTo($this->customerDetailsMock), $this->equalTo($password), '')
363  ->will($this->returnValue($this->customerMock));
364  $this->accountManagement->expects($this->once())
365  ->method('getConfirmationStatus')
366  ->with($this->equalTo($customerId))
367  ->will($this->returnValue($confirmationStatus));
368 
369  $this->subscriberMock->expects($this->once())
370  ->method('subscribeCustomerById')
371  ->with($this->equalTo($customerId));
372 
373  $this->messageManagerMock->expects($this->any())
374  ->method('addSuccess')
375  ->with($this->stringContains($successMessage))
376  ->will($this->returnSelf());
377 
378  $this->addressHelperMock->expects($this->any())
379  ->method('isVatValidationEnabled')
380  ->will($this->returnValue($vatValidationEnabled));
381  $this->addressHelperMock->expects($this->any())
382  ->method('getTaxCalculationAddressType')
383  ->will($this->returnValue($addressType));
384 
385  $this->model->execute();
386  }
387 
392  {
393  return [
394  [
395  1,
397  '123123q',
399  false,
401  'An account confirmation is required',
402  ],
403  [
404  1,
406  '123123q',
408  false,
410  'Thank you for registering with',
411  ],
412  [
413  1,
415  '123123q',
417  true,
419  'enter you shipping address for proper VAT calculation',
420  ],
421  [
422  1,
424  '123123q',
426  true,
428  'enter you billing address for proper VAT calculation',
429  ],
430  ];
431  }
432 
443  public function testSuccessRedirect(
444  $customerId,
445  $password,
446  $confirmationStatus,
447  $successUrl,
448  $isSetFlag,
449  $successMessage
450  ) {
451  $this->customerSessionMock->expects($this->once())
452  ->method('isLoggedIn')
453  ->will($this->returnValue(false));
454 
455  $this->registration->expects($this->once())
456  ->method('isAllowed')
457  ->will($this->returnValue(true));
458 
459  $this->customerSessionMock->expects($this->once())
460  ->method('regenerateId');
461 
462  $this->customerMock->expects($this->any())
463  ->method('getId')
464  ->will($this->returnValue($customerId));
465 
466  $this->customerExtractorMock->expects($this->any())
467  ->method('extract')
468  ->with($this->equalTo('customer_account_create'), $this->equalTo($this->requestMock))
469  ->will($this->returnValue($this->customerMock));
470 
471  $this->requestMock->expects($this->once())
472  ->method('isPost')
473  ->will($this->returnValue(true));
474  $this->requestMock->expects($this->any())
475  ->method('getPost')
476  ->will($this->returnValue(false));
477 
478  $this->requestMock->expects($this->any())
479  ->method('getParam')
480  ->willReturnMap([
481  ['password', null, $password],
482  ['password_confirmation', null, $password],
483  ['is_subscribed', false, true],
484  ]);
485 
486  $this->customerMock->expects($this->once())
487  ->method('setAddresses')
488  ->with($this->equalTo([]))
489  ->will($this->returnSelf());
490 
491  $this->accountManagement->expects($this->once())
492  ->method('createAccount')
493  ->with($this->equalTo($this->customerDetailsMock), $this->equalTo($password), '')
494  ->will($this->returnValue($this->customerMock));
495  $this->accountManagement->expects($this->once())
496  ->method('getConfirmationStatus')
497  ->with($this->equalTo($customerId))
498  ->will($this->returnValue($confirmationStatus));
499 
500  $this->subscriberMock->expects($this->once())
501  ->method('subscribeCustomerById')
502  ->with($this->equalTo($customerId));
503 
504  $this->messageManagerMock->expects($this->any())
505  ->method('addSuccess')
506  ->with($this->stringContains($successMessage))
507  ->will($this->returnSelf());
508 
509  $this->urlMock->expects($this->any())
510  ->method('getUrl')
511  ->willReturnMap([
512  ['*/*/index', ['_secure' => true], $successUrl],
513  ['*/*/create', ['_secure' => true], $successUrl],
514  ]);
515  $this->redirectMock->expects($this->once())
516  ->method('success')
517  ->with($this->equalTo($successUrl))
518  ->will($this->returnValue($successUrl));
519  $this->scopeConfigMock->expects($this->once())
520  ->method('isSetFlag')
521  ->with(
523  $this->equalTo(ScopeInterface::SCOPE_STORE)
524  )
525  ->will($this->returnValue($isSetFlag));
526  $this->storeMock->expects($this->any())
527  ->method('getFrontendName')
528  ->will($this->returnValue('frontend'));
529  $this->storeManagerMock->expects($this->any())
530  ->method('getStore')
531  ->will($this->returnValue($this->storeMock));
532 
533  $this->model->execute();
534  }
535 
540  {
541  return [
542  [
543  1,
544  '123123q',
546  'http://example.com/success',
547  true,
548  'Thank you for registering with',
549  ],
550  [
551  1,
552  '123123q',
554  'http://example.com/success',
555  false,
556  'Thank you for registering with',
557  ],
558  ];
559  }
560 }
getSuccessRedirectDataProvider()
$customerUrl
$resultRedirectFactoryMock
$storeMock
testRegenerateIdOnExecution()
$model
testSuccessMessage( $customerId, $customerEmail, $password, $confirmationStatus, $vatValidationEnabled, $addressType, $successMessage)
setUp()
$objectManager
Definition: bootstrap.php:17
$urlMock
$addressHelperMock
testSuccessRedirect( $customerId, $password, $confirmationStatus, $successUrl, $isSetFlag, $successMessage)
getSuccessMessageDataProvider()
testCreatePostActionRegistrationDisabled()
$subscriberMock
$storeManagerMock
$accountManagement
const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
Definition: Url.php:27
$customerMock
$customerDetailsFactoryMock
$dataObjectHelperMock
$requestMock
$scopeConfigMock
$responseMock
$customerExtractorMock
$redirectMock
$customerRepository
$registration
$messageManagerMock
$customerDetailsMock
$customerSessionMock