From 18fe79a555ca2fe52f7402f0eb3a5761500601b7 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 8 Oct 2019 09:33:26 +0200 Subject: [PATCH] Still output csv file, even if no rows are in it --- CRM/Export/BAO/ExportProcessor.php | 4 ++-- tests/phpunit/CRM/Export/BAO/ExportTest.php | 25 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 7d311aa7df9c..c1156a7e9246 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -2376,6 +2376,7 @@ public function writeCSVFromTable() { $query = "SELECT * FROM $exportTempTable"; + CRM_Core_Report_Excel::makeCSVTable($headerRows, [], NULL, TRUE, TRUE); while (1) { $limitQuery = $query . " LIMIT $offset, $limit @@ -2399,10 +2400,9 @@ public function writeCSVFromTable() { $headerRows, $componentDetails, NULL, - $writeHeader + FALSE ); - $writeHeader = FALSE; $offset += $limit; } } diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 537ed560bb6d..03947d2e1b91 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -1178,6 +1178,31 @@ public function testMergeSameAddressGreetingOptions() { $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 *