Add test for recurring links and clean up method of retrieving recurring
authoreileen <emcnaughton@wikimedia.org>
Sun, 18 Oct 2020 23:14:53 +0000 (12:14 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 19 Oct 2020 05:00:03 +0000 (18:00 +1300)
CRM/Contribute/BAO/ContributionRecur.php
CRM/Contribute/Page/Tab.php
Civi/Payment/System.php
tests/phpunit/CRM/Contribute/Page/TabTest.php [new file with mode: 0644]

index c44cd2f3bcb7a1bfc80e48dd247c6da7439dd8f1..a8f20086aa67ac6b6dac2b5b69e679453b9a7b33 100644 (file)
@@ -163,6 +163,7 @@ class CRM_Contribute_BAO_ContributionRecur extends CRM_Contribute_DAO_Contributi
    *   (since it still makes sense to update / cancel
    */
   public static function getPaymentProcessorObject($id) {
+    CRM_Core_Error::deprecatedFunctionWarning('Use Civi\Payment\System');
     $processor = self::getPaymentProcessor($id);
     return is_array($processor) ? $processor['object'] : NULL;
   }
index fac5a250bc7bf7907cd5913985a540ca8754268a..0c0ebb32442e967ddf93fc12b8e397d6da94c11c 100644 (file)
@@ -69,7 +69,7 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
     ];
 
     if ($recurID) {
-      $paymentProcessorObj = CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorObject($recurID);
+      $paymentProcessorObj = Civi\Payment\System::singleton()->getById(CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorID($recurID));
       if ($paymentProcessorObj) {
         if ($paymentProcessorObj->supports('cancelRecurring')) {
           unset($links[CRM_Core_Action::DISABLE]['extra'], $links[CRM_Core_Action::DISABLE]['ref']);
index fbbfc7d146bcb754a322ab428c3276a0b721d657..358ce7f023eba1360af77e94e9b197924c013074 100644 (file)
@@ -104,16 +104,19 @@ class System {
    *
    * @param int $id
    *
-   * @return \CRM_Core_Payment|NULL
+   * @return \CRM_Core_Payment
    *
-   * @throws \CiviCRM_API3_Exception
+   * @throws \CiviCRM_API3_Exception|\CRM_Core_Exception
    */
   public function getById($id) {
-    if ($id == 0) {
+    if (isset($this->cache[$id])) {
+      return $this->cache[$id];
+    }
+    if ((int) $id === 0) {
       return new \CRM_Core_Payment_Manual();
     }
     $processor = civicrm_api3('payment_processor', 'getsingle', ['id' => $id, 'is_test' => NULL]);
-    return self::getByProcessor($processor);
+    return $this->getByProcessor($processor);
   }
 
   /**
diff --git a/tests/phpunit/CRM/Contribute/Page/TabTest.php b/tests/phpunit/CRM/Contribute/Page/TabTest.php
new file mode 100644 (file)
index 0000000..d2bb725
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | Use of this source code is governed by the AGPL license with some  |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+use Civi\Api4\Contribution;
+use Civi\Api4\ContributionRecur;
+
+/**
+ * Class CRM_Contribute_Page_AjaxTest
+ * @group headless
+ */
+class CRM_Contribute_Page_TabTest extends CiviUnitTestCase {
+
+  /**
+   * Test links render correctly for manual processor.
+   *
+   * @throws \API_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testLinks() {
+    $contactID = $this->individualCreate();
+    $recurID = ContributionRecur::create()->setValues([
+      'contact_id' => $contactID,
+      'amount' => 10,
+      'frequency_interval' => 'week',
+      'start_date' => 'now',
+      'is_active' => TRUE,
+      'contribution_status_id:name' => 'Pending',
+    ])
+      ->addChain(
+        'contribution',
+        Contribution::create()->setValues([
+          'contribution_id' => '$id',
+          'financial_type_id:name' => 'Donation',
+          'total_amount' => 60,
+          'receive_date' => 'now',
+          'contact_id' => $contactID,
+        ])
+      )->execute()->first()['id'];
+    $page = new CRM_Contribute_Page_Tab();
+    $page->_contactId = $contactID;
+    $page->_action = CRM_Core_Action::VIEW;
+    $page->browse();
+
+    $templateVariable = CRM_Core_Smarty::singleton()->get_template_vars();
+    $this->assertEquals('Mr. Anthony Anderson II', $templateVariable['displayName']);
+    $this->assertEquals("<span><a href=\"/index.php?q=civicrm/contact/view/contributionrecur&amp;reset=1&amp;id=" . $recurID . "&amp;cid=" . $contactID . "&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><a href=\"#\" class=\"action-item crm-hover-button crm-enable-disable\" title='Cancel' >Cancel</a></span>",
+      $templateVariable['activeRecurRows'][1]['action']
+    );
+  }
+
+}