123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Controller\Account;
- use Magento\Customer\Api\AccountManagementInterface;
- use Magento\Customer\Helper\Address;
- use Magento\Customer\Model\Url;
- use Magento\Store\Model\ScopeInterface;
- /**
- * @SuppressWarnings(PHPMD.TooManyFields)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class CreatePostTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Customer\Controller\Account\CreatePost
- */
- protected $model;
- /**
- * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerSessionMock;
- /**
- * @var \Magento\Customer\Model\Url|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerUrl;
- /**
- * @var \Magento\Customer\Model\Registration|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $registration;
- /**
- * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $redirectMock;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerRepository;
- /**
- * @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $accountManagement;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $responseMock;
- /**
- * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \Magento\Framework\Url|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $urlMock;
- /**
- * @var \Magento\Customer\Model\CustomerExtractor|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerExtractorMock;
- /**
- * @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerMock;
- /**
- * @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerDetailsMock;
- /**
- * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerDetailsFactoryMock;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfigMock;
- /**
- * @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManagerMock;
- /**
- * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeMock;
- /**
- * @var \Magento\Customer\Helper\Address|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressHelperMock;
- /**
- * @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $subscriberMock;
- /**
- * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageManagerMock;
- /**
- * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $dataObjectHelperMock;
- /**
- * @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultRedirectFactoryMock;
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function setUp()
- {
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- /**
- * This test can be unskipped when the Unit test object manager helper is enabled to return correct DataBuilders
- * For now the \Magento\Customer\Test\Unit\Controller\AccountTest sufficiently covers the SUT
- */
- $this->markTestSkipped('Cannot be unit tested with the auto generated builder dependencies');
- $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
- $this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
- $this->responseMock = $this->createMock(\Magento\Framework\Webapi\Response::class);
- $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
- $this->urlMock = $this->createMock(\Magento\Framework\Url::class);
- $urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
- $urlFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($this->urlMock));
- $this->customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- $this->customerDetailsMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- $this->customerDetailsFactoryMock = $this->createMock(
- \Magento\Customer\Api\Data\CustomerInterfaceFactory::class
- );
- $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
- $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
- $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
- $this->customerRepository = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- $this->accountManagement = $this->createMock(\Magento\Customer\Api\AccountManagementInterface::class);
- $this->addressHelperMock = $this->createMock(\Magento\Customer\Helper\Address::class);
- $formFactoryMock = $this->createMock(\Magento\Customer\Model\Metadata\FormFactory::class);
- $this->subscriberMock = $this->createMock(\Magento\Newsletter\Model\Subscriber::class);
- $subscriberFactoryMock = $this->createPartialMock(
- \Magento\Newsletter\Model\SubscriberFactory::class,
- ['create']
- );
- $subscriberFactoryMock->expects($this->any())
- ->method('create')
- ->will($this->returnValue($this->subscriberMock));
- $regionFactoryMock = $this->createMock(\Magento\Customer\Api\Data\RegionInterfaceFactory::class);
- $addressFactoryMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterfaceFactory::class);
- $this->customerUrl = $this->createMock(\Magento\Customer\Model\Url::class);
- $this->registration = $this->createMock(\Magento\Customer\Model\Registration::class);
- $escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
- $this->customerExtractorMock = $this->createMock(\Magento\Customer\Model\CustomerExtractor::class);
- $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
- $eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
- $this->resultRedirectFactoryMock = $this->getMockBuilder(
- \Magento\Framework\Controller\Result\RedirectFactory::class
- )
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->resultRedirectFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->redirectMock);
- $contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
- $contextMock->expects($this->any())
- ->method('getRequest')
- ->willReturn($this->requestMock);
- $contextMock->expects($this->any())
- ->method('getResponse')
- ->willReturn($this->responseMock);
- $contextMock->expects($this->any())
- ->method('getRedirect')
- ->willReturn($this->redirectMock);
- $contextMock->expects($this->any())
- ->method('getMessageManager')
- ->willReturn($this->messageManagerMock);
- $contextMock->expects($this->any())
- ->method('getEventManager')
- ->willReturn($eventManagerMock);
- $contextMock->expects($this->any())
- ->method('getResultRedirectFactory')
- ->willReturn($this->resultRedirectFactoryMock);
- $this->model = $objectManager->getObject(
- \Magento\Customer\Controller\Account\CreatePost::class,
- [
- 'context' => $contextMock,
- 'customerSession' => $this->customerSessionMock,
- 'scopeConfig' => $this->scopeConfigMock,
- 'storeManager' => $this->storeManagerMock,
- 'accountManagement' => $this->accountManagement,
- 'addressHelper' => $this->addressHelperMock,
- 'urlFactory' => $urlFactoryMock,
- 'formFactory' => $formFactoryMock,
- 'subscriberFactory' => $subscriberFactoryMock,
- 'regionDataFactory' => $regionFactoryMock,
- 'addressDataFactory' => $addressFactoryMock,
- 'customerDetailsFactory' => $this->customerDetailsFactoryMock,
- 'customerUrl' => $this->customerUrl,
- 'registration' => $this->registration,
- 'escape' => $escaperMock,
- 'customerExtractor' => $this->customerExtractorMock,
- 'dataObjectHelper' => $this->dataObjectHelperMock,
- ]
- );
- }
- /**
- * @return void
- */
- public function testCreatePostActionRegistrationDisabled()
- {
- $this->customerSessionMock->expects($this->once())
- ->method('isLoggedIn')
- ->will($this->returnValue(false));
- $this->registration->expects($this->once())
- ->method('isAllowed')
- ->will($this->returnValue(false));
- $this->redirectMock->expects($this->once())
- ->method('redirect')
- ->with($this->responseMock, '*/*/', [])
- ->will($this->returnValue(false));
- $this->customerRepository->expects($this->never())
- ->method('save');
- $this->model->execute();
- }
- public function testRegenerateIdOnExecution()
- {
- $this->customerSessionMock->expects($this->once())
- ->method('regenerateId');
- $this->customerSessionMock->expects($this->once())
- ->method('isLoggedIn')
- ->will($this->returnValue(false));
- $this->registration->expects($this->once())
- ->method('isAllowed')
- ->will($this->returnValue(true));
- $this->requestMock->expects($this->once())
- ->method('isPost')
- ->will($this->returnValue(true));
- $this->customerExtractorMock->expects($this->once())
- ->method('extract')
- ->willReturn($this->customerMock);
- $this->accountManagement->expects($this->once())
- ->method('createAccount')
- ->willReturn($this->customerMock);
- $this->storeManagerMock->expects($this->once())
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->model->execute();
- }
- /**
- * @param $customerId
- * @param $customerEmail
- * @param $password
- * @param $confirmationStatus
- * @param $vatValidationEnabled
- * @param $addressType
- * @param $successMessage
- *
- * @dataProvider getSuccessMessageDataProvider
- */
- public function testSuccessMessage(
- $customerId,
- $customerEmail,
- $password,
- $confirmationStatus,
- $vatValidationEnabled,
- $addressType,
- $successMessage
- ) {
- $this->customerSessionMock->expects($this->once())
- ->method('isLoggedIn')
- ->will($this->returnValue(false));
- $this->registration->expects($this->once())
- ->method('isAllowed')
- ->will($this->returnValue(true));
- $this->customerUrl->expects($this->once())
- ->method('getEmailConfirmationUrl')
- ->will($this->returnValue($customerEmail));
- $this->customerSessionMock->expects($this->once())
- ->method('regenerateId');
- $this->customerMock->expects($this->any())
- ->method('getId')
- ->will($this->returnValue($customerId));
- $this->customerMock->expects($this->any())
- ->method('getEmail')
- ->will($this->returnValue($customerEmail));
- $this->customerExtractorMock->expects($this->any())
- ->method('extract')
- ->with($this->equalTo('customer_account_create'), $this->equalTo($this->requestMock))
- ->will($this->returnValue($this->customerMock));
- $this->requestMock->expects($this->once())
- ->method('isPost')
- ->will($this->returnValue(true));
- $this->requestMock->expects($this->any())
- ->method('getPost')
- ->will($this->returnValue(false));
- $this->requestMock->expects($this->any())
- ->method('getParam')
- ->willReturnMap([
- ['password', null, $password],
- ['password_confirmation', null, $password],
- ['is_subscribed', false, true],
- ]);
- $this->customerMock->expects($this->once())
- ->method('setAddresses')
- ->with($this->equalTo([]))
- ->will($this->returnSelf());
- $this->accountManagement->expects($this->once())
- ->method('createAccount')
- ->with($this->equalTo($this->customerDetailsMock), $this->equalTo($password), '')
- ->will($this->returnValue($this->customerMock));
- $this->accountManagement->expects($this->once())
- ->method('getConfirmationStatus')
- ->with($this->equalTo($customerId))
- ->will($this->returnValue($confirmationStatus));
- $this->subscriberMock->expects($this->once())
- ->method('subscribeCustomerById')
- ->with($this->equalTo($customerId));
- $this->messageManagerMock->expects($this->any())
- ->method('addSuccess')
- ->with($this->stringContains($successMessage))
- ->will($this->returnSelf());
- $this->addressHelperMock->expects($this->any())
- ->method('isVatValidationEnabled')
- ->will($this->returnValue($vatValidationEnabled));
- $this->addressHelperMock->expects($this->any())
- ->method('getTaxCalculationAddressType')
- ->will($this->returnValue($addressType));
- $this->model->execute();
- }
- /**
- * @return array
- */
- public function getSuccessMessageDataProvider()
- {
- return [
- [
- 1,
- 'customer@example.com',
- '123123q',
- AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED,
- false,
- Address::TYPE_SHIPPING,
- 'An account confirmation is required',
- ],
- [
- 1,
- 'customer@example.com',
- '123123q',
- AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED,
- false,
- Address::TYPE_SHIPPING,
- 'Thank you for registering with',
- ],
- [
- 1,
- 'customer@example.com',
- '123123q',
- AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED,
- true,
- Address::TYPE_SHIPPING,
- 'enter you shipping address for proper VAT calculation',
- ],
- [
- 1,
- 'customer@example.com',
- '123123q',
- AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED,
- true,
- Address::TYPE_BILLING,
- 'enter you billing address for proper VAT calculation',
- ],
- ];
- }
- /**
- * @param $customerId
- * @param $password
- * @param $confirmationStatus
- * @param $successUrl
- * @param $isSetFlag
- * @param $successMessage
- *
- * @dataProvider getSuccessRedirectDataProvider
- */
- public function testSuccessRedirect(
- $customerId,
- $password,
- $confirmationStatus,
- $successUrl,
- $isSetFlag,
- $successMessage
- ) {
- $this->customerSessionMock->expects($this->once())
- ->method('isLoggedIn')
- ->will($this->returnValue(false));
- $this->registration->expects($this->once())
- ->method('isAllowed')
- ->will($this->returnValue(true));
- $this->customerSessionMock->expects($this->once())
- ->method('regenerateId');
- $this->customerMock->expects($this->any())
- ->method('getId')
- ->will($this->returnValue($customerId));
- $this->customerExtractorMock->expects($this->any())
- ->method('extract')
- ->with($this->equalTo('customer_account_create'), $this->equalTo($this->requestMock))
- ->will($this->returnValue($this->customerMock));
- $this->requestMock->expects($this->once())
- ->method('isPost')
- ->will($this->returnValue(true));
- $this->requestMock->expects($this->any())
- ->method('getPost')
- ->will($this->returnValue(false));
- $this->requestMock->expects($this->any())
- ->method('getParam')
- ->willReturnMap([
- ['password', null, $password],
- ['password_confirmation', null, $password],
- ['is_subscribed', false, true],
- ]);
- $this->customerMock->expects($this->once())
- ->method('setAddresses')
- ->with($this->equalTo([]))
- ->will($this->returnSelf());
- $this->accountManagement->expects($this->once())
- ->method('createAccount')
- ->with($this->equalTo($this->customerDetailsMock), $this->equalTo($password), '')
- ->will($this->returnValue($this->customerMock));
- $this->accountManagement->expects($this->once())
- ->method('getConfirmationStatus')
- ->with($this->equalTo($customerId))
- ->will($this->returnValue($confirmationStatus));
- $this->subscriberMock->expects($this->once())
- ->method('subscribeCustomerById')
- ->with($this->equalTo($customerId));
- $this->messageManagerMock->expects($this->any())
- ->method('addSuccess')
- ->with($this->stringContains($successMessage))
- ->will($this->returnSelf());
- $this->urlMock->expects($this->any())
- ->method('getUrl')
- ->willReturnMap([
- ['*/*/index', ['_secure' => true], $successUrl],
- ['*/*/create', ['_secure' => true], $successUrl],
- ]);
- $this->redirectMock->expects($this->once())
- ->method('success')
- ->with($this->equalTo($successUrl))
- ->will($this->returnValue($successUrl));
- $this->scopeConfigMock->expects($this->once())
- ->method('isSetFlag')
- ->with(
- $this->equalTo(Url::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD),
- $this->equalTo(ScopeInterface::SCOPE_STORE)
- )
- ->will($this->returnValue($isSetFlag));
- $this->storeMock->expects($this->any())
- ->method('getFrontendName')
- ->will($this->returnValue('frontend'));
- $this->storeManagerMock->expects($this->any())
- ->method('getStore')
- ->will($this->returnValue($this->storeMock));
- $this->model->execute();
- }
- /**
- * @return array
- */
- public function getSuccessRedirectDataProvider()
- {
- return [
- [
- 1,
- '123123q',
- AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED,
- 'http://example.com/success',
- true,
- 'Thank you for registering with',
- ],
- [
- 1,
- '123123q',
- AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED,
- 'http://example.com/success',
- false,
- 'Thank you for registering with',
- ],
- ];
- }
- }
|