/**
  * @return array
  */
 public function getList()
 {
     if (!$this->attributes) {
         $this->attributes = $this->getListForEntity($this->customerMetadata->getAllAttributesMetadata(), CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $this->customerMetadataManagement);
         $this->attributes = array_merge($this->attributes, $this->getListForEntity($this->addressMetadata->getAllAttributesMetadata(), AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $this->addressMetadataManagement));
     }
     return $this->attributeFilter->filter($this->attributes);
 }
Exemplo n.º 2
0
 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\\Model\\Data\\AttributeMetadata', $attributeMetadata);
     $this->assertEquals('company', $attributeMetadata->getAttributeCode(), 'Attribute code is invalid');
     $this->assertNotEmpty($attributeMetadata->getValidationRules(), 'Validation rules are not set');
     $this->assertEquals('static', $attributeMetadata->getBackendType(), 'Backend type is invalid');
     $this->assertEquals('Company', $attributeMetadata->getFrontendLabel(), 'Frontend label is invalid');
 }
Exemplo n.º 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->_options = $this->getMock('Magento\\Customer\\Model\\Options', [], [], '', false);
     $this->attribute = $this->getMockBuilder('\\Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->getMockForAbstractClass();
     $this->customerMetadata = $this->getMockBuilder('\\Magento\\Customer\\Api\\CustomerMetadataInterface')->getMockForAbstractClass();
     $this->customerMetadata->expects($this->any())->method('getAttributeMetadata')->will($this->returnValue($this->attribute));
     $this->customerMetadata->expects($this->any())->method('getCustomAttributesMetadata')->will($this->returnValue([]));
     $this->addressMetadata = $this->getMockBuilder('\\Magento\\Customer\\Api\\AddressMetadataInterface')->getMockForAbstractClass();
     $this->addressMetadata->expects($this->any())->method('getAttributeMetadata')->will($this->returnValue($this->attribute));
     $this->_block = new Name($context, $addressHelper, $this->customerMetadata, $this->_options, $this->addressMetadata);
 }
Exemplo n.º 4
0
 /**
  * @param $attrCode
  * @param $attrClass
  * @param $customAttrClass
  * @param $result
  * @dataProvider getAttributeValidationClassDataProvider
  */
 public function testGetAttributeValidationClass($attrCode, $attrClass, $customAttrClass, $result)
 {
     $attributeMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->getMock();
     $attributeMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($attrClass));
     $customAttrMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->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));
 }
 public function testGetList()
 {
     $attributeCode = 'attribute_code';
     $billingPrefix = 'billing_';
     $this->customerMetadata->expects($this->once())->method('getAllAttributesMetadata')->willReturn([]);
     $this->addressMetadata->expects($this->once())->method('getAllAttributesMetadata')->willReturn([$this->attribute]);
     $this->addressMetadataManagement->expects($this->once())->method('canBeFilterableInGrid')->with($this->attribute)->willReturn(true);
     $this->addressMetadataManagement->expects($this->once())->method('canBeSearchableInGrid')->with($this->attribute)->willReturn(true);
     $this->attribute->expects($this->atLeastOnce())->method('getAttributeCode')->willReturn($attributeCode);
     $this->attribute->expects($this->once())->method('getFrontendInput')->willReturn('frontend-input');
     $this->attribute->expects($this->once())->method('getFrontendLabel')->willReturn('frontend-label');
     $this->attribute->expects($this->once())->method('getBackendType')->willReturn('backend-type');
     $this->attribute->expects($this->once())->method('getOptions')->willReturn([$this->option]);
     $this->attribute->expects($this->once())->method('getIsUsedInGrid')->willReturn(true);
     $this->attribute->expects($this->once())->method('getIsVisibleInGrid')->willReturn(true);
     $this->attribute->expects($this->once())->method('getValidationRules')->willReturn([]);
     $this->attribute->expects($this->once())->method('isRequired')->willReturn(false);
     $this->option->expects($this->once())->method('getLabel')->willReturn('Label');
     $this->option->expects($this->once())->method('getValue')->willReturn('Value');
     $this->attributeFilter->expects($this->once())->method('filter')->willReturnArgument(0);
     $this->assertEquals([$billingPrefix . $attributeCode => ['attribute_code' => 'billing_attribute_code', 'frontend_input' => 'frontend-input', 'frontend_label' => 'frontend-label', 'backend_type' => 'backend-type', 'options' => [['label' => 'Label', 'value' => 'Value']], 'is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => true, 'validation_rules' => [], 'required' => false, 'entity_type_code' => 'customer_address']], $this->component->getList());
 }
Exemplo n.º 6
0
 /**
  * Retrieve attributes metadata for the form
  *
  * @return \Magento\Customer\Api\Data\AttributeMetadataInterface[]
  * @throws \LogicException For undefined entity type
  */
 public function getAttributes()
 {
     if (!isset($this->_attributes)) {
         if ($this->_entityType === \Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) {
             $this->_attributes = $this->_customerMetadataService->getAttributes($this->_formCode);
         } elseif ($this->_entityType === \Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS) {
             $this->_attributes = $this->_addressMetadataService->getAttributes($this->_formCode);
         } else {
             throw new \LogicException('Undefined entity type: ' . $this->_entityType);
         }
     }
     return $this->_attributes;
 }
Exemplo n.º 7
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 = [];
     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 = $format !== null ? $format : $this->getFormatArray($addressAttributes);
     return $this->filterManager->template($format, ['variables' => $data]);
 }
Exemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 protected function _getAttribute($attributeCode)
 {
     if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
         return parent::_getAttribute($attributeCode);
     }
     try {
         $attribute = $this->addressMetadata->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;
 }
Exemplo n.º 9
0
 /**
  * Get string with frontend validation classes for attribute
  *
  * @param string $attributeCode
  * @return string
  *
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function getAttributeValidationClass($attributeCode)
 {
     $class = '';
     try {
         /** @var $attribute AttributeMetadataInterface */
         $attribute = isset($this->_attributes[$attributeCode]) ? $this->_attributes[$attributeCode] : $this->_addressMetadataService->getAttributeMetadata($attributeCode);
         $class = $attribute ? $attribute->getFrontendClass() : '';
         if (in_array($attributeCode, ['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 AttributeMetadataInterface */
             $customerAttribute = $this->_customerMetadataService->getAttributeMetadata($attributeCode);
             $class .= $customerAttribute && $customerAttribute->isVisible() ? $customerAttribute->getFrontendClass() : '';
             $class = implode(' ', array_unique(array_filter(explode(' ', $class))));
         }
     } catch (NoSuchEntityException $e) {
         // the attribute does not exist so just return an empty string
     }
     return $class;
 }
Exemplo n.º 10
0
 /**
  * Create address data object based on current address model.
  *
  * @param int|null $defaultBillingAddressId
  * @param int|null $defaultShippingAddressId
  * @return AddressInterface
  * @deprecated Use Api/Data/AddressInterface as a result of service operations. Don't rely on the model to provide
  * the instance of Api/Data/AddressInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function getDataModel($defaultBillingAddressId = null, $defaultShippingAddressId = null)
 {
     $addressId = $this->getId();
     $attributes = $this->metadataService->getAllAttributesMetadata();
     $addressData = [];
     foreach ($attributes as $attribute) {
         $code = $attribute->getAttributeCode();
         if ($this->getData($code) !== null) {
             if ($code === AddressInterface::STREET) {
                 $addressData[$code] = $this->getDataUsingMethod($code);
             } else {
                 $addressData[$code] = $this->getData($code);
             }
         }
     }
     /** @var RegionInterface $region */
     $region = $this->regionDataFactory->create();
     $region->setRegion($this->getRegion())->setRegionCode($this->getRegionCode())->setRegionId($this->getRegionId());
     $addressData[AddressData::REGION] = $region;
     $addressDataObject = $this->addressDataFactory->create();
     $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface');
     if ($addressId) {
         $addressDataObject->setId($addressId);
     }
     if ($this->getCustomerId() || $this->getParentId()) {
         $customerId = $this->getCustomerId() ?: $this->getParentId();
         $addressDataObject->setCustomerId($customerId);
         if ($defaultBillingAddressId == $addressId) {
             $addressDataObject->setIsDefaultBilling(true);
         }
         if ($defaultShippingAddressId == $addressId) {
             $addressDataObject->setIsDefaultShipping(true);
         }
     }
     return $addressDataObject;
 }
Exemplo n.º 11
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', ['legend' => __("Edit Customer's Address")]);
     $account = $customerData['account'];
     $address = $this->addressDataFactory->create();
     if (!empty($account) && isset($account['store_id'])) {
         $address->setCountryId($this->_directoryHelper->getDefaultCountry($this->_storeManager->getStore($account['store_id'])));
     } else {
         $address->setCountryId($this->_directoryHelper->getDefaultCountry());
     }
     $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', $this->addressMapper->toFlatArray($address));
     $attributes = $addressForm->getAttributes();
     if (isset($attributes['street'])) {
         if ($attributes['street']->getMultilineCount() <= 0) {
             $attributes['street']->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT);
         }
     }
     foreach ($attributes as $key => $attribute) {
         $attributes[$key]->setFrontendLabel(__($attribute->getFrontendLabel()))->setIsVisible(false);
     }
     $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');
     }
     $postcode = $form->getElement('postcode');
     if ($postcode) {
         $postcode->removeClass('required-entry')->setRequired(!$this->_directoryHelper->isZipCodeOptional($address->getCountryId()));
     }
     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->options->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->options->getNameSuffixOptions($customerStoreId);
         if (!empty($suffixOptions)) {
             $fieldset->removeField($suffixElement->getId());
             $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId());
             $suffixField->setValues($suffixOptions);
         }
     }
     $customerDataObject = $this->customerDataFactory->create();
     $this->dataObjectHelper->populateWithArray($customerDataObject, $account, '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
     $this->assign('customer', $customerDataObject);
     $addressCollection = [];
     foreach ($customerData['address'] as $key => $addressData) {
         $addressDataObject = $this->addressDataFactory->create();
         $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface');
         $addressCollection[$key] = $addressDataObject;
     }
     $this->assign('addressCollection', $addressCollection);
     $form->setValues($this->addressMapper->toFlatArray($address));
     $this->setForm($form);
     return $this;
 }