/** * Validate user input for option * * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...) * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function validateUserValue($values) { parent::validateUserValue($values); $option = $this->getOption(); $value = $this->getUserValue(); $dateValid = true; if ($this->_dateExists()) { if ($this->useCalendar()) { $dateValid = isset($value['date']) && preg_match('/^\\d{1,4}.+\\d{1,4}.+\\d{1,4}$/', $value['date']); } else { $dateValid = isset($value['day']) && isset($value['month']) && isset($value['year']) && $value['day'] > 0 && $value['month'] > 0 && $value['year'] > 0; } } $timeValid = true; if ($this->_timeExists()) { $timeValid = isset($value['hour']) && isset($value['minute']) && is_numeric($value['hour']) && is_numeric($value['minute']); } $isValid = $dateValid && $timeValid; if ($isValid) { $this->setUserValue(['date' => isset($value['date']) ? $value['date'] : '', 'year' => isset($value['year']) ? intval($value['year']) : 0, 'month' => isset($value['month']) ? intval($value['month']) : 0, 'day' => isset($value['day']) ? intval($value['day']) : 0, 'hour' => isset($value['hour']) ? intval($value['hour']) : 0, 'minute' => isset($value['minute']) ? intval($value['minute']) : 0, 'day_part' => isset($value['day_part']) ? $value['day_part'] : '', 'date_internal' => isset($value['date_internal']) ? $value['date_internal'] : '']); } elseif (!$isValid && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) { $this->setIsValid(false); if (!$dateValid) { throw new \Magento\Framework\Exception\LocalizedException(__('Please specify date required option(s).')); } elseif (!$timeValid) { throw new \Magento\Framework\Exception\LocalizedException(__('Please specify time required option(s).')); } else { throw new \Magento\Framework\Exception\LocalizedException(__('Please specify product\'s required option(s).')); } } else { $this->setUserValue(null); } return $this; }
/** * @param \Magento\Checkout\Model\Session $checkoutSession * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Quote\Model\Quote\Item\OptionFactory $itemOptionFactory * @param \Magento\Catalog\Model\Product\Option\UrlBuilder $urlBuilder * @param \Magento\Framework\Escaper $escaper * @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase * @param File\ValidatorInfo $validatorInfo * @param File\ValidatorFile $validatorFile * @param array $data * @throws \Magento\Framework\Exception\FileSystemException */ public function __construct(\Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Quote\Model\Quote\Item\OptionFactory $itemOptionFactory, \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase, \Magento\Catalog\Model\Product\Option\Type\File\ValidatorInfo $validatorInfo, \Magento\Catalog\Model\Product\Option\Type\File\ValidatorFile $validatorFile, \Magento\Catalog\Model\Product\Option\UrlBuilder $urlBuilder, \Magento\Framework\Escaper $escaper, array $data = []) { $this->_itemOptionFactory = $itemOptionFactory; $this->_urlBuilder = $urlBuilder; $this->_escaper = $escaper; $this->_coreFileStorageDatabase = $coreFileStorageDatabase; $this->validatorInfo = $validatorInfo; $this->validatorFile = $validatorFile; parent::__construct($checkoutSession, $scopeConfig, $data); }
/** * Validate user input for option * * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...) * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function validateUserValue($values) { parent::validateUserValue($values); $option = $this->getOption(); $value = trim($this->getUserValue()); // Check requires option to have some value if (strlen($value) == 0 && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) { $this->setIsValid(false); throw new LocalizedException(__('Please specify product\'s required option(s).')); } // Check maximal length limit $maxCharacters = $option->getMaxCharacters(); if ($maxCharacters > 0 && $this->string->strlen($value) > $maxCharacters) { $this->setIsValid(false); throw new LocalizedException(__('The text is too long.')); } $this->setUserValue($value); return $this; }
/** * Validate user input for option * * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...) * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function validateUserValue($values) { parent::validateUserValue($values); $option = $this->getOption(); $value = $this->getUserValue(); $dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value); $dateValid = true; $lastErrors = \DateTime::getLastErrors(); if (!($dateTime && $lastErrors['error_count'] == 0)) { $dateValid = false; } if ($dateValid && $dateTime) { $this->setUserValue(['date' => $value, 'year' => $dateTime->format('Y'), 'month' => $dateTime->format('m'), 'day' => $dateTime->format('d'), 'hour' => $dateTime->format('H'), 'minute' => intval($dateTime->format('i')), 'day_part' => $dateTime->format('a'), 'date_internal' => '']); } elseif (!$dateValid && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) { $this->setIsValid(false); throw new \Magento\Framework\Exception\LocalizedException(__('Please specify product\'s required option(s).')); } else { $this->setUserValue(null); return $this; } return $this; }
/** * Return SKU for selected option * * @param string $optionValue Prepared for cart option value * @param string $skuDelimiter Delimiter for Sku parts * @return string */ public function getOptionSku($optionValue, $skuDelimiter) { $option = $this->getOption(); if (!$this->_isSingleSelection()) { $skus = []; foreach (explode(',', $optionValue) as $value) { $optionSku = $option->getValueById($value); if ($optionSku) { $skus[] = $optionSku->getSku(); } else { if ($this->getListener()) { $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage()); break; } } } $result = implode($skuDelimiter, $skus); } elseif ($this->_isSingleSelection()) { $result = $option->getValueById($optionValue); if ($result) { return $result->getSku(); } else { if ($this->getListener()) { $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage()); } return ''; } } else { $result = parent::getOptionSku($optionValue, $skuDelimiter); } return $result; }
/** * @param \PHPUnit_Framework_MockObject_MockObject|DefaultType $group * @param \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product\Option $option * @param \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Object $buyRequest * @param \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product $product */ protected function parentClass($group, $option, $buyRequest, $product) { $group->expects($this->once())->method('setOption')->willReturnSelf(); $group->expects($this->once())->method('setProduct')->willReturnSelf(); $group->expects($this->once())->method('setRequest')->willReturnSelf(); $group->expects($this->once())->method('setProcessMode')->willReturnSelf(); $group->expects($this->once())->method('validateUserValue')->willReturnSelf(); $group->expects($this->once())->method('prepareForCart')->willReturn('someString'); $option->expects($this->once())->method('getType'); $option->expects($this->once())->method('groupFactory')->willReturn($group); $option->expects($this->at(0))->method('getId')->willReturn(333); $buyRequest->expects($this->once())->method('getData'); $buyRequest->expects($this->once())->method('getOptions'); $buyRequest->expects($this->once())->method('getSuperProductConfig')->willReturn([]); $buyRequest->expects($this->any())->method('unsetData')->willReturnSelf(); $buyRequest->expects($this->any())->method('getQty'); $product->expects($this->once())->method('getOptions')->willReturn([$option]); $product->expects($this->once())->method('prepareCustomOptions'); $product->expects($this->any())->method('addCustomOption')->willReturnSelf(); $product->expects($this->any())->method('setCartQty')->willReturnSelf(); $product->expects($this->once())->method('setQty'); $this->catalogProduct->expects($this->once())->method('getSkipSaleableCheck')->willReturn(false); }
/** * @param \Magento\Checkout\Model\Session $checkoutSession * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Sales\Model\Quote\Item\OptionFactory $itemOptionFactory * @param \Magento\Catalog\Model\Product\Option\UrlBuilder $urlBuilder * @param \Magento\Framework\Escaper $escaper * @param \Magento\Core\Helper\File\Storage\Database $coreFileStorageDatabase * @param \Magento\Framework\App\Filesystem $filesystem * @param \Magento\Framework\File\Size $fileSize * @param array $data */ public function __construct(\Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Sales\Model\Quote\Item\OptionFactory $itemOptionFactory, \Magento\Catalog\Model\Product\Option\UrlBuilder $urlBuilder, \Magento\Framework\Escaper $escaper, \Magento\Core\Helper\File\Storage\Database $coreFileStorageDatabase, \Magento\Framework\App\Filesystem $filesystem, \Magento\Framework\File\Size $fileSize, array $data = array()) { $this->_itemOptionFactory = $itemOptionFactory; $this->_urlBuilder = $urlBuilder; $this->_escaper = $escaper; $this->_coreFileStorageDatabase = $coreFileStorageDatabase; $this->_filesystem = $filesystem; $this->_rootDirectory = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem::ROOT_DIR); $this->_mediaDirectory = $this->_filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::MEDIA_DIR); $this->_fileSize = $fileSize; $this->_data = $data; parent::__construct($checkoutSession, $scopeConfig, $data); }