/** * @covers \Magento\Catalog\Model\Product\ReservedAttributeList::isReservedAttribute * @dataProvider dataProvider */ public function testIsReservedAttribute($isUserDefined, $attributeCode, $expected) { $attribute = $this->getMock('\\Magento\\Catalog\\Model\\Entity\\Attribute', ['getIsUserDefined', 'getAttributeCode', '__sleep', '__wakeup'], [], '', false); $attribute->expects($this->once())->method('getIsUserDefined')->will($this->returnValue($isUserDefined)); $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->assertEquals($expected, $this->model->isReservedAttribute($attribute)); }
/** * Prepare data for save * * @return $this * @throws Exception */ protected function _beforeSave() { // prevent overriding product data if (isset($this->_data['attribute_code']) && $this->reservedAttributeList->isReservedAttribute($this)) { throw new Exception(__('The attribute code \'%1\' is reserved by system. Please try another attribute code', $this->_data['attribute_code'])); } /** * Check for maximum attribute_code length */ if (isset($this->_data['attribute_code']) && !\Zend_Validate::is($this->_data['attribute_code'], 'StringLength', array('max' => self::ATTRIBUTE_CODE_MAX_LENGTH))) { throw new Exception(__('Maximum length of attribute code must be less than %1 symbols', self::ATTRIBUTE_CODE_MAX_LENGTH)); } $defaultValue = $this->getDefaultValue(); $hasDefaultValue = (string) $defaultValue != ''; if ($this->getBackendType() == 'decimal' && $hasDefaultValue) { if (!\Zend_Locale_Format::isNumber($defaultValue, array('locale' => $this->_localeResolver->getLocaleCode()))) { throw new Exception(__('Invalid default decimal value')); } try { $filter = new \Zend_Filter_LocalizedToNormalized(array('locale' => $this->_localeResolver->getLocaleCode())); $this->setDefaultValue($filter->filter($defaultValue)); } catch (\Exception $e) { throw new Exception(__('Invalid default decimal value')); } } if ($this->getBackendType() == 'datetime') { if (!$this->getBackendModel()) { $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\Datetime'); } if (!$this->getFrontendModel()) { $this->setFrontendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend\\Datetime'); } // save default date value as timestamp if ($hasDefaultValue) { $format = $this->_localeDate->getDateFormat(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_SHORT); try { $defaultValue = $this->_localeDate->date($defaultValue, $format, null, false)->toValue(); $this->setDefaultValue($defaultValue); } catch (\Exception $e) { throw new Exception(__('Invalid default date')); } } } if ($this->getBackendType() == 'gallery') { if (!$this->getBackendModel()) { $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\DefaultBackend'); } } return parent::_beforeSave(); }
/** * Prepare data for save * * @return $this * @throws LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function beforeSave() { // prevent overriding product data if (isset($this->_data['attribute_code']) && $this->reservedAttributeList->isReservedAttribute($this)) { throw new LocalizedException(__('The attribute code \'%1\' is reserved by system. Please try another attribute code', $this->_data['attribute_code'])); } /** * Check for maximum attribute_code length */ if (isset($this->_data['attribute_code']) && !\Zend_Validate::is($this->_data['attribute_code'], 'StringLength', ['max' => self::ATTRIBUTE_CODE_MAX_LENGTH])) { throw new LocalizedException(__('An attribute code must be fewer than %1 characters.', self::ATTRIBUTE_CODE_MAX_LENGTH)); } $defaultValue = $this->getDefaultValue(); $hasDefaultValue = (string) $defaultValue != ''; if ($this->getBackendType() == 'decimal' && $hasDefaultValue) { $numberFormatter = new \NumberFormatter($this->_localeResolver->getLocale(), \NumberFormatter::DECIMAL); $defaultValue = $numberFormatter->parse($defaultValue); if ($defaultValue === false) { throw new LocalizedException(__('Invalid default decimal value')); } $this->setDefaultValue($defaultValue); } if ($this->getBackendType() == 'datetime') { if (!$this->getBackendModel()) { $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\Datetime'); } if (!$this->getFrontendModel()) { $this->setFrontendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend\\Datetime'); } // save default date value as timestamp if ($hasDefaultValue) { $format = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT); try { $defaultValue = $this->dateTimeFormatter->formatObject(new \DateTime($defaultValue), $format); $this->setDefaultValue($defaultValue); } catch (\Exception $e) { throw new LocalizedException(__('Invalid default date')); } } } if ($this->getBackendType() == 'gallery') { if (!$this->getBackendModel()) { $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\DefaultBackend'); } } return parent::beforeSave(); }