diff --git a/lib/csvlib.class.php b/lib/csvlib.class.php index be19b377b37..5e2e94b81ba 100644 --- a/lib/csvlib.class.php +++ b/lib/csvlib.class.php @@ -110,7 +110,15 @@ class csv_import_reader { // str_getcsv doesn't iterate through the csv data properly. It has // problems with line returns. while ($fgetdata = fgetcsv($fp, 0, $csv_delimiter, $enclosure)) { - $columns[] = $fgetdata; + // Check to see if we have an empty line. + if (count($fgetdata) == 1) { + if ($fgetdata[0] !== null) { + // The element has data. Add it to the array. + $columns[] = $fgetdata; + } + } else { + $columns[] = $fgetdata; + } } $col_count = 0; diff --git a/lib/tests/csvclass_test.php b/lib/tests/csvclass_test.php index e990dfd5037..4ab71c0e5fb 100644 --- a/lib/tests/csvclass_test.php +++ b/lib/tests/csvclass_test.php @@ -34,6 +34,7 @@ class csvclass_testcase extends advanced_testcase { var $teststring = ''; var $teststring2 = ''; var $teststring3 = ''; + var $teststring4 = ''; protected function setUp(){ @@ -64,6 +65,16 @@ class csvclass_testcase extends advanced_testcase { $this->teststring2 = 'fullname,"description of things",beer "Fred Flint","

Find the stone inside the box

",Asahi,"A fourth column" "Sarah Smith","

How are the people next door?

,Yebisu,"Forget the next" +'; + + $this->teststring4 = 'fullname,"description of things",beer +"Douglas Dirk","

I am fine, thankyou.

",Becks + +"Addelyn Francis","

Thanks for the cake

",Becks +"Josh Frankson","

Everything is fine

",Asahi + + +"Heath Forscyth","

We are going to make you lose your mind

",Fosters '; } @@ -126,5 +137,12 @@ class csvclass_testcase extends advanced_testcase { $contentcount = $csvimport->load_csv_content($tabdata, 'utf-8', 'tab'); // This should import four rows including the headings. $this->assertEquals($contentcount, 4); + + // Testing for empty lines. + $iid = csv_import_reader::get_new_iid('blanklines'); + $csvimport = new csv_import_reader($iid, 'blanklines'); + $contentcount = $csvimport->load_csv_content($this->teststring4, 'utf-8', 'comma'); + // Five lines including the headings should be imported. + $this->assertEquals($contentcount, 5); } }