Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAGETWO-5015: Report error csv doesn't work when trying to import a csv file with semicolon delimiter. #11732

Merged
merged 1 commit into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/code/Magento/ImportExport/Helper/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace Magento\ImportExport\Helper;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Stdlib\DateTime;
use Magento\ImportExport\Model\Import;

/**
Expand Down Expand Up @@ -127,4 +126,14 @@ protected function getFilePath($filename)
{
return $this->varDirectory->getRelativePath(Import::IMPORT_HISTORY_DIR . $filename);
}

/**
* Get csv delimiter from request.
*
* @return string
*/
public function getDelimiter()
{
return $this->_request->getParam(Import::FIELD_FIELD_SEPARATOR, ',');
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/ImportExport/Model/Report/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ protected function createSourceCsvModel($sourceFile)
return $this->sourceCsvFactory->create(
[
'file' => $sourceFile,
'directory' => $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)
'directory' => $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR),
'delimiter' => $this->reportHelper->getDelimiter(),
]
);
}
Expand Down
25 changes: 25 additions & 0 deletions app/code/Magento/ImportExport/Test/Unit/Helper/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ class ReportTest extends \PHPUnit\Framework\TestCase
*/
protected $report;

/**
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;

/**
* Set up
*/
protected function setUp()
{
$this->context = $this->createMock(\Magento\Framework\App\Helper\Context::class);
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
->disableOriginalConstructor()
->getMock();
$this->context->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
$this->timezone = $this->createPartialMock(
\Magento\Framework\Stdlib\DateTime\Timezone::class,
['date', 'getConfigTimezone', 'diff', 'format']
Expand Down Expand Up @@ -159,4 +168,20 @@ public function testGetReportSize()
$result = $this->report->getReportSize('file');
$this->assertNull($result);
}

/**
* Test getDelimiter() take into consideration request param '_import_field_separator'.
*/
public function testGetDelimiter()
{
$testDelimiter = 'some delimiter';
$this->requestMock->expects($this->once())
->method('getParam')
->with($this->identicalTo(\Magento\ImportExport\Model\Import::FIELD_FIELD_SEPARATOR))
->willReturn($testDelimiter);
$this->assertEquals(
$testDelimiter,
$this->report->getDelimiter()
);
}
}
14 changes: 13 additions & 1 deletion app/code/Magento/ImportExport/Test/Unit/Model/Report/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class CsvTest extends \PHPUnit\Framework\TestCase
protected function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$testDelimiter = 'some_delimiter';

$this->reportHelperMock = $this->createMock(\Magento\ImportExport\Helper\Report::class);
$this->reportHelperMock->expects($this->any())->method('getDelimiter')->willReturn($testDelimiter);

$this->outputCsvFactoryMock = $this->createPartialMock(
\Magento\ImportExport\Model\Export\Adapter\CsvFactory::class,
Expand All @@ -65,7 +67,17 @@ protected function setUp()
[23 => 'first error'],
[27 => 'second error']
);
$this->sourceCsvFactoryMock->expects($this->any())->method('create')->willReturn($this->sourceCsvMock);
$this->sourceCsvFactoryMock
->expects($this->any())
->method('create')
->with(
[
'file' => 'some_file_name',
'directory' => null,
'delimiter' => $testDelimiter
]
)
->willReturn($this->sourceCsvMock);

$this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class ValidateTest extends \Magento\TestFramework\TestCase\AbstractBackendContro
* @dataProvider validationDataProvider
* @param string $fileName
* @param string $message
* @param string $delimiter
* @backupGlobals enabled
* @magentoDbIsolation enabled
*/
public function testValidationReturn($fileName, $message)
public function testValidationReturn($fileName, $message, $delimiter)
{
$validationStrategy = ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_STOP_ON_ERROR;

Expand All @@ -36,7 +37,7 @@ public function testValidationReturn($fileName, $message)
$this->getRequest()->setPostValue('behavior', 'append');
$this->getRequest()->setPostValue(Import::FIELD_NAME_VALIDATION_STRATEGY, $validationStrategy);
$this->getRequest()->setPostValue(Import::FIELD_NAME_ALLOWED_ERROR_COUNT, 0);
$this->getRequest()->setPostValue('_import_field_separator', ',');
$this->getRequest()->setPostValue('_import_field_separator', $delimiter);

/** @var \Magento\TestFramework\App\Filesystem $filesystem */
$filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
Expand Down Expand Up @@ -83,12 +84,24 @@ public function validationDataProvider()
return [
[
'file_name' => 'catalog_product.csv',
'message' => 'File is valid'
'message' => 'File is valid',
'delimiter' => ',',
],
[
'file_name' => 'test.txt',
'message' => '\'txt\' file extension is not supported'
]
'message' => '\'txt\' file extension is not supported',
'delimiter' => ',',
],
[
'file_name' => 'incorrect_catalog_product_comma.csv',
'message' => 'Download full report',
'delimiter' => ',',
],
[
'file_name' => 'incorrect_catalog_product_semicolon.csv',
'message' => 'Download full report',
'delimiter' => ';',
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sku,store_view_code,attribute_set_code,product_type,categories,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,base_image,base_image_label,small_image,small_image_label,thumbnail_image,thumbnail_image_label,swatch_image,swatch_image_label,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,related_position,crosssell_skus,crosssell_position,upsell_skus,upsell_position,additional_images,additional_image_labels,hide_from_product_page,custom_options,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values,bundle_shipment_type,configurable_variations,configurable_variation_labels,associated_skus
Simple,,Default,simple,"Default Category/New",base,Simple,,,,1,"Taxable Goods","Catalo g, Search",100.0000,,,,simple,Simple,Simple,"Simple ",,,,,,,,,"10/25/17, 8:21 AM","10/25/17, 8:21 AM",,,"Block after Info Column",,,,"Use config",,,,,,,,,,100.0000,0.0000,1,0,0,1,1.0000,1,10000.0000,1,1,1.0000,1,1,1,1,1.0000,1,0,0,0,,,,,,,,,,,,,,,,,,,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sku;store_view_code;attribute_set_code;product_type;categories;product_websites;name;description;short_description;weight;product_online;tax_class_name;visibility;price;special_price;special_price_from_date;special_price_to_date;url_key;meta_title;meta_keywords;meta_description;base_image;base_image_label;small_image;small_image_label;thumbnail_image;thumbnail_image_label;swatch_image;swatch_image_label;created_at;updated_at;new_from_date;new_to_date;display_product_options_in;map_price;msrp_price;map_enabled;gift_message_available;custom_design;custom_design_from;custom_design_to;custom_layout_update;page_layout;product_options_container;msrp_display_actual_price_type;country_of_manufacture;additional_attributes;qty;out_of_stock_qty;use_config_min_qty;is_qty_decimal;allow_backorders;use_config_backorders;min_cart_qty;use_config_min_sale_qty;max_cart_qty;use_config_max_sale_qty;is_in_stock;notify_on_stock_below;use_config_notify_stock_qty;manage_stock;use_config_manage_stock;use_config_qty_increments;qty_increments;use_config_enable_qty_inc;enable_qty_increments;is_decimal_divided;website_id;related_skus;related_position;crosssell_skus;crosssell_position;upsell_skus;upsell_position;additional_images;additional_image_labels;hide_from_product_page;custom_options;bundle_price_type;bundle_sku_type;bundle_price_view;bundle_weight_type;bundle_values;bundle_shipment_type;configurable_variations;configurable_variation_labels;associated_skus
Simple;;Default;simple;"Default Category/New";base;Simple;;;;1;"Taxable Goods";"Catalo g, Search";100.0000;;;;simple;Simple;Simple;"Simple ";;;;;;;;;"10/25/17, 8,21 AM";"10/25/17, 8,21 AM";;;"Block after Info Column";;;;"Use config";;;;;;;;;;100.0000;0.0000;1;0;0;1;1.0000;1;10000.0000;1;1;1.0000;1;1;1;1;1.0000;1;0;0;0;;;;;;;;;;;;;;;;;;;