Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function isValid($value)
 {
     $this->_clearMessages();
     if (!empty($value['_product_websites']) && !$this->storeResolver->getWebsiteCodeToId($value['_product_websites'])) {
         $this->_addMessages([self::ERROR_INVALID_WEBSITE]);
         return false;
     }
     return true;
 }
Esempio n. 2
0
 /**
  * Validate by website type
  *
  * @param array $value
  * @param string $websiteCode
  * @return bool
  */
 protected function isWebsiteValid($value, $websiteCode)
 {
     if (isset($value[$websiteCode]) && !empty($value[$websiteCode])) {
         if ($value[$websiteCode] != $this->getAllWebsitesValue() && !$this->storeResolver->getWebsiteCodeToId($value[$websiteCode])) {
             return false;
         }
     }
     return true;
 }
Esempio n. 3
0
 /**
  * @param array $data
  * @param int $groupId
  * @param array|null $website
  * @param array $expected
  * @dataProvider tierPriceDataProvider
  */
 public function testIsValid($data, $groupId, $website, $expected)
 {
     $this->processInit($groupId);
     if ($website) {
         $this->storeResolver->expects($this->any())->method('getWebsiteCodeToId')->with($website['id'])->willReturn($website['code']);
     }
     $result = $this->tierPrice->isValid($data);
     $this->assertEquals($expected['result'], $result);
     $messages = $this->tierPrice->getMessages();
     $this->assertEquals($expected['messages'], $messages);
 }
Esempio n. 4
0
 /**
  * Validate value
  *
  * @param mixed $value
  * @return bool
  */
 public function isValid($value)
 {
     $this->_clearMessages();
     if ($value[AdvancedPricing::COL_TIER_PRICE_WEBSITE] != $this->getAllWebsitesValue() && $value[AdvancedPricing::COL_GROUP_PRICE_WEBSITE] != $this->getAllWebsitesValue()) {
         if (!empty($value[AdvancedPricing::COL_TIER_PRICE_WEBSITE]) && !$this->storeResolver->getWebsiteCodeToId($value[AdvancedPricing::COL_TIER_PRICE_WEBSITE]) || !empty($value[AdvancedPricing::COL_GROUP_PRICE_WEBSITE]) && !$this->storeResolver->getWebsiteCodeToId($value[AdvancedPricing::COL_GROUP_PRICE_WEBSITE])) {
             $this->_addMessages([self::ERROR_INVALID_WEBSITE]);
             return false;
         }
     }
     return true;
 }
 public function testIsValidReturnAddMessagesCall()
 {
     $value = [AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'tier value'];
     $allWebsitesValue = 'not tier|group price website value';
     $colTierPriceWebsite = false;
     $expectedMessages = [AdvancedPricing\Validator\Website::ERROR_INVALID_WEBSITE];
     $this->website->expects($this->once())->method('_clearMessages');
     $this->website->expects($this->any())->method('getAllWebsitesValue')->willReturn($allWebsitesValue);
     $this->storeResolver->method('getWebsiteCodeToId')->willReturnMap([[$value[AdvancedPricing::COL_TIER_PRICE_WEBSITE], $colTierPriceWebsite]]);
     $this->website->expects($this->any())->method('_addMessages')->with($expectedMessages);
     $this->website->isValid($value);
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function isValid($value)
 {
     $this->_clearMessages();
     if (empty($value[ImportProduct::COL_PRODUCT_WEBSITES])) {
         return true;
     }
     $separator = $this->context->getMultipleValueSeparator();
     $websites = explode($separator, $value[ImportProduct::COL_PRODUCT_WEBSITES]);
     foreach ($websites as $website) {
         if (!$this->storeResolver->getWebsiteCodeToId($website)) {
             $this->_addMessages([self::ERROR_INVALID_WEBSITE]);
             return false;
         }
     }
     return true;
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function isValid($value)
 {
     $this->_clearMessages();
     if (isset($value['_group_price_website']) && strlen($value['_group_price_website']) || isset($value['_group_price_customer_group']) && strlen($value['_group_price_customer_group']) || isset($value['_group_price_price']) && strlen($value['_group_price_price'])) {
         if (!isset($value['_group_price_website']) || !isset($value['_group_price_customer_group']) || !strlen($value['_group_price_website']) || !strlen($value['_group_price_customer_group']) || !strlen($value['_group_price_price'])) {
             $this->_addMessages([self::ERROR_GROUP_PRICE_DATA_INCOMPLETE]);
             return false;
         } elseif ($value['_group_price_website'] != self::VALUE_ALL && !$this->storeResolver->getWebsiteCodeToId($value['_group_price_website'])) {
             $this->_addMessages([self::ERROR_INVALID_GROUP_PRICE_SITE]);
             return false;
         } elseif ($value['_group_price_customer_group'] != self::VALUE_ALL && !isset($this->customerGroups[$value['_group_price_customer_group']])) {
             $this->_addMessages([self::ERROR_INVALID_GROUP_PRICE_GROUP]);
             return false;
         }
     }
     return true;
 }
Esempio n. 8
0
 /**
  * @dataProvider validateRowCheckSpecifiedSkuDataProvider
  */
 public function testValidateRowCheckSpecifiedSku($sku, $expectedError)
 {
     $importProduct = $this->createModelMockWithErrorAggregator(['addRowError', 'getOptionEntity', 'getRowScope'], ['isRowInvalid' => true]);
     $rowNum = 0;
     $rowData = [\Magento\CatalogImportExport\Model\Import\Product::COL_SKU => $sku, \Magento\CatalogImportExport\Model\Import\Product::COL_STORE => ''];
     $this->storeResolver->expects($this->any())->method('getStoreCodeToId')->willReturn(null);
     $this->setPropertyValue($importProduct, 'storeResolver', $this->storeResolver);
     $this->setPropertyValue($importProduct, 'skuProcessor', $this->skuProcessor);
     $this->_suppressValidateRowOptionValidatorInvalidRows($importProduct);
     $importProduct->expects($this->once())->method('getRowScope')->willReturn(\Magento\CatalogImportExport\Model\Import\Product::SCOPE_STORE);
     $importProduct->expects($this->at(1))->method('addRowError')->with($expectedError, $rowNum)->willReturn(null);
     $importProduct->validateRow($rowData, $rowNum);
 }
Esempio n. 9
0
 /**
  * Validate data row.
  *
  * @param array $rowData
  * @param int $rowNum
  * @return boolean
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function validateRow(array $rowData, $rowNum)
 {
     if (isset($this->_validatedRows[$rowNum])) {
         // check that row is already validated
         return !$this->getErrorAggregator()->isRowInvalid($rowNum);
     }
     $this->_validatedRows[$rowNum] = true;
     $rowScope = $this->getRowScope($rowData);
     // BEHAVIOR_DELETE use specific validation logic
     if (\Magento\ImportExport\Model\Import::BEHAVIOR_DELETE == $this->getBehavior()) {
         if (self::SCOPE_DEFAULT == $rowScope && !isset($this->_oldSku[$rowData[self::COL_SKU]])) {
             $this->addRowError(ValidatorInterface::ERROR_SKU_NOT_FOUND_FOR_DELETE, $rowNum);
             return false;
         }
         return true;
     }
     if (!$this->validator->isValid($rowData)) {
         foreach ($this->validator->getMessages() as $message) {
             $this->addRowError($message, $rowNum);
         }
     }
     $sku = $rowData[self::COL_SKU];
     if (null === $sku) {
         $this->addRowError(ValidatorInterface::ERROR_SKU_IS_EMPTY, $rowNum);
     } elseif (false === $sku) {
         $this->addRowError(ValidatorInterface::ERROR_ROW_IS_ORPHAN, $rowNum);
     } elseif (self::SCOPE_STORE == $rowScope && !$this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE])) {
         $this->addRowError(ValidatorInterface::ERROR_INVALID_STORE, $rowNum);
     }
     // SKU is specified, row is SCOPE_DEFAULT, new product block begins
     $this->_processedEntitiesCount++;
     $sku = $rowData[self::COL_SKU];
     if (isset($this->_oldSku[$sku])) {
         // can we get all necessary data from existent DB product?
         // check for supported type of existing product
         if (isset($this->_productTypeModels[$this->_oldSku[$sku]['type_id']])) {
             $this->skuProcessor->addNewSku($sku, ['entity_id' => $this->_oldSku[$sku]['entity_id'], 'type_id' => $this->_oldSku[$sku]['type_id'], 'attr_set_id' => $this->_oldSku[$sku]['attr_set_id'], 'attr_set_code' => $this->_attrSetIdToName[$this->_oldSku[$sku]['attr_set_id']]]);
         } else {
             $this->addRowError(ValidatorInterface::ERROR_TYPE_UNSUPPORTED, $rowNum);
             // child rows of legacy products with unsupported types are orphans
             $sku = false;
         }
     } else {
         // validate new product type and attribute set
         if (!isset($rowData[self::COL_TYPE]) || !isset($this->_productTypeModels[$rowData[self::COL_TYPE]])) {
             $this->addRowError(ValidatorInterface::ERROR_INVALID_TYPE, $rowNum);
         } elseif (!isset($rowData[self::COL_ATTR_SET]) || !isset($this->_attrSetNameToId[$rowData[self::COL_ATTR_SET]])) {
             $this->addRowError(ValidatorInterface::ERROR_INVALID_ATTR_SET, $rowNum);
         } elseif (is_null($this->skuProcessor->getNewSku($sku))) {
             $this->skuProcessor->addNewSku($sku, ['entity_id' => null, 'type_id' => $rowData[self::COL_TYPE], 'attr_set_id' => $this->_attrSetNameToId[$rowData[self::COL_ATTR_SET]], 'attr_set_code' => $rowData[self::COL_ATTR_SET]]);
         }
         if ($this->getErrorAggregator()->isRowInvalid($rowNum)) {
             // mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already
             $sku = false;
         }
     }
     if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) {
         $newSku = $this->skuProcessor->getNewSku($sku);
         // set attribute set code into row data for followed attribute validation in type model
         $rowData[self::COL_ATTR_SET] = $newSku['attr_set_code'];
         $rowAttributesValid = $this->_productTypeModels[$newSku['type_id']]->isRowValid($rowData, $rowNum, !isset($this->_oldSku[$sku]));
         if (!$rowAttributesValid && self::SCOPE_DEFAULT == $rowScope) {
             // mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already
             $sku = false;
         }
     }
     // validate custom options
     $this->getOptionEntity()->validateRow($rowData, $rowNum);
     return !$this->getErrorAggregator()->isRowInvalid($rowNum);
 }
Esempio n. 10
0
 /**
  * @dataProvider validateRowCheckSpecifiedSkuDataProvider
  */
 public function testValidateRowCheckSpecifiedSku($sku, $expectedError)
 {
     $importProduct = $this->getMockBuilder('\\Magento\\CatalogImportExport\\Model\\Import\\Product')->disableOriginalConstructor()->setMethods(['addRowError', 'getOptionEntity', 'getRowScope'])->getMock();
     $rowNum = 0;
     $rowData = [\Magento\CatalogImportExport\Model\Import\Product::COL_SKU => $sku, \Magento\CatalogImportExport\Model\Import\Product::COL_STORE => ''];
     $this->storeResolver->expects($this->any())->method('getStoreCodeToId')->willReturn(null);
     $this->setPropertyValue($importProduct, 'storeResolver', $this->storeResolver);
     $this->setPropertyValue($importProduct, 'skuProcessor', $this->skuProcessor);
     $this->_suppressValidateRowOptionValidatorInvalidRows($importProduct);
     $importProduct->expects($this->once())->method('getRowScope')->willReturn(\Magento\CatalogImportExport\Model\Import\Product::SCOPE_STORE);
     $importProduct->expects($this->at(1))->method('addRowError')->with($expectedError, $rowNum)->willReturn(null);
     $importProduct->validateRow($rowData, $rowNum);
 }
Esempio n. 11
0
 /**
  * Get website id by code
  *
  * @param string $websiteCode
  * @return array|int|string
  */
 protected function getWebSiteId($websiteCode)
 {
     $result = $websiteCode == $this->_getValidator(self::VALIDATOR_WEBSITE)->getAllWebsitesValue() || $this->_catalogData->isPriceGlobal() ? 0 : $this->_storeResolver->getWebsiteCodeToId($websiteCode);
     return $result;
 }
 /**
  * Validate data row.
  *
  * @param array $rowData
  * @param int $rowNum
  * @return boolean
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function validateRow(array $rowData, $rowNum)
 {
     if (isset($this->_validatedRows[$rowNum])) {
         // check that row is already validated
         return !$this->getErrorAggregator()->isRowInvalid($rowNum);
     }
     $this->_validatedRows[$rowNum] = true;
     $rowScope = $this->getRowScope($rowData);
     // BEHAVIOR_DELETE and BEHAVIOR_REPLACE use specific validation logic
     if (Import::BEHAVIOR_REPLACE == $this->getBehavior()) {
         if (self::SCOPE_DEFAULT == $rowScope && !isset($this->_oldSku[$rowData[self::COL_SKU]])) {
             $this->addRowError(ValidatorInterface::ERROR_SKU_NOT_FOUND_FOR_DELETE, $rowNum);
             return false;
         }
     }
     if (Import::BEHAVIOR_DELETE == $this->getBehavior()) {
         if (self::SCOPE_DEFAULT == $rowScope && !isset($this->_oldSku[$rowData[self::COL_SKU]])) {
             $this->addRowError(ValidatorInterface::ERROR_SKU_NOT_FOUND_FOR_DELETE, $rowNum);
             return false;
         }
         return true;
     }
     if (!$this->validator->isValid($rowData)) {
         foreach ($this->validator->getMessages() as $message) {
             $this->addRowError($message, $rowNum, $this->validator->getInvalidAttribute());
         }
     }
     $sku = $rowData[self::COL_SKU];
     if (null === $sku) {
         $this->addRowError(ValidatorInterface::ERROR_SKU_IS_EMPTY, $rowNum);
     } elseif (false === $sku) {
         $this->addRowError(ValidatorInterface::ERROR_ROW_IS_ORPHAN, $rowNum);
     } elseif (self::SCOPE_STORE == $rowScope && !$this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE])) {
         $this->addRowError(ValidatorInterface::ERROR_INVALID_STORE, $rowNum);
     }
     // SKU is specified, row is SCOPE_DEFAULT, new product block begins
     $this->_processedEntitiesCount++;
     $sku = $rowData[self::COL_SKU];
     if (isset($this->_oldSku[$sku])) {
         // can we get all necessary data from existent DB product?
         // check for supported type of existing product
         if (isset($this->_productTypeModels[$this->_oldSku[$sku]['type_id']])) {
             $this->skuProcessor->addNewSku($sku, $this->prepareNewSkuData($sku));
         } else {
             $this->addRowError(ValidatorInterface::ERROR_TYPE_UNSUPPORTED, $rowNum);
             // child rows of legacy products with unsupported types are orphans
             $sku = false;
         }
     } else {
         // validate new product type and attribute set
         if (!isset($rowData[self::COL_TYPE]) || !isset($this->_productTypeModels[$rowData[self::COL_TYPE]])) {
             $this->addRowError(ValidatorInterface::ERROR_INVALID_TYPE, $rowNum);
         } elseif (!isset($rowData[self::COL_ATTR_SET]) || !isset($this->_attrSetNameToId[$rowData[self::COL_ATTR_SET]])) {
             $this->addRowError(ValidatorInterface::ERROR_INVALID_ATTR_SET, $rowNum);
         } elseif (is_null($this->skuProcessor->getNewSku($sku))) {
             $this->skuProcessor->addNewSku($sku, ['row_id' => null, 'entity_id' => null, 'type_id' => $rowData[self::COL_TYPE], 'attr_set_id' => $this->_attrSetNameToId[$rowData[self::COL_ATTR_SET]], 'attr_set_code' => $rowData[self::COL_ATTR_SET]]);
         }
         if ($this->getErrorAggregator()->isRowInvalid($rowNum)) {
             // mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already
             $sku = false;
         }
     }
     if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) {
         $newSku = $this->skuProcessor->getNewSku($sku);
         // set attribute set code into row data for followed attribute validation in type model
         $rowData[self::COL_ATTR_SET] = $newSku['attr_set_code'];
         $rowAttributesValid = $this->_productTypeModels[$newSku['type_id']]->isRowValid($rowData, $rowNum, !isset($this->_oldSku[$sku]));
         if (!$rowAttributesValid && self::SCOPE_DEFAULT == $rowScope) {
             // mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already
             $sku = false;
         }
     }
     // validate custom options
     $this->getOptionEntity()->validateRow($rowData, $rowNum);
     if (!empty($rowData[self::URL_KEY]) || !empty($rowData[self::COL_NAME])) {
         $urlKey = $this->getUrlKey($rowData);
         $storeCodes = empty($rowData[self::COL_STORE_VIEW_CODE]) ? array_flip($this->storeResolver->getStoreCodeToId()) : explode($this->getMultipleValueSeparator(), $rowData[self::COL_STORE_VIEW_CODE]);
         foreach ($storeCodes as $storeCode) {
             $storeId = $this->storeResolver->getStoreCodeToId($storeCode);
             $productUrlSuffix = $this->getProductUrlSuffix($storeId);
             $urlPath = $urlKey . $productUrlSuffix;
             if (empty($this->urlKeys[$storeId][$urlPath]) || $this->urlKeys[$storeId][$urlPath] == $rowData[self::COL_SKU]) {
                 $this->urlKeys[$storeId][$urlPath] = $rowData[self::COL_SKU];
                 $this->rowNumbers[$storeId][$urlPath] = $rowNum;
             } else {
                 $this->addRowError(ValidatorInterface::ERROR_DUPLICATE_URL_KEY, $rowNum);
             }
         }
     }
     return !$this->getErrorAggregator()->isRowInvalid($rowNum);
 }