From 5e7f101ad28f3a969418cab179a030d093e3613a Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Thu, 2 Feb 2017 16:48:57 +0530 Subject: [PATCH] CRM-19938: Make Civi to CMS email sync optional --- CRM/Admin/Form/Setting.php | 15 ++++------- CRM/Admin/Form/Setting/UF.php | 1 + CRM/Core/BAO/UFMatch.php | 10 +++++-- settings/Core.setting.php | 14 ++++++++++ templates/CRM/Admin/Form/Setting/UF.tpl | 4 +++ tests/phpunit/api/v3/UFMatchTest.php | 36 +++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 12 deletions(-) diff --git a/CRM/Admin/Form/Setting.php b/CRM/Admin/Form/Setting.php index 16df7ca1e2..671c8fe7ed 100644 --- a/CRM/Admin/Form/Setting.php +++ b/CRM/Admin/Form/Setting.php @@ -203,16 +203,11 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form { unset($params['enableComponents']); } - // verify ssl peer option - if (isset($params['verifySSL'])) { - Civi::settings()->set('verifySSL', $params['verifySSL']); - unset($params['verifySSL']); - } - - // force secure URLs - if (isset($params['enableSSL'])) { - Civi::settings()->set('enableSSL', $params['enableSSL']); - unset($params['enableSSL']); + foreach (array('verifySSL', 'enableSSL') as $name) { + if (isset($params[$name])) { + Civi::settings()->set($name, $params[$name]); + unset($params[$name]); + } } $settings = array_intersect_key($params, $this->_settings); $result = civicrm_api('setting', 'create', $settings + array('version' => 3)); diff --git a/CRM/Admin/Form/Setting/UF.php b/CRM/Admin/Form/Setting/UF.php index 342d6f357a..ab290514d3 100644 --- a/CRM/Admin/Form/Setting/UF.php +++ b/CRM/Admin/Form/Setting/UF.php @@ -46,6 +46,7 @@ class CRM_Admin_Form_Setting_UF extends CRM_Admin_Form_Setting { public function buildQuickForm() { $config = CRM_Core_Config::singleton(); $this->_uf = $config->userFramework; + $this->_settings['syncCMSEmail'] = CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME; if ($this->_uf == 'WordPress') { $this->_settings['wpBasePage'] = CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME; diff --git a/CRM/Core/BAO/UFMatch.php b/CRM/Core/BAO/UFMatch.php index bfd6a9b5cd..ba993670a3 100644 --- a/CRM/Core/BAO/UFMatch.php +++ b/CRM/Core/BAO/UFMatch.php @@ -256,7 +256,7 @@ AND domain_id = %2 if (!$found) { // Not sure why we're testing for this. Is there ever a case // in which $user is not an object? - if (is_object($user)) { + if (Civi::settings()->get('syncCMSEmail') && is_object($user)) { if ($config->userSystem->is_drupal) { $primary_email = $uniqId; } @@ -356,9 +356,10 @@ AND domain_id = %4 * Id of the contact to update. */ public static function updateUFName($contactId) { - if (!$contactId) { + if (!Civi::settings()->get('syncCMSEmail') || !$contactId) { return; } + $config = CRM_Core_Config::singleton(); $ufName = CRM_Contact_BAO_Contact::getPrimaryEmail($contactId); @@ -420,6 +421,11 @@ AND domain_id = %4 $ufmatch->uf_name = $emailAddress; CRM_Core_BAO_UFMatch::create((array) $ufmatch); + // If CMS integration is disabled skip Civi email update if CMS user email is changed + if (Civi::settings()->get('syncCMSEmail') == FALSE) { + return; + } + //check if the primary email for the contact exists //$contactDetails[1] - email //$contactDetails[3] - email id diff --git a/settings/Core.setting.php b/settings/Core.setting.php index 0b35e75822..eaa0fe10fb 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -918,4 +918,18 @@ return array( 'description' => ts('Default to only loading matches against this number of contacts'), 'help_text' => ts('Deduping larger databases can crash the server. By configuring a limit other than 0 here the dedupe query will only search for matches against a limited number of contacts.'), ), + 'syncCMSEmail' => array( + 'group_name' => 'CiviCRM Preferences', + 'group' => 'core', + 'name' => 'syncCMSEmail', + 'type' => 'Boolean', + 'quick_form_type' => 'YesNo', + 'default' => 1, + 'add' => '4.7', + 'title' => 'Sync CMS Email', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'If enabled, then CMS email id will be syncronised with CiviCRM contacts\'s primary email.', + 'help_text' => NULL, + ), ); diff --git a/templates/CRM/Admin/Form/Setting/UF.tpl b/templates/CRM/Admin/Form/Setting/UF.tpl index 77f245749b..a38feec15b 100644 --- a/templates/CRM/Admin/Form/Setting/UF.tpl +++ b/templates/CRM/Admin/Form/Setting/UF.tpl @@ -41,6 +41,10 @@ {/if} + + {$form.syncCMSEmail.label} + {$form.syncCMSEmail.html} +
{include file="CRM/common/formButtons.tpl" location="bottom"}
diff --git a/tests/phpunit/api/v3/UFMatchTest.php b/tests/phpunit/api/v3/UFMatchTest.php index b48e426ac1..e68e43f526 100644 --- a/tests/phpunit/api/v3/UFMatchTest.php +++ b/tests/phpunit/api/v3/UFMatchTest.php @@ -128,6 +128,42 @@ class api_v3_UFMatchTest extends CiviUnitTestCase { $this->getAndCheck($this->_params, $result['id'], 'uf_match'); } + /** + * Test Civi to CMS email sync optional + */ + public function testUFNameMatchSync() { + $this->callAPISuccess('uf_match', 'create', $this->_params); + $email1 = substr(sha1(rand()), 0, 7) . '@test.com'; + $email2 = substr(sha1(rand()), 0, 7) . '@test.com'; + + // Case A: Enable CMS integration + Civi::settings()->set('syncCMSEmail', TRUE); + $this->callAPISuccess('email', 'create', array( + 'contact_id' => $this->_contactId, + 'email' => $email1, + 'is_primary' => 1, + )); + $ufName = $this->callAPISuccess('uf_match', 'getvalue', array( + 'contact_id' => $this->_contactId, + 'return' => 'uf_name', + )); + $this->assertEquals($email1, $ufName); + + // Case B: Disable CMS integration + Civi::settings()->set('syncCMSEmail', FALSE); + $this->callAPISuccess('email', 'create', array( + 'contact_id' => $this->_contactId, + 'email' => $email2, + 'is_primary' => 1, + )); + $ufName = $this->callAPISuccess('uf_match', 'getvalue', array( + 'contact_id' => $this->_contactId, + 'return' => 'uf_name', + )); + $this->assertNotEquals($email2, $ufName, 'primary email will not match if changed on disabled CMS integration setting'); + $this->assertEquals($email1, $ufName); + } + public function testDelete() { $result = $this->callAPISuccess('uf_match', 'create', $this->_params); $this->assertEquals(1, $this->callAPISuccess('uf_match', 'getcount', array( -- 2.25.1