From: demeritcowboy Date: Sun, 22 Aug 2021 14:10:57 +0000 (-0400) Subject: avoid crash with one column and blank lines X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=460f7b9b4143b9c60134bfc4ab1e04df0c0af108;p=civicrm-core.git avoid crash with one column and blank lines --- diff --git a/CRM/Import/DataSource/CSV.php b/CRM/Import/DataSource/CSV.php index 45b2ee63b3..aaad75f717 100644 --- a/CRM/Import/DataSource/CSV.php +++ b/CRM/Import/DataSource/CSV.php @@ -214,6 +214,10 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { 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 a51eb1b0b9..f7d1dda343 100644 --- a/tests/phpunit/CRM/Import/DataSource/CsvTest.php +++ b/tests/phpunit/CRM/Import/DataSource/CsvTest.php @@ -125,4 +125,34 @@ class CRM_Import_DataSource_CsvTest extends CiviUnitTestCase { ]; } + /** + * 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 0000000000..0228dfa515 --- /dev/null +++ b/tests/phpunit/CRM/Import/DataSource/blankLineAtEnd.csv @@ -0,0 +1,3 @@ +email +yogi@yellowstone.park +