protected function setUp()
 {
     parent::setUp();
     $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
     $this->model = $this->objectManager->create('Magento\\Security\\Model\\PasswordResetRequestEvent');
     $this->resourceModel = $this->model->getResource();
 }
 /**
  * Save relations for Customer
  *
  * @param \Magento\Framework\Model\AbstractModel $customer
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function processRelation(\Magento\Framework\Model\AbstractModel $customer)
 {
     $defaultBillingId = $customer->getData('default_billing');
     $defaultShippingId = $customer->getData('default_shipping');
     /** @var \Magento\Customer\Model\Address $address */
     foreach ($customer->getAddresses() as $address) {
         if ($address->getData('_deleted')) {
             if ($address->getId() == $defaultBillingId) {
                 $customer->setData('default_billing', null);
             }
             if ($address->getId() == $defaultShippingId) {
                 $customer->setData('default_shipping', null);
             }
             $removedAddressId = $address->getId();
             $address->delete();
             // Remove deleted address from customer address collection
             $customer->getAddressesCollection()->removeItemByKey($removedAddressId);
         } else {
             $address->setParentId($customer->getId())->setStoreId($customer->getStoreId())->setIsCustomerSaveTransaction(true)->save();
             if (($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) && $address->getId() != $defaultBillingId) {
                 $customer->setData('default_billing', $address->getId());
             }
             if (($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) && $address->getId() != $defaultShippingId) {
                 $customer->setData('default_shipping', $address->getId());
             }
         }
     }
     $changedAddresses = [];
     $changedAddresses['default_billing'] = $customer->getData('default_billing');
     $changedAddresses['default_shipping'] = $customer->getData('default_shipping');
     $customer->getResource()->getConnection()->update($customer->getResource()->getTable('customer_entity'), $changedAddresses, $customer->getResource()->getConnection()->quoteInto('entity_id = ?', $customer->getId()));
 }
Exemple #3
0
 /**
  * Returns true if and only if $value meets the validation requirements.
  *
  * @param \Magento\Framework\Model\AbstractModel $entity
  * @return bool
  * @throws \InvalidArgumentException
  */
 public function isValid($entity)
 {
     $this->_messages = [];
     if (!$entity instanceof \Magento\Framework\Model\AbstractModel) {
         throw new \InvalidArgumentException('Model must be extended from \\Magento\\Framework\\Model\\AbstractModel');
     }
     /** @var \Magento\Eav\Model\Entity\AbstractEntity $resource */
     $resource = $entity->getResource();
     if (!$resource instanceof \Magento\Eav\Model\Entity\AbstractEntity) {
         throw new \InvalidArgumentException('Model resource must be extended from \\Magento\\Eav\\Model\\Entity\\AbstractEntity');
     }
     $resource->loadAllAttributes($entity);
     $attributes = $resource->getAttributesByCode();
     /** @var \Magento\Eav\Model\Entity\Attribute $attribute */
     foreach ($attributes as $attribute) {
         $backend = $attribute->getBackend();
         if (!method_exists($backend, 'validate') || !is_callable([$backend, 'validate'])) {
             continue;
         }
         try {
             $result = $backend->validate($entity);
             if (false === $result) {
                 $this->_messages[$attribute->getAttributeCode()][] = __('The value of attribute "%1" is invalid', $attribute->getAttributeCode());
             } elseif (is_string($result)) {
                 $this->_messages[$attribute->getAttributeCode()][] = $result;
             }
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->_messages[$attribute->getAttributeCode()][] = $e->getMessage();
         }
     }
     return 0 == count($this->_messages);
 }
Exemple #4
0
/**
 * 2016-08-22
 * @param AbstractModel $model
 * @return void
 */
function df_eav_partial_save(AbstractModel $model)
{
    /** @var AbstractEntity $resource */
    $resource = $model->getResource();
    $resource->isPartialSave(true);
    try {
        $model->save();
    } finally {
        $resource->isPartialSave(false);
    }
}
 /**
  * Validate product attribute value for condition
  *
  * @param \Magento\Framework\Model\AbstractModel $model
  * @return bool
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function validate(\Magento\Framework\Model\AbstractModel $model)
 {
     $attrCode = $this->getAttribute();
     if ('category_ids' == $attrCode) {
         return $this->validateAttribute($model->getAvailableInCategories());
     } elseif (!isset($this->_entityAttributeValues[$model->getId()])) {
         if (!$model->getResource()) {
             return false;
         }
         $attr = $model->getResource()->getAttribute($attrCode);
         if ($attr && $attr->getBackendType() == 'datetime' && !is_int($this->getValue())) {
             $this->setValue(strtotime($this->getValue()));
             $value = strtotime($model->getData($attrCode));
             return $this->validateAttribute($value);
         }
         if ($attr && $attr->getFrontendInput() == 'multiselect') {
             $value = $model->getData($attrCode);
             $value = strlen($value) ? explode(',', $value) : [];
             return $this->validateAttribute($value);
         }
         return parent::validate($model);
     } else {
         $result = false;
         // any valid value will set it to TRUE
         // remember old attribute state
         $oldAttrValue = $model->hasData($attrCode) ? $model->getData($attrCode) : null;
         foreach ($this->_entityAttributeValues[$model->getId()] as $value) {
             $attr = $model->getResource()->getAttribute($attrCode);
             if ($attr && $attr->getBackendType() == 'datetime') {
                 $value = strtotime($value);
             } elseif ($attr && $attr->getFrontendInput() == 'multiselect') {
                 $value = strlen($value) ? explode(',', $value) : [];
             }
             $model->setData($attrCode, $value);
             $result |= parent::validate($model);
             if ($result) {
                 break;
             }
         }
         if ($oldAttrValue === null) {
             $model->unsetData($attrCode);
         } else {
             $model->setData($attrCode, $oldAttrValue);
         }
         return (bool) $result;
     }
 }
 /**
  * Prepare multiselect attribute value
  *
  * @param mixed $value
  * @param \Magento\Framework\Model\AbstractModel $model
  * @return mixed
  */
 protected function _prepareMultiselectValue($value, \Magento\Framework\Model\AbstractModel $model)
 {
     $attribute = $model->getResource()->getAttribute($this->getAttribute());
     if ($attribute && $attribute->getFrontendInput() == 'multiselect') {
         $value = strlen($value) ? explode(',', $value) : [];
     }
     return $value;
 }
Exemple #7
0
 /**
  * Get attributes involved in validation.
  *
  * This method return specified $_attributes if they defined by setAttributes method, otherwise if $entity
  * is EAV-model it returns it's all available attributes, otherwise it return empty array.
  *
  * @param \Magento\Framework\Model\AbstractModel $entity
  * @return array
  */
 protected function _getAttributes($entity)
 {
     /** @var \Magento\Eav\Model\Attribute[] $attributes */
     $attributes = [];
     if ($this->_attributes) {
         $attributes = $this->_attributes;
     } elseif ($entity instanceof \Magento\Framework\Model\AbstractModel && $entity->getResource() instanceof \Magento\Eav\Model\Entity\AbstractEntity) {
         // $entity is EAV-model
         /** @var \Magento\Eav\Model\Entity\Type $entityType */
         $entityType = $entity->getEntityType();
         $attributes = $entityType->getAttributeCollection()->getItems();
     }
     $attributesByCode = [];
     $attributesCodes = [];
     foreach ($attributes as $attribute) {
         if (!$attribute->getIsVisible()) {
             continue;
         }
         $attributeCode = $attribute->getAttributeCode();
         $attributesByCode[$attributeCode] = $attribute;
         $attributesCodes[] = $attributeCode;
     }
     $ignoreAttributes = $this->_attributesBlackList;
     if ($this->_attributesWhiteList) {
         $ignoreAttributes = array_merge($ignoreAttributes, array_diff($attributesCodes, $this->_attributesWhiteList));
     }
     foreach ($ignoreAttributes as $attributeCode) {
         unset($attributesByCode[$attributeCode]);
     }
     return $attributesByCode;
 }