diff --git a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category.php b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category.php index 36e2910f..9c60a771 100644 --- a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category.php +++ b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category.php @@ -794,22 +794,22 @@ public function getWebsiteCodes() */ protected function _getParentCategory($rowData) { - $categoryParts = $this->_explodeEscaped('/',$rowData[self::COL_CATEGORY]); + $categoryParts = $this->_explodeEscaped('/', $rowData[self::COL_CATEGORY]); array_pop($categoryParts); - $parent = $this->_implodeEscaped('/',$categoryParts); + $parent = $this->_implodeEscaped('/', $categoryParts); - if ($parent) - { - if (isset($this->_categoriesWithRoots[$rowData[self::COL_ROOT]][$parent])) - { + if ($parent) { + if (isset($this->_categoriesWithRoots[$rowData[self::COL_ROOT]][$parent])) { return $this->_categoriesWithRoots[$rowData[self::COL_ROOT]][$parent]; } elseif (isset($this->_newCategory[$rowData[self::COL_ROOT]][$parent])) { return $this->_newCategory[$rowData[self::COL_ROOT]][$parent]; } else { return false; } - } else { + } elseif (isset($this->_categoriesWithRoots[$rowData[self::COL_ROOT]])) { return reset($this->_categoriesWithRoots[$rowData[self::COL_ROOT]]); + } else { + return false; } } @@ -876,9 +876,14 @@ public function validateRow(array $rowData, $rowNum) $root = $rowData[self::COL_ROOT]; $category = $rowData[self::COL_CATEGORY]; + //check if the root exists + if (! isset($this->_categoriesWithRoots[$root])) { + $this->addRowError(self::ERROR_INVALID_ROOT, $rowNum); + return false; + } + //check if parent category exists - if ($this->_getParentCategory($rowData) === false) - { + if ($this->_getParentCategory($rowData) === false) { $this->addRowError(self::ERROR_PARENT_NOT_FOUND, $rowNum); return false; @@ -898,14 +903,6 @@ public function validateRow(array $rowData, $rowNum) } } - //check if the root exists - if (! isset($this->_categoriesWithRoots[$root])) - { - $this->addRowError(self::ERROR_INVALID_ROOT, $rowNum); - return false; - } - - // check simple attributes foreach ($this->_attributes as $attrCode => $attrParams) { if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) { diff --git a/src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php b/src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php new file mode 100644 index 00000000..7617ff4f --- /dev/null +++ b/src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php @@ -0,0 +1,54 @@ +_model = $this->getModelMock( + 'fastsimpleimport/import_entity_category', + null, + false, + array(), + null, + false + ); + + parent::setUp(); + } + + /** + * @test + * @covers AvS_FastSimpleImport_Model_Import_Entity_Category::validateRow + * @uses AvS_FastSimpleImport_Model_Import_Entity_Category::_explodeEscaped + * @uses AvS_FastSimpleImport_Model_Import_Entity_Category::getRowScope + * @uses AvS_FastSimpleImport_Model_Import_Entity_Category::_getCategoryName + * @uses AvS_FastSimpleImport_Model_Import_Entity_Category::_filterRowData + */ + public function validateRowWrongRoot() + { + $row = array( + // Add a Salt to make sure it does not match any possible leftover from a failed fixture + '_root' => 'Not Default ' . substr(uniqid(), -6), + '_category' => 'Some category', + ); + + // Setup my own error message to prevent test for failing if it is changed later + $this->_model->addMessageTemplate( + AvS_FastSimpleImport_Model_Import_Entity_Category::ERROR_INVALID_ROOT, + 'Invalid Root' + ); + + // Assert Validation failed + $result = $this->_model->validateRow($row, 1); + $this->assertFalse($result); + + // Assert error message is correct + $errors = $this->_model->getErrorMessages(); + $this->assertArrayHasKey('Invalid Root', $errors); + } +} \ No newline at end of file