APIv4 - Fix saving multi-contactRef field values
authorColeman Watts <coleman@civicrm.org>
Sat, 3 Jul 2021 15:14:22 +0000 (11:14 -0400)
committerColeman Watts <coleman@civicrm.org>
Sun, 4 Jul 2021 17:39:05 +0000 (13:39 -0400)
Civi/Api4/Generic/Traits/DAOActionTrait.php
tests/phpunit/api/v4/Action/CustomContactRefTest.php

index 9e225bc79b9eb27f724edbe51a0e3e6186989cf8..0c5f5ebe8c47bdd42836e0e009c8d5751829129f 100644 (file)
@@ -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) {
index d6b48492d385859be94cfcbcccbf90f0c94522d6..b6ca7513162bdae535e5728638218bc00d3c5837 100644 (file)
@@ -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() {