fix sort groups on PHP8
authorJon Goldberg <jon@megaphonetech.com>
Wed, 30 Aug 2023 18:57:39 +0000 (14:57 -0400)
committerJon Goldberg <jon@megaphonetech.com>
Wed, 30 Aug 2023 19:17:38 +0000 (15:17 -0400)
CRM/Contact/BAO/Group.php
tests/phpunit/CRM/Group/Page/AjaxTest.php

index 17bedf4a4d6821e1ca6ed5e690a8177658ca892b..695522c0cab6176b08ad1cd48be4148d95549917 100644 (file)
@@ -1025,6 +1025,12 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     // CRM-16905 - Sort by count cannot be done with sql
     if (!empty($params['sort']) && strpos($params['sort'], 'count') === 0) {
       usort($values, function($a, $b) {
+        if ($a['count'] === 'unknown') {
+          return -1;
+        }
+        if ($b['count'] === 'unknown') {
+          return 1;
+        }
         return $a['count'] - $b['count'];
       });
       if (strpos($params['sort'], 'desc')) {
index b98b226af8477bca11468cfdad65314cf340a240..34438068c748ecae1115b1fe7d78d34da5188b1a 100644 (file)
@@ -530,6 +530,15 @@ class CRM_Group_Page_AjaxTest extends CiviUnitTestCase {
     $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
   }
 
+  /**
+   * Test sorting, including with smart group.
+   */
+  public function testSmartGroupSort(): void {
+    $this->smartGroupCreate();
+    $this->_params['sort'] = 'count asc';
+    CRM_Contact_BAO_Group::getGroupList($this->_params);
+  }
+
   /**
    * Don't populate smart group cache when building Group list.
    *