From 480124fa4932266e774da61c2e540b9e639ae31e Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Tue, 22 Nov 2022 13:00:32 -0500 Subject: [PATCH] Don't crash API4 if pseudoconstant lookups return nothing --- Civi/Api4/Query/Api4SelectQuery.php | 4 ++++ tests/phpunit/api/v4/Action/ContactGetTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index 284afe6bbd..57252aaa65 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -633,6 +633,10 @@ class Api4SelectQuery { return sprintf('%s %s "%s"', $fieldAlias, $operator, \CRM_Core_DAO::escapeString($value)); } + if (!$value && ($operator === 'IN' || $operator === 'NOT IN')) { + $value[] = FALSE; + } + if (is_bool($value)) { $value = (int) $value; } diff --git a/tests/phpunit/api/v4/Action/ContactGetTest.php b/tests/phpunit/api/v4/Action/ContactGetTest.php index e2ada7b656..03258bb792 100644 --- a/tests/phpunit/api/v4/Action/ContactGetTest.php +++ b/tests/phpunit/api/v4/Action/ContactGetTest.php @@ -424,4 +424,17 @@ class ContactGetTest extends Api4TestBase implements TransactionalInterface { $this->assertCount(0, $result); } + public function testInvalidPseudoConstantWithIN(): void { + $this->createTestRecord('Contact', [ + 'first_name' => uniqid(), + 'last_name' => uniqid(), + 'prefix_id:name' => 'Ms.', + ]); + $resultCount = Contact::get(FALSE) + ->addSelect('id') + ->addWhere('prefix_id:name', 'IN', ['Msssss.']) + ->execute(); + $this->assertCount(0, $resultCount); + } + } -- 2.25.1