From 96a327481acb3974d767f82a0c09ad350866c93d Mon Sep 17 00:00:00 2001 From: Barbazul Date: Mon, 6 Apr 2015 14:27:58 -0300 Subject: [PATCH 1/4] Added failing test --- .../Test/Model/Category/Import.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php 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..13b8361b --- /dev/null +++ b/src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php @@ -0,0 +1,38 @@ +_model = $this->getModelMock('fastsimpleimport/import_entity_category', null, false, array(), null, false); + parent::setUp(); + } + + /** + * @test + * @covers AvS_FastSimpleImport_Model_Import_Entity_Category::validateRow + * @group working + */ + public function testValidateRow() + { + $salt = substr(uniqid(), -6); + + $row = array( + '_root' => 'Not Default' . $salt, + '_category' => 'Some cat' . $salt, + ); + + // Assert Validation failed + $result = $this->_model->validateRow($row, 1); + $this->assertFalse($result); + + // Assert error message is correct + $errors = $this->_model->getErrorMessages(); + $this->assertArrayHasKey(AvS_FastSimpleImport_Model_Import_Entity_Category::ERROR_INVALID_ROOT, $errors); + } +} \ No newline at end of file From 77d5aed257f2576bd18149f7843fe602d780edbf Mon Sep 17 00:00:00 2001 From: Barbazul Date: Mon, 6 Apr 2015 14:35:45 -0300 Subject: [PATCH 2/4] Fixed error accessing invalid array key --- .../AvS/FastSimpleImport/Model/Import/Entity/Category.php | 4 +++- .../AvS/FastSimpleImport/Test/Model/Category/Import.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) 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..80cbee98 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 @@ -808,8 +808,10 @@ protected function _getParentCategory($rowData) } else { return false; } - } else { + } elseif (isset($this->_categoriesWithRoots[$rowData[self::COL_ROOT]])) { return reset($this->_categoriesWithRoots[$rowData[self::COL_ROOT]]); + } else { + return false; } } 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 index 13b8361b..4a25eec3 100644 --- a/src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php +++ b/src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php @@ -31,8 +31,10 @@ public function testValidateRow() $result = $this->_model->validateRow($row, 1); $this->assertFalse($result); + $this->_model->addMessageTemplate(AvS_FastSimpleImport_Model_Import_Entity_Category::ERROR_INVALID_ROOT, 'Invalid Root'); + // Assert error message is correct $errors = $this->_model->getErrorMessages(); - $this->assertArrayHasKey(AvS_FastSimpleImport_Model_Import_Entity_Category::ERROR_INVALID_ROOT, $errors); + $this->assertArrayHasKey('Invalid Root', $errors); } } \ No newline at end of file From 26ebb02ebae6610ebfd96d5a5a7748882b935594 Mon Sep 17 00:00:00 2001 From: Barbazul Date: Mon, 6 Apr 2015 14:40:43 -0300 Subject: [PATCH 3/4] Fixed validation error so the root category gets validated before the parent category --- .../Model/Import/Entity/Category.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 80cbee98..b1f277da 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 @@ -878,6 +878,13 @@ 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) { @@ -900,14 +907,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])) { From eefbb7d0d9017a1c298f9ab82ab8ec49d6694dfe Mon Sep 17 00:00:00 2001 From: Barbazul Date: Mon, 6 Apr 2015 15:16:32 -0300 Subject: [PATCH 4/4] Fixed Zend coding standard for modified lines --- .../Model/Import/Entity/Category.php | 16 ++++------ .../Test/Model/Category/Import.php | 32 +++++++++++++------ 2 files changed, 29 insertions(+), 19 deletions(-) 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 b1f277da..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,14 +794,12 @@ 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]; @@ -879,15 +877,13 @@ public function validateRow(array $rowData, $rowNum) $category = $rowData[self::COL_CATEGORY]; //check if the root exists - if (! isset($this->_categoriesWithRoots[$root])) - { + 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; 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 index 4a25eec3..7617ff4f 100644 --- a/src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php +++ b/src/app/code/community/AvS/FastSimpleImport/Test/Model/Category/Import.php @@ -9,30 +9,44 @@ class AvS_FastSimpleImport_Test_Model_Category_Import extends EcomDev_PHPUnit_Te public function setUp() { - $this->_model = $this->getModelMock('fastsimpleimport/import_entity_category', null, false, array(), null, false); + $this->_model = $this->getModelMock( + 'fastsimpleimport/import_entity_category', + null, + false, + array(), + null, + false + ); + parent::setUp(); } /** * @test * @covers AvS_FastSimpleImport_Model_Import_Entity_Category::validateRow - * @group working + * @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 testValidateRow() + public function validateRowWrongRoot() { - $salt = substr(uniqid(), -6); - $row = array( - '_root' => 'Not Default' . $salt, - '_category' => 'Some cat' . $salt, + // 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); - $this->_model->addMessageTemplate(AvS_FastSimpleImport_Model_Import_Entity_Category::ERROR_INVALID_ROOT, 'Invalid Root'); - // Assert error message is correct $errors = $this->_model->getErrorMessages(); $this->assertArrayHasKey('Invalid Root', $errors);