Merge pull request #12461 from eileenmcnaughton/domain_id
[civicrm-core.git] / tests / phpunit / CRM / Export / BAO / ExportTest.php
index bca2f636756e4ab6366c0a87af17a01add00773c..175bbd4693cabc3bf1755e44a26c3b7daf294c18 100644 (file)
@@ -35,7 +35,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
   protected $masterAddressID;
 
   public function tearDown() {
-    $this->quickCleanup(['civicrm_contact', 'civicrm_email', 'civicrm_address']);
+    $this->quickCleanup(['civicrm_contact', 'civicrm_email', 'civicrm_address', 'civicrm_relationship']);
     $this->quickCleanUpFinancialEntities();
     parent::tearDown();
   }
@@ -172,8 +172,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
       'trxn_id' => 1,
       'contribution_id' => 1,
     );
-    $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
-    $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
+
     $contactRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(
       NULL,
       NULL,
@@ -194,7 +193,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     $queryFieldAliases = array();
     preg_match_all($pattern, $select, $queryFieldAliases, PREG_PATTERN_ORDER);
 
-    list($outputFields) = CRM_Export_BAO_Export::getExportStructureArrays($returnProperties, $query, $phoneTypes, $imProviders, $contactRelationshipTypes, '', array());
+    list($outputFields) = CRM_Export_BAO_Export::getExportStructureArrays($returnProperties, $query, $contactRelationshipTypes, '', array());
     foreach (array_keys($outputFields) as $fieldAlias) {
       if ($fieldAlias == 'Home-country') {
         $this->assertTrue(in_array($fieldAlias . '_id', $queryFieldAliases[1]), 'Country is subject to some funky translate so we make sure country id is present');
@@ -369,6 +368,81 @@ 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() {
+    list($householdID, $houseHoldTypeID) = $this->setUpHousehold();
+
+    $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()) {
+      $this->assertEquals('Portland', $dao->city);
+      $this->assertEquals('ME', $dao->state_province);
+      $this->assertEquals($householdID, $dao->civicrm_primary_id);
+      $this->assertEquals($householdID, $dao->civicrm_primary_id);
+    }
+
+  }
+
+  /**
+   * Test exporting relationships.
+   */
+  public function testExportRelationshipsMergeToHouseholdAllFields() {
+    $this->markTestIncomplete('Does not yet work under CI due to mysql limitation (number of columns in table). Works on some boxes');
+    list($householdID) = $this->setUpHousehold();
+    list($tableName) = CRM_Export_BAO_Export::exportComponents(
+      FALSE,
+      $this->contactIDs,
+      [],
+      NULL,
+      NULL,
+      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()) {
+      $this->assertEquals('Portland', $dao->city);
+      $this->assertEquals('ME', $dao->state_province);
+      $this->assertEquals($householdID, $dao->civicrm_primary_id);
+      $this->assertEquals($householdID, $dao->civicrm_primary_id);
+      $this->assertEquals('Unit Test Household', $dao->addressee);
+      $this->assertEquals('Unit Test Household', $dao->display_name);
+    }
+  }
+
   /**
    * Test master_address_id field.
    */
@@ -480,4 +554,37 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     CRM_Core_DAO::executeQuery($sql);
   }
 
+  /**
+   * @return array
+   */
+  protected function setUpHousehold() {
+    $this->setUpContactExportData();
+    $householdID = $this->householdCreate([
+      'api.Address.create' => [
+        'city' => 'Portland',
+        'state_province_id' => 'Maine',
+        'location_type_id' => 'Home'
+      ]
+    ]);
+
+    $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,
+    ]);
+    return array($householdID, $houseHoldTypeID);
+  }
+
 }