CRM-20790: Unit test to ensure relationships are created on import.
authorAlok Patel <alok@agileware.com.au>
Wed, 21 Mar 2018 03:16:14 +0000 (13:16 +1000)
committerAlok Patel <alok@agileware.com.au>
Wed, 21 Mar 2018 03:25:02 +0000 (13:25 +1000)
CRM/Contact/Import/Parser/Contact.php
tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php

index ad97146add13e7123bf323774fca2987eab86c8b..b5043a135cae8a97c844c1943b1da88accf4ccee 100644 (file)
@@ -456,6 +456,16 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
     return CRM_Import_Parser::VALID;
   }
 
+  /**
+   * Get Array of all the fields that could potentially be part
+   * import process
+   *
+   * @return array
+   */
+  public function getAllFields() {
+    return $this->_fields;
+  }
+
   /**
    * Handle the values in import mode.
    *
index 55af3f8cd98b4d5eb78ea281c3f601c97cb32851..9fc3da8a92b1fe45e5885f51febf56db4bf7df79 100644 (file)
@@ -46,6 +46,55 @@ class CRM_Contact_Imports_Parser_ContactTest extends CiviUnitTestCase {
     parent::setUp();
   }
 
+  /**
+   * Test that import parser will add contact with employee of relationship.
+   *
+   * @throws \Exception
+   */
+  public function testImportParserWtihEmployeeOfRelationship() {
+    $this->organizationCreate(array(
+      "organization_name" => "Agileware",
+      "legal_name"        => "Agileware",
+    ));
+    $contactImportValues = array(
+      "first_name"  => "Alok",
+      "last_name"   => "Patel",
+      "Employee of" => "Agileware",
+    );
+
+    $fields = array_keys($contactImportValues);
+    $values = array_values($contactImportValues);
+    $parser = new CRM_Contact_Import_Parser_Contact($fields, NULL);
+    $parser->_contactType = 'Individual';
+    $parser->init();
+    $this->mapRelationshipFields($fields, $parser->getAllFields());
+
+    $parser = new CRM_Contact_Import_Parser_Contact($fields, NULL, NULL, NULL, array(
+      NULL,
+      NULL,
+      $fields[2],
+    ), array(
+      NULL,
+      NULL,
+      "Organization",
+    ), array(
+      NULL,
+      NULL,
+      "organization_name",
+    ), NULL, NULL, NULL, NULL, NULL);
+
+    $parser->_contactType = 'Individual';
+    $parser->_onDuplicate = CRM_Import_Parser::DUPLICATE_UPDATE;
+    $parser->init();
+
+    $this->assertEquals(CRM_Import_Parser::VALID, $parser->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), 'Return code from parser import was not as expected');
+    $this->callAPISuccess("Contact", "get", array(
+        "first_name"        => "Alok",
+        "last_name"         => "Patel",
+        "organization_name" => "Agileware",
+    ));
+  }
+
   /**
    * Test that import parser will not fail when same external_identifier found of deleted contact.
    *
@@ -513,6 +562,19 @@ class CRM_Contact_Imports_Parser_ContactTest extends CiviUnitTestCase {
     $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected');
   }
 
+  /**
+   * @param array $fields Array of fields to be imported
+   * @param array $allfields Array of all fields which can be part of import
+   */
+  private function mapRelationshipFields(&$fields, $allfields) {
+    foreach ($allfields as $key => $fieldtocheck) {
+      $elementIndex = array_search($fieldtocheck->_title, $fields);
+      if ($elementIndex !== FALSE) {
+        $fields[$elementIndex] = $key;
+      }
+    }
+  }
+
   /**
    * Set up the underlying contact.
    *