diff --git a/CRM/Import/DataSource/CSV.php b/CRM/Import/DataSource/CSV.php index 45b2ee63b320..aaad75f71767 100644 --- a/CRM/Import/DataSource/CSV.php +++ b/CRM/Import/DataSource/CSV.php @@ -214,6 +214,10 @@ private static function _CsvToTable( if (count($row) != $numColumns) { continue; } + // A blank line will be array(0 => NULL) + if ($row === [NULL]) { + continue; + } if (!$first) { $sql .= ', '; diff --git a/tests/phpunit/CRM/Import/DataSource/CsvTest.php b/tests/phpunit/CRM/Import/DataSource/CsvTest.php index a51eb1b0b943..f7d1dda34365 100644 --- a/tests/phpunit/CRM/Import/DataSource/CsvTest.php +++ b/tests/phpunit/CRM/Import/DataSource/CsvTest.php @@ -125,4 +125,34 @@ public function trimDataProvider(): array { ]; } + /** + * Test only one column and a blank line at the end, because + * fgetcsv will return the blank lines as array(0 => NULL) which is an + * edge case. Note if it has more than one column then the blank line gets + * skipped because of some checking for column-count matches in the import, + * and so you don't hit the current fail. + */ + public function testBlankLineAtEnd() { + $dataSource = new CRM_Import_DataSource_CSV(); + $params = [ + 'uploadFile' => [ + 'name' => __DIR__ . '/blankLineAtEnd.csv', + ], + 'skipColumnHeader' => TRUE, + ]; + + // Get the PEAR::DB object + $dao = new CRM_Core_DAO(); + $db = $dao->getDatabaseConnection(); + + $form = new CRM_Contact_Import_Form_DataSource(); + $form->controller = new CRM_Contact_Import_Controller(); + + $dataSource->postProcess($params, $db, $form); + $tableName = $form->get('importTableName'); + $json = json_encode(CRM_Core_DAO::singleValueQuery("SELECT email FROM $tableName")); + $this->assertEquals('"yogi@yellowstone.park"', $json); + CRM_Core_DAO::executeQuery("DROP TABLE $tableName"); + } + } diff --git a/tests/phpunit/CRM/Import/DataSource/blankLineAtEnd.csv b/tests/phpunit/CRM/Import/DataSource/blankLineAtEnd.csv new file mode 100644 index 000000000000..0228dfa515f8 --- /dev/null +++ b/tests/phpunit/CRM/Import/DataSource/blankLineAtEnd.csv @@ -0,0 +1,3 @@ +email +yogi@yellowstone.park +