Ejemplo n.º 1
0
 public function testAddAttributeGrouping()
 {
     $select = $this->_model->getSelect();
     $this->assertEmpty($select->getPart(\Magento\Framework\DB\Select::GROUP));
     $this->_model->addAttributeGrouping();
     $this->assertEquals(['main_table.attribute_id'], $select->getPart(\Magento\Framework\DB\Select::GROUP));
 }
 protected function setUp()
 {
     parent::setUp();
     $this->eavConfigMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
     $this->eavValidationRulesMock = $this->getMockBuilder(EavValidationRules::class)->disableOriginalConstructor()->getMock();
     $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
     $this->groupCollectionFactoryMock = $this->getMockBuilder(GroupCollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->groupCollectionMock = $this->getMockBuilder(GroupCollection::class)->disableOriginalConstructor()->getMock();
     $this->attributeMock = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock();
     $this->groupMock = $this->getMockBuilder(Group::class)->disableOriginalConstructor()->setMethods(['getAttributeGroupCode'])->getMock();
     $this->entityTypeMock = $this->getMockBuilder(EntityType::class)->disableOriginalConstructor()->getMock();
     $this->attributeCollectionMock = $this->getMockBuilder(AttributeCollection::class)->disableOriginalConstructor()->getMock();
     $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass();
     $this->formElementMapperMock = $this->getMockBuilder(FormElementMapper::class)->disableOriginalConstructor()->getMock();
     $this->metaPropertiesMapperMock = $this->getMockBuilder(MetaPropertiesMapper::class)->disableOriginalConstructor()->getMock();
     $this->groupCollectionFactoryMock->expects($this->any())->method('create')->willReturn($this->groupCollectionMock);
     $this->groupCollectionMock->expects($this->any())->method('setAttributeSetFilter')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('setSortOrder')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('load')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$this->groupMock]));
     $this->attributeCollectionMock->expects($this->any())->method('addFieldToSelect')->willReturnSelf();
     $this->attributeCollectionMock->expects($this->any())->method('load')->willReturnSelf();
     $this->eavConfigMock->expects($this->any())->method('getEntityType')->willReturn($this->entityTypeMock);
     $this->entityTypeMock->expects($this->any())->method('getAttributeCollection')->willReturn($this->attributeCollectionMock);
     $this->productMock->expects($this->any())->method('getAttributes')->willReturn([$this->attributeMock]);
 }
 /**
  * Allow to filter an attribute collection on attributes that are indexed into the search engine.
  *
  * @param AttributeCollection $attributeCollection Attribute collection (not loaded).
  *
  * @return AttributeCollection
  */
 public function addIndexedFilterToAttributeCollection(AttributeCollection $attributeCollection)
 {
     $conditions = [];
     foreach ($this->indexedAttributesConditions as $fieldName => $condition) {
         if ($condition['operator'] == 'IN' || is_array($condition['value'])) {
             $conditionString = sprintf('%s %s (?)', $fieldName, $condition['operator']);
             $conditions[] = $this->connection->quoteInto($conditionString, $condition['value']);
         } elseif (!is_array($condition['value'])) {
             $conditions[] = sprintf('%s %s %s', $fieldName, $condition['operator'], $condition['value']);
         }
     }
     if (!empty($conditions)) {
         $select = $attributeCollection->getSelect();
         $select->where(implode(' OR ', $conditions));
     }
     return $attributeCollection;
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function getAttributes($entityType, $attributeSetId)
 {
     /** @var \Magento\Eav\Api\Data\AttributeSetInterface $attributeSet */
     $attributeSet = $this->setRepository->get($attributeSetId);
     $requiredEntityTypeId = $this->eavConfig->getEntityType($entityType)->getId();
     if (!$attributeSet->getAttributeSetId() || $attributeSet->getEntityTypeId() != $requiredEntityTypeId) {
         throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
     }
     $attributeCollection = $this->attributeCollection->setAttributeSetFilter($attributeSet->getAttributeSetId())->load();
     return $attributeCollection->getItems();
 }
Ejemplo n.º 5
0
 protected function setUp()
 {
     parent::setUp();
     $this->objectManager = new ObjectManager($this);
     $this->eavConfigMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
     $this->eavValidationRulesMock = $this->getMockBuilder(EavValidationRules::class)->disableOriginalConstructor()->getMock();
     $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
     $this->groupCollectionFactoryMock = $this->getMockBuilder(GroupCollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->groupCollectionMock = $this->getMockBuilder(GroupCollection::class)->disableOriginalConstructor()->getMock();
     $this->attributeMock = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock();
     $this->groupMock = $this->getMockBuilder(Group::class)->disableOriginalConstructor()->setMethods(['getAttributeGroupCode'])->getMock();
     $this->entityTypeMock = $this->getMockBuilder(EntityType::class)->disableOriginalConstructor()->getMock();
     $this->attributeCollectionMock = $this->getMockBuilder(AttributeCollection::class)->disableOriginalConstructor()->getMock();
     $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass();
     $this->formElementMapperMock = $this->getMockBuilder(FormElementMapper::class)->disableOriginalConstructor()->getMock();
     $this->metaPropertiesMapperMock = $this->getMockBuilder(MetaPropertiesMapper::class)->disableOriginalConstructor()->getMock();
     $this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)->disableOriginalConstructor()->getMock();
     $this->attributeGroupRepositoryMock = $this->getMockBuilder(ProductAttributeGroupRepositoryInterface::class)->getMockForAbstractClass();
     $this->attributeGroupMock = $this->getMockBuilder(AttributeGroupInterface::class)->setMethods(['getAttributeGroupCode', 'getApplyTo'])->getMockForAbstractClass();
     $this->attributeRepositoryMock = $this->getMockBuilder(ProductAttributeRepositoryInterface::class)->getMockForAbstractClass();
     $this->searchCriteriaMock = $this->getMockBuilder(SearchCriteria::class)->disableOriginalConstructor()->setMethods(['getItems'])->getMock();
     $this->sortOrderBuilderMock = $this->getMockBuilder(SortOrderBuilder::class)->disableOriginalConstructor()->getMock();
     $this->searchResultsMock = $this->getMockBuilder(SearchResultsInterface::class)->getMockForAbstractClass();
     $this->eavAttributeMock = $this->getMockBuilder(Attribute::class)->setMethods(['getAttributeGroupCode', 'getApplyTo', 'getFrontendInput', 'getAttributeCode'])->disableOriginalConstructor()->getMock();
     $this->groupCollectionFactoryMock->expects($this->any())->method('create')->willReturn($this->groupCollectionMock);
     $this->groupCollectionMock->expects($this->any())->method('setAttributeSetFilter')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('setSortOrder')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('load')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$this->groupMock]));
     $this->attributeCollectionMock->expects($this->any())->method('addFieldToSelect')->willReturnSelf();
     $this->attributeCollectionMock->expects($this->any())->method('load')->willReturnSelf();
     $this->eavConfigMock->expects($this->any())->method('getEntityType')->willReturn($this->entityTypeMock);
     $this->entityTypeMock->expects($this->any())->method('getAttributeCollection')->willReturn($this->attributeCollectionMock);
     $this->productMock->expects($this->any())->method('getAttributes')->willReturn([$this->attributeMock]);
     $this->storeMock = $this->getMockBuilder(StoreInterface::class)->setMethods(['load', 'getId', 'getConfig', 'getBaseCurrencyCode'])->getMockForAbstractClass();
     $this->currencyMock = $this->getMockBuilder(Currency::class)->disableOriginalConstructor()->setMethods(['toCurrency'])->getMock();
     $this->currencyLocaleMock = $this->getMockBuilder(CurrencyLocale::class)->disableOriginalConstructor()->setMethods(['getCurrency'])->getMock();
     $this->eav = $this->getModel();
     $this->objectManager->setBackwardCompatibleProperty($this->eav, 'localeCurrency', $this->currencyLocaleMock);
 }
Ejemplo n.º 6
0
 /**
  * Return collection of same attributes for selected products without unique
  *
  * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  */
 public function getAttributes()
 {
     if ($this->_attributes === null) {
         $this->_attributes = $this->_eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getAttributeCollection()->addIsNotUniqueFilter()->setInAllAttributeSetsFilter($this->getProductsSetIds());
         if ($this->_excludedAttributes) {
             $this->_attributes->addFieldToFilter('attribute_code', ['nin' => $this->_excludedAttributes]);
         }
         // check product type apply to limitation and remove attributes that impossible to change in mass-update
         $productTypeIds = $this->getProducts()->getProductTypeIds();
         foreach ($this->_attributes as $attribute) {
             /* @var $attribute \Magento\Catalog\Model\Entity\Attribute */
             foreach ($productTypeIds as $productTypeId) {
                 $applyTo = $attribute->getApplyTo();
                 if (count($applyTo) > 0 && !in_array($productTypeId, $applyTo)) {
                     $this->_attributes->removeItemByKey($attribute->getId());
                     break;
                 }
             }
         }
     }
     return $this->_attributes;
 }
Ejemplo n.º 7
0
 /**
  * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  * @param \Psr\Log\LoggerInterface $logger
  * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  * @param \Magento\Framework\Event\ManagerInterface $eventManager
  * @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
  * @param \Magento\Eav\Model\Config $eavConfig
  * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  */
 public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null)
 {
     $this->_eavEntityFactory = $eavEntityFactory;
     parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $eavConfig, $connection, $resource);
 }
Ejemplo n.º 8
0
 /**
  * Clean up attribute collection.
  *
  * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection
  * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  */
 public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection)
 {
     $collection->load();
     foreach ($collection as $attribute) {
         if (in_array($attribute->getAttributeCode(), $this->_disabledAttrs)) {
             $collection->removeItemByKey($attribute->getId());
         }
     }
     return $collection;
 }
Ejemplo n.º 9
0
 /**
  * Clean up attribute collection.
  *
  * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection
  * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  */
 public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection)
 {
     $collection->load();
     foreach ($collection as $attribute) {
         if (in_array($attribute->getAttributeCode(), $this->_disabledAttrs)) {
             if (isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_SKIP])) {
                 if ($attribute->getAttributeCode() == ImportAdvancedPricing::COL_TIER_PRICE && in_array($attribute->getId(), $this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_SKIP])) {
                     $this->_passTierPrice = 1;
                 }
             }
             $collection->removeItemByKey($attribute->getId());
         }
     }
     return $collection;
 }
Ejemplo n.º 10
0
 /**
  * Return array of fields to load attribute values
  *
  * @return string[]
  */
 protected function _getLoadDataFields()
 {
     $fields = array_merge(parent::_getLoadDataFields(), ['additional_table.is_global', 'additional_table.is_html_allowed_on_front', 'additional_table.is_wysiwyg_enabled']);
     return $fields;
 }
Ejemplo n.º 11
0
 /**
  * Helper function that adds a FilterGroup to the collection.
  *
  * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
  * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection
  * @return void
  * @throws \Magento\Framework\Exception\InputException
  */
 private function addFilterGroupToCollection(\Magento\Framework\Api\Search\FilterGroup $filterGroup, Collection $collection)
 {
     /** @var \Magento\Framework\Api\Search\FilterGroup $filter */
     foreach ($filterGroup->getFilters() as $filter) {
         $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
         $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
     }
 }
 /**
  * Helper function that adds a FilterGroup to the collection.
  *
  * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
  * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection
  * @return void
  * @throws \Magento\Framework\Exception\InputException
  */
 private function addFilterGroupToCollection(\Magento\Framework\Api\Search\FilterGroup $filterGroup, Collection $collection)
 {
     foreach ($filterGroup->getFilters() as $filter) {
         $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
         $field = $filter->getField();
         // Prevent ambiguity during filtration
         if ($field == \Magento\Eav\Api\Data\AttributeInterface::ATTRIBUTE_ID) {
             $field = 'main_table.' . $field;
         }
         $collection->addFieldToFilter($field, [$condition => $filter->getValue()]);
     }
 }