From 4ccd49652a2aadd019cd2931720202e7629926aa Mon Sep 17 00:00:00 2001 From: Vinu Varshith S Date: Sat, 11 Nov 2017 16:40:35 +0530 Subject: [PATCH 1/4] CRM-21041: Respect default 'Communication Style' when creating a Contact via API --- CRM/Contact/BAO/Contact.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 565096516e3f..9014aa3d05c9 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -306,6 +306,11 @@ public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE } } } + + // CRM-21041: set default 'Communication Style' if unset when creating a contact. + if (empty($params['communication_style_id'])) { + $params['communication_style_id'] = array_pop(CRM_Core_OptionGroup::values('communication_style', TRUE, NULL, NULL, 'AND is_default = 1')); + } } $transaction = new CRM_Core_Transaction(); From 80b4e1db4447ba6e1f6b5f708fdd493e0c2058c6 Mon Sep 17 00:00:00 2001 From: Vinu Varshith S Date: Sun, 12 Nov 2017 10:35:08 +0530 Subject: [PATCH 2/4] Avoid 'array_pop calling a function' situation --- CRM/Contact/BAO/Contact.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 9014aa3d05c9..56599265fd5a 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -309,7 +309,8 @@ public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE // CRM-21041: set default 'Communication Style' if unset when creating a contact. if (empty($params['communication_style_id'])) { - $params['communication_style_id'] = array_pop(CRM_Core_OptionGroup::values('communication_style', TRUE, NULL, NULL, 'AND is_default = 1')); + $defaultCommunicationStyleId = CRM_Core_OptionGroup::values('communication_style', TRUE, NULL, NULL, 'AND is_default = 1'); + $params['communication_style_id'] = array_pop($defaultCommunicationStyleId); } } From ed45d5536a0310296b4502104fc9c0bd94518ec1 Mon Sep 17 00:00:00 2001 From: Vinu Varshith S Date: Mon, 13 Nov 2017 16:50:14 +0530 Subject: [PATCH 3/4] Adding test cases for CRM-21041 --- tests/phpunit/api/v3/ContactTest.php | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index c2f4f94e21e7..f80efd65dae6 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3548,4 +3548,38 @@ protected function putGroupContactCacheInClearableState($groupID, $contact) { Civi::$statics['CRM_Contact_BAO_GroupContactCache']['is_refresh_init'] = FALSE; } + /** + * CRM-21041 Test if 'communication style' is set to site default if not passed. + */ + public function testCreateCommunicationStyleUnset() { + $this->callAPISuccess('Contact', 'create', array( + 'first_name' => 'John', + 'last_name' => 'Doe', + 'contact_type' => 'Individual') + ); + $result = $this->callAPISuccessGetSingle('Contact', array('last_name' => 'Doe')); + $this->assertEquals(1, $result['communication_style_id']); + } + + /** + * CRM-21041 Test if 'communication style' is set if value is passed. + */ + public function testCreateCommunicationStylePassed() { + $this->callAPISuccess('Contact', 'create', array( + 'first_name' => 'John', + 'last_name' => 'Doe', + 'contact_type' => 'Individual', + 'communication_style_id' => 'Familiar', + )); + $result = $this->callAPISuccessGetSingle('Contact', array('last_name' => 'Doe')); + $params = array( + 'option_group_id' => 'communication_style', + 'label' => 'Familiar', + 'return' => 'value', + ); + $optionResult = civicrm_api3('OptionValue', 'get', $params); + $communicationStyle = reset($optionResult['values']); + $this->assertEquals($communicationStyle['value'], $result['communication_style_id']); + } + } From 84542bf011c28662156aa59250b55e256351df98 Mon Sep 17 00:00:00 2001 From: Vinu Varshith S Date: Mon, 13 Nov 2017 22:49:30 +0530 Subject: [PATCH 4/4] clear weird character causing warnings --- tests/phpunit/api/v3/ContactTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index f80efd65dae6..491a98849a29 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3560,7 +3560,7 @@ public function testCreateCommunicationStyleUnset() { $result = $this->callAPISuccessGetSingle('Contact', array('last_name' => 'Doe')); $this->assertEquals(1, $result['communication_style_id']); } - + /** * CRM-21041 Test if 'communication style' is set if value is passed. */