CRM-19938: Make Civi to CMS email sync optional
authordeb.monish <monish.deb@webaccessglobal.com>
Thu, 2 Feb 2017 11:18:57 +0000 (16:48 +0530)
committerdeb.monish <monish.deb@webaccessglobal.com>
Fri, 3 Feb 2017 11:13:06 +0000 (16:43 +0530)
CRM/Admin/Form/Setting.php
CRM/Admin/Form/Setting/UF.php
CRM/Core/BAO/UFMatch.php
settings/Core.setting.php
templates/CRM/Admin/Form/Setting/UF.tpl
tests/phpunit/api/v3/UFMatchTest.php

index 16df7ca1e2dcddc8c4ffff7dc62c25872e88d230..671c8fe7ed3fb7cccadaf4b84370871568c78e6f 100644 (file)
@@ -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));
index 342d6f357ae4a24716fa40aaa22653d0397c4a50..ab290514d38b8b657ce64717c4c84e39c587e242 100644 (file)
@@ -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;
index bfd6a9b5cdfba3c1abd4e1e9068717573d5983b0..ba993670a3dc16bb083c94dee8669c2c70cc4bad 100644 (file)
@@ -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
index 0b35e75822059163b85573644aa6f78a5cf5e2d3..eaa0fe10fb06be172740a2d7b2d07ac458de5974 100644 (file)
@@ -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,
+  ),
 );
index 77f245749be9dcadfe5480ecee5c650dc8e02d87..a38feec15b701043829a8d89355457b2608e0b87 100644 (file)
             </td>
         </tr>
         {/if}
+        <tr class="crm-uf-form-block-syncCMSEmail">
+           <td class="label">{$form.syncCMSEmail.label}</td>
+           <td>{$form.syncCMSEmail.html}</td>
+       </tr>
         </table>
             <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
 <div class="spacer"></div>
index b48e426ac192603e242b39b1ff391ce5929cf42a..e68e43f5265f7e158aedd4773e9a68c447ac78b0 100644 (file)
@@ -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(