1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Controller\Adminhtml\Index;
- use Magento\Customer\Api\CustomerMetadataInterface;
- use Magento\Customer\Api\Data\AttributeMetadataInterface;
- use Magento\Customer\Api\Data\CustomerInterface;
- use Magento\Customer\Controller\RegistryConstants;
- use Magento\Customer\Model\EmailNotificationInterface;
- use Magento\Customer\Model\Metadata\Form;
- use Magento\Framework\Controller\Result\Redirect;
- /**
- * Testing Save Customer use case from admin page
- *
- * @SuppressWarnings(PHPMD.TooManyFields)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @covers \Magento\Customer\Controller\Adminhtml\Index\Save
- */
- class SaveTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Customer\Controller\Adminhtml\Index\Save
- */
- protected $model;
- /**
- * @var \Magento\Backend\App\Action\Context
- */
- protected $context;
- /**
- * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \Magento\Backend\Model\View\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultForwardFactoryMock;
- /**
- * @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultForwardMock;
- /**
- * @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultPageFactoryMock;
- /**
- * @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultPageMock;
- /**
- * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $pageConfigMock;
- /**
- * @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $pageTitleMock;
- /**
- * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $sessionMock;
- /**
- * @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $formFactoryMock;
- /**
- * @var \Magento\Framework\DataObjectFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $objectFactoryMock;
- /**
- * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerDataFactoryMock;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerRepositoryMock;
- /**
- * @var \Magento\Customer\Model\Customer\Mapper|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerMapperMock;
- /**
- * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $dataHelperMock;
- /**
- * @var \Magento\Framework\AuthorizationInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $authorizationMock;
- /**
- * @var \Magento\Newsletter\Model\SubscriberFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $subscriberFactoryMock;
- /**
- * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $registryMock;
- /**
- * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageManagerMock;
- /**
- * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $redirectFactoryMock;
- /**
- * @var \Magento\Customer\Model\AccountManagement|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $managementMock;
- /**
- * @var \Magento\Customer\Api\Data\AddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressDataFactoryMock;
- /**
- * @var EmailNotificationInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $emailNotificationMock;
- /**
- * @var \Magento\Customer\Model\Address\Mapper|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerAddressMapperMock;
- /**
- * @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerAddressRepositoryMock;
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function setUp()
- {
- $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->resultForwardFactoryMock = $this->getMockBuilder(
- \Magento\Backend\Model\View\Result\ForwardFactory::class
- )->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->resultForwardMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->resultPageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
- ->disableOriginalConstructor()
- ->setMethods(['setActiveMenu', 'getConfig', 'addBreadcrumb'])
- ->getMock();
- $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
- ->disableOriginalConstructor()
- ->setMethods(['unsCustomerFormData', 'setCustomerFormData'])
- ->getMock();
- $this->formFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\Metadata\FormFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->objectFactoryMock = $this->getMockBuilder(\Magento\Framework\DataObjectFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->customerDataFactoryMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterfaceFactory::class
- )->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->customerRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerAddressRepositoryMock = $this->getMockBuilder(
- \Magento\Customer\Api\AddressRepositoryInterface::class
- )->disableOriginalConstructor()->getMock();
- $this->customerMapperMock = $this->getMockBuilder(
- \Magento\Customer\Model\Customer\Mapper::class
- )->disableOriginalConstructor()->getMock();
- $this->customerAddressMapperMock = $this->getMockBuilder(
- \Magento\Customer\Model\Address\Mapper::class
- )->disableOriginalConstructor()->getMock();
- $this->dataHelperMock = $this->getMockBuilder(
- \Magento\Framework\Api\DataObjectHelper::class
- )->disableOriginalConstructor()->getMock();
- $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\AuthorizationInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->subscriberFactoryMock = $this->getMockBuilder(\Magento\Newsletter\Model\SubscriberFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->redirectFactoryMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\RedirectFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->managementMock = $this->getMockBuilder(\Magento\Customer\Model\AccountManagement::class)
- ->disableOriginalConstructor()
- ->setMethods(['createAccount'])
- ->getMock();
- $this->addressDataFactoryMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterfaceFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->emailNotificationMock = $this->getMockBuilder(EmailNotificationInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->model = $objectManager->getObject(
- \Magento\Customer\Controller\Adminhtml\Index\Save::class,
- [
- 'resultForwardFactory' => $this->resultForwardFactoryMock,
- 'resultPageFactory' => $this->resultPageFactoryMock,
- 'formFactory' => $this->formFactoryMock,
- 'objectFactory' => $this->objectFactoryMock,
- 'customerDataFactory' => $this->customerDataFactoryMock,
- 'customerRepository' => $this->customerRepositoryMock,
- 'customerMapper' => $this->customerMapperMock,
- 'dataObjectHelper' => $this->dataHelperMock,
- 'subscriberFactory' => $this->subscriberFactoryMock,
- 'coreRegistry' => $this->registryMock,
- 'customerAccountManagement' => $this->managementMock,
- 'addressDataFactory' => $this->addressDataFactoryMock,
- 'request' => $this->requestMock,
- 'session' => $this->sessionMock,
- 'authorization' => $this->authorizationMock,
- 'messageManager' => $this->messageManagerMock,
- 'resultRedirectFactory' => $this->redirectFactoryMock,
- 'addressRepository' => $this->customerAddressRepositoryMock,
- 'addressMapper' => $this->customerAddressMapperMock,
- ]
- );
- $objectManager->setBackwardCompatibleProperty(
- $this->model,
- 'emailNotification',
- $this->emailNotificationMock
- );
- }
- /**
- * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testExecuteWithExistentCustomer()
- {
- $customerId = 22;
- $subscription = 'true';
- $postValue = [
- 'customer' => [
- 'entity_id' => $customerId,
- 'code' => 'value',
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ],
- 'subscription' => $subscription,
- ];
- $extractedData = [
- 'entity_id' => $customerId,
- 'code' => 'value',
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ];
- $compactedData = [
- 'entity_id' => $customerId,
- 'code' => 'value',
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- CustomerInterface::DEFAULT_BILLING => 2,
- CustomerInterface::DEFAULT_SHIPPING => 2
- ];
- $savedData = [
- 'entity_id' => $customerId,
- 'darkness' => true,
- 'name' => 'Name',
- CustomerInterface::DEFAULT_BILLING => false,
- CustomerInterface::DEFAULT_SHIPPING => false,
- ];
- $mergedData = [
- 'entity_id' => $customerId,
- 'darkness' => true,
- 'name' => 'Name',
- 'code' => 'value',
- 'disable_auto_group_change' => 0,
- 'confirmation' => false,
- 'sendemail_store_id' => '1',
- 'id' => $customerId,
- ];
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
- $attributeMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\AttributeMetadataInterface::class
- )->disableOriginalConstructor()->getMock();
- $attributeMock->expects($this->atLeastOnce())
- ->method('getAttributeCode')
- ->willReturn('coolness');
- $attributeMock->expects($this->atLeastOnce())
- ->method('getFrontendInput')
- ->willReturn('int');
- $attributes = [$attributeMock];
- $this->requestMock->expects($this->atLeastOnce())
- ->method('getPostValue')
- ->willReturnMap([
- [null, null, $postValue],
- [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
- ]);
- $this->requestMock->expects($this->atLeastOnce())
- ->method('getPost')
- ->willReturnMap(
- [
- ['customer', null, $postValue['customer']],
- ['subscription', null, $subscription],
- ]
- );
- /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
- $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectMock->expects($this->atLeastOnce())
- ->method('getData')
- ->willReturnMap(
- [
- ['customer', null, $postValue['customer']],
- ]
- );
- $this->objectFactoryMock->expects($this->exactly(1))
- ->method('create')
- ->with(['data' => $postValue])
- ->willReturn($objectMock);
- $customerFormMock = $this->getMockBuilder(
- \Magento\Customer\Model\Metadata\Form::class
- )->disableOriginalConstructor()->getMock();
- $customerFormMock->expects($this->once())
- ->method('extractData')
- ->with($this->requestMock, 'customer')
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('compactData')
- ->with($extractedData)
- ->willReturn($compactedData);
- $customerFormMock->expects($this->once())
- ->method('getAttributes')
- ->willReturn($attributes);
- $this->formFactoryMock->expects($this->exactly(1))
- ->method('create')
- ->willReturnMap(
- [
- [
- CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
- 'adminhtml_customer',
- $savedData,
- false,
- Form::DONT_IGNORE_INVISIBLE,
- [],
- $customerFormMock
- ],
- ]
- );
- /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
- $customerMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->disableOriginalConstructor()->getMock();
- $this->customerDataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($customerMock);
- $this->customerRepositoryMock->expects($this->exactly(2))
- ->method('getById')
- ->with($customerId)
- ->willReturn($customerMock);
- $this->customerMapperMock->expects($this->exactly(2))
- ->method('toFlatArray')
- ->with($customerMock)
- ->willReturn($savedData);
- $this->dataHelperMock->expects($this->atLeastOnce())
- ->method('populateWithArray')
- ->willReturnMap(
- [
- [
- $customerMock,
- $mergedData, \Magento\Customer\Api\Data\CustomerInterface::class,
- $this->dataHelperMock
- ],
- ]
- );
- $this->customerRepositoryMock->expects($this->once())
- ->method('save')
- ->with($customerMock)
- ->willReturnSelf();
- $customerEmail = 'customer@email.com';
- $customerMock->expects($this->once())->method('getEmail')->willReturn($customerEmail);
- $customerMock->expects($this->once())
- ->method('getAddresses')
- ->willReturn([]);
- $this->emailNotificationMock->expects($this->once())
- ->method('credentialsChanged')
- ->with($customerMock, $customerEmail)
- ->willReturnSelf();
- $this->authorizationMock->expects($this->once())
- ->method('isAllowed')
- ->with(null)
- ->willReturn(true);
- /** @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject $subscriberMock */
- $subscriberMock = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->subscriberFactoryMock->expects($this->once())
- ->method('create')
- ->with()
- ->willReturn($subscriberMock);
- $subscriberMock->expects($this->once())
- ->method('subscribeCustomerById')
- ->with($customerId);
- $subscriberMock->expects($this->never())
- ->method('unsubscribeCustomerById');
- $this->sessionMock->expects($this->once())
- ->method('unsCustomerFormData');
- $this->registryMock->expects($this->once())
- ->method('register')
- ->with(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
- $this->messageManagerMock->expects($this->once())
- ->method('addSuccess')
- ->with(__('You saved the customer.'))
- ->willReturnSelf();
- $this->requestMock->expects($this->once())
- ->method('getParam')
- ->with('back', false)
- ->willReturn(true);
- /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
- $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->redirectFactoryMock->expects($this->once())
- ->method('create')
- ->with([])
- ->willReturn($redirectMock);
- $redirectMock->expects($this->once())
- ->method('setPath')
- ->with('customer/*/edit', ['id' => $customerId, '_current' => true])
- ->willReturn(true);
- $this->assertEquals($redirectMock, $this->model->execute());
- }
- /**
- * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testExecuteWithNewCustomer()
- {
- $customerId = 22;
- $subscription = '0';
- $postValue = [
- 'customer' => [
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ],
- 'subscription' => $subscription,
- ];
- $extractedData = [
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ];
- $mergedData = [
- 'disable_auto_group_change' => 0,
- CustomerInterface::DEFAULT_BILLING => null,
- CustomerInterface::DEFAULT_SHIPPING => null,
- 'confirmation' => false,
- ];
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
- $attributeMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\AttributeMetadataInterface::class
- )->disableOriginalConstructor()->getMock();
- $attributeMock->expects($this->atLeastOnce())
- ->method('getAttributeCode')
- ->willReturn('coolness');
- $attributeMock->expects($this->atLeastOnce())
- ->method('getFrontendInput')
- ->willReturn('int');
- $attributes = [$attributeMock];
- $this->requestMock->expects($this->any())
- ->method('getPostValue')
- ->willReturnMap([
- [null, null, $postValue],
- [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
- ]);
- $this->requestMock->expects($this->atLeastOnce())
- ->method('getPost')
- ->willReturnMap(
- [
- ['customer', null, $postValue['customer']],
- ['subscription', null, $subscription],
- ]
- );
- /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
- $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectMock->expects($this->atLeastOnce())
- ->method('getData')
- ->willReturnMap(
- [
- ['customer', null, $postValue['customer']],
- ]
- );
- $this->objectFactoryMock->expects($this->atLeastOnce())
- ->method('create')
- ->with(['data' => $postValue])
- ->willReturn($objectMock);
- $customerFormMock = $this->getMockBuilder(
- \Magento\Customer\Model\Metadata\Form::class
- )->disableOriginalConstructor()->getMock();
- $customerFormMock->expects($this->once())
- ->method('extractData')
- ->with($this->requestMock, 'customer')
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('compactData')
- ->with($extractedData)
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('getAttributes')
- ->willReturn($attributes);
- $this->formFactoryMock->expects($this->exactly(1))
- ->method('create')
- ->willReturnMap(
- [
- [
- CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
- 'adminhtml_customer',
- [],
- false,
- Form::DONT_IGNORE_INVISIBLE,
- [],
- $customerFormMock
- ],
- ]
- );
- /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
- $customerMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->disableOriginalConstructor()->getMock();
- $this->customerDataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($customerMock);
- $this->dataHelperMock->expects($this->atLeastOnce())
- ->method('populateWithArray')
- ->willReturnMap(
- [
- [
- $customerMock,
- $mergedData, \Magento\Customer\Api\Data\CustomerInterface::class,
- $this->dataHelperMock
- ],
- ]
- );
- $this->managementMock->expects($this->once())
- ->method('createAccount')
- ->with($customerMock, null, '')
- ->willReturn($customerMock);
- $customerMock->expects($this->once())
- ->method('getId')
- ->willReturn($customerId);
- $this->authorizationMock->expects($this->once())
- ->method('isAllowed')
- ->with(null)
- ->willReturn(true);
- /** @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject $subscriberMock */
- $subscriberMock = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->subscriberFactoryMock->expects($this->once())
- ->method('create')
- ->with()
- ->willReturn($subscriberMock);
- $subscriberMock->expects($this->once())
- ->method('unsubscribeCustomerById')
- ->with($customerId);
- $subscriberMock->expects($this->never())
- ->method('subscribeCustomerById');
- $this->sessionMock->expects($this->once())
- ->method('unsCustomerFormData');
- $this->registryMock->expects($this->once())
- ->method('register')
- ->with(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
- $this->messageManagerMock->expects($this->once())
- ->method('addSuccess')
- ->with(__('You saved the customer.'))
- ->willReturnSelf();
- $this->requestMock->expects($this->once())
- ->method('getParam')
- ->with('back', false)
- ->willReturn(false);
- /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
- $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->redirectFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($redirectMock);
- $redirectMock->expects($this->once())
- ->method('setPath')
- ->with('customer/index', [])
- ->willReturnSelf();
- $this->assertEquals($redirectMock, $this->model->execute());
- }
- /**
- * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testExecuteWithNewCustomerAndValidationException()
- {
- $subscription = '0';
- $postValue = [
- 'customer' => [
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ],
- 'subscription' => $subscription,
- ];
- $extractedData = [
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ];
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
- $attributeMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\AttributeMetadataInterface::class
- )->disableOriginalConstructor()->getMock();
- $attributeMock->expects($this->once())
- ->method('getAttributeCode')
- ->willReturn('coolness');
- $attributeMock->expects($this->once())
- ->method('getFrontendInput')
- ->willReturn('int');
- $attributes = [$attributeMock];
- $this->requestMock->expects($this->any())
- ->method('getPostValue')
- ->willReturnMap([
- [null, null, $postValue],
- [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
- ]);
- $this->requestMock->expects($this->atLeastOnce())
- ->method('getPost')
- ->willReturnMap(
- [
- ['customer', null, $postValue['customer']],
- ]
- );
- /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
- $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectMock->expects($this->once())
- ->method('getData')
- ->with('customer')
- ->willReturn($postValue['customer']);
- $this->objectFactoryMock->expects($this->once())
- ->method('create')
- ->with(['data' => $postValue])
- ->willReturn($objectMock);
- $customerFormMock = $this->getMockBuilder(
- \Magento\Customer\Model\Metadata\Form::class
- )->disableOriginalConstructor()->getMock();
- $customerFormMock->expects($this->once())
- ->method('extractData')
- ->with($this->requestMock, 'customer')
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('compactData')
- ->with($extractedData)
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('getAttributes')
- ->willReturn($attributes);
- $this->formFactoryMock->expects($this->once())
- ->method('create')
- ->with(
- CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
- 'adminhtml_customer',
- [],
- false,
- Form::DONT_IGNORE_INVISIBLE
- )->willReturn($customerFormMock);
- /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
- $customerMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->disableOriginalConstructor()->getMock();
- $this->customerDataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($customerMock);
- $this->managementMock->expects($this->once())
- ->method('createAccount')
- ->with($customerMock, null, '')
- ->willThrowException(new \Magento\Framework\Validator\Exception(__('Validator Exception')));
- $customerMock->expects($this->never())
- ->method('getId');
- $this->authorizationMock->expects($this->never())
- ->method('isAllowed');
- $this->subscriberFactoryMock->expects($this->never())
- ->method('create');
- $this->sessionMock->expects($this->never())
- ->method('unsCustomerFormData');
- $this->registryMock->expects($this->never())
- ->method('register');
- $this->messageManagerMock->expects($this->never())
- ->method('addSuccess');
- $this->messageManagerMock->expects($this->once())
- ->method('addMessage')
- ->with(new \Magento\Framework\Message\Error('Validator Exception'));
- $this->sessionMock->expects($this->once())
- ->method('setCustomerFormData')
- ->with($postValue);
- /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
- $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->redirectFactoryMock->expects($this->once())
- ->method('create')
- ->with([])
- ->willReturn($redirectMock);
- $redirectMock->expects($this->once())
- ->method('setPath')
- ->with('customer/*/new', ['_current' => true])
- ->willReturn(true);
- $this->assertEquals($redirectMock, $this->model->execute());
- }
- /**
- * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testExecuteWithNewCustomerAndLocalizedException()
- {
- $subscription = '0';
- $postValue = [
- 'customer' => [
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ],
- 'subscription' => $subscription,
- ];
- $extractedData = [
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ];
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
- $attributeMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\AttributeMetadataInterface::class
- )->disableOriginalConstructor()->getMock();
- $attributeMock->expects($this->once())
- ->method('getAttributeCode')
- ->willReturn('coolness');
- $attributeMock->expects($this->once())
- ->method('getFrontendInput')
- ->willReturn('int');
- $attributes = [$attributeMock];
- $this->requestMock->expects($this->any())
- ->method('getPostValue')
- ->willReturnMap([
- [null, null, $postValue],
- [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
- ]);
- $this->requestMock->expects($this->atLeastOnce())
- ->method('getPost')
- ->willReturnMap(
- [
- ['customer', null, $postValue['customer']],
- ]
- );
- /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
- $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectMock->expects($this->once())
- ->method('getData')
- ->with('customer')
- ->willReturn($postValue['customer']);
- $this->objectFactoryMock->expects($this->once())
- ->method('create')
- ->with(['data' => $postValue])
- ->willReturn($objectMock);
- /** @var Form|\PHPUnit_Framework_MockObject_MockObject $formMock */
- $customerFormMock = $this->getMockBuilder(
- \Magento\Customer\Model\Metadata\Form::class
- )->disableOriginalConstructor()->getMock();
- $customerFormMock->expects($this->once())
- ->method('extractData')
- ->with($this->requestMock, 'customer')
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('compactData')
- ->with($extractedData)
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('getAttributes')
- ->willReturn($attributes);
- $this->formFactoryMock->expects($this->once())
- ->method('create')
- ->with(
- CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
- 'adminhtml_customer',
- [],
- false,
- Form::DONT_IGNORE_INVISIBLE
- )->willReturn($customerFormMock);
- $customerMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->disableOriginalConstructor()->getMock();
- $this->customerDataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($customerMock);
- $this->managementMock->expects($this->once())
- ->method('createAccount')
- ->with($customerMock, null, '')
- ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Localized Exception')));
- $customerMock->expects($this->never())
- ->method('getId');
- $this->authorizationMock->expects($this->never())
- ->method('isAllowed');
- $this->subscriberFactoryMock->expects($this->never())
- ->method('create');
- $this->sessionMock->expects($this->never())
- ->method('unsCustomerFormData');
- $this->registryMock->expects($this->never())
- ->method('register');
- $this->messageManagerMock->expects($this->never())
- ->method('addSuccess');
- $this->messageManagerMock->expects($this->once())
- ->method('addMessage')
- ->with(new \Magento\Framework\Message\Error('Localized Exception'));
- $this->sessionMock->expects($this->once())
- ->method('setCustomerFormData')
- ->with($postValue);
- /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
- $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->redirectFactoryMock->expects($this->once())
- ->method('create')
- ->with([])
- ->willReturn($redirectMock);
- $redirectMock->expects($this->once())
- ->method('setPath')
- ->with('customer/*/new', ['_current' => true])
- ->willReturn(true);
- $this->assertEquals($redirectMock, $this->model->execute());
- }
- /**
- * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testExecuteWithNewCustomerAndException()
- {
- $subscription = '0';
- $postValue = [
- 'customer' => [
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ],
- 'subscription' => $subscription,
- ];
- $extractedData = [
- 'coolness' => false,
- 'disable_auto_group_change' => 'false',
- ];
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
- $attributeMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\AttributeMetadataInterface::class
- )->disableOriginalConstructor()->getMock();
- $attributeMock->expects($this->once())
- ->method('getAttributeCode')
- ->willReturn('coolness');
- $attributeMock->expects($this->once())
- ->method('getFrontendInput')
- ->willReturn('int');
- $attributes = [$attributeMock];
- $this->requestMock->expects($this->any())
- ->method('getPostValue')
- ->willReturnMap([
- [null, null, $postValue],
- [CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, null, $postValue['customer']],
- ]);
- $this->requestMock->expects($this->atLeastOnce())
- ->method('getPost')
- ->willReturnMap(
- [
- ['customer', null, $postValue['customer']],
- ]
- );
- /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
- $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectMock->expects($this->once())
- ->method('getData')
- ->with('customer')
- ->willReturn($postValue['customer']);
- $this->objectFactoryMock->expects($this->once())
- ->method('create')
- ->with(['data' => $postValue])
- ->willReturn($objectMock);
- $customerFormMock = $this->getMockBuilder(
- \Magento\Customer\Model\Metadata\Form::class
- )->disableOriginalConstructor()->getMock();
- $customerFormMock->expects($this->once())
- ->method('extractData')
- ->with($this->requestMock, 'customer')
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('compactData')
- ->with($extractedData)
- ->willReturn($extractedData);
- $customerFormMock->expects($this->once())
- ->method('getAttributes')
- ->willReturn($attributes);
- $this->formFactoryMock->expects($this->once())
- ->method('create')
- ->with(
- CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
- 'adminhtml_customer',
- [],
- false,
- Form::DONT_IGNORE_INVISIBLE
- )->willReturn($customerFormMock);
- /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
- $customerMock = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->disableOriginalConstructor()->getMock();
- $this->customerDataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($customerMock);
- $exception = new \Exception(__('Exception'));
- $this->managementMock->expects($this->once())
- ->method('createAccount')
- ->with($customerMock, null, '')
- ->willThrowException($exception);
- $customerMock->expects($this->never())
- ->method('getId');
- $this->authorizationMock->expects($this->never())
- ->method('isAllowed');
- $this->subscriberFactoryMock->expects($this->never())
- ->method('create');
- $this->sessionMock->expects($this->never())
- ->method('unsCustomerFormData');
- $this->registryMock->expects($this->never())
- ->method('register');
- $this->messageManagerMock->expects($this->never())
- ->method('addSuccess');
- $this->messageManagerMock->expects($this->once())
- ->method('addException')
- ->with($exception, __('Something went wrong while saving the customer.'));
- $this->sessionMock->expects($this->once())
- ->method('setCustomerFormData')
- ->with($postValue);
- /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
- $redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->redirectFactoryMock->expects($this->once())
- ->method('create')
- ->with([])
- ->willReturn($redirectMock);
- $redirectMock->expects($this->once())
- ->method('setPath')
- ->with('customer/*/new', ['_current' => true])
- ->willReturn(true);
- $this->assertEquals($redirectMock, $this->model->execute());
- }
- }
|