Fix self-service recurring link on dashboard
authoreileen <emcnaughton@wikimedia.org>
Mon, 3 May 2021 05:18:12 +0000 (17:18 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 17 May 2021 02:25:24 +0000 (14:25 +1200)
This picks up on two of the issues that were blocking on
https://github.com/civicrm/civicrm-core/pull/18787 and
https://github.com/civicrm/civicrm-core/pull/18196
(but does not solve all of them). It

1) adds the basis for unit test cover and
2) separates the function for retrieving links
for self-service from back office user.

I think there is still some jumbling together of these concepts but
as long as we are careful to extend the tests as we extend the logic...

This fixes what I suspect is an oldish regression whereby the cancel link
for paypal std users was wrong

CRM/Contribute/Page/Tab.php
CRM/Contribute/Page/UserDashboard.php
CRM/Core/Session.php
CRM/Member/Page/RecurringContributions.php
tests/phpunit/CRM/Contribute/Page/TabTest.php

index f0caa917d24fe085d91f271e6351d1398fde73f6..2d75e2096c0c1576c76f712a405e88b88c015931 100644 (file)
@@ -94,6 +94,87 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
     return $links;
   }
 
+  /**
+   * Get the recur links to return for self service.
+   *
+   * These are the links to present to a logged in user wishing
+   * to service their own
+   *
+   * @param int $recurID
+   *
+   * @return array|array[]
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public static function selfServiceRecurLinks(int $recurID): array {
+    $links = [];
+    $paymentProcessorObj = Civi\Payment\System::singleton()->getById(CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorID($recurID));
+    if ($paymentProcessorObj->supports('cancelRecurring')
+      && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel')
+    ) {
+      $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel');
+      $links[CRM_Core_Action::DISABLE] = [
+        'url' => $url,
+        'name' => ts('Cancel'),
+        'title' => ts('Cancel'),
+        // Only display on-site links in a popup.
+        'class' => (stripos($url, 'http') !== FALSE) ? 'no-popup' : '',
+      ];
+    }
+
+    if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')
+      && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing')
+    ) {
+      $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing');
+      $links[CRM_Core_Action::RENEW] = [
+        'name' => ts('Change Billing Details'),
+        'title' => ts('Change Billing Details'),
+        'url' => $url,
+        // Only display on-site links in a popup.
+        'class' => (stripos($url, 'http') !== FALSE) ? 'no-popup' : '',
+      ];
+    }
+
+    if (($paymentProcessorObj->supports('ChangeSubscriptionAmount')
+    || $paymentProcessorObj->supports('EditRecurringContribution'))
+    && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update')
+    ) {
+      $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update');
+      $links[CRM_Core_Action::UPDATE] = [
+        'name' => ts('Edit'),
+        'title' => ts('Edit Recurring Payment'),
+        'url' => $url,
+        // Only display on-site links in a popup.
+        'class' => (stripos($url, 'http') !== FALSE) ? 'no-popup' : '',
+      ];
+    }
+    return $links;
+  }
+
+  /**
+   * Get recurring links appropriate to viewing a user dashboard.
+   *
+   * A contact should be able to see links appropriate to them (e.g
+   * payment processor cancel page) if viewing their own dashboard and
+   * links appropriate to the contact they are viewing, if they have
+   * permission, if viewing another user.
+   *
+   * @param int $recurID
+   * @param int $contactID
+   *
+   * @return array|array[]
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public static function dashboardRecurLinks(int $recurID, int $contactID): array {
+    $links = [];
+    if ($contactID && $contactID === CRM_Core_Session::getLoggedInContactID()) {
+      $links = self::selfServiceRecurLinks($recurID);
+    }
+    $links += self::recurLinks($recurID, 'dashboard');
+    return $links;
+  }
+
   /**
    * called when action is browse.
    *
index cad2bc4aa4680b27f30aaeb5eb21eb970a12ada9..11adcd89e3a21913fb3c0c57630cdecde267071b 100644 (file)
@@ -98,7 +98,7 @@ class CRM_Contribute_Page_UserDashboard extends CRM_Contact_Page_View_UserDashBo
       $values['recur_status'] = $recurStatus[$values['contribution_status_id']];
       $recurRow[$values['id']] = $values;
 
-      $action = array_sum(array_keys(CRM_Contribute_Page_Tab::recurLinks((int) $recur->id, 'dashboard')));
+      $action = array_sum(array_keys(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur->id, (int) $recur->contact_id)));
 
       $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recur->id, 'recur');
       $hideUpdate = $details->membership_id & $details->auto_renew;
@@ -107,7 +107,7 @@ class CRM_Contribute_Page_UserDashboard extends CRM_Contact_Page_View_UserDashBo
         $action -= CRM_Core_Action::UPDATE;
       }
 
-      $recurRow[$values['id']]['action'] = CRM_Core_Action::formLink(CRM_Contribute_Page_Tab::recurLinks((int) $recur->id, 'dashboard'),
+      $recurRow[$values['id']]['action'] = CRM_Core_Action::formLink(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur->id, (int) $this->_contactId),
         $action, [
           'cid' => $this->_contactId,
           'crid' => $values['id'],
index 698b25eb9ddf22b8b0560e35b9657a17f0c4fbaf..dcbe3a7823c04a015877b385711a11156126ef1e 100644 (file)
@@ -563,7 +563,7 @@ class CRM_Core_Session {
     if (!is_numeric($session->get('userID'))) {
       return NULL;
     }
-    return $session->get('userID');
+    return (int) $session->get('userID');
   }
 
   /**
index 3a497b748ab9d3505576a27c690de7c37125084c..44c9c1062b25baeaeafe053d8a479deb286e9ced 100644 (file)
@@ -125,7 +125,7 @@ class CRM_Member_Page_RecurringContributions extends CRM_Core_Page {
       }
 
       $recurringContribution['action'] = CRM_Core_Action::formLink(
-        CRM_Contribute_Page_Tab::recurLinks($recurID, 'contribution'),
+        CRM_Contribute_Page_Tab::recurLinks($recurID),
         $action,
         [
           'cid' => $this->contactID,
index f06faf1f5d393b77980abc4b698a50c4a09534b5..a32f323c43ab2450075982ea4ff43512595897df 100644 (file)
@@ -60,10 +60,12 @@ class CRM_Contribute_Page_TabTest extends CiviUnitTestCase {
 
     $page = new CRM_Contribute_Page_UserDashboard();
     $page->run();
-    $expected = '<span><a href="/index.php?q=civicrm/contact/view/contributionrecur&amp;reset=1&amp;id=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'View Recurring Payment\' >View</a><a href="/index.php?q=civicrm/contribute/updaterecur&amp;reset=1&amp;action=update&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Edit Recurring Payment\' >Edit</a></span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="/index.php?q=civicrm/contribute/unsubscribe&amp;reset=1&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Cancel\' >Cancel</a></li><li><a href="/index.php?q=civicrm/contribute/updatebilling&amp;reset=1&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Change Billing Details\' >Change Billing Details</a></li></ul></span>';
+    $expected = '<span><a href="https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_subscr-find&alias=sunil._1183377782_biz_api1.webaccess.co.in" class="action-item crm-hover-button no-popup" title=\'Cancel\' >Cancel</a>'
+      . '<a href="/index.php?q=civicrm/contact/view/contributionrecur&amp;reset=1&amp;id=1&amp;cid=3&amp;context=dashboard" class="action-item crm-hover-button" title=\'View Recurring Payment\' >View</a>'
+      . '</span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="/index.php?q=civicrm/contribute/updaterecur&amp;reset=1&amp;action=update&amp;crid=1&amp;cid=3&amp;context=dashboard" class="action-item crm-hover-button" title=\'Edit Recurring Payment\' >Edit</a></li><li><a href="/index.php?q=civicrm/contribute/updatebilling&amp;reset=1&amp;crid=1&amp;cid=3&amp;context=dashboard" class="action-item crm-hover-button" title=\'Change Billing Details\' >Change Billing Details</a></li></ul></span>';
     $this->assertEquals(
       $expected,
-      $this->getActionHtml()
+      $this->getDashboardActionHtml()
     );
   }
 
@@ -116,4 +118,14 @@ class CRM_Contribute_Page_TabTest extends CiviUnitTestCase {
       ->get_template_vars()['activeRecurRows'][1]['action'];
   }
 
+  /**
+   * Get the html assigned as actions.
+   *
+   * @return string
+   */
+  protected function getDashboardActionHtml(): string {
+    return CRM_Core_Smarty::singleton()
+      ->get_template_vars()['recurRows'][1]['action'];
+  }
+
 }