don't crash for nonexistent uf_id
authordemeritcowboy <demeritcowboy@hotmail.com>
Wed, 22 Sep 2021 16:39:32 +0000 (12:39 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Wed, 22 Sep 2021 18:38:53 +0000 (14:38 -0400)
CRM/Core/BAO/UFMatch.php
tests/phpunit/CRM/Core/BAO/UFMatchTest.php [new file with mode: 0644]

index 9d0a58bbac35ac79ab05c4c95ee07f6a5bf721de..cc007958a1997564d0f6b534589e2f60a520113b 100644 (file)
@@ -624,13 +624,12 @@ AND    domain_id    = %4
       return [];
     }
 
-    static $ufValues;
-    if ($ufID && !isset($ufValues[$ufID])) {
+    if (!isset(Civi::$statics[__CLASS__][__FUNCTION__][$ufID])) {
       $ufmatch = new CRM_Core_DAO_UFMatch();
       $ufmatch->uf_id = $ufID;
       $ufmatch->domain_id = CRM_Core_Config::domainID();
       if ($ufmatch->find(TRUE)) {
-        $ufValues[$ufID] = [
+        Civi::$statics[__CLASS__][__FUNCTION__][$ufID] = [
           'uf_id' => $ufmatch->uf_id,
           'uf_name' => $ufmatch->uf_name,
           'contact_id' => $ufmatch->contact_id,
@@ -638,7 +637,7 @@ AND    domain_id    = %4
         ];
       }
     }
-    return $ufValues[$ufID];
+    return Civi::$statics[__CLASS__][__FUNCTION__][$ufID] ?? NULL;
   }
 
   /**
diff --git a/tests/phpunit/CRM/Core/BAO/UFMatchTest.php b/tests/phpunit/CRM/Core/BAO/UFMatchTest.php
new file mode 100644 (file)
index 0000000..e9916b8
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+/**
+ * Class CRM_Core_BAO_UFMatchTest
+ * @group headless
+ */
+class CRM_Core_BAO_UFMatchTest extends CiviUnitTestCase {
+
+  /**
+   * Don't crash if the uf_id doesn't exist
+   */
+  public function testGetUFValuesWithNonexistentUFId() {
+    $max_id = (int) CRM_Core_DAO::singleValueQuery('SELECT MAX(uf_id) FROM civicrm_uf_match');
+    $dontcrash = CRM_Core_BAO_UFMatch::getUFValues($max_id + 1);
+    $this->assertNull($dontcrash);
+  }
+
+}