CRM-17275 Import parser: add test for what IS workding
authoreileenmcnaugton <eileen@fuzion.co.nz>
Thu, 24 Sep 2015 09:34:53 +0000 (21:34 +1200)
committermonishdeb <monish.deb@webaccessglobal.com>
Wed, 30 Sep 2015 12:50:57 +0000 (18:20 +0530)
CRM/Contact/Import/Parser/Contact.php
CRM/Import/Parser.php
tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php [new file with mode: 0644]

index c823bcd0a33a7fb458ade84c298e4b278e2efcd6..994c4e000cc68dc577416c0a424563609ef0566a 100644 (file)
@@ -547,7 +547,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
           foreach ($matchedIDs as $contactId) {
             if ($params['id'] == $contactId) {
               $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
-
               if ($formatted['contact_type'] == $contactType) {
                 //validation of subtype for update mode
                 //CRM-5125
@@ -1623,7 +1622,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
    */
   public function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = NULL, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) {
     $dupeCheck = FALSE;
-
     $newContact = NULL;
 
     if (is_null($contactId) && ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK)) {
@@ -1993,7 +1991,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
       }
     }
 
-    if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
+    if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
       !array_key_exists($customFieldID, $addressCustomFields)
     ) {
       // @todo calling api functions directly is not supported
index 04e4712686e6984de0b89d6c6421d928dd3605cb..d29bc12160ab845d7761c246dd2ce3cd719d7163 100644 (file)
@@ -182,9 +182,15 @@ abstract class CRM_Import_Parser {
    * @var int
    */
   public $_contactType;
+  /**
+   * Contact sub-type
+   *
+   * @var int
+   */
+  public $_contactSubType;
 
   /**
-   * Class constructor
+   * Class constructor.
    */
   public function __construct() {
     $this->_maxLinesToProcess = 0;
diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
new file mode 100644 (file)
index 0000000..bb33345
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+/*
++--------------------------------------------------------------------+
+| CiviCRM version 4.6                                                |
++--------------------------------------------------------------------+
+| Copyright CiviCRM LLC (c) 2004-2015                                |
++--------------------------------------------------------------------+
+| This file is a part of CiviCRM.                                    |
+|                                                                    |
+| CiviCRM is free software; you can copy, modify, and distribute it  |
+| under the terms of the GNU Affero General Public License           |
+| Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+|                                                                    |
+| CiviCRM is distributed in the hope that it will be useful, but     |
+| WITHOUT ANY WARRANTY; without even the implied warranty of         |
+| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+| See the GNU Affero General Public License for more details.        |
+|                                                                    |
+| You should have received a copy of the GNU Affero General Public   |
+| License and the CiviCRM Licensing Exception along                  |
+| with this program; if not, contact CiviCRM LLC                     |
+| at info[AT]civicrm[DOT]org. If you have questions about the        |
+| GNU Affero General Public License or the licensing of CiviCRM,     |
+| see the CiviCRM license FAQ at http://civicrm.org/licensing        |
++--------------------------------------------------------------------+
+ */
+
+/**
+ * @file
+ * File for the CRM_Contact_Imports_Parser_ContactTest class.
+ */
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+/**
+ *  Test contact import parser.
+ *
+ * @package CiviCRM
+ */
+class CRM_Contact_Imports_Parser_ContactTest extends CiviUnitTestCase {
+  protected $_tablesToTruncate = array();
+
+  /**
+   * Setup function.
+   */
+  public function setUp() {
+    parent::setUp();
+  }
+
+  /**
+   * Test that the import parser will do an update when external identifier is set.
+   *
+   * @throws \Exception
+   */
+  public function testImportParserWithUpdateWithoutExternalIdentifier() {
+    $originalValues = array(
+      'first_name' => 'Bill',
+      'last_name' => 'Gates',
+      'email' => 'bill.gates@microsoft.com',
+      'nick_name' => 'Billy-boy',
+    );
+    $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
+    $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
+    $originalValues['nick_name'] = 'Old Bill';
+    $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
+    $originalValues['id'] = $result['id'];
+    $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
+    $this->callAPISuccessGetSingle('Contact', $originalValues);
+  }
+
+  /**
+   * Test that the import parser will do an update when external identifier is set.
+   *
+   * @throws \Exception
+   */
+  public function testImportParserWithUpdateWithExternalIdentifier() {
+    $originalValues = array(
+      'first_name' => 'Bill',
+      'last_name' => 'Gates',
+      'email' => 'bill.gates@microsoft.com',
+      'external_identifier' => 'windows',
+      'nick_name' => 'Billy-boy',
+    );
+    $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
+    $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
+    $this->assertEquals($result['id'], CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', 'windows', 'id', 'external_identifier', TRUE));
+    $this->assertEquals('windows', $result['external_identifier']);
+    $originalValues['nick_name'] = 'Old Bill';
+    $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
+    $originalValues['id'] = $result['id'];
+    $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
+    $this->callAPISuccessGetSingle('Contact', $originalValues);
+  }
+
+  /**
+   * Run the import parser.
+   *
+   * @param array $originalValues
+   *
+   * @param int $onDuplicateAction
+   * @param int $expectedResult
+   */
+  protected function runImport($originalValues, $onDuplicateAction, $expectedResult) {
+    $fields = array_keys($originalValues);
+    $values = array_values($originalValues);
+    $parser = new CRM_Contact_Import_Parser_Contact($fields);
+    $parser->_contactType = 'Individual';
+    $parser->_onDuplicate = $onDuplicateAction;
+    $parser->init();
+    $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values));
+  }
+}