Skip to content

Commit

Permalink
Merge pull request #21216 from demeritcowboy/import-csv-blankline
Browse files Browse the repository at this point in the history
Alternate to 20131 - Avoid crash during import for blank lines in a one-column csv file
  • Loading branch information
mlutfy authored Aug 23, 2021
2 parents 60df685 + 460f7b9 commit 653ceed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CRM/Import/DataSource/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= ', ';
Expand Down
30 changes: 30 additions & 0 deletions tests/phpunit/CRM/Import/DataSource/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('"[email protected]"', $json);
CRM_Core_DAO::executeQuery("DROP TABLE $tableName");
}

}
3 changes: 3 additions & 0 deletions tests/phpunit/CRM/Import/DataSource/blankLineAtEnd.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
email
[email protected]

0 comments on commit 653ceed

Please sign in to comment.