Skip to content

Commit

Permalink
SearchKit - Fix updating Entity Ref fields
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Apr 22, 2021
1 parent 31abb95 commit 0d5d17a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Civi/Api4/Generic/Traits/DAOActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ protected function formatCustomParams(&$params, $entityId) {
formatCheckBoxField($value, 'custom_' . $field['id'], $this->getEntityName());
}

if ($field['data_type'] === 'ContactReference' && !is_numeric($value)) {
require_once 'api/v3/utils.php';
$value = \_civicrm_api3_resolve_contactID($value);
if ('unknown-user' === $value) {
throw new \API_Exception("\"{$field['name']}\" \"{$value}\" cannot be resolved to a contact ID", 2002, ['error_field' => $field['name'], "type" => "integer"]);
}
}

\CRM_Core_BAO_CustomField::formatCustomField(
$field['id'],
$customParams,
Expand Down Expand Up @@ -223,7 +231,7 @@ protected function getCustomFieldInfo(string $fieldExpr) {
if (!isset($info[$fieldName])) {
$info = [];
$fields = CustomField::get(FALSE)
->addSelect('id', 'name', 'html_type', 'custom_group.extends')
->addSelect('id', 'name', 'html_type', 'data_type', 'custom_group.extends')
->addWhere('custom_group.name', '=', $groupName)
->execute()->indexBy('name');
foreach ($fields as $name => $field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

crmApi4(model.entity, 'getFields', {
action: 'update',
select: ['name', 'label', 'description', 'data_type', 'serialize', 'options'],
select: ['name', 'label', 'description', 'data_type', 'serialize', 'options', 'fk_entity'],
loadOptions: ['id', 'name', 'label', 'description', 'color', 'icon'],
where: [["readonly", "=", false]],
}).then(function(fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* @group headless
*/
class CustomJoinTest extends BaseCustomValueTest {
class CustomContactRefTest extends BaseCustomValueTest {

public function testGetWithJoin() {

Expand Down Expand Up @@ -70,4 +70,38 @@ public function testGetWithJoin() {
$this->assertEquals('Person', $contact['MyContactRef.FavPerson.last_name']);
}

public function testCurrentUser() {
$currentUser = $this->createLoggedInUser();

$customGroup = CustomGroup::create(FALSE)
->addValue('name', 'MyContactRef')
->addValue('extends', 'Individual')
->execute()
->first();

CustomField::create(FALSE)
->addValue('label', 'FavPerson')
->addValue('custom_group_id', $customGroup['id'])
->addValue('html_type', 'Autocomplete-Select')
->addValue('data_type', 'ContactReference')
->execute();

$contactId = Contact::create(FALSE)
->addValue('first_name', 'Mya')
->addValue('last_name', 'Tester')
->addValue('contact_type', 'Individual')
->addValue('MyContactRef.FavPerson', 'user_contact_id')
->execute()
->first()['id'];

$contact = Contact::get(FALSE)
->addSelect('display_name')
->addSelect('MyContactRef.FavPerson')
->addWhere('id', '=', $contactId)
->execute()
->first();

$this->assertEquals($currentUser, $contact['MyContactRef.FavPerson']);
}

}
23 changes: 23 additions & 0 deletions tests/phpunit/api/v4/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace api\v4;

use api\v4\Traits\TestDataLoaderTrait;
use Civi\Api4\UFMatch;
use Civi\Test\HeadlessInterface;
use Civi\Test\TransactionalInterface;

Expand Down Expand Up @@ -77,6 +78,28 @@ public function getRowCount($table_name) {
return (int) \CRM_Core_DAO::singleValueQuery($sql);
}

/**
* Emulate a logged in user since certain functions use that.
* value to store a record in the DB (like activity)
* @see https://issues.civicrm.org/jira/browse/CRM-8180
*
* @return int
* Contact ID of the created user.
*/
public function createLoggedInUser() {
$contactID = $this->createEntity(['type' => 'Individual'])['id'];
UFMatch::delete(FALSE)->addWhere('uf_id', '=', 6)->execute();
UFMatch::create(FALSE)->setValues([
'contact_id' => $contactID,
'uf_name' => 'superman',
'uf_id' => 6,
])->execute();

$session = \CRM_Core_Session::singleton();
$session->set('userID', $contactID);
return $contactID;
}

/**
* Create sample entities (using V3 for now).
*
Expand Down

0 comments on commit 0d5d17a

Please sign in to comment.