From e5ad0335ae55f0c9b4aeff632c29eb78e6edefd7 Mon Sep 17 00:00:00 2001 From: Eileen Date: Mon, 20 Jun 2016 06:16:21 +0000 Subject: [PATCH] CRM-18435 handling for membership type etc fields --- CRM/Contact/BAO/Query.php | 2 +- CRM/Contact/BAO/SavedSearch.php | 5 +- CRM/Member/Form/Search.php | 10 +- CRM/Utils/Array.php | 28 ++- .../CRM/Contact/BAO/SavedSearchTest.php | 172 ++++++++++++++++++ 5 files changed, 207 insertions(+), 10 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 561ae08e24..180e81669a 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1641,7 +1641,7 @@ class CRM_Contact_BAO_Query { if (in_array($id, $legacyElements) && is_array($values)) { // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1), // as per the recent Search fixes $values need to be in standard array(id1, id2) format - CRM_Utils_Array::formatArrayKeys($values); + $values = CRM_Utils_Array::convertCheckboxFormatToArray($values); } } diff --git a/CRM/Contact/BAO/SavedSearch.php b/CRM/Contact/BAO/SavedSearch.php index 87158cdb51..a635591cee 100644 --- a/CRM/Contact/BAO/SavedSearch.php +++ b/CRM/Contact/BAO/SavedSearch.php @@ -91,7 +91,7 @@ class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch { * @return array * the values of the posted saved search used as default values in various Search Form */ - public static function &getFormValues($id) { + public static function getFormValues($id) { $fv = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'form_values'); $result = NULL; if ($fv) { @@ -113,6 +113,9 @@ class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch { } if (!empty($value) && is_array($value)) { if (in_array($element, $specialFields)) { + // Remove the element to minimise support for legacy formats. It is stored in $value + // so will be re-set with the right name. + unset($result[$element]); $element = str_replace('member_membership_type_id', 'membership_type_id', $element); $element = str_replace('member_status_id', 'membership_status_id', $element); CRM_Contact_BAO_Query::legacyConvertFormValues($element, $value); diff --git a/CRM/Member/Form/Search.php b/CRM/Member/Form/Search.php index a338f2213d..7e99b2ba13 100644 --- a/CRM/Member/Form/Search.php +++ b/CRM/Member/Form/Search.php @@ -31,10 +31,6 @@ * @copyright CiviCRM LLC (c) 2004-2016 */ -/** - * Files required - */ - /** * Membership search. * @@ -52,14 +48,14 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search { /** * Are we restricting ourselves to a single contact. * - * @var boolean + * @var bool */ protected $_single = FALSE; /** * Are we restricting ourselves to a single contact. * - * @var boolean + * @var bool */ protected $_limit = NULL; @@ -93,7 +89,7 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search { * driven by the wizard framework */ - $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); + $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean'); $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); diff --git a/CRM/Utils/Array.php b/CRM/Utils/Array.php index b342b3f6ff..64d7c14410 100644 --- a/CRM/Utils/Array.php +++ b/CRM/Utils/Array.php @@ -1030,8 +1030,10 @@ class CRM_Utils_Array { * Convert array where key(s) holds the actual value and value(s) as 1 into array of actual values * Ex: array('foobar' => 1, 4 => 1) formatted into array('foobar', 4) * + * @deprecated use convertCheckboxInputToArray instead (after testing) + * https://github.com/civicrm/civicrm-core/pull/8169 + * * @param array $array - * @return void */ public static function formatArrayKeys(&$array) { if (!is_array($array)) { @@ -1050,4 +1052,28 @@ class CRM_Utils_Array { } } + /** + * Convert the data format coming in from checkboxes to an array of values. + * + * The input format from check boxes looks like + * array('value1' => 1, 'value2' => 1). This function converts those values to + * array(''value1', 'value2). + * + * The function will only alter the array if all values are equal to 1. + * + * @param array $input + * + * @return array + */ + public static function convertCheckboxFormatToArray($input) { + if (isset($input[0])) { + return $input; + } + $keys = array_keys($input, 1); + if ((count($keys) == count($input))) { + return $keys; + } + return $input; + } + } diff --git a/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php b/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php new file mode 100644 index 0000000000..18be1916bc --- /dev/null +++ b/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php @@ -0,0 +1,172 @@ +quickCleanup(array( + 'civicrm_mapping_field', + 'civicrm_mapping', + 'civicrm_group', + 'civicrm_saved_search', + )); + } + + /** + * Test fixValues function. + * + * @dataProvider getSavedSearches + */ + public function testGetFormValues($formValues, $expectedResult, $searchDescription) { + CRM_Core_DAO::executeQuery( + "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')" + ); + $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()')); + $this->assertEquals(array('membership_type_id', 'membership_status_id'), array_keys($result)); + foreach ($result as $key => $value) { + $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription); + } + } + + + /** + * Get variants of the fields we want to test. + * + * @return array + */ + public function getSavedSearches() { + $return = array(); + $searches = $this->getSearches(); + foreach ($searches as $key => $search) { + $return[] = array($search['form_values'], $search['expected'], $key); + } + return $return; + } + + /** + * Get variants of potential saved form values. + * + * Note that we include 1 in various ways to cover the possibility that 1 is treated as a boolean. + * + * @return array + */ + public function getSearches() { + return array( + 'checkbox_format_1_first' => array( + 'form_values' => array( + 'member_membership_type_id' => array(1 => 1, 2 => 1), + 'member_status_id' => array(1 => 1, 2 => 1), + ), + 'expected' => array(1, 2), + ), + 'checkbox_format_1_later' => array( + 'form_values' => array( + 'member_membership_type_id' => array(2 => 1, 1 => 1), + 'member_status_id' => array(2 => 1, 1 => 1), + ), + 'expected' => array(2, 1), + ), + 'checkbox_format_single_use_1' => array( + 'form_values' => array( + 'member_membership_type_id' => array(1 => 1), + 'member_status_id' => array(1 => 1), + ), + 'expected' => array(1), + ), + 'checkbox_format_single_not_1' => array( + 'form_values' => array( + 'member_membership_type_id' => array(2 => 1), + 'member_status_id' => array(2 => 1), + ), + 'expected' => array(2), + ), + 'array_format' => array( + 'form_values' => array( + 'member_membership_type_id' => array(1, 2), + 'member_status_id' => array(1, 2), + ), + 'expected' => array(1, 2), + ), + 'array_format_1_later' => array( + 'form_values' => array( + 'member_membership_type_id' => array(2, 1), + 'member_status_id' => array(2, 1), + ), + 'expected' => array(2, 1), + ), + 'array_format_single_use_1' => array( + 'form_values' => array( + 'member_membership_type_id' => array(1), + 'member_status_id' => array(1), + ), + 'expected' => array(1), + ), + 'array_format_single_not_1' => array( + 'form_values' => array( + 'member_membership_type_id' => array(2), + 'member_status_id' => array(2), + ), + 'expected' => array(2), + ), + 'IN_format_single_not_1' => array( + 'form_values' => array( + 'membership_type_id' => array('IN' => array(2)), + 'membership_status_id' => array('IN' => array(2)), + ), + 'expected' => array(2), + ), + 'IN_format_1_later' => array( + 'form_values' => array( + 'membership_type_id' => array('IN' => array(2, 1)), + 'membership_status_id' => array('IN' => array(2, 1)), + ), + 'expected' => array(2, 1), + ), + ); + } + +} -- 2.25.1