From f1f6baa0f2f0f8624b00795719ecde1b79e9677e Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Wed, 30 Aug 2023 14:57:39 -0400 Subject: [PATCH] fix sort groups on PHP8 --- CRM/Contact/BAO/Group.php | 6 ++++++ tests/phpunit/CRM/Group/Page/AjaxTest.php | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 17bedf4a4d..695522c0ca 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -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')) { diff --git a/tests/phpunit/CRM/Group/Page/AjaxTest.php b/tests/phpunit/CRM/Group/Page/AjaxTest.php index b98b226af8..34438068c7 100644 --- a/tests/phpunit/CRM/Group/Page/AjaxTest.php +++ b/tests/phpunit/CRM/Group/Page/AjaxTest.php @@ -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. * -- 2.25.1