/** * @param ScopeInterface $scope * @param AbstractAttribute $attribute * @param bool $useDefault * @return string */ protected function getScopeValue(ScopeInterface $scope, AbstractAttribute $attribute, $useDefault = false) { if ($attribute instanceof CatalogEavAttribute) { $useDefault = $useDefault || $attribute->isScopeGlobal(); } return parent::getScopeValue($scope, $attribute, $useDefault); }
public function testBeforeSave() { $this->attributeMock->expects($this->any())->method('getName')->willReturn('attribute_name'); $object = new DataObject(['use_config_attribute_name' => true]); $this->model->beforeSave($object); $this->assertEquals(BooleanSource::VALUE_USE_CONFIG, $object->getData('attribute_name')); }
/** * @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); }
/** * @param \Magento\Catalog\Model\Product $product * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return array */ protected function getMediaEntriesDataCollection(\Magento\Catalog\Model\Product $product, \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { $attributeCode = $attribute->getAttributeCode(); $mediaData = $product->getData($attributeCode); if (!empty($mediaData['images']) && is_array($mediaData['images'])) { return $mediaData['images']; } return []; }
public function testAddValueSortToCollectionNotGlobal() { $this->attributeModel->expects($this->any())->method('isScopeGlobal')->will($this->returnValue(false)); $this->collection->expects($this->once())->method('order')->with('check_sql asc')->will($this->returnSelf()); $this->collection->expects($this->once())->method('getStoreId')->will($this->returnValue(1)); $this->collection->expects($this->any())->method('getConnection')->will($this->returnSelf()); $this->collection->expects($this->any())->method('getCheckSql')->will($this->returnValue('check_sql')); $this->status->setAttribute($this->attributeModel); $this->status->addValueSortToCollection($this->collection); }
/** * @param string $value to assign to boolean * @param bool $expected text output * @param string $countryId * @param bool $isOptional * * @dataProvider validateValueDataProvider */ public function testValidateValue($value, $expected, $countryId, $isOptional) { $storeLabel = 'Zip/Postal Code'; $this->attributeMock->expects($this->once())->method('getStoreLabel')->willReturn($storeLabel); $this->directoryHelperMock->expects($this->once())->method('isZipCodeOptional')->willReturnMap([[$countryId, $isOptional]]); $object = new \Magento\Customer\Model\Attribute\Data\Postcode($this->localeMock, $this->loggerMock, $this->localeResolverMock, $this->directoryHelperMock); $object->setAttribute($this->attributeMock); $object->setExtractedData(['country_id' => $countryId]); $actual = $object->validateValue($value); $this->assertEquals($expected, $actual); }
/** * @covers \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker::lock */ public function testLock() { $lockedFields = ['is_searchable' => 'is_searchable', 'is_filterable' => 'is_filterable']; $this->attributeMock->expects($this->once())->method('getId')->willReturn(1); $this->attributeConfigMock->expects($this->once())->method('getLockedFields')->willReturn($lockedFields); $elementMock = $this->getMockBuilder('\\Magento\\Framework\\Data\\Form\\Element\\AbstractElement')->setMethods(['setDisabled', 'setReadonly'])->disableOriginalConstructor()->getMockForAbstractClass(); $elementMock->expects($this->exactly(2))->method('setDisabled'); $elementMock->expects($this->exactly(2))->method('setReadonly'); $this->formMock->expects($this->exactly(2))->method('getElement')->willReturn($elementMock); $this->object->lock($this->formMock); }
/** * Delete product data * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return $this */ public function deleteProductData($product, $attribute) { $where = array('entity_id = ?' => (int) $product->getId(), 'attribute_id = ?' => (int) $attribute->getId()); $adapter = $this->_getWriteAdapter(); if (!$attribute->isScopeGlobal()) { $storeId = $product->getStoreId(); if ($storeId) { $where['website_id IN(?)'] = array(0, $this->_storeManager->getStore($storeId)->getWebsiteId()); } } $adapter->delete($this->getMainTable(), $where); return $this; }
/** * Delete product data * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return $this */ public function deleteProductData($product, $attribute) { $where = ['entity_id = ?' => (int) $product->getId(), 'attribute_id = ?' => (int) $attribute->getId()]; $connection = $this->getConnection(); if (!$attribute->isScopeGlobal()) { $storeId = $product->getStoreId(); if ($storeId) { $where['website_id IN(?)'] = [0, $this->_storeManager->getStore($storeId)->getWebsiteId()]; } } $connection->delete($this->getMainTable(), $where); return $this; }
/** * @param bool|string $objData * @dataProvider beforeSaveProvider */ public function testBeforeSave($objData) { $attributeName = 'attr'; $this->attribute->expects($this->once())->method('getName')->will($this->returnValue($attributeName)); /** @var \Magento\Catalog\Model\Product\Url|\PHPUnit_Framework_MockObject_MockObject $object */ $object = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Url')->setMethods(['getName', 'getData', 'setData', 'formatUrlKey'])->disableOriginalConstructor()->getMock(); $object->expects($this->once())->method('getData')->with($this->equalTo($attributeName))->will($this->returnValue($objData)); $object->expects($this->any())->method('getName')->will($this->returnValue('testData')); $object->expects($this->any())->method('setData')->with($this->equalTo($attributeName), $this->logicalOr($this->equalTo('testData'), $this->equalTo('someData'))); $object->expects($this->any())->method('formatUrlKey')->with($this->logicalOr($this->equalTo('testData'), $this->equalTo('someData')))->will($this->returnCallback(function ($data) { return $data; })); $this->model->beforeSave($object); }
public function testGetFlatColumns() { $code = 'attribute-code'; $this->attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($code)); $columns = $this->model->getFlatColumns(); $this->assertInternalType('array', $columns); $this->assertArrayHasKey($code, $columns); foreach ($columns as $column) { $this->assertArrayHasKey('unsigned', $column); $this->assertArrayHasKey('default', $column); $this->assertArrayHasKey('extra', $column); $this->assertArrayHasKey('type', $column); $this->assertArrayHasKey('nullable', $column); $this->assertArrayHasKey('comment', $column); } }
/** * @param $attributeCode * @param $data * @dataProvider validateDefaultSortException * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testValidateDefaultSortException($attributeCode, $data) { $this->_attribute->expects($this->any())->method('getName')->will($this->returnValue($attributeCode)); $this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue('another value')); $object = new \Magento\Framework\DataObject($data); $this->_model->validate($object); }
/** * Save image information to DB. * * @param \Magento\Catalog\Model\Product $product * @param array $images * @return void */ protected function storeImage($product, $images) { $baseImage = ''; $i = 1; $mediaAttribute = $this->eavConfig->getAttribute('catalog_product', 'media_gallery'); foreach ($images as $image) { if (empty($image)) { $this->errors[] = $product->getSku(); continue; } if (strpos($image, '_main') !== false) { $baseImage = $image; } $id = $this->galleryResource->insertGallery(['attribute_id' => $mediaAttribute->getAttributeId(), 'entity_id' => $product->getId(), 'value' => $image]); $this->galleryResource->insertGalleryValueInStore(['value_id' => $id, 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, 'entity_id' => $product->getId(), 'label' => 'Image', 'position' => $i, 'disables' => 0]); $this->galleryResource->bindValueToEntity($id, $product->getId()); $i++; } if (empty($baseImage)) { $baseImage = $images[0]; } if ($baseImage) { $imageAttribute = $product->getResource()->getAttribute('image'); $smallImageAttribute = $product->getResource()->getAttribute('small_image'); $thumbnailAttribute = $product->getResource()->getAttribute('thumbnail'); $adapter = $product->getResource()->getConnection(); foreach ([$imageAttribute, $smallImageAttribute, $thumbnailAttribute] as $attribute) { $table = $imageAttribute->getBackend()->getTable(); /** @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter*/ $data = [$attribute->getBackend()->getEntityIdField() => $product->getId(), 'attribute_id' => $attribute->getId(), 'value' => $baseImage]; $adapter->insertOnDuplicate($table, $data, ['value']); } } }
/** * Insert or Update attribute data * * @param \Magento\Catalog\Model\AbstractModel $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _saveAttributeValue($object, $attribute, $value) { $connection = $this->getConnection(); $storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId(); $table = $attribute->getBackend()->getTable(); $entityId = $this->resolveEntityId($object->getId(), $table); /** * If we work in single store mode all values should be saved just * for default store id * In this case we clear all not default values */ if ($this->_storeManager->hasSingleStore()) { $storeId = $this->getDefaultStoreId(); $connection->delete($table, ['attribute_id = ?' => $attribute->getAttributeId(), $this->getLinkField() . ' = ?' => $entityId, 'store_id <> ?' => $storeId]); } $data = new \Magento\Framework\DataObject(['attribute_id' => $attribute->getAttributeId(), 'store_id' => $storeId, $this->getLinkField() => $entityId, 'value' => $this->_prepareValueForSave($value, $attribute)]); $bind = $this->_prepareDataForTable($data, $table); if ($attribute->isScopeStore()) { /** * Update attribute value for store */ $this->_attributeValuesToSave[$table][] = $bind; } elseif ($attribute->isScopeWebsite() && $storeId != $this->getDefaultStoreId()) { /** * Update attribute value for website */ $storeIds = $this->_storeManager->getStore($storeId)->getWebsite()->getStoreIds(true); foreach ($storeIds as $storeId) { $bind['store_id'] = (int) $storeId; $this->_attributeValuesToSave[$table][] = $bind; } } else { /** * Update global attribute value */ $bind['store_id'] = $this->getDefaultStoreId(); $this->_attributeValuesToSave[$table][] = $bind; } return $this; }
/** * Build validation rules * * @param AbstractAttribute $attribute * @param array $data * @return array */ public function build(AbstractAttribute $attribute, array $data) { $validation = []; if (isset($data['required']) && $data['required'] == 1) { $validation = array_merge($validation, ['required-entry' => true]); } if ($attribute->getFrontendInput() === 'price') { $validation = array_merge($validation, ['validate-zero-or-greater' => true]); } if ($attribute->getValidateRules()) { $validation = array_merge($validation, $attribute->getValidateRules()); } $rules = []; foreach ($validation as $type => $ruleName) { $rule = [$type => $ruleName]; if ($type === 'input_validation') { $rule = isset($this->validationRules[$ruleName]) ? $this->validationRules[$ruleName] : []; } $rules = array_merge($rules, $rule); } return $rules; }
/** * Build validation rules * * @param AbstractAttribute $attribute * @param array $data * @return array */ public function build(AbstractAttribute $attribute, array $data) { $rules = []; if (isset($data['required']) && $data['required'] == 1) { $rules['required-entry'] = true; } $validation = $attribute->getValidateRules(); if (!empty($validation)) { foreach ($validation as $type => $ruleName) { switch ($type) { case 'input_validation': if (isset($this->validationRul[$type][$ruleName])) { $rules = array_merge($rules, $this->validationRul[$type][$ruleName]); } break; case 'min_text_length': case 'max_text_length': $rules = array_merge($rules, [$type => $ruleName]); break; } } } return $rules; }
/** * Set up test * * @return void */ protected function setUp() { $this->resource = $this->getMock('Magento\\Framework\\App\\Resource', ['getConnection', 'getTableName'], [], '', false); $this->ruleCollectionFactory = $this->getMock('Magento\\CatalogRule\\Model\\Resource\\Rule\\CollectionFactory', ['create', 'addFieldToFilter'], [], '', false); $this->backend = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend', [], [], '', false); $this->select = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $this->connection = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface'); $this->db = $this->getMock('Zend_Db_Statement_Interface', [], [], '', false); $this->website = $this->getMock('Magento\\Store\\Model\\Website', [], [], '', false); $this->storeManager = $this->getMock('Magento\\Store\\Model\\StoreManagerInterface', [], [], '', false); $this->combine = $this->getMock('Magento\\Rule\\Model\\Condition\\Combine', [], [], '', false); $this->rules = $this->getMock('Magento\\CatalogRule\\Model\\Rule', [], [], '', false); $this->logger = $this->getMock('Psr\\Log\\LoggerInterface', [], [], '', false); $this->attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', [], [], '', false); $this->priceCurrency = $this->getMock('Magento\\Framework\\Pricing\\PriceCurrencyInterface'); $this->dateFormat = $this->getMock('Magento\\Framework\\Stdlib\\DateTime', [], [], '', false); $this->dateTime = $this->getMock('Magento\\Framework\\Stdlib\\DateTime\\DateTime', [], [], '', false); $this->eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', [], [], '', false); $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false); $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false); $this->connection->expects($this->any())->method('select')->will($this->returnValue($this->select)); $this->connection->expects($this->any())->method('query')->will($this->returnValue($this->db)); $this->select->expects($this->any())->method('distinct')->will($this->returnSelf()); $this->select->expects($this->any())->method('where')->will($this->returnSelf()); $this->select->expects($this->any())->method('from')->will($this->returnSelf()); $this->select->expects($this->any())->method('order')->will($this->returnSelf()); $this->resource->expects($this->any())->method('getConnection')->will($this->returnValue($this->connection)); $this->resource->expects($this->any())->method('getTableName')->will($this->returnArgument(0)); $this->storeManager->expects($this->any())->method('getWebsites')->will($this->returnValue([$this->website])); $this->storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($this->website)); $this->rules->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->rules->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1])); $this->rules->expects($this->any())->method('getConditions')->will($this->returnValue($this->combine)); $this->rules->expects($this->any())->method('getCustomerGroupIds')->will($this->returnValue([1])); $this->ruleCollectionFactory->expects($this->any())->method('create')->will($this->returnSelf()); $this->ruleCollectionFactory->expects($this->any())->method('addFieldToFilter')->will($this->returnValue([$this->rules])); $this->product->expects($this->any())->method('load')->will($this->returnSelf()); $this->product->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->product->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1])); $this->combine->expects($this->any())->method('validate')->will($this->returnValue(true)); $this->attribute->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend)); $this->eavConfig->expects($this->any())->method('getAttribute')->will($this->returnValue($this->attribute)); $this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product)); $this->indexBuilder = new \Magento\CatalogRule\Model\Indexer\IndexBuilder($this->ruleCollectionFactory, $this->priceCurrency, $this->resource, $this->storeManager, $this->logger, $this->eavConfig, $this->dateFormat, $this->dateTime, $this->productFactory); }
/** * Retrieve Select for update Flat data * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param int $store * @param bool $hasValueField flag which require option value * @return \Magento\Framework\DB\Select */ public function getFlatUpdateSelect(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute, $store, $hasValueField = true) { $adapter = $this->_getReadAdapter(); $attributeTable = $attribute->getBackend()->getTable(); $attributeCode = $attribute->getAttributeCode(); $joinConditionTemplate = "%s.entity_id = %s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d"; $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', \Magento\Store\Model\Store::DEFAULT_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= ' AND e.child_id = t1.entity_id'; } $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); /** @var $select \Magento\Framework\DB\Select */ $select = $adapter->select()->joinLeft(['t1' => $attributeTable], $joinCondition, [])->joinLeft(['t2' => $attributeTable], sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $store), [$attributeCode => $valueExpr]); if ($attribute->getFrontend()->getInputType() != 'multiselect' && $hasValueField) { $valueIdExpr = $adapter->getCheckSql('to2.value_id > 0', 'to2.value', 'to1.value'); $select->joinLeft(['to1' => $this->getTable('eav_attribute_option_value')], "to1.option_id = {$valueExpr} AND to1.store_id = 0", [])->joinLeft(['to2' => $this->getTable('eav_attribute_option_value')], $adapter->quoteInto("to2.option_id = {$valueExpr} AND to2.store_id = ?", $store), [$attributeCode . '_value' => $valueIdExpr]); } if ($attribute->getFlatAddChildData()) { $select->where('e.is_child = 0'); } return $select; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @return void */ protected function setUp() { $this->zendDbMock = $this->getMockBuilder('Zend_Db_Statement_Interface')->getMock(); $this->zendDbMock->expects($this->any())->method('fetchColumn')->willReturn([]); $this->selectMock = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->disableOriginalConstructor()->setMethods(['from', 'where', 'joinInner', 'joinLeft', 'having', 'useStraightJoin', 'insertFromSelect', '__toString'])->getMock(); $this->selectMock->expects($this->any())->method('from')->willReturnSelf(); $this->selectMock->expects($this->any())->method('where')->willReturnSelf(); $this->selectMock->expects($this->any())->method('joinInner')->willReturnSelf(); $this->selectMock->expects($this->any())->method('joinLeft')->willReturnSelf(); $this->selectMock->expects($this->any())->method('having')->willReturnSelf(); $this->selectMock->expects($this->any())->method('useStraightJoin')->willReturnSelf(); $this->selectMock->expects($this->any())->method('insertFromSelect')->willReturnSelf(); $this->selectMock->expects($this->any())->method('__toString')->willReturn('string'); $this->connectionMock = $this->getMockBuilder('Magento\\Framework\\DB\\Adapter\\AdapterInterface')->getMock(); $this->connectionMock->expects($this->any())->method('select')->willReturn($this->selectMock); $this->connectionMock->expects($this->any())->method('query')->willReturn($this->zendDbMock); $this->resourceMock = $this->getMockBuilder('Magento\\Framework\\App\\Resource')->disableOriginalConstructor()->getMock(); $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock); $this->resourceMock->expects($this->any())->method('getTableName')->will($this->returnCallback(function ($arg) { return $arg; })); $this->contextMock = $this->getMockBuilder('Magento\\Framework\\Model\\Resource\\Db\\Context')->disableOriginalConstructor()->getMock(); $this->contextMock->expects($this->any())->method('getResources')->willReturn($this->resourceMock); $this->loggerMock = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->getMock(); $dateTime = $this->getMockBuilder('DateTime')->getMock(); $this->timezoneMock = $this->getMockBuilder('Magento\\Framework\\Stdlib\\DateTime\\TimezoneInterface')->getMock(); $this->timezoneMock->expects($this->any())->method('scopeDate')->willReturn($dateTime); $this->dateTimeMock = $this->getMockBuilder('Magento\\Framework\\Stdlib\\DateTime')->getMock(); $this->flagMock = $this->getMockBuilder('Magento\\Reports\\Model\\Flag')->disableOriginalConstructor()->setMethods(['setReportFlagCode', 'unsetData', 'loadSelf', 'setFlagData', 'setLastUpdate', 'save'])->getMock(); $this->flagFactoryMock = $this->getMockBuilder('Magento\\Reports\\Model\\FlagFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->flagFactoryMock->expects($this->any())->method('create')->willReturn($this->flagMock); $this->validatorMock = $this->getMockBuilder('Magento\\Framework\\Stdlib\\DateTime\\Timezone\\Validator')->disableOriginalConstructor()->getMock(); $this->backendMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend')->disableOriginalConstructor()->getMock(); $this->attributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute')->disableOriginalConstructor()->getMock(); $this->attributeMock->expects($this->any())->method('getBackend')->willReturn($this->backendMock); $this->productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Resource\\Product')->disableOriginalConstructor()->getMock(); $this->productMock->expects($this->any())->method('getAttribute')->willReturn($this->attributeMock); $this->helperMock = $this->getMockBuilder('Magento\\Reports\\Model\\Resource\\Helper')->disableOriginalConstructor()->getMock(); $this->viewed = new Viewed($this->contextMock, $this->loggerMock, $this->timezoneMock, $this->flagFactoryMock, $this->dateTimeMock, $this->validatorMock, $this->productMock, $this->helperMock); }
public function testProcessNotStaticAttribute() { $expectedResult = 'search_index.entity_id IN (select entity_id from (TEST QUERY PART) as filter)'; $scopeId = 0; $isNegation = false; $query = 'SELECT field FROM table'; $attributeId = 1234567; $this->scope->expects($this->once())->method('getId')->will($this->returnValue($scopeId)); $this->filter->expects($this->exactly(4))->method('getField')->will($this->returnValue('not_static_attribute')); $this->config->expects($this->exactly(1))->method('getAttribute')->with(\Magento\Catalog\Model\Product::ENTITY, 'not_static_attribute')->will($this->returnValue($this->attribute)); $this->attribute->expects($this->once())->method('isStatic')->will($this->returnValue(false)); $this->attribute->expects($this->once())->method('getBackendTable')->will($this->returnValue('backend_table')); $this->attribute->expects($this->once())->method('getAttributeId')->will($this->returnValue($attributeId)); $this->connection->expects($this->once())->method('getIfNullSql')->with('current_store.value', 'main_table.value')->will($this->returnValue('IF NULL SQL')); $this->select->expects($this->once())->method('from')->with(['main_table' => 'backend_table'], 'entity_id')->will($this->returnSelf()); $this->select->expects($this->once())->method('joinLeft')->with(['current_store' => 'backend_table'])->will($this->returnSelf()); $this->select->expects($this->once())->method('columns')->with(['not_static_attribute' => 'IF NULL SQL'])->will($this->returnSelf()); $this->select->expects($this->exactly(2))->method('where')->will($this->returnSelf()); $this->select->expects($this->once())->method('__toString')->will($this->returnValue('TEST QUERY PART')); $actualResult = $this->target->process($this->filter, $isNegation, $query); $this->assertSame($expectedResult, $this->removeWhitespaces($actualResult)); }
/** * Get attribute type for upcoming validation. * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\Magento\Eav\Model\Entity\Attribute $attribute * @return string */ public static function getAttributeType(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { $frontendInput = $attribute->getFrontendInput(); if ($attribute->usesSource() && in_array($frontendInput, ['select', 'multiselect', 'boolean'])) { return $frontendInput; } elseif ($attribute->isStatic()) { return $frontendInput == 'date' ? 'datetime' : 'varchar'; } else { return $attribute->getBackendType(); } }
/** * Returns attributes all values in label-value or value-value pairs form. Labels are lower-cased. * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return array */ public function getAttributeOptions(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { $options = []; if ($attribute->usesSource()) { // should attribute has index (option value) instead of a label? $index = in_array($attribute->getAttributeCode(), $this->_indexValueAttributes) ? 'value' : 'label'; // only default (admin) store values used $attribute->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID); try { foreach ($attribute->getSource()->getAllOptions(false) as $option) { foreach (is_array($option['value']) ? $option['value'] : [$option] as $innerOption) { if (strlen($innerOption['value'])) { // skip ' -- Please Select -- ' option $options[$innerOption['value']] = $innerOption[$index]; } } } } catch (\Exception $e) { // ignore exceptions connected with source models } } return $options; }
/** * Check is attribute value empty * * @param AbstractAttribute $attribute * @param mixed $value * @return bool */ protected function _isAttributeValueEmpty(AbstractAttribute $attribute, $value) { return $attribute->isValueEmpty($value); }
/** * @param AbstractAttribute $attribute * @return Data\Eav\AttributeMetadata */ private function createMetadataAttribute($attribute) { $data = $this->booleanPrefixMapper($attribute->getData()); // fill options and validate rules $data[AttributeMetadata::OPTIONS] = $attribute->usesSource() ? $attribute->getSource()->getAllOptions() : array(); $data[AttributeMetadata::VALIDATION_RULES] = $attribute->getValidateRules(); // fill scope $data[AttributeMetadata::SCOPE] = $attribute->isScopeGlobal() ? 'global' : ($attribute->isScopeWebsite() ? 'website' : 'store'); $data[AttributeMetadata::FRONTEND_LABEL] = []; $data[AttributeMetadata::FRONTEND_LABEL][0] = array(FrontendLabel::STORE_ID => 0, FrontendLabel::LABEL => $attribute->getFrontendLabel()); if (is_array($attribute->getStoreLabels())) { foreach ($attribute->getStoreLabels() as $storeId => $label) { $data[AttributeMetadata::FRONTEND_LABEL][$storeId] = array(FrontendLabel::STORE_ID => $storeId, FrontendLabel::LABEL => $label); } } return $this->attributeMetadataBuilder->populateWithArray($data)->create(); }
/** * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param array|\Magento\Eav\Model\Resource\Entity\Attribute\Option\Collection $optionCollection * @return array */ protected function _prepareOptionValues(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute, $optionCollection) { $type = $attribute->getFrontendInput(); if ($type === 'select' || $type === 'multiselect') { $defaultValues = explode(',', $attribute->getDefaultValue()); $inputType = $type === 'select' ? 'radio' : 'checkbox'; } else { $defaultValues = []; $inputType = ''; } $values = []; $isSystemAttribute = is_array($optionCollection); foreach ($optionCollection as $option) { $bunch = $isSystemAttribute ? $this->_prepareSystemAttributeOptionValues($option, $inputType, $defaultValues) : $this->_prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues); foreach ($bunch as $value) { $values[] = new \Magento\Framework\Object($value); } } return $values; }
/** * @param Product $product * @param AbstractAttribute $attribute * @return array */ protected function getMediaEntriesDataCollection(Product $product, AbstractAttribute $attribute) { $attributeCode = $attribute->getAttributeCode(); $mediaData = $product->getData($attributeCode); if (!empty($mediaData['images']) && is_array($mediaData['images'])) { return $mediaData['images']; } return []; }
/** * Retrieve Select For Flat Attribute update * * @param AbstractAttribute $attribute * @param int $storeId * @return Select */ public function getFlatUpdateSelect(AbstractAttribute $attribute, $storeId) { $connection = $this->getConnection(); $joinConditionTemplate = "%s.entity_id=%s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d"; $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', \Magento\Store\Model\Store::DEFAULT_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= ' AND e.child_id = t1.entity_id'; } $valueExpr = $connection->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); /** @var $select Select */ $select = $connection->select()->joinLeft(['t1' => $attribute->getBackend()->getTable()], $joinCondition, [])->joinLeft(['t2' => $attribute->getBackend()->getTable()], sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $storeId), [$attribute->getAttributeCode() => $valueExpr]); if ($attribute->getFlatAddChildData()) { $select->where("e.is_child = ?", 0); } return $select; }
/** * Check whether the attribute is a real field in entity table * Rewrited for EAV Collection * * @param integer|string|\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return bool */ public function isAttributeStatic($attribute) { $attributeCode = null; if ($attribute instanceof \Magento\Eav\Model\Entity\Attribute\AttributeInterface) { $attributeCode = $attribute->getAttributeCode(); } elseif (is_string($attribute)) { $attributeCode = $attribute; } elseif (is_numeric($attribute)) { $attributeCode = $this->getAttribute($attribute)->getAttributeCode(); } if ($attributeCode) { $columns = $this->getAllTableColumns(); if (in_array($attributeCode, $columns)) { return true; } } return false; }
/** * Return increment needed for SKU uniqueness * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param Product $object * @return int */ protected function _getLastSimilarAttributeValueIncrement($attribute, $object) { $connection = $this->getAttribute()->getEntity()->getConnection(); $select = $connection->select(); $value = $object->getData($attribute->getAttributeCode()); $bind = ['attribute_code' => trim($value) . '-%']; $select->from($this->getTable(), $attribute->getAttributeCode())->where($attribute->getAttributeCode() . ' LIKE :attribute_code')->order(['entity_id DESC', $attribute->getAttributeCode() . ' ASC'])->limit(1); $data = $connection->fetchOne($select, $bind); return abs((int) str_replace($value, '', $data)); }
/** * Insert entity attribute value * * @param \Magento\Framework\Object $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _insertAttribute($object, $attribute, $value) { /** * save required attributes in global scope every time if store id different from default */ $storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId(); if ($this->getDefaultStoreId() != $storeId) { if ($attribute->getIsRequired() || $attribute->getIsRequiredInAdminStore()) { $table = $attribute->getBackend()->getTable(); $select = $this->_getReadAdapter()->select()->from($table)->where('entity_type_id = ?', $attribute->getEntityTypeId())->where('attribute_id = ?', $attribute->getAttributeId())->where('store_id = ?', $this->getDefaultStoreId())->where('entity_id = ?', $object->getEntityId()); $row = $this->_getReadAdapter()->fetchOne($select); if (!$row) { $data = new \Magento\Framework\Object(array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getAttributeId(), 'store_id' => $this->getDefaultStoreId(), 'entity_id' => $object->getEntityId(), 'value' => $this->_prepareValueForSave($value, $attribute))); $bind = $this->_prepareDataForTable($data, $table); $this->_getWriteAdapter()->insertOnDuplicate($table, $bind, array('value')); } } } return $this->_saveAttributeValue($object, $attribute, $value); }