/** * Used in group of validateRow method's tests. * Set validator mock in importProduct, return true for isValid method. * * @param \Magento\CatalogImportExport\Model\Import\Product * @return \Magento\CatalogImportExport\Model\Import\Product\Validator|\PHPUnit_Framework_MockObject_MockObject */ private function _setValidatorMockInImportProduct($importProduct) { $this->validator->expects($this->once())->method('isValid')->willReturn(true); $this->setPropertyValue($importProduct, 'validator', $this->validator); return $importProduct; }
/** * 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); }
public function testInit() { $this->validator1->expects($this->once())->method('init'); $this->validator2->expects($this->once())->method('init'); $this->validator->init(); }
public function testInit() { $this->validatorOne->expects($this->once())->method('init'); $this->validatorTwo->expects($this->once())->method('init'); $this->validator->init(null); }
/** * 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); }