From c10e7177e3acc3441e1717eb698c1ece0ea948c6 Mon Sep 17 00:00:00 2001 From: eileenmcnaughton Date: Mon, 3 Aug 2015 12:16:31 +0000 Subject: [PATCH] CRM-16958 ensure sort_name is set when display_name set --- CRM/Contact/BAO/Individual.php | 14 ++++++++++---- tests/phpunit/api/v3/ContactTest.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CRM/Contact/BAO/Individual.php b/CRM/Contact/BAO/Individual.php index 490b1645a8..245b59059e 100644 --- a/CRM/Contact/BAO/Individual.php +++ b/CRM/Contact/BAO/Individual.php @@ -49,7 +49,7 @@ class CRM_Contact_BAO_Individual extends CRM_Contact_DAO_Contact { * * @param array $params * (reference ) an assoc array of name/value pairs. - * @param array $contact + * @param CRM $contact * Contact object. * * @return CRM_Contact_BAO_Contact @@ -254,7 +254,7 @@ class CRM_Contact_BAO_Individual extends CRM_Contact_DAO_Contact { } //now set the names. - $names = array('sortName' => 'sort_name', 'displayName' => 'display_name'); + $names = array('displayName' => 'display_name', 'sortName' => 'sort_name'); foreach ($names as $value => $name) { if (empty($$value)) { if ($email) { @@ -263,6 +263,13 @@ class CRM_Contact_BAO_Individual extends CRM_Contact_DAO_Contact { elseif ($uniqId) { $$value = $uniqId; } + elseif (!empty($params[$name])) { + $$value = $params[$name]; + } + // If we have nothing else going on set sort_name to display_name. + elseif ($displayName) { + $$value = $displayName; + } } //finally if we could not pass anything lets keep db. if (!empty($$value)) { @@ -408,11 +415,10 @@ class CRM_Contact_BAO_Individual extends CRM_Contact_DAO_Contact { * Check if there is data to create the object. * * @param array $params - * (reference ) an assoc array of name/value pairs. * * @return bool */ - public static function dataExists(&$params) { + public static function dataExists($params) { if ($params['contact_type'] == 'Individual') { return TRUE; } diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index be633e4ec9..25244cc61c 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -228,6 +228,22 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->assertEquals(1, $contact['id']); } + /** + * Test creating individual by display_name. + * + * Display name & sort name should be set. + */ + public function testCreateDisplayNameIndividual() { + $params = array( + 'display_name' => 'abc1', + 'contact_type' => 'Individual', + ); + + $contact = $this->callAPISuccess('contact', 'create', $params); + $params['sort_name'] = 'abc1'; + $this->getAndCheck($params, $contact['id'], 'contact'); + } + /** * Test old keys still work. * -- 2.25.1