/** * @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); }
/** * Retrieve customer attribute instance * * @param string $attributeCode * @return \Magento\Customer\Api\Data\AttributeMetadataInterface|null */ protected function _getAttribute($attributeCode) { try { return $this->customerMetadata->getAttributeMetadata($attributeCode); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { return null; } }
protected function setUp() { $this->context = $this->getMockBuilder('Magento\\Framework\\App\\Helper\\Context')->disableOriginalConstructor()->getMock(); $this->customerMetadataService = $this->getMock('Magento\\Customer\\Api\\CustomerMetadataInterface'); $attributeMetadata = $this->getMock('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface'); $attributeMetadata->expects($this->any())->method('isVisible')->will($this->returnValue(true)); $this->customerMetadataService->expects($this->any())->method('getAttributeMetadata')->will($this->returnValue($attributeMetadata)); $this->object = new \Magento\Customer\Helper\View($this->context, $this->customerMetadataService); }
/** * @param \Magento\Customer\Api\Data\CustomerInterface $customerData * @param string $expectedCustomerName * @param bool $isPrefixAllowed * @param bool $isMiddleNameAllowed * @param bool $isSuffixAllowed * @dataProvider getCustomerNameDataProvider */ public function testGetCustomerName($customerData, $expectedCustomerName, $isPrefixAllowed = false, $isMiddleNameAllowed = false, $isSuffixAllowed = false) { $visibleAttribute = $this->getMock('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface'); $visibleAttribute->expects($this->any())->method('isVisible')->will($this->returnValue(true)); $invisibleAttribute = $this->getMock('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface'); $invisibleAttribute->expects($this->any())->method('isVisible')->will($this->returnValue(false)); $this->_customerMetadataService->expects($this->any())->method('getAttributeMetadata')->will($this->returnValueMap([['prefix', $isPrefixAllowed ? $visibleAttribute : $invisibleAttribute], ['middlename', $isMiddleNameAllowed ? $visibleAttribute : $invisibleAttribute], ['suffix', $isSuffixAllowed ? $visibleAttribute : $invisibleAttribute]])); $this->assertEquals($expectedCustomerName, $this->_helper->getCustomerName($customerData), 'Full customer name is invalid'); }
/** * Edit/View Existing Customer form fields * * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset * @return string[] Values to set on the form */ protected function _addEditCustomerFormFields($fieldset) { $fieldset->getForm()->getElement('created_in')->setDisabled('disabled'); $fieldset->getForm()->getElement('website_id')->setDisabled('disabled'); $customerData = $this->_getCustomerDataObject(); if ($customerData->getId() && $this->_accountManagement->isReadonly($customerData->getId())) { return []; } // Prepare customer confirmation control (only for existing customers) $confirmationStatus = $this->_accountManagement->getConfirmationStatus($customerData->getId()); $confirmationKey = $customerData->getConfirmation(); if ($confirmationStatus != AccountManagementInterface::ACCOUNT_CONFIRMED) { $confirmationAttr = $this->_customerMetadata->getAttributeMetadata('confirmation'); if (!$confirmationKey) { $confirmationKey = $this->_getRandomConfirmationKey(); } $element = $fieldset->addField('confirmation', 'select', ['name' => 'confirmation', 'label' => __($confirmationAttr->getFrontendLabel())]); $element->setEntityAttribute($confirmationAttr); $element->setValues(['' => 'Confirmed', $confirmationKey => 'Not confirmed']); // Prepare send welcome email checkbox if customer is not confirmed // no need to add it, if website ID is empty if ($customerData->getConfirmation() && $customerData->getWebsiteId()) { $fieldset->addField('sendemail', 'checkbox', ['name' => 'sendemail', 'label' => __('Send Welcome Email after Confirmation')]); return ['sendemail' => '1']; } } return []; }
/** * Get attribute metadata. * * @param string $attributeCode * @return \Magento\Customer\Api\Data\AttributeMetadataInterface|null */ private function getAttributeMetadata($attributeCode) { try { return $this->customerMetadata->getAttributeMetadata($attributeCode); } catch (NoSuchEntityException $e) { return null; } }
/** * Concatenate all customer name parts into full customer name. * * @param CustomerInterface $customerData * @return string */ public function getCustomerName(CustomerInterface $customerData) { $name = ''; $prefixMetadata = $this->_customerMetadataService->getAttributeMetadata('prefix'); if ($prefixMetadata->isVisible() && $customerData->getPrefix()) { $name .= $customerData->getPrefix() . ' '; } $name .= $customerData->getFirstname(); $middleNameMetadata = $this->_customerMetadataService->getAttributeMetadata('middlename'); if ($middleNameMetadata->isVisible() && $customerData->getMiddlename()) { $name .= ' ' . $customerData->getMiddlename(); } $name .= ' ' . $customerData->getLastname(); $suffixMetadata = $this->_customerMetadataService->getAttributeMetadata('suffix'); if ($suffixMetadata->isVisible() && $customerData->getSuffix()) { $name .= ' ' . $customerData->getSuffix(); } return $name; }
/** * @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 testGetAttributes() { $formAttributesMetadata = $this->_service->getAttributes('adminhtml_customer'); $this->assertCount(14, $formAttributesMetadata, "Invalid number of attributes for the specified form."); /** Check some fields of one attribute metadata */ $attributeMetadata = $formAttributesMetadata['firstname']; $this->assertInstanceOf('Magento\\Customer\\Model\\Data\\AttributeMetadata', $attributeMetadata); $this->assertEquals('firstname', $attributeMetadata->getAttributeCode(), 'Attribute code is invalid'); $this->assertNotEmpty($attributeMetadata->getValidationRules(), 'Validation rules are not set'); $this->assertEquals('1', $attributeMetadata->isSystem(), '"Is system" field value is invalid'); $this->assertEquals('40', $attributeMetadata->getSortOrder(), 'Sort order is invalid'); }
/** * @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute */ public function testGetWebsiteAttribute() { $websiteId = 1; $this->document->setData('website_id', $websiteId); $this->groupRepository->expects(static::never())->method('getById'); $this->customerMetadata->expects(static::never())->method('getAttributeMetadata'); $website = $this->getMockForAbstractClass(WebsiteInterface::class); $this->storeManager->expects(static::once())->method('getWebsites')->willReturn([$websiteId => $website]); $website->expects(static::once())->method('getName')->willReturn('Main Website'); $attribute = $this->document->getCustomAttribute('website_id'); static::assertEquals('Main Website', $attribute->getValue()); }
/** * 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; }
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()); }
/** * Update customer gender value * Method set gender label instead of id value * @return void */ private function setGenderValue() { $value = $this->getData(self::$genderAttributeCode); if (!$value) { $this->setCustomAttribute(self::$genderAttributeCode, 'N/A'); return; } try { $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode); $option = $attributeMetadata->getOptions()[$value]; $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel()); } catch (NoSuchEntityException $e) { $this->setCustomAttribute(self::$genderAttributeCode, 'N/A'); } }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetList() { $sortOrder = $this->getMock('Magento\\Framework\\Api\\SortOrder', [], [], '', false); $filterGroup = $this->getMock('Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false); $filter = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false); $collection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Customer\\Collection', [], [], '', false); $searchResults = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressSearchResultsInterface', [], '', false); $searchCriteria = $this->getMockForAbstractClass('Magento\\Framework\\Api\\SearchCriteriaInterface', [], '', false); $customerModel = $this->getMock('Magento\\Customer\\Model\\Customer', ['getId', 'setId', 'setStoreId', 'getStoreId', 'getAttributeSetId', 'setAttributeSetId', 'setRpToken', 'setRpTokenCreatedAt', 'getDataModel', 'setPasswordHash', 'getCollection'], [], 'customerModel', false); $metadata = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface', [], '', false); $this->searchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults); $searchResults->expects($this->once())->method('setSearchCriteria')->with($searchCriteria); $this->customerFactory->expects($this->once())->method('create')->willReturn($customerModel); $customerModel->expects($this->once())->method('getCollection')->willReturn($collection); $this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection, 'Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->customerMetadata->expects($this->once())->method('getAllAttributesMetadata')->willReturn([$metadata]); $metadata->expects($this->once())->method('getAttributeCode')->willReturn('attribute-code'); $collection->expects($this->once())->method('addAttributeToSelect')->with('attribute-code'); $collection->expects($this->once())->method('addNameToSelect'); $collection->expects($this->at(2))->method('joinAttribute')->with('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(3))->method('joinAttribute')->with('billing_city', 'customer_address/city', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(4))->method('joinAttribute')->with('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(5))->method('joinAttribute')->with('billing_region', 'customer_address/region', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(6))->method('joinAttribute')->with('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(7))->method('joinAttribute')->with('company', 'customer_address/company', 'default_billing', null, 'left')->willReturnSelf(); $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $collection->expects($this->once())->method('addFieldToFilter')->with([['attribute' => 'Field', 'eq' => 'Value']], []); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]); $filter->expects($this->once())->method('getConditionType')->willReturn(false); $filter->expects($this->once())->method('getField')->willReturn('Field'); $filter->expects($this->atLeastOnce())->method('getValue')->willReturn('Value'); $collection->expects($this->once())->method('addOrder')->with('Field', 'ASC'); $searchCriteria->expects($this->atLeastOnce())->method('getSortOrders')->willReturn([$sortOrder]); $sortOrder->expects($this->once())->method('getField')->willReturn('Field'); $sortOrder->expects($this->once())->method('getDirection')->willReturn(\Magento\Framework\Api\SortOrder::SORT_ASC); $searchCriteria->expects($this->once())->method('getCurrentPage')->willReturn(1); $collection->expects($this->once())->method('setCurPage')->with(1); $searchCriteria->expects($this->once())->method('getPageSize')->willReturn(10); $collection->expects($this->once())->method('setPageSize')->with(10); $collection->expects($this->once())->method('getSize')->willReturn(23); $searchResults->expects($this->once())->method('setTotalCount')->with(23); $collection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$customerModel])); $customerModel->expects($this->atLeastOnce())->method('getDataModel')->willReturn($this->customer); $searchResults->expects($this->once())->method('setItems')->with([$this->customer]); $this->assertSame($searchResults, $this->model->getList($searchCriteria)); }
/** * 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; }
/** * Return array of additional account data * Value is option style array * * @return array */ public function getCustomerAccountData() { $accountData = []; $entityType = 'customer'; /* @var \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute */ foreach ($this->metadata->getAllAttributesMetadata($entityType) as $attribute) { if (!$attribute->isVisible() || $attribute->isSystem()) { continue; } $orderKey = sprintf('customer_%s', $attribute->getAttributeCode()); $orderValue = $this->getOrder()->getData($orderKey); if ($orderValue != '') { $metadataElement = $this->_metadataElementFactory->create($attribute, $orderValue, $entityType); $value = $metadataElement->outputValue(AttributeDataFactory::OUTPUT_FORMAT_HTML); $sortOrder = $attribute->getSortOrder() + $attribute->isUserDefined() ? 200 : 0; $sortOrder = $this->_prepareAccountDataSortOrder($accountData, $sortOrder); $accountData[$sortOrder] = ['label' => $attribute->getFrontendLabel(), 'value' => $this->escapeHtml($value, ['br'])]; } } ksort($accountData, SORT_NUMERIC); return $accountData; }
/** * {@inheritdoc} */ public function getList(SearchCriteriaInterface $searchCriteria) { $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); /** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $collection */ $collection = $this->customerFactory->create()->getCollection(); $this->extensionAttributesJoinProcessor->process($collection, 'Magento\\Customer\\Api\\Data\\CustomerInterface'); // This is needed to make sure all the attributes are properly loaded foreach ($this->customerMetadata->getAllAttributesMetadata() as $metadata) { $collection->addAttributeToSelect($metadata->getAttributeCode()); } // Needed to enable filtering on name as a whole $collection->addNameToSelect(); // Needed to enable filtering based on billing address attributes $collection->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left'); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $searchResults->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders) { /** @var SortOrder $sortOrder */ foreach ($searchCriteria->getSortOrders() as $sortOrder) { $collection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $customers = []; /** @var \Magento\Customer\Model\Customer $customerModel */ foreach ($collection as $customerModel) { $customers[] = $customerModel->getDataModel(); } $searchResults->setItems($customers); return $searchResults; }
public function testGetMaxDateRangeWithException() { $this->customerMetadata->expects($this->any())->method('getAttributeMetadata')->will($this->throwException(new NoSuchEntityException(__(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => 'field', 'fieldValue' => 'value'])))); $this->assertNull($this->_block->getMaxDateRange()); }
public function testIsRequiredWithException() { $this->customerMetadata->expects($this->any())->method('getAttributeMetadata')->will($this->throwException(new NoSuchEntityException(__(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => 'field', 'fieldValue' => 'value'])))); $this->assertSame(false, $this->_block->isRequired()); }