From: monishdeb Date: Mon, 25 Jan 2016 19:46:00 +0000 (+0530) Subject: test coverage X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2fe91f9d1e1d608d000de172ed03154e0b21a7a5;p=civicrm-core.git test coverage --- diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 2fb40419a3..7db2bafb47 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4390,9 +4390,7 @@ civicrm_relationship.is_permission_a_b = 0 $from = CRM_Utils_Array::value($customFieldName . '_from', $formValues, NULL); $to = CRM_Utils_Array::value($customFieldName . '_to', $formValues, NULL); - $customFieldID = CRM_Core_BAO_CustomField::getKeyID($customFieldName); - $customFieldType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldName, 'data_type'); - if ($customFieldType == 'Date') { + if (self::isCustomDateField($customFieldName)) { list($from, $to) = CRM_Utils_Date::getFromTo(NULL, $from, $to); } } @@ -4417,6 +4415,23 @@ civicrm_relationship.is_permission_a_b = 0 ); } + /** + * Are we dealing with custom field of type date. + * + * @param $fieldName + * + * @return bool + */ + public static function isCustomDateField($fieldName) { + if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($fieldName)) == FALSE) { + return FALSE; + } + if ('Date' == civicrm_api3('CustomField', 'getvalue', array('id' => $customFieldID, 'return' => 'data_type'))) { + return TRUE; + } + return FALSE; + } + /** * Has this field already been reformatting to Query object syntax. * @@ -5363,7 +5378,9 @@ SELECT COUNT( conts.total_amount ) as cancel_count, // We could get away with keeping this in 4.6 if we make it such that it throws an enotice in 4.7 so // people have to de-slopify it. if (!empty($value[0])) { - $dragonPlace = $iAmAnIntentionalENoticeThatWarnsOfAProblemYouShouldReport; + if ($op != 'BETWEEN') { + $dragonPlace = $iAmAnIntentionalENoticeThatWarnsOfAProblemYouShouldReport; + } if (($queryString = CRM_Core_DAO::createSqlFilter($field, array($op => $value), $dataType)) != FALSE) { return $queryString; } diff --git a/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php b/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php new file mode 100644 index 0000000000..bafecd7d1a --- /dev/null +++ b/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php @@ -0,0 +1,227 @@ +quickCleanup($tablesToTruncate, TRUE); + parent::tearDown(); + } + + /** + * Test filtering by relative custom data dates. + */ + public function testSearchCustomDataDateRelative() { + $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); + $dateCustomField = $this->customFieldCreate(array( + 'custom_group_id' => $ids['custom_group_id'], + 'label' => 'date field', + 'data_type' => 'Date', + 'html_type' => 'Select Date', + 'default_value' => NULL, + )); + $dateCustomFieldName = 'custom_' . $dateCustomField['id']; + $formValues = array( + $dateCustomFieldName . '_relative' => 'this.year', + $dateCustomFieldName . '_from' => '', + $dateCustomFieldName . '_to' => '', + ); + // Assigning the relevant form value to be within a custom key is normally done in + // build field params. It would be better if it were all done in convertFormValues + // but for now we just imitate it. + $params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues); + $queryObj = new CRM_Core_BAO_CustomQuery($params); + $queryObj->Query(); + $this->assertEquals( + 'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "' . date('Y') . '0101000000" AND "' . date('Y') . '1231235959"', + $queryObj->_where[0][0] + ); + $this->assertEquals($queryObj->_qill[0][0], "date field BETWEEN 'January 1st, " . date('Y') . " 12:00 AM AND December 31st, " . date('Y') . " 11:59 PM'"); + } + + /** + * Test filtering by relative custom data dates. + */ + public function testSearchCustomDataDateFromTo() { + $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); + $dateCustomField = $this->customFieldCreate(array( + 'custom_group_id' => $ids['custom_group_id'], + 'label' => 'date field', + 'data_type' => 'Date', + 'html_type' => 'Select Date', + 'default_value' => NULL, + )); + $dateCustomFieldName = 'custom_' . $dateCustomField['id']; + // Assigning the relevant form value to be within a custom key is normally done in + // build field params. It would be better if it were all done in convertFormValues + // but for now we just imitate it. + $formValues = array( + $dateCustomFieldName . '_from' => '2014-06-06', + $dateCustomFieldName . '_to' => '2015-06-06', + ); + + $params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues); + $queryObj = new CRM_Core_BAO_CustomQuery($params); + $queryObj->Query(); + $this->assertEquals( + 'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "20140606000000" AND "20150606235959"', + $queryObj->_where[0][0] + ); + $this->assertEquals($queryObj->_qill[0][0], "date field BETWEEN 'June 6th, 2014 12:00 AM AND June 6th, 2015 11:59 PM'"); + } + + /** + * Test filtering by relative custom data. + */ + public function testSearchCustomDataFromTo() { + $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); + $datas = array( + 'Int' => 2, + 'Float' => 12.123, + 'Money' => 91.21, + ); + foreach ($datas as $type => $data) { + $customField = $this->customFieldCreate( + array( + 'custom_group_id' => $ids['custom_group_id'], + 'label' => "$type field", + 'data_type' => $type, + 'html_type' => 'Text', + 'default_value' => NULL, + ) + ); + $customFieldName = 'custom_' . $customField['id']; + // Assigning the relevant form value to be within a custom key is normally done in + // build field params. It would be better if it were all done in convertFormValues + // but for now we just imitate it. + $from = $data - 1; + $to = $data; + $formValues = array( + $customFieldName . '_from' => $from, + $customFieldName . '_to' => $to, + ); + + $params = array($customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)); + $queryObj = new CRM_Core_BAO_CustomQuery($params); + $queryObj->Query(); + $this->assertEquals( + "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} BETWEEN \"$from\" AND \"$to\"", + $queryObj->_where[0][0] + ); + $this->assertEquals($queryObj->_qill[0][0], "$type field BETWEEN $from, $to"); + } + } + + /** + * Test filtering by relative custom data. + */ + public function testSearchCustomDataFromAndTo() { + $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); + $datas = array( + 'Date' => '2015-06-06', + 'Int' => 2, + 'Float' => 12.123, + 'Money' => 91.21, + ); + foreach ($datas as $type => $data) { + $isDate = ($type === 'Date'); + $customField = $this->customFieldCreate( + array( + 'custom_group_id' => $ids['custom_group_id'], + 'label' => "$type field", + 'data_type' => $type, + 'html_type' => ($isDate) ? 'Select Date' : 'Text', + 'default_value' => NULL, + ) + ); + $customFieldName = 'custom_' . $customField['id']; + + $expectedValue = ($isDate) ? '"20150606235959"' : (($type == 'Money') ? $data : "\"$data\""); + $expectedQillValue = ($isDate) ? "'June 6th, 2015 11:59 PM'" : $data; + + // Assigning the relevant form value to be within a custom key is normally done in + // build field params. It would be better if it were all done in convertFormValues + // but for now we just imitate it. + + //Scenrio 2 : TO date filter + $formValues = array( + $customFieldName . '_to' => $data, + ); + + $params = array($customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)); + $queryObj = new CRM_Core_BAO_CustomQuery($params); + $queryObj->Query(); + $wierdStringThatMeansGreaterEquals = chr(226) . chr(137) . chr(164); + + $this->assertEquals( + "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} <= $expectedValue", + $queryObj->_where[0][0] + ); + $this->assertEquals($queryObj->_qill[0][0], + "$type field " . $wierdStringThatMeansGreaterEquals . " $expectedQillValue" + ); + + //Scenrio 2 : FROM date filter + $formValues = array( + $customFieldName . '_from' => $data, + ); + + $params = array($customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)); + $queryObj = new CRM_Core_BAO_CustomQuery($params); + $queryObj->Query(); + $wierdStringThatMeansLessThanEquals = chr(226) . chr(137) . chr(165); + + $expectedValue = ($isDate) ? '"20150606000000"' : $expectedValue; + $expectedQillValue = ($isDate) ? "'June 6th, 2015 12:00 AM'" : $expectedQillValue; + $this->assertEquals( + "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} >= $expectedValue", + $queryObj->_where[0][0] + ); + $this->assertEquals($queryObj->_qill[0][0], + "$type field " . $wierdStringThatMeansLessThanEquals . " $expectedQillValue" + ); + } + } + + /** + * Test filtering by relative custom data dates. + */ + public function testSearchCustomDataDateEquals() { + $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); + $dateCustomField = $this->customFieldCreate(array( + 'custom_group_id' => $ids['custom_group_id'], + 'label' => 'date field', + 'data_type' => 'Date', + 'html_type' => 'Select Date', + 'default_value' => NULL, + )); + $dateCustomFieldName = 'custom_' . $dateCustomField['id']; + $this->individualCreate(array($dateCustomFieldName => "2015-01-01")); + // Assigning the relevant form value to be within a custom key is normally done in + // build field params. It would be better if it were all done in convertFormValues + // but for now we just imitate it. + $formValues = array($dateCustomFieldName => '2015-06-06'); + $params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues); + $queryObj = new CRM_Core_BAO_CustomQuery($params); + $queryObj->Query(); + + $this->assertEquals( + "civicrm_value_testsearchcus_1.date_field_2 = '2015-06-06'", + $queryObj->_where[0][0] + ); + $this->assertEquals($queryObj->_qill[0][0], "date field = 'June 6th, 2015'"); + } + +}