From e525d6af8e6bc8c3c0c28fc3fafaee22778e2f3c Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Mon, 11 Jul 2016 16:46:48 +0530 Subject: [PATCH] CRM-19056 - Add record button edits existing custom field record instead --- CRM/Core/BAO/CustomOption.php | 2 +- CRM/Core/BAO/CustomValueTable.php | 4 +- CRM/Custom/Page/AJAX.php | 6 +- .../Page/MultipleRecordFieldsListing.php | 4 +- tests/phpunit/CRM/Custom/Page/AjaxTest.php | 119 ++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 3 + 6 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/CRM/Custom/Page/AjaxTest.php diff --git a/CRM/Core/BAO/CustomOption.php b/CRM/Core/BAO/CustomOption.php index e0b87dd24a..12e708f219 100644 --- a/CRM/Core/BAO/CustomOption.php +++ b/CRM/Core/BAO/CustomOption.php @@ -81,7 +81,7 @@ class CRM_Core_BAO_CustomOption { $optionValues = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, array(), $inactiveNeeded ? 'get' : 'create'); - foreach ($optionValues as $value => $label) { + foreach ((array) $optionValues as $value => $label) { $options[] = array( 'label' => $label, 'value' => $value, diff --git a/CRM/Core/BAO/CustomValueTable.php b/CRM/Core/BAO/CustomValueTable.php index 4c4c4a0363..2c3b5e3a67 100644 --- a/CRM/Core/BAO/CustomValueTable.php +++ b/CRM/Core/BAO/CustomValueTable.php @@ -501,8 +501,8 @@ AND $cond } } } - if (!empty($DTparams)) { - return array($result, $sortedResult); + if (!empty($sortedResult)) { + $result['sortedResult'] = $sortedResult; } return $result; } diff --git a/CRM/Custom/Page/AJAX.php b/CRM/Custom/Page/AJAX.php index 7bb96f7d24..93dd3e4cb5 100644 --- a/CRM/Custom/Page/AJAX.php +++ b/CRM/Custom/Page/AJAX.php @@ -125,7 +125,7 @@ class CRM_Custom_Page_AJAX { $obj->_contactType = $contactType; $obj->_DTparams['offset'] = ($params['page'] - 1) * $params['rp']; $obj->_DTparams['rowCount'] = $params['rp']; - if ($params['sortBy']) { + if (!empty($params['sortBy'])) { $obj->_DTparams['sort'] = $params['sortBy']; } @@ -154,6 +154,10 @@ class CRM_Custom_Page_AJAX { $multiRecordFields['recordsTotal'] = $totalRecords; $multiRecordFields['recordsFiltered'] = $totalRecords; + if (!empty($_GET['is_unit_test'])) { + return $multiRecordFields; + } + CRM_Utils_JSON::output($multiRecordFields); } diff --git a/CRM/Profile/Page/MultipleRecordFieldsListing.php b/CRM/Profile/Page/MultipleRecordFieldsListing.php index f2db4ba0eb..d973c27f5f 100644 --- a/CRM/Profile/Page/MultipleRecordFieldsListing.php +++ b/CRM/Profile/Page/MultipleRecordFieldsListing.php @@ -262,9 +262,11 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic { $DTparams = !empty($this->_DTparams) ? $this->_DTparams : NULL; // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView) - list($result, $sortedResult) = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE, $DTparams); + $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE, $DTparams); $resultCount = !empty($result['count']) ? $result['count'] : count($result); + $sortedResult = !empty($result['sortedResult']) ? $result['sortedResult'] : array(); unset($result['count']); + unset($result['sortedResult']); if ($this->_pageViewType == 'profileDataView') { if (!empty($fieldIDs)) { diff --git a/tests/phpunit/CRM/Custom/Page/AjaxTest.php b/tests/phpunit/CRM/Custom/Page/AjaxTest.php new file mode 100644 index 0000000000..d4914c175c --- /dev/null +++ b/tests/phpunit/CRM/Custom/Page/AjaxTest.php @@ -0,0 +1,119 @@ +CustomGroupMultipleCreateWithFields(array('style' => 'Tab with table')); + $params = array( + 'contact_type' => 'Individual', + 'first_name' => 'Test', + 'last_name' => 'Contact', + ); + $customFields = $ids['custom_field_id']; + $result = $this->callAPISuccess('contact', 'create', $params); + $contactId = $result['id']; + + //enter values for custom fields + $customParams = array( + "custom_{$customFields[0]}_-1" => "test value {$customFields[0]} one", + "custom_{$customFields[0]}_-2" => "test value {$customFields[0]} two", + "custom_{$customFields[1]}_-1" => "test value {$customFields[1]} one", + "custom_{$customFields[1]}_-2" => "test value {$customFields[1]} two", + "custom_{$customFields[2]}_-1" => "test value {$customFields[2]} one", + "custom_{$customFields[2]}_-2" => "test value {$customFields[2]} two", + ); + CRM_Core_BAO_CustomValueTable::postProcess($customParams, "civicrm_contact", $contactId, NULL); + + $_GET = array( + 'cid' => $contactId, + 'cgid' => $ids['custom_group_id'], + 'is_unit_test' => TRUE, + ); + $multiRecordFields = CRM_Custom_Page_AJAX::getMultiRecordFieldList(); + + //check sorting + foreach ($customFields as $fieldId) { + $columnName = "field_{$fieldId}{$ids['custom_group_id']}_{$fieldId}"; + $_GET['columns'][] = array( + 'data' => $columnName, + ); + } + // get the results in descending order + $_GET['order'] = array( + '0' => array( + 'column' => 0, + 'dir' => 'desc', + ), + ); + $sortedRecords = CRM_Custom_Page_AJAX::getMultiRecordFieldList(); + + $this->assertEquals(2, $sortedRecords['recordsTotal']); + $this->assertEquals(2, $multiRecordFields['recordsTotal']); + foreach ($customFields as $fieldId) { + $columnName = "field_{$fieldId}{$ids['custom_group_id']}_{$fieldId}"; + $this->assertEquals("test value {$fieldId} one", $multiRecordFields['data'][0][$columnName]['data']); + $this->assertEquals("test value {$fieldId} two", $multiRecordFields['data'][1][$columnName]['data']); + + // this should be sorted in descending order. + $this->assertEquals("test value {$fieldId} two", $sortedRecords['data'][0][$columnName]['data']); + $this->assertEquals("test value {$fieldId} one", $sortedRecords['data'][1][$columnName]['data']); + } + + //check the links + $sorted = FALSE; + foreach (array($multiRecordFields, $sortedRecords) as $records) { + $count = 1; + //check links for result sorted in descending order + if ($sorted) { + $count = 2; + } + foreach ($records['data'] as $key => $val) { + preg_match_all('!https?://\S+!', $val['action'], $matches); + foreach ($matches[0] as $match) { + $parts = parse_url($match); + $parts['query'] = str_replace('&', '&', $parts['query']); + parse_str($parts['query'], $query); + + switch (trim($query['mode'], '"')) { + case 'view': + $this->assertEquals($count, $query['recId']); + break; + + case 'edit': + $this->assertEquals($count, $query['cgcount']); + break; + + case 'copy': + $this->assertEquals(3, $query['cgcount']); + break; + } + } + + //decrement the count as we're sorting in descending order. + if ($sorted) { + $count--; + } + else { + $count++; + } + } + $sorted = TRUE; + } + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 27dd1118b0..0ffa342f6f 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -2074,6 +2074,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $customField = $this->customFieldCreate(array( 'custom_group_id' => $ids['custom_group_id'], 'label' => 'field_1' . $ids['custom_group_id'], + 'in_selector' => 1, )); $ids['custom_field_id'][] = $customField['id']; @@ -2082,6 +2083,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { 'custom_group_id' => $ids['custom_group_id'], 'default_value' => '', 'label' => 'field_2' . $ids['custom_group_id'], + 'in_selector' => 1, )); $ids['custom_field_id'][] = $customField['id']; @@ -2089,6 +2091,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { 'custom_group_id' => $ids['custom_group_id'], 'default_value' => '', 'label' => 'field_3' . $ids['custom_group_id'], + 'in_selector' => 1, )); $ids['custom_field_id'][] = $customField['id']; -- 2.25.1