From: Eileen Date: Wed, 9 Apr 2014 03:57:51 +0000 (+0000) Subject: CRM-14446 contact.get api tests + fix on date filters X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b1f09beaa08649ad6f97e0d2103b052e7c0c7a60;p=civicrm-core.git CRM-14446 contact.get api tests + fix on date filters ---------------------------------------- * CRM-14446: Contact.get api - birth_date & deceased_date fixes https://issues.civicrm.org/jira/browse/CRM-14446 --- diff --git a/api/v3/utils.php b/api/v3/utils.php index 2312c043aa..fbfa481098 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -463,7 +463,6 @@ function _civicrm_api3_get_using_query_object($entity, $params, $additional_opti } $skipPermissions = CRM_Utils_Array::value('check_permissions', $params)? 0 :1; - list($entities, $options) = CRM_Contact_BAO_Query::apiQuery( $newParams, $returnProperties, @@ -1262,7 +1261,8 @@ function _civicrm_api3_validate_date(&$params, &$fieldName, &$fieldInfo) { if (strtotime($params[$fieldInfo['name']]) === FALSE) { throw new Exception($fieldInfo['name'] . " is not a valid date: " . $params[$fieldInfo['name']]); } - $params[$fieldInfo['name']] = CRM_Utils_Date::processDate($params[$fieldInfo['name']]); + $format = ($fieldInfo['type'] == 4) ? 'Ymd000000' : 'YmdHis'; + $params[$fieldInfo['name']] = CRM_Utils_Date::processDate($params[$fieldInfo['name']], NULL, FALSE, $format); } if ((CRM_Utils_Array::value('name', $fieldInfo) != $fieldName) && CRM_Utils_Array::value($fieldName, $params)) { //If the unique field name differs from the db name & is set handle it here diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 7c1ef06a8f..e7ca3fd241 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -1208,6 +1208,52 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->callAPISuccess('contact', 'delete', $contact); } + /** + * Test birthdate params incl value, array & birth_date_high, birthdate_low + * && deceased + */ + public function testContactGetBirthdate() { + $date = date('d M', strtotime('+ 2 days')); + $contact1 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month - 2 years'))); + $contact2 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month - 5 years'))); + $contact3 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month -20 years'))); + + $result = $this->callAPISuccess('contact', 'get', array()); + $this->assertEquals(date('Y-m-d', strtotime('first day of next month -2 years')), $result['values'][$contact1['id']]['birth_date']); + $result = $this->callAPISuccess('contact', 'get', array('birth_date' => 'first day of next month -5 years')); + $this->assertEquals(1, $result['count']); + $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']); + $result = $this->callAPISuccess('contact', 'get', array('birth_date_high' => date('Y-m-d', strtotime('-6 years')))); + $this->assertEquals(1, $result['count']); + $this->assertEquals(date('Y-m-d', strtotime('first day of next month -20 years')), $result['values'][$contact3['id']]['birth_date']); + $result = $this->callAPISuccess('contact', 'get', array('birth_date_low' => date('Y-m-d', strtotime('-6 years')), 'birth_date_high' => date('Y-m-d', strtotime('- 3 years')))); + $this->assertEquals(1, $result['count']); + $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']); + } + + /** + * Test Deceaseddate params incl value, array & Deceased_date_high, Deceaseddate_low + * && deceased + */ + public function testContactGetDeceaseddate() { + $date = date('d M', strtotime('+ 2 days')); + $contact1 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month - 2 years'))); + $contact2 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month - 5 years'))); + $contact3 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month -20 years'))); + + $result = $this->callAPISuccess('contact', 'get', array()); + $this->assertEquals(date('Y-m-d', strtotime('first day of next month -2 years')), $result['values'][$contact1['id']]['deceased_date']); + $result = $this->callAPISuccess('contact', 'get', array('deceased_date' => 'first day of next month -5 years')); + $this->assertEquals(1, $result['count']); + $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['deceased_date']); + $result = $this->callAPISuccess('contact', 'get', array('deceased_date_high' => date('Y-m-d', strtotime('-6 years')))); + $this->assertEquals(1, $result['count']); + $this->assertEquals(date('Y-m-d', strtotime('first day of next month -20 years')), $result['values'][$contact3['id']]['deceased_date']); + $result = $this->callAPISuccess('contact', 'get', array('deceased_date_low' => date('Y-m-d', strtotime('-6 years')), 'deceased_date_high' => date('Y-m-d', strtotime('- 3 years')))); + $this->assertEquals(1, $result['count']); + $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['deceased_date']); + } + /** * Test for Contact.get id=@user:username */