Fix PCP getPcpDashboardInfo to be tested & use sensible functions
authoreileen <emcnaughton@wikimedia.org>
Mon, 16 Mar 2020 01:41:01 +0000 (14:41 +1300)
committereileen <emcnaughton@wikimedia.org>
Tue, 17 Mar 2020 01:29:03 +0000 (14:29 +1300)
CRM/PCP/BAO/PCP.php
tests/phpunit/CRM/PCP/BAO/PCPTest.php
tests/phpunit/CRMTraits/PCP/PCPTestTrait.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 6b4660548e3e3619ee2433c70c8327519bebb753..ab87781e6036deca2c35f82e77550a0292337e16 100644 (file)
@@ -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);
+  }
+
 }
index 88ec08277d8b5bfa02e9c9244e71ebf648a62cfd..fc4feafb0899959b1f39292b76a97fdba6427ebd 100644 (file)
@@ -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' => '<span><a href="/index.php?q=civicrm/pcp/info&amp;action=update&amp;reset=1&amp;id=' . $contributionPage['id'] . '&amp;component=contribute" class="action-item crm-hover-button" title=\'Configure\' >Edit Your Page</a><a href="/index.php?q=civicrm/friend&amp;eid=1&amp;blockId=1&amp;reset=1&amp;pcomponent=pcp&amp;component=contribute" class="action-item crm-hover-button" title=\'Tell Friends\' >Tell Friends</a></span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="/index.php?q=civicrm/pcp/info&amp;reset=1&amp;id=1&amp;component=contribute" class="action-item crm-hover-button" title=\'URL for this Page\' >URL for this Page</a></li><li><a href="/index.php?q=civicrm/pcp/info&amp;action=browse&amp;reset=1&amp;id=1&amp;component=contribute" class="action-item crm-hover-button" title=\'Update Contact Information\' >Update Contact Information</a></li><li><a href="/index.php?q=civicrm/pcp&amp;action=disable&amp;reset=1&amp;id=1&amp;component=contribute" class="action-item crm-hover-button" title=\'Disable\' >Disable</a></li><li><a href="/index.php?q=civicrm/pcp&amp;action=delete&amp;reset=1&amp;id=1&amp;component=contribute" class="action-item crm-hover-button small-popup" title=\'Delete\' onclick = "return confirm(\'Are you sure you want to delete this Personal Campaign Page?\nThis action cannot be undone.\');">Delete</a></li></ul></span>',
+          'pcpId' => 1,
+          'pcpTitle' => 'pcp',
+          'pcpStatus' => 'Approved',
+          'class' => '',
+        ],
+      ],
+    ], CRM_PCP_BAO_PCP::getPcpDashboardInfo($contactID));
   }
 
 }
index 81ec98e81306d827b2ffd46d528a92c5d6df3e8d..f6d4113e9924a13a3fd2109f29ed895de72bdfbb 100644 (file)
@@ -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;
   }
index 173c06e6e1ebf239ecc31515661961246b5db6ec..1a0dfb0841c13681c90bc5c95b91b68ba9a217f3 100644 (file)
@@ -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',