From ef9170e41de4546eb37eb3c4898bcdb407599137 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 3 Jul 2021 11:14:22 -0400 Subject: [PATCH] APIv4 - Fix saving multi-contactRef field values --- Civi/Api4/Generic/Traits/DAOActionTrait.php | 5 ++- .../api/v4/Action/CustomContactRefTest.php | 34 ++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Civi/Api4/Generic/Traits/DAOActionTrait.php b/Civi/Api4/Generic/Traits/DAOActionTrait.php index 9e225bc79b..0c5f5ebe8c 100644 --- a/Civi/Api4/Generic/Traits/DAOActionTrait.php +++ b/Civi/Api4/Generic/Traits/DAOActionTrait.php @@ -224,7 +224,10 @@ trait DAOActionTrait { formatCheckBoxField($value, 'custom_' . $field['id'], $this->getEntityName()); } - if ($field['data_type'] === 'ContactReference' && !is_numeric($value)) { + // Match contact id to strings like "user_contact_id" + // FIXME handle arrays for multi-value contact reference fields, etc. + if ($field['data_type'] === 'ContactReference' && is_string($value) && !is_numeric($value)) { + // FIXME decouple from v3 API require_once 'api/v3/utils.php'; $value = \_civicrm_api3_resolve_contactID($value); if ('unknown-user' === $value) { diff --git a/tests/phpunit/api/v4/Action/CustomContactRefTest.php b/tests/phpunit/api/v4/Action/CustomContactRefTest.php index d6b48492d3..b6ca751316 100644 --- a/tests/phpunit/api/v4/Action/CustomContactRefTest.php +++ b/tests/phpunit/api/v4/Action/CustomContactRefTest.php @@ -29,6 +29,7 @@ use Civi\Api4\CustomGroup; class CustomContactRefTest extends BaseCustomValueTest { public function testGetWithJoin() { + $firstName = uniqid('fav'); $customGroup = CustomGroup::create(FALSE) ->addValue('name', 'MyContactRef') @@ -43,18 +44,41 @@ class CustomContactRefTest extends BaseCustomValueTest { ->addValue('data_type', 'ContactReference') ->execute(); + CustomField::create(FALSE) + ->addValue('label', 'FavPeople') + ->addValue('custom_group_id', $customGroup['id']) + ->addValue('html_type', 'Autocomplete-Select') + ->addValue('data_type', 'ContactReference') + ->addValue('serialize', 1) + ->execute(); + $favPersonId = Contact::create(FALSE) - ->addValue('first_name', 'Favorite') + ->addValue('first_name', $firstName) ->addValue('last_name', 'Person') ->addValue('contact_type', 'Individual') ->execute() ->first()['id']; - $contactId = Contact::create(FALSE) + $favPeopleId1 = Contact::create(FALSE) + ->addValue('first_name', 'Favorite1') + ->addValue('last_name', 'People1') + ->addValue('contact_type', 'Individual') + ->execute() + ->first()['id']; + + $favPeopleId2 = Contact::create(FALSE) + ->addValue('first_name', 'Favorite2') + ->addValue('last_name', 'People2') + ->addValue('contact_type', 'Individual') + ->execute() + ->first()['id']; + + $contactId1 = Contact::create(FALSE) ->addValue('first_name', 'Mya') ->addValue('last_name', 'Tester') ->addValue('contact_type', 'Individual') ->addValue('MyContactRef.FavPerson', $favPersonId) + ->addValue('MyContactRef.FavPeople', [$favPeopleId1, $favPeopleId2]) ->execute() ->first()['id']; @@ -62,12 +86,14 @@ class CustomContactRefTest extends BaseCustomValueTest { ->addSelect('display_name') ->addSelect('MyContactRef.FavPerson.first_name') ->addSelect('MyContactRef.FavPerson.last_name') - ->addWhere('id', '=', $contactId) + ->addSelect('MyContactRef.FavPeople') + ->addWhere('MyContactRef.FavPerson.first_name', '=', $firstName) ->execute() ->first(); - $this->assertEquals('Favorite', $contact['MyContactRef.FavPerson.first_name']); + $this->assertEquals($firstName, $contact['MyContactRef.FavPerson.first_name']); $this->assertEquals('Person', $contact['MyContactRef.FavPerson.last_name']); + $this->assertEquals([$favPeopleId1, $favPeopleId2], $contact['MyContactRef.FavPeople']); } public function testCurrentUser() { -- 2.25.1