Refactor towards fixing / reviewing export issues
authoreileen <emcnaughton@wikimedia.org>
Thu, 22 Feb 2018 02:21:47 +0000 (15:21 +1300)
committereileen <emcnaughton@wikimedia.org>
Sun, 27 May 2018 22:44:58 +0000 (10:44 +1200)
CRM/Export/BAO/Export.php
tests/phpunit/CRM/Export/BAO/ExportTest.php

index 31c4218426e1a7cf75fe2734d43cd482a445e651..d9fab44146a159141587c82634dc982b5f5cc96c 100644 (file)
@@ -348,20 +348,14 @@ class CRM_Export_BAO_Export {
       );
 
       foreach ($fields as $key => $value) {
-        $phoneTypeId = $imProviderId = $relationField = NULL;
+        $relationField = NULL;
         $relationshipTypes = $fieldName = CRM_Utils_Array::value(1, $value);
         if (!$fieldName) {
           continue;
         }
-        // get phoneType id and IM service provider id separately
-        if ($fieldName == 'phone') {
-          $phoneTypeId = CRM_Utils_Array::value(3, $value);
-        }
-        elseif ($fieldName == 'im') {
-          $imProviderId = CRM_Utils_Array::value(3, $value);
-        }
 
-        if (array_key_exists($relationshipTypes, $contactRelationshipTypes)) {
+        if (array_key_exists($relationshipTypes, $contactRelationshipTypes) && (!empty($value[2]) || empty($value[4]))) {
+          $relPhoneTypeId = $relIMProviderId = NULL;
           if (!empty($value[2])) {
             $relationField = CRM_Utils_Array::value(2, $value);
             if (trim(CRM_Utils_Array::value(3, $value))) {
@@ -388,11 +382,6 @@ class CRM_Export_BAO_Export {
               $relIMProviderId = CRM_Utils_Array::value(6, $value);
             }
           }
-        }
-
-        $locTypeId = CRM_Utils_Array::value(2, $value);
-
-        if ($relationField) {
           if (in_array($relationField, $locationTypeFields) && is_numeric($relLocTypeId)) {
             if ($relPhoneTypeId) {
               $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['phone-' . $relPhoneTypeId] = 1;
@@ -403,18 +392,22 @@ class CRM_Export_BAO_Export {
             else {
               $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]][$relationField] = 1;
             }
-            $relPhoneTypeId = $relIMProviderId = NULL;
           }
           else {
             $returnProperties[$relationshipTypes][$relationField] = 1;
           }
         }
-        elseif (is_numeric($locTypeId)) {
-          if ($phoneTypeId) {
-            $returnProperties['location'][$locationTypes[$locTypeId]]['phone-' . $phoneTypeId] = 1;
+
+        if ($relationField) {
+          // already handled.
+        }
+        elseif (is_numeric(CRM_Utils_Array::value(2, $value))) {
+          $locTypeId = $value[2];
+          if ($fieldName == 'phone') {
+            $returnProperties['location'][$locationTypes[$locTypeId]]['phone-' . CRM_Utils_Array::value(3, $value)] = 1;
           }
-          elseif ($imProviderId) {
-            $returnProperties['location'][$locationTypes[$locTypeId]]['im-' . $imProviderId] = 1;
+          elseif ($fieldName == 'im') {
+            $returnProperties['location'][$locationTypes[$locTypeId]]['im-' . CRM_Utils_Array::value(3, $value)] = 1;
           }
           else {
             $returnProperties['location'][$locationTypes[$locTypeId]][$fieldName] = 1;
index ccaba5a0755e69b15d2336681096207a0c60b10f..6ff501f82ac23af5de3f01cacfad904853d578a3 100644 (file)
@@ -368,6 +368,65 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     $this->assertEquals('Big campaign,', CRM_Core_DAO::singleValueQuery("SELECT GROUP_CONCAT(contribution_campaign_title) FROM {$tableName}"));
   }
 
+  /**
+   * Test exporting relationships.
+   *
+   * This is to ensure that CRM-13995 remains fixed.
+   */
+  public function testExportRelationshipsMergeToHousehold() {
+    $this->setUpContactExportData();
+    $householdID = $this->householdCreate(['city' => 'Portland', 'state_province_id' => 'Oregan']);
+
+    $relationshipTypes = $this->callAPISuccess('RelationshipType', 'get', [])['values'];
+    $houseHoldTypeID = NULL;
+    foreach ($relationshipTypes as $id => $relationshipType) {
+      if ($relationshipType['name_a_b'] === 'Household Member of') {
+        $houseHoldTypeID = $relationshipType['id'];
+      }
+    }
+    $this->callAPISuccess('Relationship', 'create', [
+      'contact_id_a' => $this->contactIDs[0],
+      'contact_id_b' => $householdID,
+      'relationship_type_id' => $houseHoldTypeID,
+    ]);
+    $this->callAPISuccess('Relationship', 'create', [
+      'contact_id_a' => $this->contactIDs[1],
+      'contact_id_b' => $householdID,
+      'relationship_type_id' => $houseHoldTypeID,
+    ]);
+
+    $selectedFields = [
+      ['Individual', $houseHoldTypeID . '_a_b', 'state_province', ''],
+      ['Individual', $houseHoldTypeID . '_a_b', 'city', ''],
+      ['Individual', 'city', ''],
+      ['Individual', 'state_province', ''],
+    ];
+    list($tableName) = CRM_Export_BAO_Export::exportComponents(
+      FALSE,
+      $this->contactIDs,
+      [],
+      NULL,
+      $selectedFields,
+      NULL,
+      CRM_Export_Form_Select::CONTACT_EXPORT,
+      "contact_a.id IN (" . implode(",", $this->contactIDs) . ")",
+      NULL,
+      FALSE,
+      TRUE,
+      [
+        'exportOption' => CRM_Export_Form_Select::CONTACT_EXPORT,
+        'suppress_csv_for_testing' => TRUE,
+      ]
+    );
+    $dao = CRM_Core_DAO::executeQuery("SELECT * FROM {$tableName}");
+    while ($dao->fetch()) {
+      // Do some checks here
+      // $this->assertEquals('Portland', $dao->city);
+      // $this->assertEquals('Oregan', $dao0>state_province);
+    }
+
+  }
+
   /**
    * Test master_address_id field.
    */