/**
  * @param array $data
  * @param array $expected
  * @dataProvider buildDataProvider
  */
 public function testBuild($attributeInputType, $validateRules, $data, $expected)
 {
     $this->attributeMock->expects($this->once())->method('getFrontendInput')->willReturn($attributeInputType);
     $this->attributeMock->expects($this->any())->method('getValidateRules')->willReturn($validateRules);
     $validationRules = $this->subject->build($this->attributeMock, $data);
     $this->assertEquals($expected, $validationRules);
 }
Exemplo n.º 2
0
 /**
  * Get attributes meta
  *
  * @param Type $entityType
  * @return array
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function getAttributesMeta(Type $entityType)
 {
     $meta = [];
     $attributes = $entityType->getAttributeCollection();
     /* @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
     foreach ($attributes as $attribute) {
         $code = $attribute->getAttributeCode();
         // use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
         foreach ($this->metaProperties as $metaName => $origName) {
             $value = $attribute->getDataUsingMethod($origName);
             $meta[$code][$metaName] = $metaName === 'label' ? __($value) : $value;
             if ('frontend_input' === $origName) {
                 $meta[$code]['formElement'] = isset($this->formElement[$value]) ? $this->formElement[$value] : $value;
             }
             if ($attribute->usesSource()) {
                 $meta[$code]['options'] = $attribute->getSource()->getAllOptions();
             }
         }
         $rules = $this->eavValidationRules->build($attribute, $meta[$code]);
         if (!empty($rules)) {
             $meta[$code]['validation'] = $rules;
         }
     }
     return $meta;
 }
 /**
  * Get attributes meta
  *
  * @param Type $entityType
  * @return array
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function getAttributesMeta(Type $entityType)
 {
     $meta = [];
     $attributes = $entityType->getAttributeCollection();
     /* @var EavAttribute $attribute */
     foreach ($attributes as $attribute) {
         $code = $attribute->getAttributeCode();
         // use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
         foreach ($this->metaProperties as $metaName => $origName) {
             $value = $attribute->getDataUsingMethod($origName);
             $meta[$code][$metaName] = $value;
             if ('frontend_input' === $origName) {
                 $meta[$code]['formElement'] = isset($this->formElement[$value]) ? $this->formElement[$value] : $value;
             }
             if ($attribute->usesSource()) {
                 $meta[$code]['options'] = $attribute->getSource()->getAllOptions();
             }
         }
         $rules = $this->eavValidationRules->build($attribute, $meta[$code]);
         if (!empty($rules)) {
             $meta[$code]['validation'] = $rules;
         }
         $meta[$code]['scopeLabel'] = $this->getScopeLabel($attribute);
         $meta[$code]['componentType'] = Field::NAME;
     }
     $result = [];
     foreach ($meta as $key => $item) {
         $result[$key] = $item;
         $result[$key]['sortOrder'] = 0;
     }
     $result = $this->getDefaultMetaData($result);
     return $result;
 }
 /**
  * @return AbstractAttribute[]|\PHPUnit_Framework_MockObject_MockObject[]
  */
 protected function getAttributeMock()
 {
     $attributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute')->setMethods(['getAttributeCode', 'getDataUsingMethod', 'usesSource', 'getSource'])->disableOriginalConstructor()->getMockForAbstractClass();
     $sourceMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Source\\AbstractSource')->disableOriginalConstructor()->getMockForAbstractClass();
     $sourceMock->expects($this->any())->method('getAllOptions')->willReturn(self::OPTIONS_RESULT);
     $attributeMock->expects($this->once())->method('getAttributeCode')->willReturn(self::ATTRIBUTE_CODE);
     $attributeMock->expects($this->any())->method('getDataUsingMethod')->willReturnCallback(function ($origName) {
         return $origName;
     });
     $attributeMock->expects($this->any())->method('usesSource')->willReturn(true);
     $attributeMock->expects($this->any())->method('getSource')->willReturn($sourceMock);
     $this->eavValidationRulesMock->expects($this->any())->method('build')->with($attributeMock, $this->logicalNot($this->isEmpty()));
     return [$attributeMock];
 }
Exemplo n.º 5
0
 /**
  * Get attributes meta
  *
  * @param ProductAttributeInterface[] $attributes
  * @param string $groupCode
  * @return array
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function getAttributesMeta(array $attributes, $groupCode)
 {
     $meta = [];
     foreach ($attributes as $sortKey => $attribute) {
         if (in_array($attribute->getFrontendInput(), $this->bannedInputTypes)) {
             continue;
         }
         $code = $attribute->getAttributeCode();
         if (in_array($code, $this->attributesToEliminate)) {
             continue;
         }
         $child = $this->setupMetaProperties($attribute);
         $meta[static::CONTAINER_PREFIX . $code] = ['arguments' => ['data' => ['config' => ['formElement' => 'container', 'componentType' => 'container', 'breakLine' => false, 'label' => __('%1', $attribute->getDefaultFrontendLabel()), 'sortOrder' => $sortKey * self::SORT_ORDER_MULTIPLIER, 'required' => $attribute->getIsRequired(), 'scopeLabel' => $this->getScopeLabel($attribute)]]]];
         if ($attribute->getIsWysiwygEnabled()) {
             $meta[static::CONTAINER_PREFIX . $code]['arguments']['data']['config']['component'] = 'Magento_Ui/js/form/components/group';
         }
         $child['arguments']['data']['config']['code'] = $code;
         $child['arguments']['data']['config']['source'] = $groupCode;
         $child['arguments']['data']['config']['scopeLabel'] = $this->getScopeLabel($attribute);
         $child['arguments']['data']['config']['globalScope'] = $this->isScopeGlobal($attribute);
         $child['arguments']['data']['config']['sortOrder'] = $sortKey * self::SORT_ORDER_MULTIPLIER;
         if (!isset($child['arguments']['data']['config']['componentType'])) {
             $child['arguments']['data']['config']['componentType'] = Field::NAME;
         }
         if (in_array($code, $this->attributesToDisable)) {
             $child['arguments']['data']['config']['disabled'] = true;
         }
         // TODO: getAttributeModel() should not be used when MAGETWO-48284 is complete
         $childData = $child['arguments']['data']['config'];
         if ($rules = $this->eavValidationRules->build($this->getAttributeModel($attribute), $childData)) {
             $child['arguments']['data']['config']['validation'] = $rules;
         }
         $meta[static::CONTAINER_PREFIX . $code]['children'][$code] = $child;
     }
     return $meta;
 }