public function testExecute()
 {
     $this->requestMock->expects($this->any())->method('getParam')->willReturnMap([['frontend_label', null, 'test_frontend_label'], ['attribute_code', null, 'test_attribute_code'], ['new_attribute_set_name', null, 'test_attribute_set_name']]);
     $this->objectManagerMock->expects($this->exactly(2))->method('create')->willReturnMap([['Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', [], $this->attributeMock], ['Magento\\Eav\\Model\\Entity\\Attribute\\Set', [], $this->attributeSetMock]]);
     $this->attributeMock->expects($this->once())->method('loadByCode')->willReturnSelf();
     $this->requestMock->expects($this->once())->method('has')->with('new_attribute_set_name')->willReturn(true);
     $this->attributeSetMock->expects($this->once())->method('setEntityTypeId')->willReturnSelf();
     $this->attributeSetMock->expects($this->once())->method('load')->willReturnSelf();
     $this->attributeSetMock->expects($this->once())->method('getId')->willReturn(false);
     $this->resultJsonFactoryMock->expects($this->once())->method('create')->willReturn($this->resultJson);
     $this->resultJson->expects($this->once())->method('setJsonData')->willReturnSelf();
     $this->assertInstanceOf(ResultJson::class, $this->getModel()->execute());
 }
Exemple #2
0
 /**
  * Invalidate EAV indexer if attribute set has indexable attributes changes
  *
  * @param \Magento\Eav\Model\Entity\Attribute\Set $subject
  * @param callable $proceed
  *
  * @return \Magento\Eav\Model\Entity\Attribute\Set
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundSave(\Magento\Eav\Model\Entity\Attribute\Set $subject, \Closure $proceed)
 {
     $requiresReindex = false;
     if ($subject->getId()) {
         /** @var \Magento\Eav\Model\Entity\Attribute\Set $originalSet */
         $originalSet = clone $subject;
         $originalSet->initFromSkeleton($subject->getId());
         $originalAttributeCodes = array_flip($this->_attributeFilter->filter($originalSet));
         $subjectAttributeCodes = array_flip($this->_attributeFilter->filter($subject));
         $requiresReindex = (bool) count(array_merge(array_diff_key($subjectAttributeCodes, $originalAttributeCodes), array_diff_key($originalAttributeCodes, $subjectAttributeCodes)));
     }
     $result = $proceed();
     if ($requiresReindex) {
         $this->_indexerEavProcessor->markIndexerAsInvalid();
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function save()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'save');
     if (!$pluginInfo) {
         return parent::save();
     } else {
         return $this->___callPlugins('save', func_get_args(), $pluginInfo);
     }
 }
 /**
  * Retrieve codes of indexable attributes from given attribute set
  *
  * @param \Magento\Eav\Model\Entity\Attribute\Set $set
  * @return array
  */
 public function filter(\Magento\Eav\Model\Entity\Attribute\Set $set)
 {
     $codes = [];
     $catalogResource = $this->_attributeFactory->create();
     foreach ($set->getGroups() as $group) {
         /** @var $group \Magento\Eav\Model\Entity\Attribute\Group */
         foreach ($group->getAttributes() as $attribute) {
             /** @var $attribute \Magento\Eav\Model\Entity\Attribute */
             $catalogResource->load($attribute->getId());
             if ($catalogResource->isIndexable()) {
                 // Attribute requires to be cloned for new dataset to maintain attribute set changes
                 $attributeClone = clone $attribute;
                 $attributeClone->load($attribute->getAttributeId());
                 $codes[] = $attributeClone->getAttributeCode();
                 unset($attributeClone);
             }
         }
     }
     return $codes;
 }
Exemple #5
0
 private function saveAttributeSet()
 {
     if ($this->attributeSetObj->getId()) {
         return array('result' => true, 'obj' => $this->attributeSetObj);
     }
     $this->attributeSetObj->setEntityTypeId($this->entityTypeId)->setAttributeSetName($this->setName);
     try {
         $this->attributeSetObj->validate();
         $this->attributeSetObj->save();
         $this->attributeSetObj->initFromSkeleton($this->skeletonId)->save();
     } catch (\Exception $e) {
         return array('result' => false, 'error' => $e->getMessage());
     }
     return array('result' => true, 'obj' => $this->attributeSetObj);
 }
Exemple #6
0
 /**
  * Retrieve sorted attributes
  *
  * @param int $setId
  * @return array
  */
 public function getSortedAttributes($setId = null)
 {
     $attributes = $this->getAttributesByCode();
     if ($setId === null) {
         $setId = $this->getEntityType()->getDefaultAttributeSetId();
     }
     // initialize set info
     $this->_attrSetEntity->addSetInfo($this->getEntityType(), $attributes, $setId);
     foreach ($attributes as $code => $attribute) {
         /* @var $attribute AbstractAttribute */
         if (!$attribute->isInSet($setId)) {
             unset($attributes[$code]);
         }
     }
     $this->_sortingSetId = $setId;
     uasort($attributes, array($this, 'attributesCompare'));
     return $attributes;
 }
Exemple #7
0
 public function setAttributeSetObj(\Magento\Eav\Model\Entity\Attribute\Set $obj)
 {
     $this->attributeSetObj = $obj;
     $this->setId = $obj->getId();
     return $this;
 }
Exemple #8
0
 /**
  * Validate attribute set name
  *
  * @param \Magento\Eav\Model\Entity\Attribute\Set $object
  * @param string $attributeSetName
  * @return bool
  */
 public function validate($object, $attributeSetName)
 {
     $adapter = $this->_getReadAdapter();
     $bind = ['attribute_set_name' => trim($attributeSetName), 'entity_type_id' => $object->getEntityTypeId()];
     $select = $adapter->select()->from($this->getMainTable())->where('attribute_set_name = :attribute_set_name')->where('entity_type_id = :entity_type_id');
     if ($object->getId()) {
         $bind['attribute_set_id'] = $object->getId();
         $select->where('attribute_set_id != :attribute_set_id');
     }
     return !$adapter->fetchOne($select, $bind) ? true : false;
 }
 public function testValidateWithNonexistentValidName()
 {
     $this->_model->getResource()->expects($this->any())->method('validate')->will($this->returnValue(true));
     $this->_model->setAttributeSetName('nonexistent_name');
     $this->assertTrue($this->_model->validate());
 }
 /**
  * {@inheritdoc}
  */
 public function offsetGet($offset)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'offsetGet');
     if (!$pluginInfo) {
         return parent::offsetGet($offset);
     } else {
         return $this->___callPlugins('offsetGet', func_get_args(), $pluginInfo);
     }
 }