From: deb.monish Date: Tue, 26 Jun 2018 09:56:39 +0000 (+0530) Subject: dev/core#41: Add unit test X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=8ad22b15e97fbfd18a03aabbcc44efd8ad52ccb5;p=civicrm-core.git dev/core#41: Add unit test --- diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 3154ed24ed..554eb94dc9 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -93,15 +93,17 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { return [ 'String' => CRM_Utils_Type::T_STRING, 'Int' => CRM_Utils_Type::T_INT, + 'Money' => CRM_Utils_Type::T_MONEY, + 'Memo' => CRM_Utils_Type::T_LONGTEXT, 'Float' => CRM_Utils_Type::T_FLOAT, - 'Money' => CRM_Utils_Type::T_FLOAT, - 'Memo' => CRM_Utils_Type::T_TEXT, 'Date' => CRM_Utils_Type::T_DATE, + 'DateTime' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, 'Boolean' => CRM_Utils_Type::T_BOOLEAN, 'StateProvince' => CRM_Utils_Type::T_INT, - 'Country' => CRM_Utils_Type::T_INT, + 'File' => CRM_Utils_Type::T_STRING, 'Link' => CRM_Utils_Type::T_STRING, 'ContactReference' => CRM_Utils_Type::T_INT, + 'Country' => CRM_Utils_Type::T_INT, ]; } diff --git a/api/v3/utils.php b/api/v3/utils.php index 814d11e0b8..094beb2375 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1977,43 +1977,15 @@ function _civicrm_api_get_custom_fields($entity, &$params) { // Regular fields have a 'name' property $value['name'] = 'custom_' . $key; $value['title'] = $value['label']; - $value['type'] = _getStandardTypeFromCustomDataType($value); + if ($value['data_type'] == 'Date' && CRM_Utils_Array::value('time_format', $value, 0) > 0) { + $value['data_type'] = 'DateTime'; + } + $value['type'] = CRM_Utils_Array::value($value['data_type'], CRM_Core_BAO_CustomField::dataToType()); $ret['custom_' . $key] = $value; } return $ret; } -/** - * Translate the custom field data_type attribute into a std 'type'. - * - * @param array $value - * - * @return int - */ -function _getStandardTypeFromCustomDataType($value) { - $dataType = $value['data_type']; - //CRM-15792 - If date custom field contains timeformat change type to DateTime - if ($value['data_type'] == 'Date' && isset($value['time_format']) && $value['time_format'] > 0) { - $dataType = 'DateTime'; - } - $mapping = array( - 'String' => CRM_Utils_Type::T_STRING, - 'Int' => CRM_Utils_Type::T_INT, - 'Money' => CRM_Utils_Type::T_MONEY, - 'Memo' => CRM_Utils_Type::T_LONGTEXT, - 'Float' => CRM_Utils_Type::T_FLOAT, - 'Date' => CRM_Utils_Type::T_DATE, - 'DateTime' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, - 'Boolean' => CRM_Utils_Type::T_BOOLEAN, - 'StateProvince' => CRM_Utils_Type::T_INT, - 'File' => CRM_Utils_Type::T_STRING, - 'Link' => CRM_Utils_Type::T_STRING, - 'ContactReference' => CRM_Utils_Type::T_INT, - 'Country' => CRM_Utils_Type::T_INT, - ); - return $mapping[$dataType]; -} - /** * Fill params array with alternate (alias) values where a field has an alias and that is filled & the main field isn't. diff --git a/tests/phpunit/CRM/Contact/SelectorTest.php b/tests/phpunit/CRM/Contact/SelectorTest.php index a000c70e1c..23732e4669 100644 --- a/tests/phpunit/CRM/Contact/SelectorTest.php +++ b/tests/phpunit/CRM/Contact/SelectorTest.php @@ -420,6 +420,60 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { $this->assertTrue(strpos($query->_fromClause, $cgTableName) !== FALSE); } + /** + * Check where clause of a date custom field when 'IS NOT EMPTY' operator is used + */ + public function testCustomDateField() { + $contactID = $this->individualCreate(); + //Create a test custom group and field. + $customGroup = $this->callAPISuccess('CustomGroup', 'create', array( + 'title' => "test custom group", + 'extends' => "Individual", + )); + $customTableName = $this->callAPISuccess('CustomGroup', 'getValue', ['id' => $customGroup, 'return' => 'table_name']); + $customGroupTableName = $customGroup['values'][$customGroup['id']]['table_name']; + + $createdField = $this->callAPISuccess('customField', 'create', [ + 'data_type' => 'Date', + 'html_type' => 'Select Date', + 'date_format' => 'd M yy', + 'time_format' => 1, + 'label' => 'test field', + 'custom_group_id' => $customGroup['id'], + ]); + $customFieldColumnName = $createdField['values'][$createdField['id']]['column_name']; + + $this->callAPISuccess('Contact', 'create', [ + 'id' => $contactID, + 'custom_' . $createdField['id'] => date('YmdHis'), + ]); + + $selector = new CRM_Contact_Selector( + 'CRM_Contact_Selector', + ['custom_' . $createdField['id'] => ['IS NOT EMPTY' => 1]], + [[ + 0 => 'custom_' . $createdField['id'], + 1 => 'IS NOT NULL', + 2 => 1, + 3 => 1, + 4 => 0, + ]], + [], + CRM_Core_Action::NONE, + NULL, + FALSE, + 'builder' + ); + + $whereClause = $selector->getQueryObject()->query()[2]; + $expectedClause = sprintf("( %s.%s IS NOT NULL )", $customGroupTableName, $customFieldColumnName); + // test the presence of expected date clause + $this->assertEquals(TRUE, strpos($whereClause, $expectedClause)); + + $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, NULL); + $this->assertEquals(1, count($rows)); + } + /** * Get the default select string since this is generally consistent. */