Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#1293 Still output csv file on export, even if no rows are in it #15440

Merged
merged 1 commit into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,7 @@ public function writeCSVFromTable() {

$query = "SELECT * FROM $exportTempTable";

$this->instantiateTempTable($headerRows);
while (1) {
$limitQuery = $query . "
LIMIT $offset, $limit
Expand All @@ -2395,16 +2396,38 @@ public function writeCSVFromTable() {
}
$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
);
}

}
25 changes: 25 additions & 0 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down