From 3a167fed96159665e8f7e2c933a0ea261162edd1 Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Thu, 24 Sep 2015 21:34:53 +1200 Subject: [PATCH] CRM-17275 Import parser: add test for what IS workding --- CRM/Contact/Import/Parser/Contact.php | 4 +- CRM/Import/Parser.php | 8 +- .../CRM/Contact/Import/Parser/ContactTest.php | 112 ++++++++++++++++++ 3 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 51a69d5b22..b19cc4b837 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -551,7 +551,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 @@ -1631,7 +1630,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)) { @@ -2001,7 +1999,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 diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index e55ead633b..d42b2b719e 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -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 index 0000000000..bb3334540e --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -0,0 +1,112 @@ + '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)); + } +} -- 2.25.1