/** * Two custom attributes are created, one for customer and another for customer address. * * Attribute related to customer should be returned only. * * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_address.php * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_customer.php */ public function testGetCustomAttributesCodes() { $userDefinedAttributeCode = FIXTURE_ATTRIBUTE_USER_DEFINED_CUSTOMER_NAME; $attributeCodes = $this->_customerBuilder->getCustomAttributesCodes(); $expectedAttributes = array('disable_auto_group_change', $userDefinedAttributeCode); $this->assertEquals($expectedAttributes, $attributeCodes, 'Custom attribute codes list is invalid.'); }
/** * @return \Magento\Customer\Service\V1\Data\Customer */ public function getCustomer() { if (!$this->customer) { $this->customer = $this->customerBuilder->populateWithArray($this->_backendSession->getCustomerData()['account'])->create(); } return $this->customer; }
/** * @param string $formCode * @param RequestInterface $request * @return \Magento\Customer\Service\V1\Data\Customer */ public function extract($formCode, RequestInterface $request) { $customerForm = $this->formFactory->create('customer', $formCode); $allowedAttributes = $customerForm->getAllowedAttributes(); $isGroupIdEmpty = true; $customerData = array(); foreach ($allowedAttributes as $attribute) { // confirmation in request param is the repeated password, not a confirmation code. if ($attribute === 'confirmation') { continue; } $attributeCode = $attribute->getAttributeCode(); if ($attributeCode == 'group_id') { $isGroupIdEmpty = false; } $customerData[$attributeCode] = $request->getParam($attributeCode); } $this->customerBuilder->populateWithArray($customerData); $store = $this->storeManager->getStore(); if ($isGroupIdEmpty) { $this->customerBuilder->setGroupId($this->groupService->getDefaultGroup($store->getId())->getId()); } $this->customerBuilder->setWebsiteId($store->getWebsiteId()); $this->customerBuilder->setStoreId($store->getId()); return $this->customerBuilder->create(); }
/** * Change customer password action * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { if (!$this->_formKeyValidator->validate($this->getRequest())) { $this->_redirect('*/*/edit'); return; } if ($this->getRequest()->isPost()) { $customerId = $this->_getSession()->getCustomerId(); $customer = $this->customerExtractor->extract('customer_account_edit', $this->_request); $this->_customerBuilder->populate($customer); $this->_customerBuilder->setId($customerId); $customer = $this->_customerBuilder->create(); if ($this->getRequest()->getParam('change_password')) { $currPass = $this->getRequest()->getPost('current_password'); $newPass = $this->getRequest()->getPost('password'); $confPass = $this->getRequest()->getPost('confirmation'); if (strlen($newPass)) { if ($newPass == $confPass) { try { $this->_customerAccountService->changePassword($customerId, $currPass, $newPass); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addException($e, __('A problem was encountered trying to change password.')); } } else { $this->messageManager->addError(__('Confirm your new password')); } } else { $this->messageManager->addError(__('New password field cannot be empty.')); } } try { $this->_customerDetailsBuilder->setCustomer($customer); $this->_customerAccountService->updateCustomer($this->_customerDetailsBuilder->create()); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (InputException $e) { $this->messageManager->addException($e, __('Invalid input')); } catch (\Exception $e) { $this->messageManager->addException($e, __('Cannot save the customer.') . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>'); } if ($this->messageManager->getMessages()->getCount() > 0) { $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()); $this->_redirect('*/*/edit'); return; } $this->messageManager->addSuccess(__('The account information has been saved.')); $this->_redirect('customer/account'); return; } $this->_redirect('*/*/edit'); }
/** * Retrieve Customer instance * * @return \Magento\Customer\Service\V1\Data\Customer */ public function getCustomer() { if (is_null($this->_customer)) { $this->_customer = $this->_customerBuilder->create(); $params = $this->_coreData->urlDecode($this->_getRequest()->getParam('data')); $data = explode(',', $params); $cId = abs(intval($data[0])); if ($cId && $cId == $this->_customerSession->getCustomerId()) { $this->_customer = $this->_customerSession->getCustomerDataObject(); } } return $this->_customer; }
/** * Handle customer VAT number if needed on collect_totals_before event of quote address * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Sales\Model\Quote\Address $quoteAddress */ $quoteAddress = $observer->getQuoteAddress(); /** @var \Magento\Sales\Model\Quote $quote */ $quote = $quoteAddress->getQuote(); $customerData = $quote->getCustomerData(); $storeId = $customerData->getStoreId(); if ($customerData->getCustomAttribute('disable_auto_group_change') && $customerData->getCustomAttribute('disable_auto_group_change')->getValue() || false == $this->vatValidator->isEnabled($quoteAddress, $storeId)) { return; } $customerCountryCode = $quoteAddress->getCountryId(); $customerVatNumber = $quoteAddress->getVatId(); $groupId = null; if (empty($customerVatNumber) || false == $this->customerHelper->isCountryInEU($customerCountryCode)) { $groupId = $customerData->getId() ? $this->customerHelper->getDefaultCustomerGroupId($storeId) : \Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID; } else { // Magento always has to emulate group even if customer uses default billing/shipping address $groupId = $this->customerHelper->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($quoteAddress, $storeId), $storeId); } if ($groupId) { $quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId()); $quote->setCustomerGroupId($groupId); $customerData = $this->customerBuilder->mergeDataObjectWithArray($customerData, array('group_id' => $groupId)); $quote->setCustomerData($customerData); } }
public function testToFlatArray() { $customerData = array('email' => '*****@*****.**', 'firstname' => 'John', 'lastname' => 'Doe', 'unknown_key' => 'Golden Necklace', 'warehouse_zip' => '78777', 'warehouse_alternate' => '90051'); $expectedCustomerData = array('email' => '*****@*****.**', 'firstname' => 'John', 'lastname' => 'Doe', 'warehouse_zip' => '78777', 'warehouse_alternate' => '90051'); $customer = $this->_customerBuilder->populateWithArray($customerData)->create(); $this->assertEquals($expectedCustomerData, \Magento\Framework\Service\ExtensibleDataObjectConverter::toFlatArray($customer)); }
public function testSetAccountData() { $taxClassId = 1; $attributes = array(array('email', '*****@*****.**'), array('group_id', 1)); $attributeMocks = array(); foreach ($attributes as $attribute) { $attributeMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Eav\\AttributeMetadata', array(), array(), '', false); $attributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attribute[0])); $attributeMocks[] = $attributeMock; } $customerGroupMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\CustomerGroup', array(), array(), '', false); $customerGroupMock->expects($this->once())->method('getTaxClassId')->will($this->returnValue($taxClassId)); $customerFormMock = $this->getMock('Magento\\Customer\\Model\\Metadata\\Form', array(), array(), '', false); $customerFormMock->expects($this->any())->method('getAttributes')->will($this->returnValue($attributeMocks)); $customerFormMock->expects($this->any())->method('extractData')->will($this->returnValue(array())); $customerFormMock->expects($this->any())->method('restoreData')->will($this->returnValue(array())); $customerFormMock->expects($this->any())->method('prepareRequest')->will($this->returnValue($this->getMock('Magento\\Framework\\App\\RequestInterface'))); $customerMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Customer', array(), array(), '', false); $customerMock->expects($this->any())->method('__toArray')->will($this->returnValue(array('email' => '*****@*****.**', 'group_id' => 1))); $quoteMock = $this->getMock('Magento\\Sales\\Model\\Quote', array(), array(), '', false); $quoteMock->expects($this->any())->method('getCustomerData')->will($this->returnValue($customerMock)); $quoteMock->expects($this->once())->method('addData')->with(array('customer_email' => $attributes[0][1], 'customer_group_id' => $attributes[1][1], 'customer_tax_class_id' => $taxClassId)); $this->formFactoryMock->expects($this->any())->method('create')->will($this->returnValue($customerFormMock)); $this->sessionQuoteMock->expects($this->any())->method('getQuote')->will($this->returnValue($quoteMock)); $this->customerBuilderMock->expects($this->any())->method('populateWithArray')->will($this->returnSelf()); $this->customerBuilderMock->expects($this->any())->method('create')->will($this->returnValue($customerMock)); $this->customerBuilderMock->expects($this->any())->method('mergeDataObjectWithArray')->will($this->returnArgument(0)); $this->customerGroupServiceMock->expects($this->once())->method('getGroup')->will($this->returnValue($customerGroupMock)); $this->adminOrderCreate->setAccountData(array()); }
/** * @return Customer */ private function _createCustomer() { /** @var \Magento\Customer\Service\V1\Data\Customer $customer */ $customer = $this->_customerBuilder->setFirstname('firstname')->setLastname('lastname')->setEmail('*****@*****.**')->create(); $data = array('account' => $customer->__toArray()); $this->_context->getBackendSession()->setCustomerData($data); return $customer; }
public function testPopulateFromCustomerIdInArray() { $customer = $this->_customerBuilder->populateWithArray(array(Customer::FIRSTNAME => self::FIRSTNAME, Customer::LASTNAME => self::LASTNAME, Customer::EMAIL => self::EMAIL, Customer::ID => self::ID))->create(); $this->assertEquals(self::FIRSTNAME, $customer->getFirstname()); $this->assertEquals(self::LASTNAME, $customer->getLastname()); $this->assertEquals(self::EMAIL, $customer->getEmail()); $this->assertEquals(self::ID, $customer->getId()); }
/** * Obtain customer data from session and create customer object * * @return \Magento\Customer\Service\V1\Data\Customer */ protected function _getCustomerDataObject() { if (is_null($this->_customerDataObject)) { $customerData = $this->_backendSession->getCustomerData(); $accountData = isset($customerData['account']) ? $customerData['account'] : array(); $this->_customerDataObject = $this->_customerBuilder->populateWithArray($accountData)->create(); } return $this->_customerDataObject; }
/** * Sample customer data * * @return CustomerData */ private function getSampleCustomerEntity() { $email = '*****@*****.**'; $storeId = 1; $firstname = 'Tester'; $lastname = 'McTest'; $groupId = 1; $this->_customerBuilder->setStoreId($storeId)->setEmail($email)->setFirstname($firstname)->setLastname($lastname)->setGroupId($groupId); return $this->_customerBuilder->create(); }
/** * Builds the entity. * * @return \Magento\Customer\Service\V1\Data\CustomerDetails */ public function create() { if (!isset($this->_data[CustomerDetails::KEY_CUSTOMER])) { $this->_data[CustomerDetails::KEY_CUSTOMER] = $this->_customerBuilder->create(); } if (!isset($this->_data[CustomerDetails::KEY_ADDRESSES])) { $this->_data[CustomerDetails::KEY_ADDRESSES] = null; } return parent::create(); }
/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer.php * @expectedException \Magento\Framework\Exception\StateException */ public function testUpdateCustomerDetailsByEmailWithException() { $customerId = 1; $customerDetails = $this->_customerAccountService->getCustomerDetails($customerId); $email = $customerDetails->getCustomer()->getEmail(); $customerData = array_merge($customerDetails->getCustomer()->__toArray(), ['firstname' => 'fname', 'id' => 1234567]); $this->_customerBuilder->populateWithArray($customerData); $this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create())->setAddresses([]); $this->_customerAccountService->updateCustomerDetailsByEmail($email, $this->_customerDetailsBuilder->create()); }
/** * Forgot customer account information page * * @return void */ public function execute() { $this->_view->loadLayout(); $this->_view->getLayout()->initMessages(); $block = $this->_view->getLayout()->getBlock('customer_edit'); if ($block) { $block->setRefererUrl($this->_redirect->getRefererUrl()); } $data = $this->_getSession()->getCustomerFormData(true); $customerId = $this->_getSession()->getCustomerId(); $customerDataObject = $this->_customerAccountService->getCustomer($customerId); if (!empty($data)) { $customerDataObject = $this->_customerBuilder->mergeDataObjectWithArray($customerDataObject, $data); } $this->_getSession()->setCustomerData($customerDataObject); $this->_getSession()->setChangePassword($this->getRequest()->getParam('changepass') == 1); $this->_view->getLayout()->getBlock('head')->setTitle(__('Account Information')); $this->_view->getLayout()->getBlock('messages')->setEscapeMessageFlag(true); $this->_view->renderLayout(); }
/** * Get customer data from session or service. * * @param int|null $customerId possible customer ID from DB * @return Customer * @throws NoSuchEntityException */ protected function getCustomer($customerId) { $customerData = $this->_backendSession->getCustomerData(); if (!empty($customerData['account'])) { return $this->_customerBuilder->populateWithArray($customerData['account'])->create(); } elseif ($customerId) { return $this->_customerAccountService->getCustomer($customerId); } else { return $this->_customerBuilder->create(); } }
/** * test getCustomer method, method returns depersonalized customer Data */ public function testGetCustomerDepersonalizeCustomerData() { $this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(false)); $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue(true)); $this->viewMock->expects($this->once())->method('isLayoutLoaded')->will($this->returnValue(true)); $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with($this->equalTo('Magento_PageCache'))->will($this->returnValue(true)); $this->customerSessionMock->expects($this->once())->method('getCustomerGroupId')->will($this->returnValue($this->customerGroupId)); $this->customerDataBuilderMock->expects($this->once())->method('create')->will($this->returnValue($this->customerDataMock)); $this->customerDataBuilderMock->expects($this->once())->method('setGroupId')->with($this->equalTo($this->customerGroupId))->will($this->returnSelf()); $this->assertEquals($this->customerDataMock, $this->currentCustomer->getCustomer()); }
/** * {@inheritdoc} */ public function updateCustomerDetailsByEmail($customerEmail, CustomerDetails $customerDetails, $websiteId = null) { $customerData = $customerDetails->getCustomer(); $customerId = $this->getCustomerByEmail($customerEmail, $websiteId)->getId(); if ($customerData->getId() && $customerData->getId() !== $customerId) { throw new StateException('Altering the customer ID is not permitted'); } $customerData = $this->customerBuilder->populate($customerData)->setId($customerId)->create(); $customerDetails = $this->customerDetailsBuilder->populate($customerDetails)->setCustomer($customerData)->create(); return $this->updateCustomer($customerDetails); }
public function connectByCreatingAccount($facebookId, $token, $email, $firstName, $lastName) { $customerDetails = array('firstname' => $firstName, 'lastname' => $lastName, 'email' => $email, 'sendemail' => 0, 'confirmation' => 0, 'custom_attributes' => array(array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_fid', \Magento\Framework\Service\Data\AttributeValue::VALUE => $facebookId), array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_ftoken', \Magento\Framework\Service\Data\AttributeValue::VALUE => serialize($token)))); $customer = $this->_customerBuilder->populateWithArray($customerDetails)->create(); // Save customer $customerDetails = $this->_customerDetailsBuilder->setCustomer($customer)->setAddresses(null)->create(); $customerDataObject = $this->_customerAccountService->createCustomer($customerDetails); /* @var $customer \Magento\Customer\Service\V1\Data\Customer */ // Convert data object to customer model $customer = $this->_converter->createCustomerModel($customerDataObject); /* @var $customer \Magento\Customer\Model\Customer */ $customer->sendNewAccountEmail('confirmed', ''); $this->_customerSession->setCustomerAsLoggedIn($customer); }
/** * Update Customer name in Quote */ protected function _updateQuoteCustomerName() { /** @var $emptyAddress \Magento\Sales\Model\Quote\Address */ $emptyAddress = $this->_quoteAddressFactory->create(); $emptyAddress->setFirstname(null); $emptyAddress->setLastname(null); $this->_block->getQuote()->setBillingAddress($emptyAddress); $customerData = $this->_customerService->getCustomer(self::FIXTURE_CUSTOMER_ID); $customerData = $this->_customerBuilder->populate($customerData)->setFirstname(self::SAMPLE_FIRST_NAME)->setLastname(self::SAMPLE_LAST_NAME)->create(); $this->_block->getQuote()->setCustomerData($customerData); $this->_block->getQuote()->save(); $this->assertEquals(self::SAMPLE_FIRST_NAME, $this->_block->getFirstname()); $this->assertEquals(self::SAMPLE_LAST_NAME, $this->_block->getLastname()); }
/** * Loads the values from a customer model * * @param Customer $customerModel * @return CustomerDataObjectBuilder */ protected function _populateBuilderWithAttributes(Customer $customerModel) { $attributes = array(); foreach ($customerModel->getAttributes() as $attribute) { $attrCode = $attribute->getAttributeCode(); $value = $customerModel->getDataUsingMethod($attrCode); $value = $value ? $value : $customerModel->getData($attrCode); if (null !== $value) { if ($attrCode == 'entity_id') { $attributes[CustomerDataObject::ID] = $value; } else { $attributes[$attrCode] = $value; } } } return $this->_customerBuilder->populateWithArray($attributes); }
/** * @dataProvider saveBillingDataProvider * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.ExcessiveParameterList) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function testSaveBilling($data, $customerAddressId, $quoteCustomerId, $addressCustomerId, $isAddress, $validateDataResult, $validateResult, $checkoutMethod, $customerPassword, $confirmPassword, $validationResultMessages, $isEmailAvailable, $isVirtual, $getStepDataResult, $expected) { $passwordHash = 'password hash'; $this->requestMock->expects($this->any())->method('isAjax')->will($this->returnValue(false)); $customerValidationResultMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\CustomerValidationResults', [], [], '', false); $customerValidationResultMock->expects($this->any())->method('isValid')->will($this->returnValue(empty($validationResultMessages))); $customerValidationResultMock->expects($this->any())->method('getMessages')->will($this->returnValue($validationResultMessages)); $this->customerAccountServiceMock->expects($this->any())->method('getPasswordHash')->with($customerPassword)->will($this->returnValue($passwordHash)); $this->customerAccountServiceMock->expects($this->any())->method('validateCustomerData')->will($this->returnValue($customerValidationResultMock)); $this->customerAccountServiceMock->expects($this->any())->method('isEmailAvailable')->will($this->returnValue($isEmailAvailable)); /** @var \Magento\Sales\Model\Quote|\PHPUnit_Framework_MockObject_MockObject $quoteMock */ $quoteMock = $this->getMock('Magento\\Sales\\Model\\Quote', ['getCustomerId', '__wakeup', 'getBillingAddress', 'setPasswordHash', 'getCheckoutMethod', 'isVirtual', 'getShippingAddress', 'getCustomerData', 'collectTotals', 'save'], [], '', false); $shippingAddressMock = $this->getMock('Magento\\Sales\\Model\\Quote\\Address', ['setSameAsBilling', '__wakeup', 'unserialize'], [], '', false); $shippingAddressMock->expects($this->any())->method('setSameAsBilling')->with((int) $data['use_for_shipping']); $quoteMock->expects($this->any())->method('setPasswordHash')->with($passwordHash); $quoteMock->expects($this->any())->method('getCheckoutMethod')->will($this->returnValue($checkoutMethod)); $quoteMock->expects($this->any())->method('isVirtual')->will($this->returnValue($isVirtual)); $quoteMock->expects($this->any())->method('getShippingAddress')->will($this->returnValue($shippingAddressMock)); $addressMock = $this->getMock('Magento\\Sales\\Model\\Quote\\Address', ['setSaveInAddressBook', 'getData', 'setEmail', '__wakeup', 'importCustomerAddressData', 'validate'], [], '', false); $addressMock->expects($this->any())->method('importCustomerAddressData')->will($this->returnSelf()); $addressMock->expects($this->atLeastOnce())->method('validate')->will($this->returnValue($validateResult)); $addressMock->expects($this->any())->method('getData')->will($this->returnValue([])); $quoteMock->expects($this->any())->method('getBillingAddress')->will($this->returnValue($addressMock)); $quoteMock->expects($this->any())->method('getCustomerId')->will($this->returnValue($quoteCustomerId)); $formMock = $this->getMock('Magento\\Customer\\Model\\Metadata\\Form', [], [], '', false); $formMock->expects($this->atLeastOnce())->method('validateData')->will($this->returnValue($validateDataResult)); $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap([['customer_password', $customerPassword], ['confirm_password', $confirmPassword]])); $formMock->expects($this->any())->method('prepareRequest')->will($this->returnValue($this->requestMock)); $this->customerFormFactoryMock->expects($this->any())->method('create')->will($this->returnValue($formMock)); $customerDataMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Customer', [], [], '', false); $customerDataMock->expects($this->any())->method('__toArray')->will($this->returnValue([])); $this->customerBuilderMock->expects($this->any())->method('create')->will($this->returnValue($customerDataMock)); $quoteMock->expects($this->atLeastOnce())->method('getCustomerData')->will($this->returnValue($customerDataMock)); $this->checkoutSessionMock->expects($this->any())->method('getQuote')->will($this->returnValue($quoteMock)); $this->checkoutSessionMock->expects($this->any())->method('getStepData')->will($this->returnValue((int) $data['use_for_shipping'] === 1 ? true : $getStepDataResult)); $this->checkoutSessionMock->expects($this->any())->method('setStepData')->will($this->returnSelf()); $customerAddressMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Address', [], [], '', false); $customerAddressMock->expects($this->any())->method('getCustomerId')->will($this->returnValue($addressCustomerId)); $this->customerAddressServiceMock->expects($this->any())->method('getAddress')->will($isAddress ? $this->returnValue($customerAddressMock) : $this->throwException(new \Exception())); $this->customerBuilderMock->expects($checkoutMethod === Onepage::METHOD_REGISTER ? $this->never() : $this->once())->method('populate'); $this->customerBuilderMock->expects($checkoutMethod === Onepage::METHOD_REGISTER ? $this->never() : $this->once())->method('setGroupId'); $websiteMock = $this->getMock('Magento\\Store\\Model\\Website', [], [], '', false); $this->storeManagerMock->expects($this->any())->method('getWebsite')->will($this->returnValue($websiteMock)); $this->assertEquals($expected, $this->onepage->saveBilling($data, $customerAddressId)); }
/** * @param $customerData * @param $isSingleStoreMode * @param $canModifyCustomer */ private function _setupStoreMode($customerData, $isSingleStoreMode, $canModifyCustomer) { $backendSessionMock = $this->getMock('\\Magento\\Backend\\Model\\Session', ['getCustomerData'], [], '', false); $backendSessionMock->expects($this->any())->method('getCustomerData')->will($this->returnValue([])); $layoutMock = $this->getMock('\\Magento\\Framework\\View\\Layout\\Element\\Layout', ['createBlock'], [], '', false); $layoutMock->expects($this->at(0))->method('createBlock')->with('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Group')->will($this->returnValue($this->objectManagerHelper->getObject('\\Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Group'))); $layoutMock->expects($this->at(1))->method('createBlock')->with('Magento\\Backend\\Block\\Store\\Switcher\\Form\\Renderer\\Fieldset\\Element')->will($this->returnValue($this->objectManagerHelper->getObject('Magento\\Backend\\Block\\Store\\Switcher\\Form\\Renderer\\Fieldset\\Element'))); if (empty($customerData['id'])) { $layoutMock->expects($this->at(2))->method('createBlock')->with('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Sendemail')->will($this->returnValue($this->objectManagerHelper->getObject('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Sendemail'))); } $urlBuilderMock = $this->getMock('\\Magento\\Backend\\Model\\Url', ['getBaseUrl'], [], '', false); $urlBuilderMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue('someUrl')); $storeManagerMock = $this->getMock('Magento\\Store\\Model\\StoreManager', [], [], '', false); $storeManagerMock->expects($this->any())->method('isSingleStoreMode')->will($this->returnValue($isSingleStoreMode)); $customerObject = $this->getMockBuilder('\\Magento\\Customer\\Service\\V1\\Data\\Customer')->setMethods(['__toArray', 'getId'])->disableOriginalConstructor()->getMock(); $customerObject->expects($this->any())->method('__toArray')->will($this->returnValue($customerData)); if (!empty($customerData['id'])) { $customerObject->expects($this->any())->method('getId')->will($this->returnValue($customerData['id'])); } $fieldset = $this->getMockBuilder('\\Magento\\Framework\\Data\\Form\\Element\\Fieldset')->setMethods(['getForm', 'addField', 'removeField'])->disableOriginalConstructor()->getMock(); $accountForm = $this->getMockBuilder('Magento\\Framework\\Data\\Form')->setMethods(['create', 'addFieldset', 'getElement', 'setValues'])->disableOriginalConstructor()->getMock(); $accountForm->expects($this->any())->method('addFieldset')->with('base_fieldset')->will($this->returnValue($fieldset)); $accountForm->expects($this->any())->method('setValues')->will($this->returnValue($accountForm)); $fieldset->expects($this->any())->method('getForm')->will($this->returnValue($accountForm)); $formElement = $this->getMockBuilder('\\Magento\\Framework\\Data\\Form\\Element\\Select')->setMethods(['setRenderer', 'addClass', 'setDisabled'])->disableOriginalConstructor()->getMock(); $formElement->expects($this->any())->method('setRenderer')->will($this->returnValue(null)); $formElement->expects($this->any())->method('addClass')->will($this->returnValue(null)); $formElement->expects($this->any())->method('setDisabled')->will($this->returnValue(null)); $accountForm->expects($this->any())->method('getElement')->withAnyParameters()->will($this->returnValue($formElement)); $fieldset->expects($this->any())->method('addField')->will($this->returnValue($formElement)); $customerForm = $this->getMock('\\Magento\\Customer\\Model\\Metadata\\Form', ['getAttributes'], [], '', false); $customerForm->expects($this->any())->method('getAttributes')->will($this->returnValue([])); $this->contextMock->expects($this->any())->method('getBackendSession')->will($this->returnValue($backendSessionMock)); $this->contextMock->expects($this->any())->method('getLayout')->will($this->returnValue($layoutMock)); $this->contextMock->expects($this->any())->method('getUrlBuilder')->will($this->returnValue($urlBuilderMock)); $this->contextMock->expects($this->any())->method('getStoreManager')->will($this->returnValue($storeManagerMock)); $this->customerBuilderMock->expects($this->any())->method('populateWithArray')->will($this->returnValue($this->customerBuilderMock)); $this->customerBuilderMock->expects($this->any())->method('create')->will($this->returnValue($customerObject)); $this->customerHelperMock->expects($this->any())->method('getNamePrefixOptions')->will($this->returnValue(['Pref1', 'Pref2'])); $this->customerHelperMock->expects($this->any())->method('getNameSuffixOptions')->will($this->returnValue(['Suf1', 'Suf2'])); $this->formFactoryMock->expects($this->any())->method('create')->will($this->returnValue($accountForm)); $this->customerFormFactoryMock->expects($this->any())->method('create')->with('customer', 'adminhtml_customer')->will($this->returnValue($customerForm)); $this->customerAccountServiceInterfaceMock->expects($this->any())->method('canModify')->withAnyParameters()->will($this->returnValue($canModifyCustomer)); $this->customerAccountServiceInterfaceMock->expects($this->any())->method('getConfirmationStatus')->withAnyParameters()->will($this->returnValue(CustomerAccountServiceInterface::ACCOUNT_CONFIRMED)); }
/** * Prepare quote for customer registration and customer order submit * * @return void */ protected function _prepareNewCustomerQuote() { $quote = $this->getQuote(); $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); $customerData = $quote->getCustomerData(); $customerBillingData = $billing->exportCustomerAddressData(); $customerBillingData = $this->_addressBuilder->populate($customerBillingData)->setDefaultBilling(true)->create(); if ($shipping) { if (!$shipping->getSameAsBilling()) { $customerShippingData = $shipping->exportCustomerAddressData(); $customerShippingData = $this->_addressBuilder->populate($customerShippingData)->setDefaultShipping(true)->create(); $shipping->setCustomerAddressData($customerShippingData); // Add shipping address to quote since customer Data Object does not hold address information $quote->addCustomerAddressData($customerShippingData); } else { $shipping->setCustomerAddressData($customerBillingData); $customerBillingData = $this->_addressBuilder->populate($customerBillingData)->setDefaultShipping(true)->create(); } } else { $customerBillingData = $this->_addressBuilder->populate($customerBillingData)->setDefaultShipping(true)->create(); } $billing->setCustomerAddressData($customerBillingData); $dataArray = $this->_objectCopyService->getDataFromFieldset('checkout_onepage_quote', 'to_customer', $quote); $customerData = $this->_customerBuilder->mergeDataObjectWithArray($customerData, $dataArray); $quote->setCustomerData($customerData)->setCustomerId(true); // TODO : Eventually need to remove this legacy hack // Add billing address to quote since customer Data Object does not hold address information $quote->addCustomerAddressData($customerBillingData); }
/** * Prepare quote for customer registration and customer order submit * and restore magento customer data from quote * * @return void */ protected function _prepareNewCustomerQuote() { $quote = $this->_quote; $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $billingAddressBuilder */ $billingAddressBuilder = $this->_addressBuilderFactory->create(); $customerBilling = $billingAddressBuilder->populate($billing->exportCustomerAddressData())->setDefaultBilling(true); if ($shipping && !$shipping->getSameAsBilling()) { /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $shippingAddressBuilder */ $shippingAddressBuilder = $this->_addressBuilderFactory->create(); $customerShipping = $shippingAddressBuilder->populate($shipping->exportCustomerAddressData())->setDefaultShipping(true)->create(); $shipping->setCustomerAddressData($customerShipping); } elseif ($shipping) { $customerBilling->setDefaultShipping(true); } $customerBilling = $customerBilling->create(); $billing->setCustomerAddressData($customerBilling); /** * @todo integration with dynamic attributes customer_dob, customer_taxvat, customer_gender */ if ($quote->getCustomerDob() && !$billing->getCustomerDob()) { $billing->setCustomerDob($quote->getCustomerDob()); } if ($quote->getCustomerTaxvat() && !$billing->getCustomerTaxvat()) { $billing->setCustomerTaxvat($quote->getCustomerTaxvat()); } if ($quote->getCustomerGender() && !$billing->getCustomerGender()) { $billing->setCustomerGender($quote->getCustomerGender()); } $customerData = $this->_objectCopyService->getDataFromFieldset('checkout_onepage_billing', 'to_customer', $billing); $customer = $this->_customerBuilder->populateWithArray($customerData); $customer->setEmail($quote->getCustomerEmail()); $customer->setPrefix($quote->getCustomerPrefix()); $customer->setFirstname($quote->getCustomerFirstname()); $customer->setMiddlename($quote->getCustomerMiddlename()); $customer->setLastname($quote->getCustomerLastname()); $customer->setSuffix($quote->getCustomerSuffix()); $quote->setCustomerData($customer->create())->addCustomerAddressData($customerBilling); if (isset($customerShipping)) { $quote->addCustomerAddressData($customerShipping); } }
/** * @expectedException \Magento\Customer\Exception * @expectedExceptionMessage exception message */ public function testCreateCustomerWithPasswordHashException() { $storeId = 5; $customerData = array('id' => self::ID, 'email' => self::EMAIL, 'firstname' => self::FIRSTNAME, 'lastname' => self::LASTNAME, 'store_id' => $storeId, 'website_id' => self::WEBSITE_ID); $this->_customerBuilder->populateWithArray($customerData); $customerEntity = $this->_customerBuilder->create(); $customerDetails = $this->_customerDetailsBuilder->setCustomer($customerEntity)->create(); $this->_storeManagerMock->expects($this->once())->method('getStores')->will($this->returnValue(array())); $this->_converter = $this->getMock('Magento\\Customer\\Model\\Converter', array('getCustomerModel', 'createCustomerModel'), array(), '', false); $this->_converter->expects($this->once())->method('getCustomerModel')->will($this->returnValue($this->_customerModelMock)); $this->_converter->expects($this->once())->method('createCustomerModel')->will($this->throwException(new \Magento\Customer\Exception('exception message', 0))); $customerService = $this->_createService(); $customerService->createCustomerWithPasswordHash($customerDetails, '', ''); }
/** * Initialize form object * * @return $this * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function initForm() { $customerData = $this->_backendSession->getCustomerData(); /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $fieldset = $form->addFieldset('address_fieldset', array('legend' => __("Edit Customer's Address"))); $account = $customerData['account']; $this->_addressBuilder->populateWithArray(array()); if (!empty($account) && isset($account['store_id'])) { $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry($this->_storeManager->getStore($account['store_id']))); } else { $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry()); } $address = $this->_addressBuilder->create(); $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($address)); $attributes = $addressForm->getAttributes(); if (isset($attributes['street'])) { if ($attributes['street']->getMultilineCount() <= 0) { $attributes['street'] = $this->_attributeMetadataBuilder->populate($attributes['street'])->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT)->create(); } } foreach ($attributes as $key => $attribute) { $attributes[$key] = $this->_attributeMetadataBuilder->populate($attribute)->setFrontendLabel(__($attribute->getFrontendLabel()))->setVisible(false)->create(); } $this->_setFieldset($attributes, $fieldset); $regionElement = $form->getElement('region'); if ($regionElement) { $regionElement->setRenderer($this->_regionFactory->create()); } $regionElement = $form->getElement('region_id'); if ($regionElement) { $regionElement->setNoDisplay(true); } $country = $form->getElement('country_id'); if ($country) { $country->addClass('countries'); } if ($this->isReadonly()) { foreach ($this->_metadataService->getAllAddressAttributeMetadata() as $attribute) { $element = $form->getElement($attribute->getAttributeCode()); if ($element) { $element->setReadonly(true, true); } } } $customerStoreId = null; if (!empty($account) && isset($account['id']) && isset($account['website_id'])) { $customerStoreId = $this->_storeManager->getWebsite($account['website_id'])->getDefaultStore()->getId(); } $prefixElement = $form->getElement('prefix'); if ($prefixElement) { $prefixOptions = $this->_customerHelper->getNamePrefixOptions($customerStoreId); if (!empty($prefixOptions)) { $fieldset->removeField($prefixElement->getId()); $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^'); $prefixField->setValues($prefixOptions); } } $suffixElement = $form->getElement('suffix'); if ($suffixElement) { $suffixOptions = $this->_customerHelper->getNameSuffixOptions($customerStoreId); if (!empty($suffixOptions)) { $fieldset->removeField($suffixElement->getId()); $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId()); $suffixField->setValues($suffixOptions); } } $this->assign('customer', $this->_customerBuilder->populateWithArray($account)->create()); $addressCollection = array(); foreach ($customerData['address'] as $key => $addressData) { $addressCollection[$key] = $this->_addressBuilder->populateWithArray($addressData)->create(); } $this->assign('addressCollection', $addressCollection); $form->setValues(AddressConverter::toFlatArray($address)); $this->setForm($form); return $this; }
/** * 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 */ public function _prepareCustomer() { if ($this->getQuote()->getCustomerIsGuest()) { return $this; } /** @var $store \Magento\Store\Model\Store */ $store = $this->getSession()->getStore(); $customerDataObject = $this->getQuote()->getCustomerData(); if ($customerDataObject->getId() && !$this->_customerIsInStore($store)) { /** Create a new customer record if it is not available in the specified store */ $customerDataObject = $this->_customerBuilder->populate($customerDataObject)->setId(null)->setStoreId($store->getId())->setWebsiteId($store->getWebsiteId())->setCreatedAt(null)->create(); $customerDataObject = $this->_validateCustomerData($customerDataObject); } else { if (!$customerDataObject->getId()) { /** Create new customer */ $customerBillingAddressDataObject = $this->getBillingAddress()->exportCustomerAddressData(); $customerDataObject = $this->_customerBuilder->populate($customerDataObject)->setSuffix($customerBillingAddressDataObject->getSuffix())->setFirstname($customerBillingAddressDataObject->getFirstname())->setLastname($customerBillingAddressDataObject->getLastname())->setMiddlename($customerBillingAddressDataObject->getMiddlename())->setPrefix($customerBillingAddressDataObject->getPrefix())->setStoreId($store->getId())->setEmail($this->_getNewCustomerEmail())->create(); $customerDataObject = $this->_validateCustomerData($customerDataObject); } } if ($this->getBillingAddress()->getSaveInAddressBook()) { $this->_prepareCustomerAddress($customerDataObject, $this->getBillingAddress()); } if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSaveInAddressBook()) { $this->_prepareCustomerAddress($customerDataObject, $this->getShippingAddress()); } $this->getQuote()->updateCustomerData($customerDataObject); $customerData = \Magento\Framework\Service\ExtensibleDataObjectConverter::toFlatArray($customerDataObject); foreach ($this->_createCustomerForm($customerDataObject)->getUserAttributes() as $attribute) { if (isset($customerData[$attribute->getAttributeCode()])) { $quoteCode = sprintf('customer_%s', $attribute->getAttributeCode()); $this->getQuote()->setData($quoteCode, $customerData[$attribute->getAttributeCode()]); } } return $this; }
/** * @return \Magento\Customer\Service\V1\Data\Customer */ protected function _getCustomer() { return $this->_customerBuilder->populateWithArray($this->_backendSession->getCustomerData()['account'])->create(); }
/** * Returns customer Data with customer group only * * @return \Magento\Customer\Service\V1\Data\Customer */ protected function getDepersonalizedCustomer() { return $this->customerBuilder->setGroupId($this->customerSession->getCustomerGroupId())->create(); }