From 641a8cb544729558b1e8b8c33bb5d73cd96c4262 Mon Sep 17 00:00:00 2001 From: JKingsnorth Date: Thu, 22 Feb 2018 14:02:56 +0000 Subject: [PATCH] CRM-21773 Refactor related table static variable caches to support unit testing --- CRM/Dedupe/Merger.php | 49 +++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 414e8f5d43..e8eeb347e7 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -39,25 +39,25 @@ class CRM_Dedupe_Merger { * @return array */ public static function relTables() { - static $relTables; - // Setting these merely prevents enotices - but it may be more appropriate not to add the user table below - // if the url can't be retrieved. A more standardised way to retrieve them is. - // CRM_Core_Config::singleton()->userSystem->getUserRecordUrl() - however that function takes a contact_id & - // we may need a different function when it is not known. - $title = $userRecordUrl = ''; + if (!isset(Civi::$statics[__CLASS__]['relTables'])) { - $config = CRM_Core_Config::singleton(); - if ($config->userSystem->is_drupal) { - $userRecordUrl = CRM_Utils_System::url('user/%ufid'); - $title = ts('%1 User: %2; user id: %3', array(1 => $config->userFramework, 2 => '$ufname', 3 => '$ufid')); - } - elseif ($config->userFramework == 'Joomla') { - $userRecordUrl = $config->userSystem->getVersion() > 1.5 ? $config->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . '%ufid' : $config->userFrameworkBaseURL . "index2.php?option=com_users&view=user&task=edit&id[]=" . '%ufid'; - $title = ts('%1 User: %2; user id: %3', array(1 => $config->userFramework, 2 => '$ufname', 3 => '$ufid')); - } + // Setting these merely prevents enotices - but it may be more appropriate not to add the user table below + // if the url can't be retrieved. A more standardised way to retrieve them is. + // CRM_Core_Config::singleton()->userSystem->getUserRecordUrl() - however that function takes a contact_id & + // we may need a different function when it is not known. + $title = $userRecordUrl = ''; + + $config = CRM_Core_Config::singleton(); + if ($config->userSystem->is_drupal) { + $userRecordUrl = CRM_Utils_System::url('user/%ufid'); + $title = ts('%1 User: %2; user id: %3', array(1 => $config->userFramework, 2 => '$ufname', 3 => '$ufid')); + } + elseif ($config->userFramework == 'Joomla') { + $userRecordUrl = $config->userSystem->getVersion() > 1.5 ? $config->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . '%ufid' : $config->userFrameworkBaseURL . "index2.php?option=com_users&view=user&task=edit&id[]=" . '%ufid'; + $title = ts('%1 User: %2; user id: %3', array(1 => $config->userFramework, 2 => '$ufname', 3 => '$ufid')); + } - if (!$relTables) { $relTables = array( 'rel_table_contributions' => array( 'title' => ts('Contributions'), @@ -155,8 +155,12 @@ class CRM_Dedupe_Merger { // Allow hook_civicrm_merge() to adjust $relTables CRM_Utils_Hook::merge('relTables', $relTables); + + // Cache the results in a static variable + Civi::$statics[__CLASS__]['relTables'] = $relTables; } - return $relTables; + + return Civi::$statics[__CLASS__]['relTables']; } /** @@ -273,11 +277,12 @@ class CRM_Dedupe_Merger { * We treat multi-valued custom sets as "related tables" similar to activities, contributions, etc. * @param string $request * 'relTables' or 'cidRefs'. + * @return array * @see CRM-13836 */ public static function getMultiValueCustomSets($request) { - static $data = NULL; - if ($data === NULL) { + + if (!isset(Civi::$statics[__CLASS__]['multiValueCustomSets'])) { $data = array( 'relTables' => array(), 'cidRefs' => array(), @@ -296,8 +301,12 @@ class CRM_Dedupe_Merger { 'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&force=1&cid=$cid' . $urlSuffix), ); } + + // Store the result in a static variable cache + Civi::$statics[__CLASS__]['multiValueCustomSets'] = $data; } - return $data[$request]; + + return Civi::$statics[__CLASS__]['multiValueCustomSets'][$request]; } /** -- 2.25.1