From 4fa732d290bf3df8d7cb04591364c2033f6414c8 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 16 Mar 2020 14:41:01 +1300 Subject: [PATCH] Fix PCP getPcpDashboardInfo to be tested & use sensible functions --- CRM/PCP/BAO/PCP.php | 34 ++++++++---- tests/phpunit/CRM/PCP/BAO/PCPTest.php | 56 +++++++++++--------- tests/phpunit/CRMTraits/PCP/PCPTestTrait.php | 8 +-- tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 + 4 files changed, 59 insertions(+), 41 deletions(-) diff --git a/CRM/PCP/BAO/PCP.php b/CRM/PCP/BAO/PCP.php index 6b4660548e..ab87781e60 100644 --- a/CRM/PCP/BAO/PCP.php +++ b/CRM/PCP/BAO/PCP.php @@ -90,12 +90,12 @@ WHERE civicrm_pcp.contact_id = civicrm_contact.id public static function getPcpDashboardInfo($contactId) { $links = self::pcpLinks(); - $query = " + $query = ' SELECT pcp.*, block.is_tellfriend_enabled FROM civicrm_pcp pcp LEFT JOIN civicrm_pcp_block block ON block.id = pcp.pcp_block_id WHERE pcp.is_active = 1 AND pcp.contact_id = %1 -ORDER BY page_type, page_id"; +ORDER BY page_type, page_id'; $params = [1 => [$contactId, 'Integer']]; @@ -104,10 +104,7 @@ ORDER BY page_type, page_id"; $hide = $mask = array_sum(array_keys($links['all'])); $contactPCPPages = []; - $event = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); - $contribute = CRM_Contribute_PseudoConstant::contributionPage(); - $pcpStatus = CRM_Contribute_PseudoConstant::pcpStatus(); - $approved = CRM_Utils_Array::key('Approved', $pcpStatus); + $approved = CRM_Core_PseudoConstant::getKey('CRM_PCP_BAO_PCP', 'status_id', 'Approved'); while ($pcpInfoDao->fetch()) { $mask = $hide; @@ -137,14 +134,14 @@ ORDER BY page_type, page_id"; } $action = CRM_Core_Action::formLink($pcpLink, $mask, $replace, ts('more'), FALSE, 'pcp.dashboard.active', 'PCP', $pcpInfoDao->id); - $component = $pcpInfoDao->page_type; - $pageTitle = CRM_Utils_Array::value($pcpInfoDao->page_id, $$component); + + $pageTitle = self::getPcpTitle($pcpInfoDao->page_type, (int) $pcpInfoDao->page_id); $pcpInfo[] = [ 'pageTitle' => $pageTitle, 'pcpId' => $pcpInfoDao->id, 'pcpTitle' => $pcpInfoDao->title, - 'pcpStatus' => $pcpStatus[$pcpInfoDao->status_id], + 'pcpStatus' => CRM_Core_PseudoConstant::getLabel('CRM_PCP_BAO_PCP', 'status_id', $pcpInfoDao->status_id), 'action' => $action, 'class' => $class, ]; @@ -185,8 +182,8 @@ ORDER BY target_entity_type, target_entity_id $pcpLink = $links['add']; $action = CRM_Core_Action::formLink($pcpLink, $mask, $replace, ts('more'), FALSE, 'pcp.dashboard.other', "{$pcpBlockDao->target_entity_type}_PCP", $pcpBlockDao->target_entity_id); - $component = $pcpBlockDao->target_entity_type; - if ($pageTitle = CRM_Utils_Array::value($pcpBlockDao->target_entity_id, $$component)) { + $pageTitle = self::getPcpTitle($pcpBlockDao->target_entity_type, (int) $pcpBlockDao->target_entity_id); + if ($pageTitle) { $pcpBlock[] = [ 'pageId' => $pcpBlockDao->target_entity_id, 'pageTitle' => $pageTitle, @@ -946,4 +943,19 @@ INNER JOIN civicrm_uf_group ufgroup } } + /** + * Get the title of the pcp. + * + * @param string $component + * @param int $id + * + * @return bool|string|null + */ + protected static function getPcpTitle(string $component, int $id) { + if ($component === 'contribute') { + return CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'contribution_page_id', $id); + } + return CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Participant', 'event_id', $id); + } + } diff --git a/tests/phpunit/CRM/PCP/BAO/PCPTest.php b/tests/phpunit/CRM/PCP/BAO/PCPTest.php index 88ec08277d..fc4feafb08 100644 --- a/tests/phpunit/CRM/PCP/BAO/PCPTest.php +++ b/tests/phpunit/CRM/PCP/BAO/PCPTest.php @@ -20,11 +20,13 @@ class CRM_PCP_BAO_PCPTest extends CiviUnitTestCase { use CRMTraits_PCP_PCPTestTrait; /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. + * Clean up after test. + * + * @throws \CRM_Core_Exception */ - protected function setUp() { - parent::setUp(); + public function tearDown() { + $this->quickCleanUpFinancialEntities(); + parent::tearDown(); } public function testAddPCPBlock() { @@ -41,10 +43,6 @@ class CRM_PCP_BAO_PCPTest extends CiviUnitTestCase { $this->assertEquals($params['tellfriend_limit'], $pcpBlock->tellfriend_limit, 'Check for tell friend limit .'); $this->assertEquals($params['link_text'], $pcpBlock->link_text, 'Check for link text.'); $this->assertEquals($params['is_active'], $pcpBlock->is_active, 'Check for is_active.'); - // Delete our test object - $delParams = ['id' => $pcpBlock->id]; - // FIXME: Currently this delete fails with an FK constraint error: DELETE FROM civicrm_contribution_type WHERE ( civicrm_contribution_type.id = 5 ) - // CRM_Core_DAO::deleteTestObjects( 'CRM_PCP_DAO_PCPBlock', $delParams ); } public function testAddPCP() { @@ -67,16 +65,11 @@ class CRM_PCP_BAO_PCPTest extends CiviUnitTestCase { $this->assertEquals($params['is_honor_roll'], $pcp->is_honor_roll, 'Check for is_honor_roll.'); $this->assertEquals($params['goal_amount'], $pcp->goal_amount, 'Check for goal_amount.'); $this->assertEquals($params['is_active'], $pcp->is_active, 'Check for is_active.'); - - // Delete our test object - $delParams = ['id' => $pcp->id]; - // FIXME: Currently this delete fails with an FK constraint error: DELETE FROM civicrm_contribution_type WHERE ( civicrm_contribution_type.id = 5 ) - // CRM_Core_DAO::deleteTestObjects( 'CRM_PCP_DAO_PCP', $delParams ); } public function testAddPCPNoStatus() { $blockParams = $this->pcpBlockParams(); - $pcpBlock = CRM_PCP_BAO_PCPBlock::create($blockParams, TRUE); + $pcpBlock = CRM_PCP_BAO_PCPBlock::create($blockParams); $params = $this->pcpParams(); $params['pcp_block_id'] = $pcpBlock->id; @@ -95,28 +88,39 @@ class CRM_PCP_BAO_PCPTest extends CiviUnitTestCase { $this->assertEquals($params['is_honor_roll'], $pcp->is_honor_roll, 'Check for is_honor_roll.'); $this->assertEquals($params['goal_amount'], $pcp->goal_amount, 'Check for goal_amount.'); $this->assertEquals($params['is_active'], $pcp->is_active, 'Check for is_active.'); - - // Delete our test object - $delParams = ['id' => $pcp->id]; - // FIXME: Currently this delete fails with an FK constraint error: DELETE FROM civicrm_contribution_type WHERE ( civicrm_contribution_type.id = 5 ) - // CRM_Core_DAO::deleteTestObjects( 'CRM_PCP_DAO_PCP', $delParams ); } public function testDeletePCP() { $pcp = CRM_Core_DAO::createTestObject('CRM_PCP_DAO_PCP'); $pcpId = $pcp->id; - $del = CRM_PCP_BAO_PCP::deleteById($pcpId); - $this->assertDBRowNotExist('CRM_PCP_DAO_PCP', $pcpId, - 'Database check PCP deleted successfully.' - ); + CRM_PCP_BAO_PCP::deleteById($pcpId); + $this->assertDBRowNotExist('CRM_PCP_DAO_PCP', $pcpId, 'Database check PCP deleted successfully.'); } /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. + * Get getPCPDashboard info function. + * + * @throws \CRM_Core_Exception */ - protected function tearDown() { + public function testGetPcpDashboardInfo() { + $block = CRM_PCP_BAO_PCPBlock::create($this->pcpBlockParams()); + $contactID = $this->individualCreate(); + $contributionPage = $this->callAPISuccessGetSingle('ContributionPage', []); + $this->callAPISuccess('Pcp', 'create', ['contact_id' => $contactID, 'title' => 'pcp', 'page_id' => $contributionPage['id'], 'pcp_block_id' => $block->id, 'is_active' => TRUE, 'status_id' => 'Approved']); + $this->assertEquals([ + [], + [ + [ + 'pageTitle' => $contributionPage['title'], + 'action' => 'Edit Your PageTell Friendsmore', + 'pcpId' => 1, + 'pcpTitle' => 'pcp', + 'pcpStatus' => 'Approved', + 'class' => '', + ], + ], + ], CRM_PCP_BAO_PCP::getPcpDashboardInfo($contactID)); } } diff --git a/tests/phpunit/CRMTraits/PCP/PCPTestTrait.php b/tests/phpunit/CRMTraits/PCP/PCPTestTrait.php index 81ec98e813..f6d4113e99 100644 --- a/tests/phpunit/CRMTraits/PCP/PCPTestTrait.php +++ b/tests/phpunit/CRMTraits/PCP/PCPTestTrait.php @@ -29,7 +29,7 @@ trait CRMTraits_PCP_PCPTestTrait { $supporterProfile = CRM_Core_DAO::createTestObject('CRM_Core_DAO_UFGroup'); $supporterProfileId = $supporterProfile->id; - $params = array( + $params = [ 'entity_table' => 'civicrm_contribution_page', 'entity_id' => $contribPageId, 'supporter_profile_id' => $supporterProfileId, @@ -39,7 +39,7 @@ trait CRMTraits_PCP_PCPTestTrait { 'tellfriend_limit' => 1, 'link_text' => 'Create your own PCP', 'is_active' => 1, - ); + ]; return $params; } @@ -56,7 +56,7 @@ trait CRMTraits_PCP_PCPTestTrait { $contribPage = CRM_Core_DAO::createTestObject('CRM_Contribute_DAO_ContributionPage'); $contribPageId = $contribPage->id; - $params = array( + $params = [ 'contact_id' => $contactId, 'status_id' => '1', 'title' => 'My PCP', @@ -68,7 +68,7 @@ trait CRMTraits_PCP_PCPTestTrait { 'is_honor_roll' => 1, 'goal_amount' => 10000.00, 'is_active' => 1, - ); + ]; return $params; } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 173c06e6e1..1a0dfb0841 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1841,6 +1841,8 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) 'civicrm_participant', 'civicrm_participant_payment', 'civicrm_pledge', + 'civicrm_pcp_block', + 'civicrm_pcp', 'civicrm_pledge_block', 'civicrm_pledge_payment', 'civicrm_price_set_entity', -- 2.25.1