From 98e424ab24de14082fd5a1c3925f48971025f1a4 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Mon, 7 Feb 2022 21:25:26 +0000 Subject: [PATCH] APIv4 - Fix setting/getting a multi-record custom field with date+time --- Civi/Api4/Utils/FormattingUtil.php | 2 +- .../api/v4/Action/CreateCustomValueTest.php | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Civi/Api4/Utils/FormattingUtil.php b/Civi/Api4/Utils/FormattingUtil.php index 4c62b46592..140e149244 100644 --- a/Civi/Api4/Utils/FormattingUtil.php +++ b/Civi/Api4/Utils/FormattingUtil.php @@ -117,7 +117,7 @@ class FormattingUtil { switch ($fieldSpec['data_type'] ?? NULL) { case 'Timestamp': - $value = self::formatDateValue('Y-m-d H:i:s', $value, $operator, $index); + $value = self::formatDateValue('YmdHis', $value, $operator, $index); break; case 'Date': diff --git a/tests/phpunit/api/v4/Action/CreateCustomValueTest.php b/tests/phpunit/api/v4/Action/CreateCustomValueTest.php index 9b42c38748..2419625f09 100644 --- a/tests/phpunit/api/v4/Action/CreateCustomValueTest.php +++ b/tests/phpunit/api/v4/Action/CreateCustomValueTest.php @@ -21,6 +21,7 @@ namespace api\v4\Action; use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; +use Civi\Api4\CustomValue; use Civi\Api4\OptionGroup; use Civi\Api4\OptionValue; @@ -73,4 +74,50 @@ class CreateCustomValueTest extends BaseCustomValueTest { $this->assertEquals($optionValues, $createdOptionValues); } + /** + * Test setting/getting a multivalue customfield with date+time + */ + public function testCustomDataWithDateTime() { + CustomGroup::create(FALSE) + ->addValue('title', 'MyContactDateFields') + ->addValue('name', 'MyContactDateFields') + ->addValue('extends', 'Contact') + ->addValue('is_multiple', TRUE) + ->execute(); + + CustomField::create(FALSE) + ->addValue('custom_group_id:name', 'MyContactDateFields') + ->addValue('label', 'Date field') + ->addValue('name', 'date_field') + ->addValue('data_type', 'Date') + ->addValue('html_type', 'Select Date') + ->addValue('date_format', 'yy-mm-dd') + ->execute(); + + CustomField::create(FALSE) + ->addValue('custom_group_id:name', 'MyContactDateFields') + ->addValue('label', 'Date time field') + ->addValue('name', 'date_time_field') + ->addValue('data_type', 'Date') + ->addValue('html_type', 'Select Date') + ->addValue('date_format', 'yy-mm-dd') + ->addValue('time_format', 2) + ->execute(); + + $contactID = $this->createEntity(['type' => 'Individual'])['id']; + + CustomValue::create('MyContactDateFields', FALSE) + ->addValue('date_field', '2022-02-02') + ->addValue('date_time_field', '2022-02-02 12:07:31') + ->addValue('entity_id', $contactID) + ->execute(); + $result = CustomValue::get('MyContactDateFields', FALSE) + ->execute() + ->first(); + + $this->assertEquals('2022-02-02', $result['date_field']); + $this->assertEquals('2022-02-02 12:07:31', $result['date_time_field']); + + } + } -- 2.25.1