protected function prepareMocksForTesting($populateSequence = 0)
 {
     $this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
     $this->request->expects($this->at(0))->method('getParam')->with('items', [])->willReturn($this->items);
     $this->request->expects($this->at(1))->method('getParam')->with('isAjax')->willReturn(true);
     $this->customerRepository->expects($this->once())->method('getById')->with(14)->willReturn($this->customerData);
     $this->customerMapper->expects($this->once())->method('toFlatArray')->with($this->customerData)->willReturn(['name' => 'Firstname Lastname']);
     $this->dataObjectHelper->expects($this->at($populateSequence))->method('populateWithArray')->with($this->customerData, ['name' => 'Firstname Lastname', 'email' => '*****@*****.**'], '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
     $this->customerData->expects($this->any())->method('getId')->willReturn(12);
 }
Example #2
0
 public function testSetAccountData()
 {
     $taxClassId = 1;
     $attributes = [['email', '*****@*****.**'], ['group_id', 1]];
     $attributeMocks = [];
     foreach ($attributes as $attribute) {
         $attributeMock = $this->getMock('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface', [], [], '', false);
         $attributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attribute[0]));
         $attributeMocks[] = $attributeMock;
     }
     $customerGroupMock = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\GroupInterface', [], '', false, true, true, ['getTaxClassId']);
     $customerGroupMock->expects($this->once())->method('getTaxClassId')->will($this->returnValue($taxClassId));
     $customerFormMock = $this->getMock('Magento\\Customer\\Model\\Metadata\\Form', [], [], '', false);
     $customerFormMock->expects($this->any())->method('getAttributes')->will($this->returnValue($attributeMocks));
     $customerFormMock->expects($this->any())->method('extractData')->will($this->returnValue([]));
     $customerFormMock->expects($this->any())->method('restoreData')->will($this->returnValue([]));
     $customerFormMock->expects($this->any())->method('prepareRequest')->will($this->returnValue($this->getMock('Magento\\Framework\\App\\RequestInterface')));
     $customerMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false);
     $this->customerMapper->expects($this->atLeastOnce())->method('toFlatArray')->willReturn(['email' => '*****@*****.**', 'group_id' => 1, 'gender' => 1]);
     $quoteMock = $this->getMock('Magento\\Quote\\Model\\Quote', [], [], '', false);
     $quoteMock->expects($this->any())->method('getCustomer')->will($this->returnValue($customerMock));
     $quoteMock->expects($this->once())->method('addData')->with(['customer_email' => $attributes[0][1], 'customer_group_id' => $attributes[1][1], 'customer_tax_class_id' => $taxClassId]);
     $this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with($customerMock, ['email' => '*****@*****.**', 'group_id' => 1, 'gender' => 1], '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
     $this->formFactoryMock->expects($this->any())->method('create')->will($this->returnValue($customerFormMock));
     $this->sessionQuoteMock->expects($this->any())->method('getQuote')->will($this->returnValue($quoteMock));
     $this->customerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($customerMock));
     $this->groupRepositoryMock->expects($this->once())->method('getById')->will($this->returnValue($customerGroupMock));
     $this->adminOrderCreate->setAccountData([]);
 }
Example #3
0
 /**
  * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testExecuteWithExistentCustomer()
 {
     $customerId = 22;
     $addressId = 11;
     $subscription = 'true';
     $postValue = ['customer' => ['entity_id' => $customerId, 'code' => 'value', 'coolness' => false, 'disable_auto_group_change' => 'false'], 'address' => ['_template_' => '_template_', $addressId => ['entity_id' => $addressId, 'default_billing' => 'true', 'default_shipping' => 'true', 'code' => 'value', 'coolness' => false, 'region' => 'region', 'region_id' => 'region_id']], 'subscription' => $subscription];
     $filteredData = ['entity_id' => $customerId, 'code' => 'value', 'coolness' => false, 'disable_auto_group_change' => 'false'];
     $addressFilteredData = ['entity_id' => $addressId, 'default_billing' => 'true', 'default_shipping' => 'true', 'code' => 'value', 'coolness' => false, 'region' => 'region', 'region_id' => 'region_id'];
     $savedData = ['entity_id' => $customerId, 'darkness' => true, 'name' => 'Name', \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_BILLING => false, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_SHIPPING => false];
     $mergedData = ['entity_id' => $customerId, 'darkness' => true, 'name' => 'Name', 'code' => 'value', 'disable_auto_group_change' => 0, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_BILLING => $addressId, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_SHIPPING => $addressId, 'confirmation' => false, 'sendemail_store_id' => '1', 'id' => $customerId];
     $mergedAddressData = ['entity_id' => $addressId, 'default_billing' => true, 'default_shipping' => true, 'code' => 'value', 'region' => ['region' => 'region', 'region_id' => 'region_id'], 'region_id' => 'region_id', 'id' => $addressId];
     /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $formMock */
     $attributeMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->disableOriginalConstructor()->getMock();
     $attributeMock->expects($this->exactly(2))->method('getAttributeCode')->willReturn('coolness');
     $attributeMock->expects($this->exactly(2))->method('getFrontendInput')->willReturn('int');
     $attributes = [$attributeMock];
     $this->requestMock->expects($this->exactly(3))->method('getPostValue')->willReturn($postValue);
     $this->requestMock->expects($this->exactly(3))->method('getPost')->willReturnMap([['customer', null, $postValue['customer']], ['address', null, $postValue['address']], ['subscription', null, $subscription]]);
     /** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject $formMock */
     $formMock = $this->getMockBuilder('Magento\\Customer\\Model\\Metadata\\Form')->disableOriginalConstructor()->getMock();
     $this->formFactoryMock->expects($this->exactly(2))->method('create')->willReturnMap([[\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'adminhtml_customer', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE, [], $formMock], [\Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS, 'adminhtml_customer_address', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE, [], $formMock]]);
     $formMock->expects($this->exactly(2))->method('extractData')->willReturnMap([[$this->requestMock, 'customer', true, $filteredData], [$this->requestMock, 'address/' . $addressId, true, $addressFilteredData]]);
     /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
     $objectMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock();
     $this->objectFactoryMock->expects($this->exactly(2))->method('create')->with(['data' => $postValue])->willReturn($objectMock);
     $objectMock->expects($this->exactly(2))->method('getData')->willReturnMap([['customer', null, $postValue['customer']], ['address/' . $addressId, null, $postValue['address'][$addressId]]]);
     $formMock->expects($this->exactly(2))->method('getAttributes')->willReturn($attributes);
     /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
     $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock();
     $this->customerDataFactoryMock->expects($this->once())->method('create')->willReturn($customerMock);
     $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)->willReturn($customerMock);
     $this->customerMapperMock->expects($this->once())->method('toFlatArray')->with($customerMock)->willReturn($savedData);
     $addressMock = $this->getMockBuilder('\\Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock();
     $this->addressDataFactoryMock->expects($this->once())->method('create')->willReturn($addressMock);
     $this->dataHelperMock->expects($this->exactly(2))->method('populateWithArray')->willReturnMap([[$customerMock, $mergedData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface', $this->dataHelperMock], [$addressMock, $mergedAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface', $this->dataHelperMock]]);
     $customerMock->expects($this->once())->method('setAddresses')->with([$addressMock])->willReturnSelf();
     $this->customerRepositoryMock->expects($this->once())->method('save')->with($customerMock)->willReturnSelf();
     $customerEmail = '*****@*****.**';
     $customerMock->expects($this->once())->method('getEmail')->willReturn($customerEmail);
     $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')->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(\Magento\Customer\Controller\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')->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());
 }
 /**
  * Put existing customer data into the backend session
  */
 protected function setupExistingCustomerData()
 {
     /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
     $customer = $this->_customerRepository->getById(1);
     $this->_customerData = ['customer_id' => $customer->getId(), 'account' => $this->customerMapper->toFlatArray($customer)];
     $this->_customerData['account']['id'] = $customer->getId();
     /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */
     $addresses = $customer->getAddresses();
     foreach ($addresses as $addressData) {
         $this->_customerData['address'][$addressData->getId()] = $this->addressMapper->toFlatArray($addressData);
         $this->_customerData['address'][$addressData->getId()]['id'] = $addressData->getId();
     }
     $this->_backendSession->setCustomerData($this->_customerData);
 }
Example #5
0
 /**
  * Prepare customer data for order creation.
  *
  * Create customer if not created using data from customer form.
  * Create customer billing/shipping address if necessary using data from customer address forms.
  * Set customer data to quote.
  *
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function _prepareCustomer()
 {
     if ($this->getQuote()->getCustomerIsGuest()) {
         return $this;
     }
     /** @var $store \Magento\Store\Model\Store */
     $store = $this->getSession()->getStore();
     $customer = $this->getQuote()->getCustomer();
     if ($customer->getId() && !$this->_customerIsInStore($store)) {
         /** Create a new customer record if it is not available in the specified store */
         /** Unset customer ID to ensure that new customer will be created */
         $customer->setId(null)->setStoreId($store->getId())->setWebsiteId($store->getWebsiteId())->setCreatedAt(null);
         $customer = $this->_validateCustomerData($customer);
     } else {
         if (!$customer->getId()) {
             /** Create new customer */
             $customerBillingAddressDataObject = $this->getBillingAddress()->exportCustomerAddress();
             $customer->setSuffix($customerBillingAddressDataObject->getSuffix())->setFirstname($customerBillingAddressDataObject->getFirstname())->setLastname($customerBillingAddressDataObject->getLastname())->setMiddlename($customerBillingAddressDataObject->getMiddlename())->setPrefix($customerBillingAddressDataObject->getPrefix())->setStoreId($store->getId())->setEmail($this->_getNewCustomerEmail());
             $customer = $this->_validateCustomerData($customer);
         }
     }
     $this->getQuote()->setCustomer($customer);
     if ($this->getBillingAddress()->getSaveInAddressBook()) {
         $this->_prepareCustomerAddress($this->getQuote()->getCustomer(), $this->getBillingAddress());
     }
     if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSaveInAddressBook()) {
         $this->_prepareCustomerAddress($this->getQuote()->getCustomer(), $this->getShippingAddress());
     }
     $this->getQuote()->updateCustomerData($this->getQuote()->getCustomer());
     $customer = $this->getQuote()->getCustomer();
     $origAddresses = $customer->getAddresses();
     // save original addresses
     $customer->setAddresses([]);
     $customerData = $this->customerMapper->toFlatArray($customer);
     $customer->setAddresses($origAddresses);
     // restore original addresses
     foreach ($this->_createCustomerForm($customer)->getUserAttributes() as $attribute) {
         if (isset($customerData[$attribute->getAttributeCode()])) {
             $quoteCode = sprintf('customer_%s', $attribute->getAttributeCode());
             $this->getQuote()->setData($quoteCode, $customerData[$attribute->getAttributeCode()]);
         }
     }
     return $this;
 }
 /**
  * Update customer data
  *
  * @param array $data
  * @return void
  */
 protected function updateCustomer(array $data)
 {
     $customer = $this->getCustomer();
     $customerData = array_merge($this->customerMapper->toFlatArray($customer), $data);
     $this->dataObjectHelper->populateWithArray($customer, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
 }