From 995f30cc810475bba6b926b52116fd80c77d3c38 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 18 Jun 2019 12:14:58 +1000 Subject: [PATCH] dev/core#1052 fix fatal error when first target contact is not visible to the logged in user and update test appropriately --- CRM/Activity/BAO/Activity.php | 39 ++++++++++--------- .../phpunit/CRM/Activity/BAO/ActivityTest.php | 8 +++- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 56f12e5288..fedf0b6d1b 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -751,8 +751,8 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { GROUP BY activity_id', [ 1 => [ CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'), - 'Integer' - ] + 'Integer', + ], ])->fetchAll(); } foreach ($targetCount as $activityTarget) { @@ -2566,23 +2566,26 @@ INNER JOIN civicrm_option_group grp ON (grp.id = option_group_id AND grp.name = $firstTargetName = reset($values['target_contact_name']); $firstTargetContactID = key($values['target_contact_name']); - $targetLink = CRM_Utils_System::href($firstTargetName, 'civicrm/contact/view', "reset=1&cid={$firstTargetContactID}"); - if ($showContactOverlay) { - $targetTypeImage = CRM_Contact_BAO_Contact_Utils::getImage( - CRM_Contact_BAO_Contact::getContactType($firstTargetContactID), - FALSE, - $firstTargetContactID); - $activity['target_contact_name'] .= "
$targetTypeImage $targetLink"; - } - else { - $activity['target_contact_name'] .= $targetLink; - } + // The first target may not be accessable to the logged in user dev/core#1052 + if ($firstTargetName) { + $targetLink = CRM_Utils_System::href($firstTargetName, 'civicrm/contact/view', "reset=1&cid={$firstTargetContactID}"); + if ($showContactOverlay) { + $targetTypeImage = CRM_Contact_BAO_Contact_Utils::getImage( + CRM_Contact_BAO_Contact::getContactType($firstTargetContactID), + FALSE, + $firstTargetContactID); + $activity['target_contact_name'] .= "
$targetTypeImage $targetLink"; + } + else { + $activity['target_contact_name'] .= $targetLink; + } - if ($extraCount = $values['target_contact_count'] - 1) { - $activity['target_contact_name'] .= ";
" . "(" . ts('%1 more', [1 => $extraCount]) . ")"; - } - if ($showContactOverlay) { - $activity['target_contact_name'] .= "
"; + if ($extraCount = $values['target_contact_count'] - 1) { + $activity['target_contact_name'] .= ";
" . "(" . ts('%1 more', [1 => $extraCount]) . ")"; + } + if ($showContactOverlay) { + $activity['target_contact_name'] .= "
"; + } } } elseif (!$values['target_contact_name']) { diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index 56a4f4327c..3f082bbc34 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -188,8 +188,12 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { $activityGetParams = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); $activityGetParams += ['contact_id' => $contactId]; $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($activityGetParams); - $this->assertEquals(1, $activites[1]['target_contact_count']); - $this->assertEquals([], $activites[1]['target_contact_name']); + // Aseert that we have sensible data to display in the contact tab + $this->assertEquals('Anderson, Anthony', $activities['data'][0]['source_contact_name']); + // Note that becasue there is a target contact but it is not accessable the output is an empty string not n/a + $this->assertEquals('', $activities['data'][0]['target_contact_name']); + // verify that doing the underlying query shows we get a target contact_id + $this->assertEquals(1, CRM_Activity_BAO_Activity::getActivities(['contact_id' => $contactId])[1]['target_contact_count']); $config = CRM_Core_Config::singleton(); $config->userPermissionClass->permissions = array(); } -- 2.25.1