Skip to content
Snippets Groups Projects
Unverified Commit b4b7b8df authored by Eileen McNaughton's avatar Eileen McNaughton Committed by GitHub
Browse files

Merge pull request #14489 from aydun/core_1026

Fix #1026 where an email address synced from a CMS contact is created with no location field.
parents 87408bd1 cd976ae6
Branches
Tags
No related merge requests found
......@@ -431,25 +431,23 @@ AND domain_id = %4
$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,
......
......@@ -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;
}
}
/**
......
......@@ -78,6 +78,22 @@ class api_v3_EmailTest extends CiviUnitTestCase {
$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
*
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testCreateEmailDefaultLocation($version) {
$this->_apiversion = $version;
$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.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment