CRM-17681 - "Master address belongs to" field gives the master address id rather...
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Wed, 7 Sep 2016 09:37:38 +0000 (15:07 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Wed, 7 Sep 2016 09:46:23 +0000 (15:16 +0530)
CRM/Contact/BAO/Query.php
CRM/Export/BAO/Export.php
tests/phpunit/CRM/Export/BAO/ExportTest.php

index eeb4281951eff88baaa164c8ec70731f4dda8bfa..e351444690d7d6d488facf5120a815d9cf606434 100644 (file)
@@ -369,6 +369,7 @@ class CRM_Contact_BAO_Query {
     'email',
     'im',
     'address_name',
+    'master_id',
   );
 
   /**
index 6077d1a83d594f5fcc71f13fb83c964996addd69..e180c596b0b2421521077f785f01c65b8b7c0adb 100644 (file)
@@ -810,10 +810,10 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
             elseif ($field == 'provider_id' || $field == 'im_provider') {
               $fieldValue = CRM_Utils_Array::value($fieldValue, $imProviders);
             }
-            elseif ($field == 'master_id') {
+            elseif (strstr($field, 'master_id')) {
               $masterAddressId = NULL;
-              if (isset($iterationDAO->master_id)) {
-                $masterAddressId = $iterationDAO->master_id;
+              if (isset($iterationDAO->$field)) {
+                $masterAddressId = $iterationDAO->$field;
               }
               // get display name of contact that address is shared.
               $fieldValue = CRM_Contact_BAO_Contact::getMasterDisplayName($masterAddressId, $iterationDAO->contact_id);
@@ -1127,14 +1127,16 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
       if (empty($exportParams['suppress_csv_for_testing'])) {
         self::writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode);
       }
+      else {
+        // return tableName and sqlColumns in test context
+        return array($exportTempTable, $sqlColumns);
+      }
 
       // delete the export temp table and component table
       $sql = "DROP TABLE IF EXISTS {$exportTempTable}";
       CRM_Core_DAO::executeQuery($sql);
-      // Do not exit in test context.
-      if (empty($exportParams['suppress_csv_for_testing'])) {
-        CRM_Utils_System::civiExit();
-      }
+
+      CRM_Utils_System::civiExit();
     }
     else {
       CRM_Core_Error::fatal(ts('No records to export'));
index 65ff7f4fbe5ee8ce2c07b45ed31831581aa09ece..b40441669b96a458466f80f7ea108c96551a0147 100644 (file)
@@ -24,7 +24,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
    * Basic test to ensure the exportComponents function completes without error.
    */
   public function testExportComponentsNull() {
-    CRM_Export_BAO_Export::exportComponents(
+    list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
       TRUE,
       array(),
       array(),
@@ -41,6 +41,10 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
         'suppress_csv_for_testing' => TRUE,
       )
     );
+
+    // delete the export temp table and component table
+    $sql = "DROP TABLE IF EXISTS {$tableName}";
+    CRM_Core_DAO::executeQuery($sql);
   }
 
   /**
@@ -59,7 +63,8 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
       array('Individual', 'email', 1),
       array('Contribution', 'trxn_id'),
     );
-    CRM_Export_BAO_Export::exportComponents(
+
+    list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
       TRUE,
       $this->contributionIDs,
       array(),
@@ -76,6 +81,10 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
         'suppress_csv_for_testing' => TRUE,
       )
     );
+
+    // delete the export temp table and component table
+    $sql = "DROP TABLE IF EXISTS {$tableName}";
+    CRM_Core_DAO::executeQuery($sql);
   }
   /**
    * Test the function that extracts the arrays used to structure the output.
@@ -158,4 +167,61 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     $this->contactIDs[] = $this->individualCreate();
   }
 
+  public function testExportMasterAddress() {
+    $contactA = $this->individualCreate(array(), 0);
+    $contactB = $this->individualCreate(array(), 1);
+
+    //create address for contact A
+    $params = array(
+      'contact_id' => $contactA,
+      'location_type_id' => 'Home',
+      'street_address' => 'Ambachtstraat 23',
+      'postal_code' => '6971 BN',
+      'country_id' => '1152',
+      'city' => 'Brummen',
+      'is_primary' => 1,
+    );
+    $result = $this->callAPISuccess('address', 'create', $params);
+    $addressId = $result['id'];
+
+    //share address with contact B
+    $result = $this->callAPISuccess('address', 'create', array(
+      'contact_id' => $contactB,
+      'location_type_id' => "Home",
+      'master_id' => $addressId,
+    ));
+
+    //export the master address for contact B
+    $selectedFields = array(
+      array('Individual', 'master_id', 1),
+    );
+    list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
+      TRUE,
+      array($contactB),
+      array(),
+      NULL,
+      $selectedFields,
+      NULL,
+      CRM_Export_Form_Select::CONTACT_EXPORT,
+      "contact_a.id IN ({$contactB})",
+      NULL,
+      FALSE,
+      FALSE,
+      array(
+        'exportOption' => CRM_Export_Form_Select::CONTACT_EXPORT,
+        'suppress_csv_for_testing' => TRUE,
+      )
+    );
+    $field = key($sqlColumns);
+
+    //assert the exported result
+    $masterName = CRM_Core_DAO::singleValueQuery("SELECT {$field} FROM {$tableName}");
+    $displayName = CRM_Contact_BAO_Contact::getMasterDisplayName(NULL, $contactB);
+    $this->assertEquals($displayName, $masterName);
+
+    // delete the export temp table and component table
+    $sql = "DROP TABLE IF EXISTS {$tableName}";
+    CRM_Core_DAO::executeQuery($sql);
+  }
+
 }