From de889434dad7cabe2642b561d5599abf2526f14c Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 29 Sep 2008 00:57:02 +0000 Subject: [PATCH] Let the product importer import files with windows style line endings. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Asset/Wobject/Shelf.pm | 2 ++ t/Asset/Wobject/Shelf.t | 26 ++++++++++++++++++- .../productTables/windowsTable.csv | 4 +++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 t/supporting_collateral/productTables/windowsTable.csv diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 61862cf35..a05119702 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -61,6 +61,7 @@ - rfe: Teach Thingy to be a Web Service (SDH Consulting Group) - fixed: ~~~ showing up on my site. - fixed: Shelf/Product import export does not work + - fixed: Shelf/Product import does not work with windows files 7.5.21 - fixed: purchase detail screen shows incorrectly in Safari diff --git a/lib/WebGUI/Asset/Wobject/Shelf.pm b/lib/WebGUI/Asset/Wobject/Shelf.pm index 50cc87b58..83dd7b82f 100644 --- a/lib/WebGUI/Asset/Wobject/Shelf.pm +++ b/lib/WebGUI/Asset/Wobject/Shelf.pm @@ -158,6 +158,7 @@ sub importProducts { my $headers; $headers = <$table>; chomp $headers; + $headers =~ tr/\r//d; my @headers = WebGUI::Text::splitCSV($headers); WebGUI::Error::InvalidFile->throw(error => qq{Bad header found in the CSV file}, brokenFile => $filePath) unless (join(q{-}, sort @headers) eq 'mastersku-price-quantity-shortdescription-title-varSku-weight') @@ -167,6 +168,7 @@ sub importProducts { my $line = 1; while (my $productRow = <$table>) { chomp $productRow; + $productRow =~ tr/\r//d; $productRow =~ s/\s*#.+$//; next unless $productRow; local $_; diff --git a/t/Asset/Wobject/Shelf.t b/t/Asset/Wobject/Shelf.t index 6f452ed1e..4b8d66f42 100644 --- a/t/Asset/Wobject/Shelf.t +++ b/t/Asset/Wobject/Shelf.t @@ -33,7 +33,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 45; +my $tests = 48; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -391,6 +391,30 @@ SKIP: { $e = Exception::Class->caught(); is($e, '', 'No exception thrown on a file with quoted fields'); is($shelf2->getChildCount, 3, 'imported 3 children skus for shelf2 with quoted fields'); + + $shelf2->purge; + undef $shelf2; + + ####################################################################### + # + # import, windows line endings + # + ####################################################################### + + $shelf2 = WebGUI::Asset->getRoot($session)->addChild({className => $class}); + + $pass = 0; + eval { + $pass = $shelf2->importProducts( + WebGUI::Test->getTestCollateralPath('productTables/windowsTable.csv'), + ); + }; + ok($pass, 'Able to load a table with windows style newlines'); + $e = Exception::Class->caught(); + is($e, '', 'No exception thrown on a file with quoted fields'); + is($shelf2->getChildCount, 2, 'imported 2 children skus for shelf2 with windows line endings fields'); + + $shelf2->purge; } #---------------------------------------------------------------------------- diff --git a/t/supporting_collateral/productTables/windowsTable.csv b/t/supporting_collateral/productTables/windowsTable.csv new file mode 100644 index 000000000..6cd1e5aff --- /dev/null +++ b/t/supporting_collateral/productTables/windowsTable.csv @@ -0,0 +1,4 @@ +"mastersku","varSku","title","shortdescription","price","weight","quantity" +"Software","Windows-Vista","Windows Vista","Bad software",120,5.00,5000 +"Software","Windows-XP","Windows XP","Mainly bad software",80,4.50,2000 +"Hardware","X-box","X-box","Very expensive hardware",280,10,150