Still output csv file, even if no rows are in it
authoreileen <emcnaughton@wikimedia.org>
Tue, 8 Oct 2019 07:33:26 +0000 (09:33 +0200)
committereileen <emcnaughton@wikimedia.org>
Tue, 8 Oct 2019 07:39:56 +0000 (09:39 +0200)
CRM/Export/BAO/ExportProcessor.php
tests/phpunit/CRM/Export/BAO/ExportTest.php

index 7d311aa7df9c74f0a182347ddfc1091085fb18df..a9e02220fa9a3561f047425aaf074a69fe2ddfd0 100644 (file)
@@ -2376,6 +2376,7 @@ WHERE  id IN ( $deleteIDString )
 
     $query = "SELECT * FROM $exportTempTable";
 
+    $this->instantiateTempTable($headerRows);
     while (1) {
       $limitQuery = $query . "
 LIMIT $offset, $limit
@@ -2395,16 +2396,38 @@ LIMIT $offset, $limit
         }
         $componentDetails[] = $row;
       }
-      CRM_Core_Report_Excel::writeCSVFile($this->getExportFileName(),
-        $headerRows,
-        $componentDetails,
-        NULL,
-        $writeHeader
-      );
-
-      $writeHeader = FALSE;
+      $this->writeRows($headerRows, $componentDetails);
+
       $offset += $limit;
     }
   }
 
+  /**
+   * Set up the temp table.
+   *
+   * @param array $headerRows
+   */
+  protected function instantiateTempTable(array $headerRows) {
+    CRM_Utils_System::download(CRM_Utils_String::munge($this->getExportFileName()),
+      'text/x-csv',
+      CRM_Core_DAO::$_nullObject,
+      'csv',
+      FALSE
+    );
+    CRM_Core_Report_Excel::makeCSVTable($headerRows, [], NULL, TRUE, TRUE);
+  }
+
+  /**
+   * @param array $headerRows
+   * @param array $componentDetails
+   */
+  protected function writeRows(array $headerRows, array $componentDetails) {
+    CRM_Core_Report_Excel::writeCSVFile($this->getExportFileName(),
+      $headerRows,
+      $componentDetails,
+      NULL,
+      FALSE
+    );
+  }
+
 }
index 537ed560bb6d7b836c0c6cfc8f8c3b5a7cb0e518..03947d2e1b9152f95bddfa0aa3143cf6fe9af812 100644 (file)
@@ -1178,6 +1178,31 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     $this->assertCount(2, $this->csv);
   }
 
+  /**
+   * Test exporting when no rows are retrieved.
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \League\Csv\Exception
+   */
+  public function testExportNoRows() {
+    $contactA = $this->callAPISuccess('contact', 'create', [
+      'first_name' => 'John',
+      'last_name' => 'Doe',
+      'contact_type' => 'Individual',
+    ]);
+    $this->doExportTest([
+      'selectAll' => TRUE,
+      'ids' => [$contactA['id']],
+      'exportParams' => [
+        'postal_mailing_export' => [
+          'postal_mailing_export' => TRUE,
+        ],
+        'mergeSameAddress' => TRUE,
+      ],
+    ]);
+    $this->assertEquals('Contact ID', $this->csv->getHeader()[0]);
+  }
+
   /**
    * Test that deceased and do not mail contacts are removed from contacts before
    *