Add test to UpdateSubscription form
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 27 Aug 2021 07:31:39 +0000 (19:31 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 29 Aug 2021 21:04:49 +0000 (09:04 +1200)
CRM/Contribute/Form/UpdateSubscription.php
tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php [new file with mode: 0644]

index 31e16f76cff9b524edb164c2161d50b24736cf70..135c1c18ac3926e1ccbf7e0bb61b11f09ede6dd6 100644 (file)
@@ -333,7 +333,7 @@ class CRM_Contribute_Form_UpdateSubscription extends CRM_Contribute_Form_Contrib
         CRM_Utils_System::setUFMessage($status);
       }
       // keep result as 1, since we not displaying anything on the redirected page anyway
-      return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus',
+      CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus',
         "reset=1&task=update&result=1"));
     }
   }
diff --git a/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php b/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php
new file mode 100644 (file)
index 0000000..d32b6de
--- /dev/null
@@ -0,0 +1,101 @@
+<?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       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Contribute_Form_UpdateSubscriptionTest
+ */
+class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase {
+
+  /**
+   * Test the mail sent on update.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testMail(): void {
+    $mut = new CiviMailUtils($this, TRUE);
+    $this->addContribution();
+    /* @var CRM_Contribute_Form_UpdateSubscription $form */
+    $form = $this->getFormObject('CRM_Contribute_Form_UpdateSubscription', ['is_notify' => TRUE]);
+    $form->set('crid', $this->getContributionRecurID());
+    $form->buildForm();
+    try {
+      $form->postProcess();
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $mut->checkMailLog($this->getExpectedMailStrings());
+      return;
+    }
+    $this->fail('should not be reachable');
+  }
+
+  /**
+   * Get the strings to check for.
+   *
+   * @return string[]
+   */
+  public function getExpectedMailStrings(): array {
+    return [
+      'MIME-Version: 1.0',
+      'From: FIXME <info@EXAMPLE.ORG>',
+      'To: Anthony Anderson <anthony_anderson@civicrm.org>',
+      'Subject: Recurring Contribution Update Notification - Mr. Anthony Anderson II',
+      'Return-Path: info@EXAMPLE.ORG',
+      'Dear Anthony,',
+      'Your recurring contribution has been updated as requested:',
+      'Recurring contribution is for $ 10.00, every 1 month(s) for 12 installments.',
+      'If you have questions please contact us at FIXME <info@EXAMPLE.ORG>.',
+    ];
+  }
+
+  /**
+   * Get contact id.
+   *
+   *  return int
+   */
+  public function getContactID(): int {
+    if (!isset($this->ids['Contact'][0])) {
+      $this->ids['Contact'][0] = $this->individualCreate();
+    }
+    return $this->ids['Contact'][0];
+  }
+
+  /**
+   *
+   */
+  public function addContribution(): void {
+    $this->callAPISuccess('Order', 'create', [
+      'contact_id' => $this->getContactID(),
+      'contribution_recur_id' => $this->getContributionRecurID(),
+      'financial_type_id' => 'Donation',
+      'total_amount' => 10,
+      'api.Payment.create' => ['total_amount' => 10],
+    ]);
+  }
+
+  /**
+   * Get contribution recur ID.
+   *
+   * return int
+   */
+  public function getContributionRecurID(): int {
+    if (!isset($this->ids['ContributionRecur'][0])) {
+      $this->ids['ContributionRecur'][0] = $this->callAPISuccess('ContributionRecur', 'create', [
+        'contact_id' => $this->getContactID(),
+        'amount' => 10,
+        'installments' => 12,
+        'frequency_interval' => 1,
+        'frequency_unit' => 'month',
+      ])['id'];
+    }
+    return $this->ids['ContributionRecur'][0];
+  }
+
+}