From 12e2ccdcc4a7708d938d49378ef30ecd17aee759 Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Fri, 10 May 2019 20:08:26 +1200 Subject: [PATCH] dev/core#953 Fix failure to set new emails for existing contacts to primary on import Add test for updating email via import & ensuring it is primary --- CRM/Core/BAO/Block.php | 14 ++++++++-- .../CRM/Contact/Import/Parser/ContactTest.php | 28 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/CRM/Core/BAO/Block.php b/CRM/Core/BAO/Block.php index d37b01d94e..0486142464 100644 --- a/CRM/Core/BAO/Block.php +++ b/CRM/Core/BAO/Block.php @@ -344,8 +344,18 @@ class CRM_Core_BAO_Block { } $blockFields = array_merge($value, $contactFields); - $baoString = 'CRM_Core_BAO_' . $name; - $blocks[] = $baoString::add($blockFields); + if ($name === 'Email') { + // @todo ideally all would call the api which is our main tested function, + // and towards that call the create rather than add which is preferred by the + // api. In order to be careful with change only email is swapped over here because it + // is specifically tested in testImportParserWithUpdateWithContactID + // and the primary handling is otherwise bypassed on importing an email update. + $blocks[] = CRM_Core_BAO_Email::create($blockFields); + } + else { + $baoString = 'CRM_Core_BAO_' . $name; + $blocks[] = $baoString::add($blockFields); + } } return $blocks; diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 2ab4aaf85c..4213c50a77 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -162,10 +162,10 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { * @throws \Exception */ public function testImportParserWithUpdateWithExternalIdentifierButNoPrimaryMatch() { - list($originalValues, $result) = $this->setUpBaseContact(array( + list($originalValues, $result) = $this->setUpBaseContact([ 'external_identifier' => 'windows', 'email' => NULL, - )); + ]); $this->assertEquals('windows', $result['external_identifier']); @@ -177,6 +177,30 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $this->callAPISuccessGetSingle('Contact', $originalValues); } + /** + * Test import parser will fallback to external identifier. + * + * In this case no primary match exists (e.g the details are not supplied) so it falls back on external identifier. + * + * CRM-17275 + * + * @throws \Exception + */ + public function testImportParserWithUpdateWithContactID() { + list($originalValues, $result) = $this->setUpBaseContact(array( + 'external_identifier' => '', + 'email' => NULL, + )); + $updateValues = ['id' => $result['id'], 'email' => 'bill@example.com']; + // This is some deep weirdness - this sets a flag for updatingBlankLocinfo - allowing input to be blanked + // (which IS a good thing but it's pretty weird & all to do with legacy profile stuff). + CRM_Core_Session::singleton()->set('authSrc', CRM_Core_Permission::AUTH_SRC_CHECKSUM); + $this->runImport($updateValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [NULL, 1]); + $originalValues['id'] = $result['id']; + $this->callAPISuccessGetSingle('Email', ['contact_id' => $originalValues['id'], 'is_primary' => 1]); + $this->callAPISuccessGetSingle('Contact', $originalValues); + } + /** * Test that the import parser adds the external identifier where none is set. * -- 2.25.1