From 325033b92947d6bba4565157d6fa3f34019901ea Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 18 Oct 2017 20:42:12 +1300 Subject: [PATCH] CRM-21324 - Support 'null' on date fields in the api --- api/v3/utils.php | 6 ++++++ tests/phpunit/api/v3/ContributionTest.php | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/api/v3/utils.php b/api/v3/utils.php index 0eb47fa992..8cd62eb150 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1685,6 +1685,12 @@ function _civicrm_api3_validate_date(&$params, &$fieldName, &$fieldInfo) { if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) { return; } + + if ($fieldValue === 'null' && empty($fieldInfo['api.required'])) { + // This is the wierd & wonderful way PEAR sets null. + return; + } + //should we check first to prevent it from being copied if they have passed in sql friendly format? if (!empty($params[$fieldInfo['name']])) { $fieldValue = _civicrm_api3_getValidDate($fieldValue, $fieldInfo['name'], $fieldInfo['type']); diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 3166a5a9ff..76ed69c027 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -678,6 +678,23 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date']))); } + /** + * Create test with unique field name on source. + */ + public function testCreateContributionNullOutThankyouDate() { + + $params = $this->_params; + $params['thankyou_date'] = 'yesterday'; + + $contribution = $this->callAPISuccess('contribution', 'create', $params); + $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id'])); + $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date']))); + + $params['thankyou_date'] = 'null'; + $contribution = $this->callAPISuccess('contribution', 'create', $params); + $contribution = $this->assertTrue(empty($contribution['thankyou_date'])); + } + /** * Create test with unique field name on source. */ -- 2.25.1