/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer_non_default_website_id.php */ public function testUpdateCustomerDetailsByEmail() { $customerId = 1; $firstName = 'Firstsave'; $lastName = 'Lastsave'; $newEmail = '*****@*****.**'; $city = 'San Jose'; $customerDetails = $this->_customerAccountService->getCustomerDetails($customerId); $website = Bootstrap::getObjectManager()->get('Magento\\Store\\Model\\Website'); $websiteId = $website->load('newwebsite')->getId(); $email = $customerDetails->getCustomer()->getEmail(); $customerData = array_merge($customerDetails->getCustomer()->__toArray(), ['firstname' => $firstName, 'lastname' => $lastName, 'email' => $newEmail, 'id' => null]); $addresses = $customerDetails->getAddresses(); $addressId = $addresses[0]->getId(); $newAddress = array_merge($addresses[0]->__toArray(), ['city' => $city]); $this->_customerBuilder->populateWithArray($customerData); $this->_addressBuilder->populateWithArray($newAddress); $this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create())->setAddresses([$this->_addressBuilder->create(), $addresses[1]]); $this->_customerAccountService->updateCustomerDetailsByEmail($email, $this->_customerDetailsBuilder->create(), $websiteId); $updatedCustomerDetails = $this->_customerAccountService->getCustomerDetails($customerId); $updateCustomerData = $updatedCustomerDetails->getCustomer(); $this->assertEquals($firstName, $updateCustomerData->getFirstname()); $this->assertEquals($lastName, $updateCustomerData->getLastname()); $this->assertEquals($newEmail, $updateCustomerData->getEmail()); $this->assertEquals(2, count($updatedCustomerDetails->getAddresses())); foreach ($updatedCustomerDetails->getAddresses() as $newAddress) { if ($newAddress->getId() == $addressId) { $this->assertEquals($city, $newAddress->getCity()); } } }
/** * Add address to customer during create account * * @return \Magento\Customer\Service\V1\Data\Address|null */ protected function _extractAddress() { if (!$this->getRequest()->getPost('create_address')) { return null; } $addressForm = $this->_formFactory->create('customer_address', 'customer_register_address'); $allowedAttributes = $addressForm->getAllowedAttributes(); $addressData = array(); foreach ($allowedAttributes as $attribute) { $attributeCode = $attribute->getAttributeCode(); $value = $this->getRequest()->getParam($attributeCode); if (is_null($value)) { continue; } switch ($attributeCode) { case 'region_id': $this->_regionBuilder->setRegionId($value); break; case 'region': $this->_regionBuilder->setRegion($value); break; default: $addressData[$attributeCode] = $value; } } $this->_addressBuilder->populateWithArray($addressData); $this->_addressBuilder->setRegion($this->_regionBuilder->create()); $this->_addressBuilder->setDefaultBilling($this->getRequest()->getParam('default_billing', false))->setDefaultShipping($this->getRequest()->getParam('default_shipping', false)); return $this->_addressBuilder->create(); }
/** * Sample Address data * * @return array */ private function getSampleAddressEntity() { $regionBuilder = Bootstrap::getObjectManager()->create('\\Magento\\Customer\\Service\\V1\\Data\\RegionBuilder'); $this->_customerAddressBuilder->setCountryId('US')->setDefaultBilling(true)->setDefaultShipping(true)->setPostcode('75477')->setRegion($regionBuilder->setRegion('Alabama')->setRegionId(1)->setRegionCode('AL')->create())->setStreet(array('Green str, 67'))->setTelephone('3468676')->setCity('CityM')->setFirstname('John')->setLastname('Smith'); $address1 = $this->_customerAddressBuilder->create(); $this->_customerAddressBuilder->setCountryId('US')->setDefaultBilling(false)->setDefaultShipping(false)->setPostcode('47676')->setRegion($regionBuilder->setRegion('Alabama')->setRegionId(1)->setRegionCode('AL')->create())->setStreet(array('Black str, 48'))->setCity('CityX')->setTelephone('3234676')->setFirstname('John')->setLastname('Smith'); $address2 = $this->_customerAddressBuilder->create(); return array($address1, $address2); }
/** * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture Magento/Customer/_files/customer_address.php * @magentoAppIsolation enabled */ public function testSaveNewAddressWithAttributes() { $this->_addressBuilder->populateWithArray(array_merge(AddressConverter::toFlatArray($this->_expectedAddresses[1]), array('firstname' => 'Jane', 'id' => 4200, 'weird' => 'something_strange_with_hair')))->setId(null); $proposedAddress = $this->_addressBuilder->create(); $customerId = 1; $this->_service->saveAddresses($customerId, array($proposedAddress)); $addresses = $this->_service->getAddresses($customerId); $this->assertNotEquals(V1\Data\AddressConverter::toFlatArray($proposedAddress), V1\Data\AddressConverter::toFlatArray($addresses[1])); $this->assertArrayNotHasKey('weird', V1\Data\AddressConverter::toFlatArray($proposedAddress), 'Only valid attributes should be available.'); $this->assertArrayNotHasKey('weird', V1\Data\AddressConverter::toFlatArray($addresses[1]), 'Only valid attributes should be available.'); }
/** * Make address Data Object out of an address model * * @param AbstractAddress $addressModel * @param int $defaultBillingId * @param int $defaultShippingId * @return Address */ public function createAddressFromModel(AbstractAddress $addressModel, $defaultBillingId, $defaultShippingId) { $addressId = $addressModel->getId(); $attributes = $this->_metadataService->getAllAddressAttributeMetadata(); $addressData = array(); foreach ($attributes as $attribute) { $code = $attribute->getAttributeCode(); if (!is_null($addressModel->getData($code))) { $addressData[$code] = $addressModel->getData($code); } } $isDefaultBilling = $addressModel->getData('is_default_billing') === null && intval($addressId) ? $addressId === $defaultBillingId : $addressModel->getData('is_default_billing'); $isDefaultShipping = $addressModel->getData('is_default_shipping') === null && intval($addressId) ? $addressId === $defaultShippingId : $addressModel->getData('is_default_shipping'); $this->_addressBuilder->populateWithArray(array_merge($addressData, array(Address::KEY_STREET => $addressModel->getStreet(), Address::KEY_DEFAULT_BILLING => $isDefaultBilling, Address::KEY_DEFAULT_SHIPPING => $isDefaultShipping, Address::KEY_REGION => array(Region::KEY_REGION => $addressModel->getRegion(), Region::KEY_REGION_ID => $addressModel->getRegionId(), Region::KEY_REGION_CODE => $addressModel->getRegionCode())))); if ($addressId) { $this->_addressBuilder->setId($addressId); } if ($addressModel->getCustomerId() || $addressModel->getParentId()) { $customerId = $addressModel->getCustomerId() ?: $addressModel->getParentId(); $this->_addressBuilder->setCustomerId($customerId); } $addressDataObject = $this->_addressBuilder->create(); return $addressDataObject; }
public function testValidateAddressesBoth() { // Setup address mock, no first name $mockBadAddress = $this->_createAddress(1, ''); // Setup address mock, with first name $mockAddress = $this->_createAddress(1, 'John'); $addressBad = $this->_addressBuilder->create(); $addressGood = $this->_addressBuilder->create(); $this->_addressConverterMock->expects($this->any())->method('createAddressModel')->will($this->returnValueMap(array(array($addressBad, $mockBadAddress), array($addressGood, $mockAddress)))); $customerService = $this->_createService(); try { $customerService->validateAddresses(array($addressBad, $addressGood)); $this->fail("InputException was expected but not thrown"); } catch (InputException $actualException) { $expectedException = new InputException(); $expectedException->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'firstname', 'index' => 0]); $this->assertEquals($expectedException, $actualException); } }
/** * Map Address to Address data object * * @param AddressBuilder $addressBuilder * @param Address $address * @return \Magento\Customer\Service\V1\Data\Address */ public function mapAddress(AddressBuilder $addressBuilder, Address $address) { $addressBuilder->setCountryId($address->getCountryId()); $addressBuilder->setRegion($addressBuilder->getRegionBuilder()->setRegionId($address->getRegionId())->create()); $addressBuilder->setPostcode($address->getPostcode()); $addressBuilder->setCity($address->getCity()); $addressBuilder->setStreet($address->getStreet()); return $addressBuilder->create(); }
/** * 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; }
public function testGetAttributes() { $this->_fillAllFields($this->_addressBuilder); $expected = $this->_expectedValues; $this->assertEquals($expected, $this->_addressBuilder->create()->__toArray()); }
/** * Prepare quote for customer order submit * * @return void */ protected function _prepareCustomerQuote() { $quote = $this->getQuote(); $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); $customer = $this->_customerAccountService->getCustomer($this->getCustomerSession()->getCustomerId()); $hasDefaultBilling = (bool) $customer->getDefaultBilling(); $hasDefaultShipping = (bool) $customer->getDefaultShipping(); if ($shipping && !$shipping->getSameAsBilling() && (!$shipping->getCustomerId() || $shipping->getSaveInAddressBook())) { $shippingAddress = $shipping->exportCustomerAddressData(); if (!$hasDefaultShipping) { //Make provided address as default shipping address $shippingAddress = $this->_addressBuilder->populate($shippingAddress)->setDefaultShipping(true)->create(); $hasDefaultShipping = true; } $quote->addCustomerAddressData($shippingAddress); $shipping->setCustomerAddressData($shippingAddress); } if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) { $billingAddress = $billing->exportCustomerAddressData(); if (!$hasDefaultBilling) { //Make provided address as default shipping address $this->_addressBuilder->populate($billingAddress); if (!$hasDefaultShipping) { //Make provided address as default shipping address $this->_addressBuilder->setDefaultShipping(true); } $this->_addressBuilder->setDefaultBilling(true); $billingAddress = $this->_addressBuilder->create(); } $quote->addCustomerAddressData($billingAddress); $billing->setCustomerAddressData($billingAddress); } }