avoid crash with one column and blank lines
authordemeritcowboy <demeritcowboy@hotmail.com>
Sun, 22 Aug 2021 14:10:57 +0000 (10:10 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Sun, 22 Aug 2021 14:10:57 +0000 (10:10 -0400)
CRM/Import/DataSource/CSV.php
tests/phpunit/CRM/Import/DataSource/CsvTest.php
tests/phpunit/CRM/Import/DataSource/blankLineAtEnd.csv [new file with mode: 0644]

index 45b2ee63b320d026306b9297a639477c1ca04be1..aaad75f71767d00c2f3cb760966f89b44d71bc69 100644 (file)
@@ -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 .= ', ';
index a51eb1b0b943b8a93d7a32c01d5ff7b74c1ba0f3..f7d1dda343656a4cd266b8ffc01c103bff97ada9 100644 (file)
@@ -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 (file)
index 0000000..0228dfa
--- /dev/null
@@ -0,0 +1,3 @@
+email
+yogi@yellowstone.park
+