From e616e67d373f3cd36ba774395d40134ff873ac88 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Fri, 3 Jan 2020 00:59:10 -0500 Subject: [PATCH] fix blank group title in recent items list when editing description --- CRM/Contact/BAO/Group.php | 15 ++++++++ tests/phpunit/CRM/Contact/BAO/GroupTest.php | 38 +++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 12a024f367..479109c07c 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -334,6 +334,21 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { CRM_Utils_Hook::pre('create', 'Group', NULL, $params); } + // If title isn't specified, retrieve it because we use it later, e.g. + // for RecentItems. But note we use array_key_exists not isset or empty + // since otherwise there would be no way to blank out an existing title. + // I'm not sure what the use-case is for that, but you're allowed to do it + // currently. + if (!empty($params['id']) && !array_key_exists('title', $params)) { + try { + $groupTitle = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $params['id'], 'title', 'id'); + $params['title'] = $groupTitle; + } + catch (CRM_Core_Exception $groupTitleException) { + // don't set title + } + } + // dev/core#287 Disable child groups if all parents are disabled. if (!empty($params['id'])) { $allChildGroupIds = self::getChildGroupIds($params['id']); diff --git a/tests/phpunit/CRM/Contact/BAO/GroupTest.php b/tests/phpunit/CRM/Contact/BAO/GroupTest.php index e14f655612..3ab280ae71 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupTest.php @@ -259,4 +259,42 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { $this->assertEquals(1, $recipients['count'], 'Check recipient count'); } + /** + * Test updating a group with just description and check the recent items + * list has the right title. + */ + public function testGroupUpdateDescription() { + // Create a group. Copied from $this->testAddSimple(). + // Note we need $checkParams because the function call changes $params. + $checkParams = $params = [ + 'title' => 'Group Uno', + 'description' => 'Group One', + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + ]; + $group = CRM_Contact_BAO_Group::create($params); + + // Update the group with just id and description. + $newParams = [ + 'id' => $group->id, + 'description' => 'The first group', + ]; + CRM_Contact_BAO_Group::create($newParams); + + // Check it against original array, except description. + $result = $this->callAPISuccess('Group', 'getsingle', ['id' => $group->id]); + foreach ($checkParams as $key => $value) { + if ($key === 'description') { + $this->assertEquals($newParams[$key], $result[$key], "$key doesn't match"); + } + else { + $this->assertEquals($checkParams[$key], $result[$key], "$key doesn't match"); + } + } + + // Check recent items list. + $recentItems = CRM_Utils_Recent::get(); + $this->assertEquals($checkParams['title'], $recentItems[0]['title']); + } + } -- 2.25.1