/** * @dataProvider attributeValueDataProvider */ public function testValidate($data) { $this->_attribute->expects($this->atLeastOnce())->method('getAttributeCode')->will($this->returnValue('code')); $product = new \Magento\Framework\DataObject(['code' => $data]); $this->_model->validate($product); $this->assertEquals('1,2,3', $product->getCode()); }
private function saveAttribute() { if ($this->attributeObj->getId()) { return array('result' => true, 'obj' => $this->attributeObj); } if (!$this->validate()) { return array('result' => false, 'error' => 'Attribute builder. Validation failed.'); } $this->attributeObj = $this->catalogAttributeFactory->create(); $data = $this->params; $data['attribute_code'] = $this->code; $data['frontend_label'] = array(\Magento\Store\Model\Store::DEFAULT_STORE_ID => $this->primaryLabel); $data['frontend_input'] = $this->inputType; $data['entity_type_id'] = $this->entityTypeId; $data['is_user_defined'] = 1; $data['source_model'] = $this->productHelper->getAttributeSourceModelByInputType($this->inputType); $data['backend_model'] = $this->productHelper->getAttributeBackendModelByInputType($this->inputType); $data['backend_type'] = $this->attributeObj->getBackendTypeByInput($this->inputType); !isset($data['is_global']) && ($data['is_global'] = self::SCOPE_STORE); !isset($data['is_configurable']) && ($data['is_configurable'] = 0); !isset($data['is_filterable']) && ($data['is_filterable'] = 0); !isset($data['is_filterable_in_search']) && ($data['is_filterable_in_search'] = 0); !isset($data['apply_to']) && ($data['apply_to'] = array()); $this->prepareOptions($data); $this->prepareDefault($data); $this->attributeObj->addData($data); try { $this->attributeObj->save(); } catch (\Exception $e) { return array('result' => false, 'error' => $e->getMessage()); } return array('result' => true, 'obj' => $this->attributeObj); }
protected function setUp() { $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false); $this->attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute', [], [], '', false); $this->attribute->expects($this->any())->method('getAttributeCode')->willReturn('media_gallery'); $this->resourceModel = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Product\\Gallery', [], [], '', false); $this->mediaGalleryReadHandler = $this->getMock('Magento\\Catalog\\Model\\Product\\Gallery\\ReadHandler', [], [], '', false); $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->subject = $objectManager->getObject('Magento\\ProductVideo\\Model\\Plugin\\Catalog\\Product\\Gallery\\ReadHandler', ['resourceModel' => $this->resourceModel]); }
/** * @param Attribute $attribute * @param int $optionId * @return null|string * @toso: add mapping/caching */ public function getOptionText($attribute, $optionId) { $result = null; /** @var \Magento\Eav\Api\Data\AttributeOptionInterface $option */ foreach ($attribute->getOptions() as $option) { if ($option->getValue() == $optionId) { $result = $option->getLabel(); } } return $result; }
/** * Add Join with option value for collection select * * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection * @param \Magento\Eav\Model\Entity\Attribute $attribute * @param \Zend_Db_Expr $valueExpr * @return $this */ public function addOptionValueToCollection($collection, $attribute, $valueExpr) { $adapter = $this->_getReadAdapter(); $attributeCode = $attribute->getAttributeCode(); $optionTable1 = $attributeCode . '_option_value_t1'; $optionTable2 = $attributeCode . '_option_value_t2'; $tableJoinCond1 = "{$optionTable1}.option_id={$valueExpr} AND {$optionTable1}.store_id=0"; $tableJoinCond2 = $adapter->quoteInto("{$optionTable2}.option_id={$valueExpr} AND {$optionTable2}.store_id=?", $collection->getStoreId()); $valueExpr = $adapter->getCheckSql("{$optionTable2}.value_id IS NULL", "{$optionTable1}.value", "{$optionTable2}.value"); $collection->getSelect()->joinLeft([$optionTable1 => $this->getTable('eav_attribute_option_value')], $tableJoinCond1, [])->joinLeft([$optionTable2 => $this->getTable('eav_attribute_option_value')], $tableJoinCond2, [$attributeCode => $valueExpr]); return $this; }
/** * Retrive options ids from a labels array. * * @param Attribute $attribute Attribute. * @param string[] $labels Labels * * @return integer[] */ private function getOptionIds(Attribute $attribute, $labels) { $optionIds = []; if (!is_array($labels)) { $labels = [$labels]; } $options = $attribute->getSource()->getAllOptions(); foreach ($labels as $label) { foreach ($options as $option) { if ($option['label'] == $label) { $optionIds[] = (int) $option['value']; } } } return $optionIds; }
public function setUp() { $this->productFactoryMock = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\ProductFactory', ['create'], [], '', false); $this->productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', [], [], '', false); $this->fileStorageDbMock = $this->getMock('\\Magento\\MediaStorage\\Helper\\File\\Storage\\Database', [], [], '', false); $this->jsonHelperMock = $this->getMock('\\Magento\\Framework\\Json\\Helper\\Data', [], [], '', false); $this->mediaConfigMock = $this->getMock('\\Magento\\Catalog\\Model\\Product\\Media\\Config', [], [], '', false); $this->filesystemMock = $this->getMock('\\Magento\\Framework\\Filesystem', [], [], '', false); $write = $this->getMock('\\Magento\\Framework\\Filesystem\\Directory\\Write', [], [], '', false); $this->filesystemMock->expects($this->any())->method('getDirectoryWrite')->with('media')->willReturn($write); $this->resourceEntryMediaGalleryMock = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Product\\Attribute\\Backend\\Media', [], [], '', false); $this->attributeMock = $this->getMock('\\Magento\\Eav\\Model\\Entity\\Attribute', [], [], '', false); $this->attributeMock->expects($this->any())->method('getAttributeCode')->willReturn('media_gallery'); $this->mediaBackendModel = $this->getMock('Magento\\Catalog\\Model\\Product\\Attribute\\Backend\\Media', [], [], '', false); $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->modelObject = $objectManager->getObject('\\Magento\\ProductVideo\\Model\\Plugin\\ExternalVideoEntryProcessor', ['productFactory' => $this->productFactoryMock, 'fileStorageDb' => $this->fileStorageDbMock, 'jsonHelper' => $this->jsonHelperMock, 'mediaConfig' => $this->mediaConfigMock, 'filesystem' => $this->filesystemMock, 'resourceEntryMediaGallery' => $this->resourceEntryMediaGalleryMock]); }
public function testGetPrefixes() { $entityTypeId = 3; /** @var \Magento\Eav\Model\Entity\Type|\PHPUnit_Framework_MockObject_MockObject $entityType */ $entityType = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Type')->disableOriginalConstructor()->getMock(); $entityType->expects($this->once())->method('getId')->will($this->returnValue($entityTypeId)); /** @var AbstractFrontend|\PHPUnit_Framework_MockObject_MockObject $frontend */ $frontend = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend\\AbstractFrontend')->setMethods(['getLabel'])->disableOriginalConstructor()->getMockForAbstractClass(); $frontend->expects($this->once())->method('getLabel')->will($this->returnValue('testLabel')); $this->attributeCollection->expects($this->once())->method('setEntityTypeFilter')->with($this->equalTo($entityTypeId)); $this->attributeCollection->expects($this->once())->method('setFrontendInputTypeFilter')->with($this->equalTo('media_image')); $this->attribute->expects($this->once())->method('getAttributeCode')->will($this->returnValue('attributeCode')); $this->attribute->expects($this->once())->method('getFrontend')->will($this->returnValue($frontend)); $this->attributeCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$this->attribute]))); $this->eavConfig->expects($this->any())->method('getEntityType')->with($this->equalTo(Product::ENTITY))->will($this->returnValue($entityType)); $this->assertEquals([['field' => 'attributeCode_', 'label' => 'testLabel']], $this->model->getPrefixes()); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetItemsIfFacetedDataIsEmpty() { $attributeCode = 'attributeCode'; $this->attribute->expects($this->atLeastOnce())->method('getAttributeCode')->willReturn($attributeCode); $this->attribute->expects($this->atLeastOnce())->method('getIsFilterable')->willReturn(0); $this->target->setAttributeModel($this->attribute); $this->fulltextCollection->expects($this->once())->method('getFacetedData')->willReturn([]); $this->itemDataBuilder->expects($this->once())->method('build')->willReturn([]); $this->assertEquals([], $this->target->getItems()); }
public function testApply() { $filter = '10-150'; $requestVar = 'test_request_var'; $this->target->setRequestVar($requestVar); $this->request->expects($this->exactly(1))->method('getParam')->will($this->returnCallback(function ($field) use($requestVar, $filter) { $this->assertTrue(in_array($field, [$requestVar, 'id'])); return $filter; })); $attributeCode = 'AttributeCode'; $this->attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->fulltextCollection->expects($this->once())->method('addFieldToFilter')->with($attributeCode)->will($this->returnSelf()); $this->target->apply($this->request); }
public function testGetItems() { $attributeCode = 'attributeCode'; $attributeValue = 'attributeValue'; $attributeLabel = 'attributeLabel'; $this->attribute->expects($this->once())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->target->setAttributeModel($this->attribute); $this->request->expects($this->once())->method('getParam')->with($attributeCode)->will($this->returnValue($attributeValue)); $this->frontend->expects($this->once())->method('getOption')->with($attributeValue)->will($this->returnValue($attributeLabel)); $filterItem = $this->createFilterItem(0, $attributeLabel, $attributeValue, 0); $this->state->expects($this->once())->method('addFilter')->with($filterItem)->will($this->returnSelf()); $expectedFilterItems = []; $result = $this->target->apply($this->request)->getItems(); $this->assertEquals($expectedFilterItems, $result); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetItemsWithoutApply() { $attributeCode = 'attributeCode'; $selectedOptions = [['label' => 'selectedOptionLabel1', 'value' => 'selectedOptionValue1', 'count' => 25], ['label' => 'selectedOptionLabel2', 'value' => 'selectedOptionValue2', 'count' => 13], ['label' => 'selectedOptionLabel3', 'value' => 'selectedOptionValue3', 'count' => 10]]; $facetedData = ['selectedOptionValue1' => ['count' => 10], 'selectedOptionValue2' => ['count' => 45], 'selectedOptionValue3' => ['count' => 50]]; $builtData = [['label' => $selectedOptions[0]['label'], 'value' => $selectedOptions[0]['value'], 'count' => $facetedData[$selectedOptions[0]['value']]['count']], ['label' => $selectedOptions[1]['label'], 'value' => $selectedOptions[1]['value'], 'count' => $facetedData[$selectedOptions[1]['value']]['count']], ['label' => $selectedOptions[2]['label'], 'value' => $selectedOptions[2]['value'], 'count' => $facetedData[$selectedOptions[2]['value']]['count']]]; $this->attribute->expects($this->exactly(2))->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->target->setAttributeModel($this->attribute); $this->frontend->expects($this->once())->method('getSelectOptions')->will($this->returnValue($selectedOptions)); $this->fulltextCollection->expects($this->once())->method('getFacetedData')->will($this->returnValue($facetedData)); $this->itemDataBuilder->expects($this->at(0))->method('addItemData')->with($selectedOptions[0]['label'], $selectedOptions[0]['value'], $facetedData[$selectedOptions[0]['value']]['count'])->will($this->returnSelf()); $this->itemDataBuilder->expects($this->at(1))->method('addItemData')->with($selectedOptions[1]['label'], $selectedOptions[1]['value'], $facetedData[$selectedOptions[1]['value']]['count'])->will($this->returnSelf()); $this->itemDataBuilder->expects($this->once())->method('build')->will($this->returnValue($builtData)); $this->fulltextCollection->expects($this->once())->method('getSize')->will($this->returnValue(50)); $expectedFilterItems = [$this->createFilterItem(0, $builtData[0]['label'], $builtData[0]['value'], $builtData[0]['count']), $this->createFilterItem(1, $builtData[1]['label'], $builtData[1]['value'], $builtData[1]['count']), $this->createFilterItem(2, $builtData[2]['label'], $builtData[2]['value'], $builtData[2]['count'])]; $result = $this->target->getItems(); $this->assertEquals($expectedFilterItems, $result); }
public function testGetItems() { $this->target->setAttributeModel($this->attribute); $attributeCode = 'attributeCode'; $this->attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->fulltextCollection->expects($this->once())->method('getFacetedData')->with($attributeCode)->will($this->returnValue([])); $this->target->getItems(); }
/** * Determine filter type for static attribute. * * @static * @param \Magento\Eav\Model\Entity\Attribute $attribute * @return string */ public static function getStaticAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute) { if (in_array($attribute->getAttributeCode(), ['category_ids', 'media_gallery'])) { return self::FILTER_TYPE_INPUT; } $columns = $attribute->getFlatColumns(); switch ($columns[$attribute->getAttributeCode()]['type']) { case \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER: case \Magento\Framework\DB\Ddl\Table::TYPE_BIGINT: $type = self::FILTER_TYPE_NUMBER; break; case \Magento\Framework\DB\Ddl\Table::TYPE_DATE: case \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME: case \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP: $type = self::FILTER_TYPE_DATE; break; default: $type = self::FILTER_TYPE_INPUT; } return $type; }
/** * Retrieve attribute field name * * * @param Attribute $attribute * @return string */ public function getAttributeFieldName($attribute) { $name = $attribute->getAttributeCode(); if ($suffix = $this->getForm()->getFieldNameSuffix()) { $name = $this->getForm()->addSuffixToName($name, $suffix); } return $name; }
/** * Return entities where attribute value is * * @param array|int $entityIdsFilter * @param \Magento\Eav\Model\Entity\Attribute $attribute * @param mixed $expectedValue * @return array */ public function findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValue) { $bind = array('attribute_id' => $attribute->getId(), 'value' => $expectedValue); $select = $this->_getReadAdapter()->select()->from($attribute->getBackend()->getTable(), array('entity_id'))->where('attribute_id = :attribute_id')->where('value = :value')->where('entity_id IN(?)', $entityIdsFilter); return $this->_getReadAdapter()->fetchCol($select, $bind); }
/** * Update attribute default value * * @param EntityAttribute|AbstractModel $object * @param int|string $optionId * @param int $intOptionId * @param array $defaultValue * @return void */ protected function _updateDefaultValue($object, $optionId, $intOptionId, &$defaultValue) { if (in_array($optionId, $object->getDefault())) { $frontendInput = $object->getFrontendInput(); if ($frontendInput === 'multiselect') { $defaultValue[] = $intOptionId; } elseif ($frontendInput === 'select') { $defaultValue = [$intOptionId]; } } }
/** * Build attribute representation * * @param \Magento\Eav\Model\Entity\Attribute $attribute * @return array */ public function map(\Magento\Eav\Model\Entity\Attribute $attribute) { $isUnassignable = !in_array($attribute->getAttributeCode(), $this->unassignableAttributes); return ['text' => $attribute->getAttributeCode(), 'id' => $attribute->getAttributeId(), 'cls' => $isUnassignable ? 'leaf' : 'system-leaf', 'allowDrop' => false, 'allowDrag' => true, 'leaf' => true, 'is_user_defined' => $attribute->getIsUserDefined(), 'is_unassignable' => $isUnassignable, 'entity_id' => $attribute->getEntityAttributeId()]; }
/** * Create filter fields for 'Filter' column. * * @param mixed $value * @param Attribute $row * @param \Magento\Framework\DataObject $column * @param boolean $isExport * @return string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function decorateFilter($value, Attribute $row, \Magento\Framework\DataObject $column, $isExport) { $value = null; $values = $column->getValues(); if (is_array($values) && isset($values[$row->getAttributeCode()])) { $value = $values[$row->getAttributeCode()]; } $code = $row->getAttributeCode(); if (isset($this->_filterTypeByAttrCode[$code])) { $filterType = $this->_filterTypeByAttrCode[$code]; } else { $filterType = \Magento\ImportExport\Model\Export::getAttributeFilterType($row); } switch ($filterType) { case \Magento\ImportExport\Model\Export::FILTER_TYPE_SELECT: $cell = $this->_getSelectHtmlWithValue($row, $value); break; case \Magento\ImportExport\Model\Export::FILTER_TYPE_INPUT: $cell = $this->_getInputHtmlWithValue($row, $value); break; case \Magento\ImportExport\Model\Export::FILTER_TYPE_DATE: $cell = $this->_getDateFromToHtmlWithValue($row, $value); break; case \Magento\ImportExport\Model\Export::FILTER_TYPE_NUMBER: $cell = $this->_getNumberFromToHtmlWithValue($row, $value); break; default: $cell = __('Unknown attribute filter type'); } return $cell; }
/** * @return array */ public function getMediaAttributeCodes() { return $this->attributeHelper->getAttributeCodesByFrontendType('media_image'); }
public function testGetItems() { $this->target->setAttributeModel($this->attribute); $attributeCode = 'attributeCode'; $this->attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->algorithm->expects($this->any())->method('getItemsData')->will($this->returnValue([])); $this->target->getItems(); }
/** * Add data about search criteria to object state * * @param EntityAttribute $attribute * @param mixed $value * @return $this */ protected function _addSearchCriteria($attribute, $value) { $name = $attribute->getStoreLabel(); if (is_array($value)) { if (isset($value['from']) && isset($value['to'])) { if (!empty($value['from']) || !empty($value['to'])) { if (isset($value['currency'])) { /** @var $currencyModel Currency */ $currencyModel = $this->_currencyFactory->create()->load($value['currency']); $from = $currencyModel->format($value['from'], array(), false); $to = $currencyModel->format($value['to'], array(), false); } else { $currencyModel = null; } if (strlen($value['from']) > 0 && strlen($value['to']) > 0) { // - $value = sprintf('%s - %s', $currencyModel ? $from : $value['from'], $currencyModel ? $to : $value['to']); } elseif (strlen($value['from']) > 0) { // and more $value = __('%1 and greater', $currencyModel ? $from : $value['from']); } elseif (strlen($value['to']) > 0) { // to $value = __('up to %1', $currencyModel ? $to : $value['to']); } } else { return $this; } } } if (($attribute->getFrontendInput() == 'select' || $attribute->getFrontendInput() == 'multiselect') && is_array($value)) { foreach ($value as $key => $val) { $value[$key] = $attribute->getSource()->getOptionText($val); if (is_array($value[$key])) { $value[$key] = $value[$key]['label']; } } $value = implode(', ', $value); } elseif ($attribute->getFrontendInput() == 'select' || $attribute->getFrontendInput() == 'multiselect') { $value = $attribute->getSource()->getOptionText($value); if (is_array($value)) { $value = $value['label']; } } elseif ($attribute->getFrontendInput() == 'boolean') { $value = $value == 1 ? __('Yes') : __('No'); } $this->_searchCriterias[] = array('name' => $name, 'value' => $value); return $this; }
/** * @param array|null $sortWeights * @param float $expected * @dataProvider getSortWeightDataProvider */ public function testGetSortWeight($sortWeights, $expected) { $setId = 123; $this->_model->setAttributeSetInfo([$setId => $sortWeights]); $this->assertEquals($expected, $this->_model->getSortWeight($setId)); }
/** * Set attribute model to filter * * @param \Magento\Eav\Model\Entity\Attribute $attribute * @return \Magento\Catalog\Model\Layer\Filter\AbstractFilter */ public function setAttributeModel($attribute) { $this->setRequestVar($attribute->getAttributeCode()); $this->setData('attribute_model', $attribute); return $this; }
/** * Processing object after save data * * @return \Magento\Framework\Model\AbstractModel */ public function afterSave() { /** * Fix saving attribute in admin */ $this->_eavConfig->clear(); return parent::afterSave(); }
/** * {@inheritdoc} */ public function afterDelete() { $this->_eavConfig->clear(); return parent::afterDelete(); }
/** * Compare attributes * * @param Attribute $firstAttribute * @param Attribute $secondAttribute * @return int */ public function attributesCompare($firstAttribute, $secondAttribute) { $firstSort = $firstAttribute->getSortWeight((int) $this->_sortingSetId); $secondSort = $secondAttribute->getSortWeight((int) $this->_sortingSetId); if ($firstSort > $secondSort) { return 1; } elseif ($firstSort < $secondSort) { return -1; } return 0; }
/** * Processing object after save data * * @return \Magento\Framework\Model\AbstractModel */ protected function _afterSave() { /** * Fix saving attribute in admin */ $this->_eavConfig->clear(); return parent::_afterSave(); }
/** * Return entities where attribute value is * * @param array|int $entityIdsFilter * @param \Magento\Eav\Model\Entity\Attribute $attribute * @param mixed $expectedValue * @return array */ public function findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValue) { $linkField = $this->getLinkField(); $bind = ['attribute_id' => $attribute->getId(), 'value' => $expectedValue]; $selectEntities = $this->getConnection()->select()->from(['ce' => $this->getTable('catalog_category_entity')], ['entity_id'])->joinLeft(['ci' => $attribute->getBackend()->getTable()], "ci.{$linkField} = ce.{$linkField} AND attribute_id = :attribute_id", ['value'])->where('ci.value = :value')->where('ce.entity_id IN (?)', $entityIdsFilter); return $this->getConnection()->fetchCol($selectEntities, $bind); }
public function setAttributeObj(\Magento\Eav\Model\Entity\Attribute $obj) { $this->attributeObj = $obj; $this->code = $obj->getAttributeCode(); return $this; }