Add getPreview function
authoreileen <emcnaughton@wikimedia.org>
Wed, 10 Jul 2019 14:13:43 +0000 (02:13 +1200)
committerColeman Watts <coleman@civicrm.org>
Fri, 12 Jul 2019 13:57:08 +0000 (09:57 -0400)
CRM/Export/BAO/ExportProcessor.php
tests/phpunit/CRM/Export/BAO/ExportTest.php

index 609e6471b7d14a95c1a51df537d67c288892bb44..06e5c19c0cef15e23f6f2474f16a0243bcee1c96 100644 (file)
@@ -2114,4 +2114,23 @@ WHERE  id IN ( $deleteIDString )
     return $parsedString;
   }
 
+  /**
+   * Preview export output.
+   *
+   * @param int $limit
+   * @return array
+   */
+  public function getPreview($limit) {
+    $rows = [];
+    list($outputColumns, $metadata) = $this->getExportStructureArrays();
+    $query = $this->runQuery([], '');
+    CRM_Core_DAO::disableFullGroupByMode();
+    $result = CRM_Core_DAO::executeQuery($query[1] . ' LIMIT ' . (int) $limit);
+    CRM_Core_DAO::reenableFullGroupByMode();
+    while ($result->fetch()) {
+      $rows[] = $this->buildRow($query[0], $result, $outputColumns, $metadata, [], []);
+    }
+    return $rows;
+  }
+
 }
index 29756b17c51c9e144300acc32b6ec25eaf07eecc..a46c653f1b371dc0b2785bfaf1d75403980acf88 100644 (file)
@@ -2912,4 +2912,47 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test get preview function on export processor.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testExportGetPreview() {
+    $this->setUpContactExportData();
+    $fields = [
+      ['Individual', 'first_name'],
+      ['Individual', 'last_name'],
+      ['Individual', 'street_address', 1],
+      ['Individual', 'city', 1],
+      ['Individual', 'country', 1],
+      ['Individual', 'email', 1],
+    ];
+    $mappedFields = [];
+    foreach ($fields as $field) {
+      $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field);
+    }
+    $processor = new CRM_Export_BAO_ExportProcessor(FALSE, $mappedFields,
+    'AND');
+    $processor->setComponentClause('contact_a.id IN (' . implode(',', $this->contactIDs) . ')');
+    $result = $processor->getPreview(2);
+    $this->assertEquals([
+      [
+        'first_name' => 'Anthony',
+        'last_name' => 'Anderson',
+        'Home-street_address' => 'Ambachtstraat 23',
+        'Home-city' => 'Brummen',
+        'Home-country' => 'Netherlands',
+        'Home-email' => 'home@example.com',
+      ],
+      [
+        'first_name' => 'Anthony',
+        'last_name' => 'Anderson',
+        'Home-street_address' => '',
+        'Home-city' => '',
+        'Home-country' => '',
+        'Home-email' => 'anthony_anderson@civicrm.org',
+      ],
+    ], $result);
+  }
+
 }