123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Controller\Account;
- use Magento\Customer\Controller\Account\Confirm;
- use Magento\Customer\Helper\Address;
- use Magento\Customer\Model\Url;
- use Magento\Store\Model\ScopeInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class ConfirmTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Confirm
- */
- protected $model;
- /**
- * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $responseMock;
- /**
- * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerSessionMock;
- /**
- * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $redirectMock;
- /**
- * @var \Magento\Framework\Url|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $urlMock;
- /**
- * @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerAccountManagementMock;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerRepositoryMock;
- /**
- * @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerDataMock;
- /**
- * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageManagerMock;
- /**
- * @var \Magento\Customer\Helper\Address|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressHelperMock;
- /**
- * @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManagerMock;
- /**
- * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeMock;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfigMock;
- /**
- * @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $contextMock;
- /**
- * @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $redirectResultMock;
- protected function setUp()
- {
- $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
- $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
- $this->responseMock = $this->createPartialMock(
- \Magento\Framework\App\Response\Http::class,
- ['setRedirect', '__wakeup']
- );
- $viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
- $this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
- $this->urlMock = $this->createMock(\Magento\Framework\Url::class);
- $urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
- $urlFactoryMock->expects($this->any())
- ->method('create')
- ->will($this->returnValue($this->urlMock));
- $this->customerAccountManagementMock =
- $this->getMockForAbstractClass(\Magento\Customer\Api\AccountManagementInterface::class);
- $this->customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- $this->customerRepositoryMock =
- $this->getMockForAbstractClass(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
- $this->addressHelperMock = $this->createMock(\Magento\Customer\Helper\Address::class);
- $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
- $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
- $this->redirectResultMock = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
- $resultFactoryMock = $this->createPartialMock(\Magento\Framework\Controller\ResultFactory::class, ['create']);
- $resultFactoryMock->expects($this->once())
- ->method('create')
- ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
- ->willReturn($this->redirectResultMock);
- $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
- $this->contextMock->expects($this->any())
- ->method('getRequest')
- ->willReturn($this->requestMock);
- $this->contextMock->expects($this->any())
- ->method('getResponse')
- ->willReturn($this->responseMock);
- $this->contextMock->expects($this->any())
- ->method('getRedirect')
- ->willReturn($this->redirectMock);
- $this->contextMock->expects($this->any())
- ->method('getView')
- ->willReturn($viewMock);
- $this->contextMock->expects($this->any())
- ->method('getMessageManager')
- ->willReturn($this->messageManagerMock);
- $this->contextMock->expects($this->any())
- ->method('getResultFactory')
- ->willReturn($resultFactoryMock);
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->model = $objectManagerHelper->getObject(
- \Magento\Customer\Controller\Account\Confirm::class,
- [
- 'context' => $this->contextMock,
- 'customerSession' => $this->customerSessionMock,
- 'scopeConfig' => $this->scopeConfigMock,
- 'storeManager' => $this->storeManagerMock,
- 'customerAccountManagement' => $this->customerAccountManagementMock,
- 'customerRepository' => $this->customerRepositoryMock,
- 'addressHelper' => $this->addressHelperMock,
- 'urlFactory' => $urlFactoryMock,
- ]
- );
- }
- public function testIsLoggedIn()
- {
- $this->customerSessionMock->expects($this->once())
- ->method('isLoggedIn')
- ->will($this->returnValue(true));
- $this->redirectResultMock->expects($this->once())
- ->method('setPath')
- ->with('*/*/')
- ->willReturnSelf();
- $this->assertInstanceOf(\Magento\Framework\Controller\Result\Redirect::class, $this->model->execute());
- }
- /**
- * @dataProvider getParametersDataProvider
- */
- public function testNoCustomerIdInRequest($customerId, $key)
- {
- $this->customerSessionMock->expects($this->once())
- ->method('isLoggedIn')
- ->will($this->returnValue(false));
- $this->requestMock->expects($this->at(0))
- ->method('getParam')
- ->with($this->equalTo('id'), false)
- ->will($this->returnValue($customerId));
- $this->requestMock->expects($this->at(1))
- ->method('getParam')
- ->with($this->equalTo('key'), false)
- ->will($this->returnValue($key));
- $exception = new \Exception('Bad request.');
- $this->messageManagerMock->expects($this->once())
- ->method('addException')
- ->with($this->equalTo($exception), $this->equalTo('There was an error confirming the account'));
- $testUrl = 'http://example.com';
- $this->urlMock->expects($this->once())
- ->method('getUrl')
- ->with($this->equalTo('*/*/index'), ['_secure' => true])
- ->will($this->returnValue($testUrl));
- $this->redirectMock->expects($this->once())
- ->method('error')
- ->with($this->equalTo($testUrl))
- ->will($this->returnValue($testUrl));
- $this->redirectResultMock->expects($this->once())
- ->method('setUrl')
- ->with($this->equalTo($testUrl))
- ->willReturnSelf();
- $this->assertInstanceOf(\Magento\Framework\Controller\Result\Redirect::class, $this->model->execute());
- }
- /**
- * @return array
- */
- public function getParametersDataProvider()
- {
- return [
- [true, false],
- [false, true],
- ];
- }
- /**
- * @param $customerId
- * @param $key
- * @param $vatValidationEnabled
- * @param $addressType
- * @param $successMessage
- *
- * @dataProvider getSuccessMessageDataProvider
- */
- public function testSuccessMessage($customerId, $key, $vatValidationEnabled, $addressType, $successMessage)
- {
- $this->customerSessionMock->expects($this->once())
- ->method('isLoggedIn')
- ->will($this->returnValue(false));
- $this->requestMock->expects($this->any())
- ->method('getParam')
- ->willReturnMap([
- ['id', false, $customerId],
- ['key', false, $key],
- ]);
- $this->customerRepositoryMock->expects($this->any())
- ->method('getById')
- ->with($customerId)
- ->will($this->returnValue($this->customerDataMock));
- $email = 'test@example.com';
- $this->customerDataMock->expects($this->once())
- ->method('getEmail')
- ->will($this->returnValue($email));
- $this->customerAccountManagementMock->expects($this->once())
- ->method('activate')
- ->with($this->equalTo($email), $this->equalTo($key))
- ->will($this->returnValue($this->customerDataMock));
- $this->customerSessionMock->expects($this->any())
- ->method('setCustomerDataAsLoggedIn')
- ->with($this->equalTo($this->customerDataMock))
- ->willReturnSelf();
- $this->messageManagerMock->expects($this->any())
- ->method('addSuccess')
- ->with($this->stringContains($successMessage))
- ->willReturnSelf();
- $this->addressHelperMock->expects($this->once())
- ->method('isVatValidationEnabled')
- ->will($this->returnValue($vatValidationEnabled));
- $this->addressHelperMock->expects($this->any())
- ->method('getTaxCalculationAddressType')
- ->will($this->returnValue($addressType));
- $this->storeMock->expects($this->any())
- ->method('getFrontendName')
- ->will($this->returnValue('frontend'));
- $this->storeManagerMock->expects($this->any())
- ->method('getStore')
- ->will($this->returnValue($this->storeMock));
- $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $cookieMetadataManager->expects($this->once())
- ->method('getCookie')
- ->with('mage-cache-sessid')
- ->willReturn(true);
- $cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
- ->disableOriginalConstructor()
- ->getMock();
- $cookieMetadataFactory->expects($this->once())
- ->method('createCookieMetadata')
- ->willReturn($cookieMetadata);
- $cookieMetadata->expects($this->once())
- ->method('setPath')
- ->with('/');
- $cookieMetadataManager->expects($this->once())
- ->method('deleteCookie')
- ->with('mage-cache-sessid', $cookieMetadata);
- $refClass = new \ReflectionClass(Confirm::class);
- $cookieMetadataManagerProperty = $refClass->getProperty('cookieMetadataManager');
- $cookieMetadataManagerProperty->setAccessible(true);
- $cookieMetadataManagerProperty->setValue($this->model, $cookieMetadataManager);
- $cookieMetadataFactoryProperty = $refClass->getProperty('cookieMetadataFactory');
- $cookieMetadataFactoryProperty->setAccessible(true);
- $cookieMetadataFactoryProperty->setValue($this->model, $cookieMetadataFactory);
- $this->model->execute();
- }
- /**
- * @return array
- */
- public function getSuccessMessageDataProvider()
- {
- return [
- [1, 1, false, null, __('Thank you for registering with')],
- [1, 1, true, Address::TYPE_BILLING, __('enter your billing address for proper VAT calculation')],
- [1, 1, true, Address::TYPE_SHIPPING, __('enter your shipping address for proper VAT calculation')],
- ];
- }
- /**
- * @param $customerId
- * @param $key
- * @param $backUrl
- * @param $successUrl
- * @param $resultUrl
- * @param $isSetFlag
- * @param $successMessage
- *
- * @dataProvider getSuccessRedirectDataProvider
- */
- public function testSuccessRedirect(
- $customerId,
- $key,
- $backUrl,
- $successUrl,
- $resultUrl,
- $isSetFlag,
- $successMessage
- ) {
- $this->customerSessionMock->expects($this->once())
- ->method('isLoggedIn')
- ->will($this->returnValue(false));
- $this->requestMock->expects($this->any())
- ->method('getParam')
- ->willReturnMap([
- ['id', false, $customerId],
- ['key', false, $key],
- ['back_url', false, $backUrl],
- ]);
- $this->customerRepositoryMock->expects($this->any())
- ->method('getById')
- ->with($customerId)
- ->will($this->returnValue($this->customerDataMock));
- $email = 'test@example.com';
- $this->customerDataMock->expects($this->once())
- ->method('getEmail')
- ->will($this->returnValue($email));
- $this->customerAccountManagementMock->expects($this->once())
- ->method('activate')
- ->with($this->equalTo($email), $this->equalTo($key))
- ->will($this->returnValue($this->customerDataMock));
- $this->customerSessionMock->expects($this->any())
- ->method('setCustomerDataAsLoggedIn')
- ->with($this->equalTo($this->customerDataMock))
- ->willReturnSelf();
- $this->messageManagerMock->expects($this->any())
- ->method('addSuccess')
- ->with($this->stringContains($successMessage))
- ->willReturnSelf();
- $this->storeMock->expects($this->any())
- ->method('getFrontendName')
- ->will($this->returnValue('frontend'));
- $this->storeManagerMock->expects($this->any())
- ->method('getStore')
- ->will($this->returnValue($this->storeMock));
- $this->urlMock->expects($this->any())
- ->method('getUrl')
- ->with($this->equalTo('*/*/index'), ['_secure' => true])
- ->will($this->returnValue($successUrl));
- $this->redirectMock->expects($this->once())
- ->method('success')
- ->with($this->equalTo($resultUrl))
- ->willReturn($resultUrl);
- $this->scopeConfigMock->expects($this->any())
- ->method('isSetFlag')
- ->with(
- Url::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD,
- ScopeInterface::SCOPE_STORE
- )
- ->willReturn($isSetFlag);
- $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $cookieMetadataManager->expects($this->once())
- ->method('getCookie')
- ->with('mage-cache-sessid')
- ->willReturn(false);
- $refClass = new \ReflectionClass(Confirm::class);
- $refProperty = $refClass->getProperty('cookieMetadataManager');
- $refProperty->setAccessible(true);
- $refProperty->setValue($this->model, $cookieMetadataManager);
- $this->model->execute();
- }
- /**
- * @return array
- */
- public function getSuccessRedirectDataProvider()
- {
- return [
- [
- 1,
- 1,
- 'http://example.com/back',
- null,
- 'http://example.com/back',
- true,
- __('Thank you for registering with'),
- ],
- [
- 1,
- 1,
- null,
- 'http://example.com/success',
- 'http://example.com/success',
- true,
- __('Thank you for registering with'),
- ],
- [
- 1,
- 1,
- null,
- 'http://example.com/success',
- 'http://example.com/success',
- false,
- __('Thank you for registering with'),
- ],
- ];
- }
- }
|