From 7d198b2dbc060b889500a81d23d9bd9fee2bd1a3 Mon Sep 17 00:00:00 2001 From: Mathieu Lutfy Date: Wed, 23 Sep 2020 16:07:39 -0400 Subject: [PATCH] dev/core#2061 PCP: call the hook_civicrm_links hook for PCP page user actions --- CRM/PCP/BAO/PCP.php | 17 ++++++---- CRM/PCP/Page/PCPInfo.php | 2 +- tests/phpunit/CRM/PCP/BAO/PCPTest.php | 46 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/CRM/PCP/BAO/PCP.php b/CRM/PCP/BAO/PCP.php index e71609eccd..6fdd36d027 100644 --- a/CRM/PCP/BAO/PCP.php +++ b/CRM/PCP/BAO/PCP.php @@ -88,8 +88,6 @@ WHERE civicrm_pcp.contact_id = civicrm_contact.id * array of Pcp if found */ public static function getPcpDashboardInfo($contactId) { - $links = self::pcpLinks(); - $query = ' SELECT pcp.*, block.is_tellfriend_enabled FROM civicrm_pcp pcp LEFT JOIN civicrm_pcp_block block ON block.id = pcp.pcp_block_id @@ -98,13 +96,13 @@ WHERE pcp.is_active = 1 ORDER BY page_type, page_id'; $params = [1 => [$contactId, 'Integer']]; - $pcpInfoDao = CRM_Core_DAO::executeQuery($query, $params); - $pcpInfo = []; - $hide = $mask = array_sum(array_keys($links['all'])); - $contactPCPPages = []; + $links = self::pcpLinks(); + $hide = $mask = array_sum(array_keys($links['all'])); $approved = CRM_Core_PseudoConstant::getKey('CRM_PCP_BAO_PCP', 'status_id', 'Approved'); + $contactPCPPages = []; + $pcpInfo = []; while ($pcpInfoDao->fetch()) { $mask = $hide; @@ -264,10 +262,13 @@ WHERE pcp.id = %1 AND cc.contribution_status_id = %2 AND cc.is_test = 0"; /** * Get action links. * + * @param int $pcpId + * Contains the pcp ID. Defaults to NULL for backwards compatibility. + * * @return array * (reference) of action links */ - public static function &pcpLinks() { + public static function &pcpLinks($pcpId = NULL) { if (!(self::$_pcpLinks)) { $deleteExtra = ts('Are you sure you want to delete this Personal Campaign Page?') . '\n' . ts('This action cannot be undone.'); @@ -326,6 +327,8 @@ WHERE pcp.id = %1 AND cc.contribution_status_id = %2 AND cc.is_test = 0"; 'title' => ts('Delete'), ], ]; + + CRM_Utils_Hook::links('pcp.user.actions', 'Pcp', $pcpId, self::$_pcpLinks); } return self::$_pcpLinks; } diff --git a/CRM/PCP/Page/PCPInfo.php b/CRM/PCP/Page/PCPInfo.php index 437ef4d968..38140ba235 100644 --- a/CRM/PCP/Page/PCPInfo.php +++ b/CRM/PCP/Page/PCPInfo.php @@ -141,7 +141,7 @@ class CRM_PCP_Page_PCPInfo extends CRM_Core_Page { $this->assign('owner', $owner); - $link = CRM_PCP_BAO_PCP::pcpLinks(); + $link = CRM_PCP_BAO_PCP::pcpLinks($pcpInfo['id']); $hints = [ CRM_Core_Action::UPDATE => ts('Change the content and appearance of your page'), diff --git a/tests/phpunit/CRM/PCP/BAO/PCPTest.php b/tests/phpunit/CRM/PCP/BAO/PCPTest.php index d03f933036..adfe01f2ba 100644 --- a/tests/phpunit/CRM/PCP/BAO/PCPTest.php +++ b/tests/phpunit/CRM/PCP/BAO/PCPTest.php @@ -123,6 +123,52 @@ class CRM_PCP_BAO_PCPTest extends CiviUnitTestCase { ], CRM_PCP_BAO_PCP::getPcpDashboardInfo($contactID)); } + /** + * Test that hook_civicrm_links is called. + */ + public function testPcpInfoLinksHook() { + Civi::dispatcher()->addListener('hook_civicrm_links', [$this, 'hookLinks']); + + // Reset the cache otherwise our hook will not be called + CRM_PCP_BAO_PCP::$_pcpLinks = NULL; + + $block = CRM_PCP_BAO_PCPBlock::create($this->pcpBlockParams()); + $contactID = $this->individualCreate(); + $contributionPage = $this->callAPISuccessGetSingle('ContributionPage', []); + $pcp = $this->callAPISuccess('Pcp', 'create', ['contact_id' => $contactID, 'title' => 'pcp', 'page_id' => $contributionPage['id'], 'pcp_block_id' => $block->id, 'is_active' => TRUE, 'status_id' => 'Approved']); + + $links = CRM_PCP_BAO_PCP::pcplinks($pcp['id']); + + foreach ($links['all'] as $link) { + if ($link['name'] == 'URL for this Page') { + $found = TRUE; + $this->assertEquals($link['url'], 'https://civicrm.org/mih'); + } + } + + $this->assertEquals($found, TRUE); + } + + /** + * This is the listener for hook_civicrm_links + * + * Replaces the "URL for this Page" link by a hardcoded link. + * This is the listener for hook_civicrm_alterReportVar + * + * @param \Civi\Core\Event\GenericHookEvent $e + * Should contain 'op', 'links', and other members corresponding + * to the hook parameters. + */ + public function hookLinks(\Civi\Core\Event\GenericHookEvent $e) { + if ($e->op == 'pcp.user.actions') { + foreach ($e->links['all'] as $key => &$link) { + if ($link['name'] == 'URL for this Page') { + $e->links['all'][$key]['url'] = 'https://civicrm.org/mih'; + } + } + } + } + /** * Test that CRM_Contribute_BAO_Contribution::_gatherMessageValues() works * with PCP. -- 2.25.1