public function testGetAttributes()
 {
     $formAttributesMetadata = $this->_service->getAttributes('customer_address_edit');
     $this->assertCount(15, $formAttributesMetadata, "Invalid number of attributes for the specified form.");
     /** Check some fields of one attribute metadata */
     $attributeMetadata = $formAttributesMetadata['company'];
     $this->assertInstanceOf('Magento\\Customer\\Service\\V1\\Data\\Eav\\AttributeMetadata', $attributeMetadata);
     $this->assertEquals('company', $attributeMetadata->getAttributeCode(), 'Attribute code is invalid');
     $this->assertNotEmpty($attributeMetadata->getValidationRules(), 'Validation rules are not set');
     $this->assertEquals('varchar', $attributeMetadata->getBackendType(), 'Backend type is invalid');
     $this->assertEquals('Company', $attributeMetadata->getFrontendLabel(), 'Frontend label is invalid');
 }
Example #2
0
 /**
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function testCreateAddressFromModelWithCustomerId()
 {
     $defaultBillingId = 1;
     $defaultShippingId = 1;
     $customerId = 1;
     $attributeCode = 'attribute_code';
     $addressModelMock = $this->getAddressMockForCreate();
     $addressModelMock->expects($this->once())->method('getId')->will($this->returnValue(null));
     $addressModelMock->expects($this->any())->method('getCustomerId')->will($this->returnValue($customerId));
     $addressModelMock->expects($this->any())->method('getParentId');
     $getData = function ($key, $index = null) use($attributeCode, $customerId) {
         $result = null;
         switch ($key) {
             case $attributeCode:
                 $result = 'some_data';
                 break;
             case 'customer_id':
                 $result = $customerId;
                 break;
         }
         return $result;
     };
     $addressModelMock->expects($this->any())->method('getData')->will($this->returnCallback($getData));
     $attributeMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Eav\\AttributeMetadata', array('getAttributeCode'), array(), '', false);
     $attributeMock->expects($this->once())->method('getAttributeCode')->will($this->returnValue($attributeCode));
     $addressMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Address', array(), array(), '', false);
     $this->addressMetadataServiceMock->expects($this->once())->method('getAllAttributesMetadata')->will($this->returnValue(array($attributeMock)));
     $this->addressBuilderMock->expects($this->once())->method('create')->will($this->returnValue($addressMock));
     $this->addressBuilderMock->expects($this->once())->method('setCustomerId')->with($this->equalTo($customerId));
     $this->assertEquals($addressMock, $this->model->createAddressFromModel($addressModelMock, $defaultBillingId, $defaultShippingId));
 }
Example #3
0
 public function setUp()
 {
     $this->_objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
     $this->_escaper = $this->getMock('Magento\\Framework\\Escaper', [], [], '', false);
     $context = $this->getMock('Magento\\Framework\\View\\Element\\Template\\Context', [], [], '', false);
     $context->expects($this->any())->method('getEscaper')->will($this->returnValue($this->_escaper));
     $addressHelper = $this->getMock('Magento\\Customer\\Helper\\Address', [], [], '', false);
     $this->_customerHelper = $this->getMock('Magento\\Customer\\Helper\\Data', [], [], '', false);
     $this->_attributeMetadata = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Eav\\AttributeMetadata', [], [], '', false);
     $this->customerMetadataService = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\CustomerMetadataService')->disableOriginalConstructor()->getMock();
     $this->customerMetadataService->expects($this->any())->method('getCustomAttributesMetadata')->will($this->returnValue([]));
     $this->customerMetadataService->expects($this->any())->method('getAttributeMetadata')->will($this->returnValue($this->_attributeMetadata));
     $this->addressMetadataService = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\AddressMetadataService')->disableOriginalConstructor()->getMock();
     $this->addressMetadataService->expects($this->any())->method('getAttributeMetadata')->will($this->returnValue($this->_attributeMetadata));
     $this->_block = new Name($context, $addressHelper, $this->customerMetadataService, $this->addressMetadataService, $this->_customerHelper);
 }
Example #4
0
 /**
  * @param $attrCode
  * @param $attrClass
  * @param $customAttrClass
  * @param $result
  * @dataProvider getAttributeValidationClassDataProvider
  */
 public function testGetAttributeValidationClass($attrCode, $attrClass, $customAttrClass, $result)
 {
     $attributeMock = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\Data\\Eav\\AttributeMetadata')->disableOriginalConstructor()->getMock();
     $attributeMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($attrClass));
     $customAttrMock = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\Data\\Eav\\AttributeMetadata')->disableOriginalConstructor()->getMock();
     $customAttrMock->expects($this->any())->method('isVisible')->will($this->returnValue(true));
     $customAttrMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($customAttrClass));
     $this->customerMetadataService->expects($this->any())->method('getAttributeMetadata')->will($this->returnValue($customAttrMock));
     $this->addressMetadataService->expects($this->any())->method('getAttributeMetadata')->will($this->returnValue($attributeMock));
     $this->assertEquals($result, $this->helper->getAttributeValidationClass($attrCode));
 }
Example #5
0
 /**
  * {@inheritdoc}
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function renderArray($addressAttributes, $format = null)
 {
     switch ($this->getType()->getCode()) {
         case 'html':
             $dataFormat = ElementFactory::OUTPUT_FORMAT_HTML;
             break;
         case 'pdf':
             $dataFormat = ElementFactory::OUTPUT_FORMAT_PDF;
             break;
         case 'oneline':
             $dataFormat = ElementFactory::OUTPUT_FORMAT_ONELINE;
             break;
         default:
             $dataFormat = ElementFactory::OUTPUT_FORMAT_TEXT;
             break;
     }
     $attributesMetadata = $this->_addressMetadataService->getAllAttributesMetadata();
     $data = array();
     foreach ($attributesMetadata as $attributeMetadata) {
         if (!$attributeMetadata->isVisible()) {
             continue;
         }
         $attributeCode = $attributeMetadata->getAttributeCode();
         if ($attributeCode == 'country_id' && isset($addressAttributes['country_id'])) {
             $data['country'] = $this->_countryFactory->create()->loadByCode($addressAttributes['country_id'])->getName();
         } elseif ($attributeCode == 'region' && isset($addressAttributes['region'])) {
             $data['region'] = __($addressAttributes['region']);
         } elseif (isset($addressAttributes[$attributeCode])) {
             $value = $addressAttributes[$attributeCode];
             $dataModel = $this->_elementFactory->create($attributeMetadata, $value, 'customer_address');
             $value = $dataModel->outputValue($dataFormat);
             if ($attributeMetadata->getFrontendInput() == 'multiline') {
                 $values = $dataModel->outputValue(ElementFactory::OUTPUT_FORMAT_ARRAY);
                 // explode lines
                 foreach ($values as $k => $v) {
                     $key = sprintf('%s%d', $attributeCode, $k + 1);
                     $data[$key] = $v;
                 }
             }
             $data[$attributeCode] = $value;
         }
     }
     if ($this->getType()->getEscapeHtml()) {
         foreach ($data as $key => $value) {
             $data[$key] = $this->escapeHtml($value);
         }
     }
     $format = !is_null($format) ? $format : $this->getFormatArray($addressAttributes);
     return $this->filterManager->template($format, array('variables' => $data));
 }
Example #6
0
 /**
  * Retrieve attributes metadata for the form
  *
  * @return \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata[]
  * @throws \LogicException For undefined entity type
  */
 public function getAttributes()
 {
     if (!isset($this->_attributes)) {
         if ($this->_entityType === CustomerMetadataServiceInterface::ENTITY_TYPE_CUSTOMER) {
             $this->_attributes = $this->_customerMetadataService->getAttributes($this->_formCode);
         } else {
             if ($this->_entityType === AddressMetadataServiceInterface::ENTITY_TYPE_ADDRESS) {
                 $this->_attributes = $this->_addressMetadataService->getAttributes($this->_formCode);
             } else {
                 throw new \LogicException('Undefined entity type: ' . $this->_entityType);
             }
         }
     }
     return $this->_attributes;
 }
Example #7
0
 /**
  * Get string with frontend validation classes for attribute
  *
  * @param string $attributeCode
  * @return string
  *
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function getAttributeValidationClass($attributeCode)
 {
     /** @var $attribute \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata */
     $attribute = isset($this->_attributes[$attributeCode]) ? $this->_attributes[$attributeCode] : $this->_addressMetadataService->getAttributeMetadata($attributeCode);
     $class = $attribute ? $attribute->getFrontendClass() : '';
     if (in_array($attributeCode, array('firstname', 'middlename', 'lastname', 'prefix', 'suffix', 'taxvat'))) {
         if ($class && !$attribute->isVisible()) {
             // address attribute is not visible thus its validation rules are not applied
             $class = '';
         }
         /** @var $customerAttribute \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata */
         $customerAttribute = $this->_customerMetadataService->getAttributeMetadata($attributeCode);
         $class .= $customerAttribute && $customerAttribute->isVisible() ? $customerAttribute->getFrontendClass() : '';
         $class = implode(' ', array_unique(array_filter(explode(' ', $class))));
     }
     return $class;
 }
Example #8
0
 /**
  * Retrieve customer or customer address attribute instance
  *
  * @param string $attributeCode
  * @return \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata|null
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _getAttribute($attributeCode)
 {
     if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof Customer) {
         return parent::_getAttribute($attributeCode);
     }
     try {
         $attribute = $this->_addressMetadataService->getAttributeMetadata($attributeCode);
     } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
         return null;
     }
     if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) {
         $customerAttribute = parent::_getAttribute($attributeCode);
         if ($customerAttribute && $customerAttribute->isRequired()) {
             $attribute = $customerAttribute;
         }
     }
     return $attribute;
 }
Example #9
0
 /**
  * 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->_addressMetadataService->getAllAttributesMetadata();
     $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;
 }
Example #10
0
 /**
  * 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->_addressMetadataService->getAllAttributesMetadata() 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;
 }