From 2890c0f91eca7f45ad32c43823483efdd6e05264 Mon Sep 17 00:00:00 2001 From: Aidan Saunders Date: Mon, 10 Jun 2019 13:38:53 +0100 Subject: [PATCH 1/3] Set a default location in Email.create API call. Add test for this. Default location used to create the email is the default LocationType --- api/v3/Email.php | 4 ++++ tests/phpunit/api/v3/EmailTest.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/api/v3/Email.php b/api/v3/Email.php index 095e082e7365..d1dd4ab66973 100644 --- a/api/v3/Email.php +++ b/api/v3/Email.php @@ -57,6 +57,10 @@ function _civicrm_api3_email_create_spec(&$params) { $params['is_primary']['api.default'] = 0; $params['email']['api.required'] = 1; $params['contact_id']['api.required'] = 1; + $defaultLocation = CRM_Core_BAO_LocationType::getDefault(); + if ($defaultLocation) { + $params['location_type_id']['api.default'] = $defaultLocation->id; + } } /** diff --git a/tests/phpunit/api/v3/EmailTest.php b/tests/phpunit/api/v3/EmailTest.php index 2a6910635052..4299e87d3325 100644 --- a/tests/phpunit/api/v3/EmailTest.php +++ b/tests/phpunit/api/v3/EmailTest.php @@ -78,6 +78,20 @@ public function testCreateEmail($version) { $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id'])); } + /** + * If no location is specified when creating a new email, it should default to + * the LocationType default + * + * Only API v3 + */ + public function testCreateEmailDefaultLocation() { + $params = $this->_params; + unset($params['location_type_id']); + $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__); + $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']); + $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id'])); + } + /** * If a new email is set to is_primary the prev should no longer be. * From 56e50a8b6c3e1240d78e3cb878c304f0d3011559 Mon Sep 17 00:00:00 2001 From: Aidan Saunders Date: Mon, 10 Jun 2019 13:42:13 +0100 Subject: [PATCH 2/3] When syncing to UF record, use API instead of DAO to create email - with the addition of a default location in this change, this fixes core#1026 Bonus code tidy: if UF email matches the contact primary email, avoid unnecessary update. If update is needed, use API not direct SQL. --- CRM/Core/BAO/UFMatch.php | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/CRM/Core/BAO/UFMatch.php b/CRM/Core/BAO/UFMatch.php index 18ce81f8aa22..cab931e41b5d 100644 --- a/CRM/Core/BAO/UFMatch.php +++ b/CRM/Core/BAO/UFMatch.php @@ -431,25 +431,23 @@ public static function updateContactEmail($contactId, $emailAddress) { $contactDetails = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactId); if (trim($contactDetails[1])) { + //update if record is found but different $emailID = $contactDetails[3]; - //update if record is found - $query = "UPDATE civicrm_email - SET email = %1 - WHERE id = %2"; - $p = [ - 1 => [$emailAddress, 'String'], - 2 => [$emailID, 'Integer'], - ]; - $dao = CRM_Core_DAO::executeQuery($query, $p); + if (trim($contactDetails[1]) != $emailAddress) { + civicrm_api3('Email', 'create', [ + 'id' => $emailID, + 'email' => $emailAddress, + ]); + } } else { //else insert a new email record - $email = new CRM_Core_DAO_Email(); - $email->contact_id = $contactId; - $email->is_primary = 1; - $email->email = $emailAddress; - $email->save(); - $emailID = $email->id; + $result = civicrm_api3('Email', 'create', [ + 'contact_id' => $contactId, + 'email' => $emailAddress, + 'is_primary' => 1, + ]); + $emailID = $result->id; } CRM_Core_BAO_Log::register($contactId, From cd976ae627e5fc117882f0a6c4ce1ef69b1604d8 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 11 Jun 2019 14:25:41 -0400 Subject: [PATCH 3/3] Extend location_type_id test to api4 --- tests/phpunit/api/v3/EmailTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/api/v3/EmailTest.php b/tests/phpunit/api/v3/EmailTest.php index 4299e87d3325..3f7719172cd0 100644 --- a/tests/phpunit/api/v3/EmailTest.php +++ b/tests/phpunit/api/v3/EmailTest.php @@ -82,9 +82,11 @@ public function testCreateEmail($version) { * If no location is specified when creating a new email, it should default to * the LocationType default * - * Only API v3 + * @param int $version + * @dataProvider versionThreeAndFour */ - public function testCreateEmailDefaultLocation() { + public function testCreateEmailDefaultLocation($version) { + $this->_apiversion = $version; $params = $this->_params; unset($params['location_type_id']); $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);