123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Observer;
- use Magento\Customer\Api\GroupManagementInterface;
- use Magento\Customer\Helper\Address as HelperAddress;
- use Magento\Customer\Model\Address\AbstractAddress;
- use Magento\Customer\Model\Vat;
- use Magento\Customer\Observer\AfterAddressSaveObserver;
- use Magento\Customer\Observer\BeforeAddressSaveObserver;
- use Magento\Framework\App\Area;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\App\State as AppState;
- use Magento\Framework\Escaper;
- use Magento\Framework\Message\ManagerInterface;
- use Magento\Framework\Registry;
- use Magento\Store\Model\ScopeInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class AfterAddressSaveObserverTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var AfterAddressSaveObserver
- */
- protected $model;
- /**
- * @var Vat |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $vat;
- /**
- * @var HelperAddress |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $helperAddress;
- /**
- * @var Registry |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $registry;
- /**
- * @var GroupManagementInterface |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $groupManagement;
- /**
- * @var ScopeConfigInterface |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfig;
- /**
- * @var ManagerInterface |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageManager;
- /**
- * @var Escaper |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $escaper;
- /**
- * @var AppState |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $appState;
- /**
- * @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerMock;
- /**
- * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerSessionMock;
- protected function setUp()
- {
- $this->vat = $this->getMockBuilder(\Magento\Customer\Model\Vat::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->helperAddress = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->group = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)
- ->setMethods(['getId'])
- ->getMockForAbstractClass();
- $this->group->expects($this->any())->method('getId')->willReturn(1);
- $this->groupManagement = $this->getMockBuilder(\Magento\Customer\Api\GroupManagementInterface::class)
- ->setMethods(['getDefaultGroup'])
- ->getMockForAbstractClass();
- $this->groupManagement->expects($this->any())->method('getDefaultGroup')->willReturn($this->group);
- $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
- ->getMockForAbstractClass();
- $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
- ->getMockForAbstractClass();
- $this->escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->model = new AfterAddressSaveObserver(
- $this->vat,
- $this->helperAddress,
- $this->registry,
- $this->groupManagement,
- $this->scopeConfig,
- $this->messageManager,
- $this->escaper,
- $this->appState,
- $this->customerSessionMock
- );
- }
- /**
- * @param bool $isVatValidationEnabled
- * @param bool $processedFlag
- * @param bool $forceProcess
- * @param int $addressId
- * @param int $registeredAddressId
- * @param string $configAddressType
- * @dataProvider dataProviderAfterAddressSaveRestricted
- */
- public function testAfterAddressSaveRestricted(
- $isVatValidationEnabled,
- $processedFlag,
- $forceProcess,
- $addressId,
- $registeredAddressId,
- $configAddressType
- ) {
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->setMethods(['getDefaultBilling', 'getStore', 'getDefaultShipping', 'getGroupId'])
- ->getMock();
- $customer->expects($this->any())
- ->method('getStore')
- ->willReturn($store);
- $customer->expects($this->any())
- ->method('getDefaultBilling')
- ->willReturn(null);
- $customer->expects($this->any())
- ->method('getDefaultShipping')
- ->willReturn(null);
- $customer->expects($this->any())
- ->method('getGroupID')
- ->willReturn(1);
- $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'getId',
- 'getIsDefaultBilling',
- 'getIsDefaultShipping',
- 'setForceProcess',
- 'getIsPrimaryBilling',
- 'getIsPrimaryShipping',
- 'getCustomer',
- 'getForceProcess'
- ]
- )
- ->getMock();
- $address->expects($this->any())
- ->method('getId')
- ->willReturn($addressId);
- $address->expects($this->any())
- ->method('getCustomer')
- ->willReturn($customer);
- $address->expects($this->any())
- ->method('getForceProcess')
- ->willReturn($forceProcess);
- $address->expects($this->any())
- ->method('getIsPrimaryBilling')
- ->willReturn(null);
- $address->expects($this->any())
- ->method('getIsDefaultBilling')
- ->willReturn($addressId);
- $address->expects($this->any())
- ->method('getIsPrimaryShipping')
- ->willReturn(null);
- $address->expects($this->any())
- ->method('getIsDefaultShipping')
- ->willReturn($addressId);
- $observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getCustomerAddress',
- ])
- ->getMock();
- $observer->expects($this->once())
- ->method('getCustomerAddress')
- ->willReturn($address);
- $this->helperAddress->expects($this->once())
- ->method('isVatValidationEnabled')
- ->with($store)
- ->willReturn($isVatValidationEnabled);
- $this->registry->expects($this->any())
- ->method('registry')
- ->willReturnMap([
- [AfterAddressSaveObserver::VIV_PROCESSED_FLAG, $processedFlag],
- [BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS, $registeredAddressId],
- ]);
- $this->helperAddress->expects($this->any())
- ->method('getTaxCalculationAddressType')
- ->willReturn($configAddressType);
- $this->model->execute($observer);
- }
- /**
- * @return array
- */
- public function dataProviderAfterAddressSaveRestricted()
- {
- return [
- [false, false, false, 1, null, null],
- [true, true, false, 1, null, null],
- [true, false, false, 1, null, null],
- [true, false, false, 1, 1, AbstractAddress::TYPE_BILLING],
- [true, false, false, 1, 1, AbstractAddress::TYPE_SHIPPING],
- ];
- }
- public function testAfterAddressSaveException()
- {
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customer->expects($this->any())
- ->method('getStore')
- ->willReturn($store);
- $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getCustomer',
- 'getForceProcess',
- 'getVatId',
- ])
- ->getMock();
- $address->expects($this->any())
- ->method('getCustomer')
- ->willReturn($customer);
- $address->expects($this->once())
- ->method('getForceProcess')
- ->willReturn(true);
- $address->expects($this->any())
- ->method('getVatId')
- ->willThrowException(new \Exception('Exception'));
- $observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getCustomerAddress',
- ])
- ->getMock();
- $observer->expects($this->once())
- ->method('getCustomerAddress')
- ->willReturn($address);
- $this->helperAddress->expects($this->once())
- ->method('isVatValidationEnabled')
- ->with($store)
- ->willReturn(true);
- $this->registry->expects($this->any())
- ->method('registry')
- ->with(AfterAddressSaveObserver::VIV_PROCESSED_FLAG)
- ->willReturn(false);
- $this->registry->expects($this->any())
- ->method('register')
- ->willReturnMap([
- [AfterAddressSaveObserver::VIV_PROCESSED_FLAG, true, false, $this->registry],
- [AfterAddressSaveObserver::VIV_PROCESSED_FLAG, false, true, $this->registry],
- ]);
- $this->model->execute($observer);
- }
- /**
- * @param string $vatId
- * @param int $countryId
- * @param bool $isCountryInEU
- * @param int $defaultGroupId
- * @dataProvider dataProviderAfterAddressSaveDefaultGroup
- */
- public function testAfterAddressSaveDefaultGroup(
- $vatId,
- $countryId,
- $isCountryInEU,
- $defaultGroupId
- ) {
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $dataGroup = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)
- ->getMockForAbstractClass();
- $dataGroup->expects($this->any())
- ->method('getId')
- ->willReturn($defaultGroupId);
- $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getStore',
- 'getDisableAutoGroupChange',
- 'getGroupId',
- 'setGroupId',
- 'save',
- ])
- ->getMock();
- $customer->expects($this->exactly(2))
- ->method('getStore')
- ->willReturn($store);
- $customer->expects($this->once())
- ->method('getDisableAutoGroupChange')
- ->willReturn(false);
- $customer->expects($this->once())
- ->method('getGroupId')
- ->willReturn(null);
- $customer->expects($this->once())
- ->method('setGroupId')
- ->with($defaultGroupId)
- ->willReturnSelf();
- $customer->expects($this->once())
- ->method('save')
- ->willReturnSelf();
- $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getCustomer',
- 'getForceProcess',
- 'getVatId',
- 'getCountry',
- ])
- ->getMock();
- $address->expects($this->once())
- ->method('getCustomer')
- ->willReturn($customer);
- $address->expects($this->once())
- ->method('getForceProcess')
- ->willReturn(true);
- $address->expects($this->once())
- ->method('getVatId')
- ->willReturn($vatId);
- $address->expects($this->any())
- ->method('getCountry')
- ->willReturn($countryId);
- $observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getCustomerAddress',
- ])
- ->getMock();
- $observer->expects($this->once())
- ->method('getCustomerAddress')
- ->willReturn($address);
- $this->helperAddress->expects($this->once())
- ->method('isVatValidationEnabled')
- ->with($store)
- ->willReturn(true);
- $this->vat->expects($this->any())
- ->method('isCountryInEU')
- ->with($countryId)
- ->willReturn($isCountryInEU);
- $this->groupManagement->expects($this->any())
- ->method('getDefaultGroup')
- ->with($store)
- ->willReturn($dataGroup);
- $this->model->execute($observer);
- }
- /**
- * @return array
- */
- public function dataProviderAfterAddressSaveDefaultGroup()
- {
- return [
- ['', 1, false, 1],
- [1, 1, false, 1],
- ];
- }
- /**
- * @param string $vatId
- * @param $vatClass
- * @param int $countryId
- * @param string $country
- * @param int $newGroupId
- * @param string $areaCode
- * @param bool $resultVatIsValid
- * @param bool $resultRequestSuccess
- * @param string $resultValidMessage
- * @param string $resultInvalidMessage
- * @param string $resultErrorMessage
- * @dataProvider dataProviderAfterAddressSaveNewGroup
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function testAfterAddressSaveNewGroup(
- $vatId,
- $vatClass,
- $countryId,
- $country,
- $newGroupId,
- $areaCode,
- $resultVatIsValid,
- $resultRequestSuccess,
- $resultValidMessage,
- $resultInvalidMessage,
- $resultErrorMessage
- ) {
- $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $validationResult = $this->getMockBuilder(\Magento\Framework\DataObject::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getIsValid',
- 'getRequestSuccess',
- ])
- ->getMock();
- $validationResult->expects($this->any())
- ->method('getIsValid')
- ->willReturn($resultVatIsValid);
- $validationResult->expects($this->any())
- ->method('getRequestSuccess')
- ->willReturn($resultRequestSuccess);
- $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getStore',
- 'getDisableAutoGroupChange',
- 'getGroupId',
- 'setGroupId',
- 'save',
- ])
- ->getMock();
- $customer->expects($this->exactly(2))
- ->method('getStore')
- ->willReturn($store);
- $customer->expects($this->any())
- ->method('getDisableAutoGroupChange')
- ->willReturn(false);
- $customer->expects($this->once())
- ->method('getGroupId')
- ->willReturn(null);
- $customer->expects($this->once())
- ->method('setGroupId')
- ->with($newGroupId)
- ->willReturnSelf();
- $customer->expects($this->once())
- ->method('save')
- ->willReturnSelf();
- $this->customerSessionMock->expects($this->once())
- ->method('setCustomerGroupId')
- ->with($newGroupId)
- ->willReturnSelf();
- $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getCustomer',
- 'getForceProcess',
- 'getVatId',
- 'getCountryId',
- 'getCountry',
- 'setVatValidationResult',
- ])
- ->getMock();
- $address->expects($this->any())
- ->method('getCustomer')
- ->willReturn($customer);
- $address->expects($this->once())
- ->method('getForceProcess')
- ->willReturn(true);
- $address->expects($this->any())
- ->method('getVatId')
- ->willReturn($vatId);
- $address->expects($this->any())
- ->method('getCountryId')
- ->willReturn($countryId);
- $address->expects($this->once())
- ->method('getCountry')
- ->willReturn($country);
- $address->expects($this->once())
- ->method('setVatValidationResult')
- ->with($validationResult)
- ->willReturnSelf();
- $observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getCustomerAddress',
- ])
- ->getMock();
- $observer->expects($this->once())
- ->method('getCustomerAddress')
- ->willReturn($address);
- $this->helperAddress->expects($this->once())
- ->method('isVatValidationEnabled')
- ->with($store)
- ->willReturn(true);
- $this->vat->expects($this->any())
- ->method('isCountryInEU')
- ->with($country)
- ->willReturn(true);
- $this->vat->expects($this->once())
- ->method('checkVatNumber')
- ->with($countryId, $vatId)
- ->willReturn($validationResult);
- $this->vat->expects($this->once())
- ->method('getCustomerGroupIdBasedOnVatNumber')
- ->with($countryId, $validationResult, $store)
- ->willReturn($newGroupId);
- $this->vat->expects($this->any())
- ->method('getCustomerVatClass')
- ->with($countryId, $validationResult)
- ->willReturn($vatClass);
- $this->appState->expects($this->once())
- ->method('getAreaCode')
- ->willReturn($areaCode);
- $this->scopeConfig->expects($this->any())
- ->method('isSetFlag')
- ->with(HelperAddress::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT)
- ->willReturn(false);
- if ($resultValidMessage) {
- $this->messageManager->expects($this->once())
- ->method('addSuccess')
- ->with($resultValidMessage)
- ->willReturnSelf();
- }
- if ($resultInvalidMessage) {
- $this->escaper->expects($this->once())
- ->method('escapeHtml')
- ->with($vatId)
- ->willReturn($vatId);
- $this->messageManager->expects($this->once())
- ->method('addError')
- ->with($resultInvalidMessage)
- ->willReturnSelf();
- }
- if ($resultErrorMessage) {
- $this->scopeConfig->expects($this->once())
- ->method('getValue')
- ->with('trans_email/ident_support/email', ScopeInterface::SCOPE_STORE)
- ->willReturn('admin@example.com');
- $this->messageManager->expects($this->once())
- ->method('addError')
- ->with($resultErrorMessage)
- ->willReturnSelf();
- }
- $this->model->execute($observer);
- }
- /**
- * @return array
- */
- public function dataProviderAfterAddressSaveNewGroup()
- {
- return [
- [
- 'vat_id' => 1,
- 'vat_class' => null,
- 'country_id' => 1,
- 'country_code' => 'US',
- 'group_id' => 1,
- 'area_code' => Area::AREA_ADMINHTML,
- 'is_vat_valid' => false,
- 'request_sucess' => false,
- 'valid_message' => '',
- 'invalid_message' => '',
- 'error_message' => '',
- ],
- [
- 'vat_id' => 1,
- 'vat_class' => Vat::VAT_CLASS_DOMESTIC,
- 'country_id' => 1,
- 'country_code' => 'US',
- 'group_id' => 1,
- 'area_code' => Area::AREA_FRONTEND,
- 'is_vat_valid' => true,
- 'request_sucess' => false,
- 'valid_message' => 'Your VAT ID was successfully validated. You will be charged tax.',
- 'invalid_message' => '',
- 'error_message' => '',
- ],
- [
- 'vat_id' => 1,
- 'vat_class' => Vat::VAT_CLASS_INTRA_UNION,
- 'country_id' => 1,
- 'country_code' => 'US',
- 'group_id' => 1,
- 'area_code' => Area::AREA_FRONTEND,
- 'is_vat_valid' => true,
- 'request_sucess' => false,
- 'valid_message' => 'Your VAT ID was successfully validated. You will not be charged tax.',
- 'invalid_message' => '',
- 'error_message' => '',
- ],
- [
- 'vat_id' => 1,
- 'vat_class' => Vat::VAT_CLASS_INTRA_UNION,
- 'country_id' => 1,
- 'country_code' => 'US',
- 'group_id' => 1,
- 'area_code' => Area::AREA_FRONTEND,
- 'is_vat_valid' => false,
- 'request_sucess' => true,
- 'valid_message' => '',
- 'invalid_message' => 'The VAT ID entered (1) is not a valid VAT ID. You will be charged tax.',
- 'error_message' => '',
- ],
- [
- 'vat_id' => 1,
- 'vat_class' => Vat::VAT_CLASS_INTRA_UNION,
- 'country_id' => 1,
- 'country_code' => 'US',
- 'group_id' => 1,
- 'area_code' => Area::AREA_FRONTEND,
- 'is_vat_valid' => false,
- 'request_sucess' => false,
- 'valid_message' => '',
- 'invalid_message' => '',
- 'error_message' => 'Your Tax ID cannot be validated. You will be charged tax. '
- . 'If you believe this is an error, please contact us at admin@example.com',
- ],
- ];
- }
- }
|