Add export test
authoreileen <emcnaughton@wikimedia.org>
Tue, 5 Jan 2016 01:10:30 +0000 (14:10 +1300)
committereileen <emcnaughton@wikimedia.org>
Tue, 5 Jan 2016 01:10:30 +0000 (14:10 +1300)
Change-Id: Ide9668a25394fc4ef10efc63925133d89e309c13

CRM/Export/BAO/Export.php
tests/phpunit/CRM/Export/BAO/ExportTest.php [new file with mode: 0644]

index e767e8f1ff1f60d56d42bf439c36f3b6fc7cb8ff..5aeff00104fe51d909b2f93691f6c821b55d8c2d 100644 (file)
@@ -301,6 +301,7 @@ class CRM_Export_BAO_Export {
     $exportParams = array(),
     $queryOperator = 'AND'
   ) {
+
     $headerRows = $returnProperties = array();
     $primary = $paymentFields = $selectedPaymentFields = FALSE;
     $origFields = $fields;
@@ -1258,14 +1259,19 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
       // call export hook
       CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode);
 
-      // now write the CSV file
-      self::writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode);
+      // In order to be able to write a unit test against this function we need to suppress
+      // the csv writing. In future hopefully the csv writing & the main processing will be in separate functions.
+      if (empty($exportParams['suppress_csv_for_testing'])) {
+        self::writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode);
+      }
 
       // delete the export temp table and component table
       $sql = "DROP TABLE IF EXISTS {$exportTempTable}";
       CRM_Core_DAO::executeQuery($sql);
-
-      CRM_Utils_System::civiExit();
+      // Do not exit in test context.
+      if (empty($exportParams['suppress_csv_for_testing'])) {
+        CRM_Utils_System::civiExit();
+      }
     }
     else {
       CRM_Core_Error::fatal(ts('No records to export'));
diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php
new file mode 100644 (file)
index 0000000..af08969
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+/**
+ * Class CRM_Core_DAOTest
+ */
+class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
+
+  /**
+   * Basic test to ensure the exportComponents function completes without error.
+   */
+  function testExportComponentsNull() {
+    CRM_Export_BAO_Export::exportComponents(
+      TRUE,
+      array(),
+      array(),
+      NULL,
+      NULL,
+      NULL,
+      CRM_Export_Form_Select::CONTACT_EXPORT,
+      NULL,
+      NULL,
+      FALSE,
+      FALSE,
+      array(
+        'exportOption' => 1,
+        'suppress_csv_for_testing' => TRUE,
+      )
+    );
+  }
+}