CRM-16964 - fixing style warnings.
[civicrm-core.git] / tests / phpunit / CRM / Contact / Import / Parser / ContactTest.php
index eb23b93dc7d9f8fa65ab84cf52ae0aed91c26625..d0bc4d56899d048da96d2bf98826552a1ae39b79 100644 (file)
@@ -36,7 +36,7 @@
  * @package CiviCRM
  * @group headless
  */
-class CRM_Contact_Imports_Parser_ContactTest extends CiviUnitTestCase {
+class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
   protected $_tablesToTruncate = array();
 
   /**
@@ -139,6 +139,283 @@ class CRM_Contact_Imports_Parser_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccessGetSingle('Contact', $contactValues);
   }
 
+  /**
+   * Test that the import parser adds the address to the right location.
+   *
+   * @throws \Exception
+   */
+  public function testImportBillingAddress() {
+    list($contactValues) = $this->setUpBaseContact();
+    $contactValues['nick_name'] = 'Old Bill';
+    $contactValues['external_identifier'] = 'android';
+    $contactValues['street_address'] = 'Big Mansion';
+    $contactValues['phone'] = '911';
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 2, 6 => 2));
+    $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
+    $this->assertEquals(2, $address['location_type_id']);
+
+    $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '911'));
+    $this->assertEquals(2, $phone['location_type_id']);
+
+    $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
+    $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
+  }
+
+  /**
+   * Test that the import parser adds the address to the primary location.
+   *
+   * @throws \Exception
+   */
+  public function testImportPrimaryAddress() {
+    list($contactValues) = $this->setUpBaseContact();
+    $contactValues['nick_name'] = 'Old Bill';
+    $contactValues['external_identifier'] = 'android';
+    $contactValues['street_address'] = 'Big Mansion';
+    $contactValues['phone'] = 12334;
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary'));
+    $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
+    $this->assertEquals(1, $address['location_type_id']);
+    $this->assertEquals(1, $address['is_primary']);
+
+    $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '12334'));
+    $this->assertEquals(1, $phone['location_type_id']);
+
+    $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
+    $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
+  }
+
+  /**
+   * Test that the import parser adds the address to the primary location.
+   *
+   * @throws \Exception
+   */
+  public function testImportDeceased() {
+    list($contactValues) = $this->setUpBaseContact();
+    CRM_Core_Session::singleton()->set("dateTypes", 1);
+    $contactValues['birth_date'] = '1910-12-17';
+    $contactValues['deceased_date'] = '2010-12-17';
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
+    $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
+    $this->assertEquals('1910-12-17', $contact['birth_date']);
+    $this->assertEquals('2010-12-17', $contact['deceased_date']);
+    $this->assertEquals(1, $contact['is_deceased']);
+    $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
+  }
+
+
+  /**
+   * Test that the import parser adds the address to the primary location.
+   *
+   * @throws \Exception
+   */
+  public function testImportTwoAddressFirstPrimary() {
+    list($contactValues) = $this->setUpBaseContact();
+    $contactValues['nick_name'] = 'Old Bill';
+    $contactValues['external_identifier'] = 'android';
+    $contactValues['street_address'] = 'Big Mansion';
+    $contactValues['phone'] = 12334;
+    $fields = array_keys($contactValues);
+    $contactValues['street_address_2'] = 'Teeny Mansion';
+    $contactValues['phone_2'] = 4444;
+    $fields[] = 'street_address';
+    $fields[] = 'phone';
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary', 7 => 3, 8 => 3), $fields);
+    $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android'));
+    $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
+
+    $this->assertEquals(3, $address['values'][0]['location_type_id']);
+    $this->assertEquals(0, $address['values'][0]['is_primary']);
+    $this->assertEquals('Teeny Mansion', $address['values'][0]['street_address']);
+
+    $this->assertEquals(1, $address['values'][1]['location_type_id']);
+    $this->assertEquals(1, $address['values'][1]['is_primary']);
+    $this->assertEquals('Big Mansion', $address['values'][1]['street_address']);
+
+    $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
+    $this->assertEquals(1, $phone['values'][0]['location_type_id']);
+    $this->assertEquals(1, $phone['values'][0]['is_primary']);
+    $this->assertEquals(12334, $phone['values'][0]['phone']);
+    $this->assertEquals(3, $phone['values'][1]['location_type_id']);
+    $this->assertEquals(0, $phone['values'][1]['is_primary']);
+    $this->assertEquals(4444, $phone['values'][1]['phone']);
+
+    $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
+  }
+
+  /**
+   * Test that the import parser adds the address to the primary location.
+   *
+   * @throws \Exception
+   */
+  public function testImportTwoAddressSecondPrimary() {
+    list($contactValues) = $this->setUpBaseContact();
+    $contactValues['nick_name'] = 'Old Bill';
+    $contactValues['external_identifier'] = 'android';
+    $contactValues['street_address'] = 'Big Mansion';
+    $contactValues['phone'] = 12334;
+    $fields = array_keys($contactValues);
+    $contactValues['street_address_2'] = 'Teeny Mansion';
+    $contactValues['phone_2'] = 4444;
+    $fields[] = 'street_address';
+    $fields[] = 'phone';
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 3, 6 => 3, 7 => 'Primary', 8 => 'Primary'), $fields);
+    $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android'));
+    $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
+
+    $this->assertEquals(1, $address['values'][0]['location_type_id']);
+    $this->assertEquals(1, $address['values'][0]['is_primary']);
+    $this->assertEquals('Teeny Mansion', $address['values'][0]['street_address']);
+
+    $this->assertEquals(3, $address['values'][1]['location_type_id']);
+    $this->assertEquals(0, $address['values'][1]['is_primary']);
+    $this->assertEquals('Big Mansion', $address['values'][1]['street_address']);
+
+    $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
+    $this->assertEquals(3, $phone['values'][0]['location_type_id']);
+    $this->assertEquals(0, $phone['values'][0]['is_primary']);
+    $this->assertEquals(12334, $phone['values'][0]['phone']);
+    $this->assertEquals(1, $phone['values'][1]['location_type_id']);
+    $this->assertEquals(1, $phone['values'][1]['is_primary']);
+    $this->assertEquals(4444, $phone['values'][1]['phone']);
+
+    $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
+  }
+
+  /**
+   * Test that the import parser updates the address on the existing primary location.
+   *
+   * @throws \Exception
+   */
+  public function testImportPrimaryAddressUpdate() {
+    list($contactValues) = $this->setUpBaseContact(array('external_identifier' => 'android'));
+    $contactValues['nick_name'] = 'Old Bill';
+    $contactValues['external_identifier'] = 'android';
+    $contactValues['street_address'] = 'Big Mansion';
+    $contactValues['city'] = 'Big City';
+    $contactID = $this->callAPISuccessGetValue('Contact', array('external_identifier' => 'android', 'return' => 'id'));
+    $originalAddress = $this->callAPISuccess('Address', 'create', array('location_type_id' => 2, 'street_address' => 'small house', 'contact_id' => $contactID));
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary'));
+    $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
+    $this->assertEquals(2, $address['location_type_id']);
+    $this->assertEquals($originalAddress['id'], $address['id']);
+    $this->assertEquals('Big City', $address['city']);
+    $this->callAPISuccessGetSingle('Contact', $contactValues);
+  }
+
+  /**
+   * Test that setting duplicate action to fill doesn't blow away data
+   * that exists, but does fill in where it's empty.
+   *
+   * @throw \Exception
+   */
+  public function testImportFill() {
+    // Create a custom field group for testing.
+    $custom_group_name = 'importFillGroup';
+    $results = $this->callAPISuccess('customGroup', 'get', array('title' => $custom_group_name));
+    if ($results['count'] == 0) {
+      $api_params = array(
+        'title' => $custom_group_name,
+        'extends' => 'Individual',
+        'is_active' => TRUE,
+      );
+      $customGroup = $this->callAPISuccess('customGroup', 'create', $api_params);
+    }
+
+    // Add two custom fields.
+    $api_params = array(
+      'custom_group_id' => $customGroup['id'],
+      'label' => 'importFillField1',
+      'html_type' => 'Select',
+      'data_type' => 'String',
+      'option_values' => array(
+        'foo' => 'Foo',
+        'bar' => 'Bar',
+      ),
+    );
+    $result = $this->callAPISuccess('custom_field', 'create', $api_params);
+    $customField1 = $result['id'];
+
+    $api_params = array(
+      'custom_group_id' => $customGroup['id'],
+      'label' => 'importFillField2',
+      'html_type' => 'Select',
+      'data_type' => 'String',
+      'option_values' => array(
+        'baz' => 'Baz',
+        'boo' => 'Boo',
+      ),
+    );
+    $result = $this->callAPISuccess('custom_field', 'create', $api_params);
+    $customField2 = $result['id'];
+
+    // Now set up values.
+    $original_gender = 'Male';
+    $original_custom1 = 'foo';
+    $original_job_title = '';
+    $original_custom2 = '';
+    $original_email = 'test-import-fill@example.org';
+
+    $import_gender = 'Female';
+    $import_custom1 = 'bar';
+    $import_job_title = 'Chief data importer';
+    $import_custom2 = 'baz';
+
+    // Create contact with both one known core field and one custom
+    // field filled in.
+    $api_params = array(
+      'contact_type' => 'Individual',
+      'email' => $original_email,
+      'gender' => $original_gender,
+      'custom_' . $customField1 => $original_custom1,
+    );
+    $result = $this->callAPISuccess('contact', 'create', $api_params);
+    $contact_id = $result['id'];
+
+    // Run an import.
+    $import = array(
+      'email' => $original_email,
+      'gender_id' => $import_gender,
+      'custom_' . $customField1 => $import_custom1,
+      'job_title' => $import_job_title,
+      'custom_' . $customField2 => $import_custom2,
+    );
+
+    $this->runImport($import, CRM_Import_Parser::DUPLICATE_FILL, CRM_Import_Parser::VALID);
+
+    $expected = array(
+      'gender' => $original_gender,
+      'custom_' . $customField1 => $original_custom1,
+      'job_title' => $import_job_title,
+      'custom_' . $customField2 => $import_custom2,
+    );
+
+    $params = array(
+      'id' => $contact_id,
+      'return' => array(
+        'gender',
+        'custom_' . $customField1,
+        'job_title',
+        'custom_' . $customField2,
+       ),
+    );
+    $result = civicrm_api3('Contact', 'get', $params);
+    $values = array_pop($result['values']);
+    foreach ($expected as $field => $expected_value) {
+      if (!isset($values[$field])) {
+        $given_value = NULL;
+      }
+      else {
+        $given_value = $values[$field];
+      }
+      // We expect:
+      //   gender: Male
+      //   job_title: Chief Data Importer
+      //   importFillField1: foo
+      //   importFillField2: baz
+      $this->assertEquals($expected_value, $given_value, "$field properly handled during Fill import");
+    }
+  }
+
   /**
    * Run the import parser.
    *
@@ -146,11 +423,17 @@ class CRM_Contact_Imports_Parser_ContactTest extends CiviUnitTestCase {
    *
    * @param int $onDuplicateAction
    * @param int $expectedResult
+   * @param array|null $mapperLocType
+   * @param array|null $fields
+   *   Array of field names. Will be calculated from $originalValues if not passed in, but
+   *   that method does not cope with duplicates.
    */
-  protected function runImport($originalValues, $onDuplicateAction, $expectedResult) {
-    $fields = array_keys($originalValues);
+  protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperLocType = NULL, $fields = NULL) {
+    if (!$fields) {
+      $fields = array_keys($originalValues);
+    }
     $values = array_values($originalValues);
-    $parser = new CRM_Contact_Import_Parser_Contact($fields);
+    $parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType);
     $parser->_contactType = 'Individual';
     $parser->_onDuplicate = $onDuplicateAction;
     $parser->init();