Skip to content

Commit

Permalink
Merge pull request #21363 from colemanw/searchDownloadTest
Browse files Browse the repository at this point in the history
SearchKit - Add 'array' option to download API and add test
  • Loading branch information
demeritcowboy authored Sep 4, 2021
2 parents 51d1b57 + 378b13c commit 49d2b8c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
24 changes: 20 additions & 4 deletions ext/search_kit/Civi/Api4/Action/SearchDisplay/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@
/**
* Download the results of a SearchDisplay as a spreadsheet.
*
* Note: unlike other APIs this action directly outputs a file.
* Note: unlike other APIs this action will directly output a file
* if 'format' is set to anything other than 'array'.
*
* @package Civi\Api4\Action\SearchDisplay
*/
class Download extends AbstractRunAction {

/**
* Requested file format
* Requested file format.
*
* 'array' will return a normal api result, with table headers as the first row.
* 'csv', etc. will directly output a file to the browser.
*
* @var string
* @required
* @options csv
* @options array,csv
*/
protected $format;
protected $format = 'array';

/**
* @param \Civi\Api4\Generic\Result $result
Expand Down Expand Up @@ -60,6 +65,17 @@ protected function processResult(\Civi\Api4\Generic\Result $result) {
$fileName = \CRM_Utils_File::makeFilenameWithUnicode($this->display['label']) . '.' . $this->format;

switch ($this->format) {
case 'array':
$result[] = $columns;
foreach ($rows as $data) {
$row = [];
foreach ($columns as $col) {
$row[] = $this->formatColumnValue($col, $data);
}
$result[] = $row;
}
return;

case 'csv':
$this->outputCSV($rows, $columns, $fileName);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,65 @@ public function setUpHeadless() {
->apply();
}

/**
* Test downloading array format.
*/
public function testDownloadArray() {
$lastName = uniqid(__FUNCTION__);
$sampleData = [
['first_name' => 'One', 'last_name' => $lastName],
['first_name' => 'Two', 'last_name' => $lastName],
['first_name' => 'Three', 'last_name' => $lastName],
['first_name' => 'Four', 'last_name' => $lastName],
];
Contact::save(FALSE)->setRecords($sampleData)->execute();

$params = [
'checkPermissions' => FALSE,
'format' => 'array',
'savedSearch' => [
'api_entity' => 'Contact',
'api_params' => [
'version' => 4,
'select' => ['last_name'],
'where' => [],
],
],
'display' => [
'type' => 'table',
'label' => '',
'settings' => [
'limit' => 2,
'actions' => TRUE,
'pager' => [],
'columns' => [
[
'key' => 'last_name',
'label' => 'First Last',
'dataType' => 'String',
'type' => 'field',
'rewrite' => '[first_name] [last_name]',
],
],
'sort' => [
['id', 'ASC'],
],
],
],
'filters' => ['last_name' => $lastName],
'afform' => NULL,
];

$download = (array) civicrm_api4('SearchDisplay', 'download', $params);
$header = array_shift($download);

$this->assertEquals('First Last', $header[0]['label']);

foreach ($download as $rowNum => $data) {
$this->assertEquals($sampleData[$rowNum]['first_name'] . ' ' . $lastName, $data[0]);
}
}

/**
* Test downloading CSV format.
*
Expand Down

0 comments on commit 49d2b8c

Please sign in to comment.