Add test to cancel subscription form
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 23 Sep 2021 21:12:25 +0000 (09:12 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 23 Sep 2021 22:27:27 +0000 (10:27 +1200)
CRM/Contribute/Form/CancelSubscription.php
tests/phpunit/CRM/Contribute/Form/CancelSubscriptionTest.php [new file with mode: 0644]
tests/phpunit/CRM/Contribute/Form/RecurForms.php [new file with mode: 0644]
tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php

index a5d1a36852eeca3e92bb2098dbc37a507ab37fb3..1504cb2db3a3e2f0bdf767c6c3e3bb365bf21706 100644 (file)
@@ -222,7 +222,7 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib
         'id' => $this->getSubscriptionDetails()->recur_id,
         'membership_id' => $this->_mid,
         'processor_message' => $message,
-        'cancel_reason' => $params['cancel_reason'],
+        'cancel_reason' => $this->getSubmittedValue('cancel_reason'),
       ]);
 
       $tplParams = [];
@@ -255,25 +255,6 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib
       }
 
       if (CRM_Utils_Array::value('is_notify', $params) == 1) {
-        if ($this->getSubscriptionDetails()->contribution_page_id) {
-          CRM_Core_DAO::commonRetrieveAll(
-            'CRM_Contribute_DAO_ContributionPage',
-            'id',
-            $this->getSubscriptionDetails()->contribution_page_id,
-            $value,
-            ['title', 'receipt_from_name', 'receipt_from_email']
-          );
-          $receiptFrom
-            = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->getSubscriptionDetails()->contribution_page_id]) .
-            '" <' .
-            $value[$this->getSubscriptionDetails()->contribution_page_id]['receipt_from_email'] .
-            '>';
-        }
-        else {
-          $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
-          $receiptFrom = "$domainValues[0] <$domainValues[1]>";
-        }
-
         // send notification
         $sendTemplateParams
           = [
@@ -283,7 +264,7 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib
             'tplParams' => $tplParams,
             //'isTest'    => $isTest, set this from _objects
             'PDFFilename' => 'receipt.pdf',
-            'from' => $receiptFrom,
+            'from' => CRM_Contribute_BAO_ContributionRecur::getRecurFromAddress($this->getContributionRecurID()),
             'toName' => $this->_donorDisplayName,
             'toEmail' => $this->_donorEmail,
           ];
diff --git a/tests/phpunit/CRM/Contribute/Form/CancelSubscriptionTest.php b/tests/phpunit/CRM/Contribute/Form/CancelSubscriptionTest.php
new file mode 100644 (file)
index 0000000..acdb085
--- /dev/null
@@ -0,0 +1,56 @@
+<?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_CancelSubscriptionTest extends CRM_Contribute_Form_RecurForms {
+
+  /**
+   * Test the mail sent on update.
+   *
+   * @throws \CRM_Core_Exception|\API_Exception
+   */
+  public function testMail(): void {
+    $mut = new CiviMailUtils($this, TRUE);
+    $this->addContribution();
+    /* @var CRM_Contribute_Form_CancelSubscription $form */
+    $form = $this->getFormObject('CRM_Contribute_Form_CancelSubscription', ['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: "Bob" <bob@example.org>',
+      'To: Anthony Anderson <anthony_anderson@civicrm.org>',
+      "Subject: Recurring Contribution Cancellation Notification - Mr. Anthony\n Anderson II",
+      'Return-Path: bob@example.org',
+      'Dear Anthony,',
+      'Your recurring contribution of $ 10.00, every 1 month has been cancelled as requested',
+    ];
+  }
+
+}
diff --git a/tests/phpunit/CRM/Contribute/Form/RecurForms.php b/tests/phpunit/CRM/Contribute/Form/RecurForms.php
new file mode 100644 (file)
index 0000000..991212a
--- /dev/null
@@ -0,0 +1,82 @@
+<?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_RecurForms extends CiviUnitTestCase {
+
+  /**
+   * 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->paymentProcessorId = $this->processorCreate();
+    $this->callAPISuccess('Order', 'create', [
+      'contact_id' => $this->getContactID(),
+      'contribution_recur_id' => $this->getContributionRecurID(),
+      'financial_type_id' => 'Donation',
+      'total_amount' => 10,
+      'contribution_page_id' => $this->getContributionPageID(),
+      'api.Payment.create' => [
+        'total_amount' => 10,
+        'payment_processor_id' => $this->paymentProcessorId,
+        'is_send_contribution_notification' => FALSE,
+      ],
+    ]);
+  }
+
+  /**
+   * 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];
+  }
+
+  /**
+   * Get a contribution page id.
+   *
+   * @return int
+   */
+  public function getContributionPageID(): int {
+    if (!isset($this->ids['ContributionPage'][0])) {
+      $this->ids['ContributionPage'][0] = $this->callAPISuccess('ContributionPage', 'create', [
+        'receipt_from_name' => 'Bob',
+        'receipt_from_email' => 'bob@example.org',
+        'financial_type_id' => 'Donation',
+      ])['id'];
+    }
+    return $this->ids['ContributionPage'][0];
+  }
+
+}
index 9c97d094652b6b675a2a759cac5c0db4f79502b6..d758acedfe470ea72b7be96d0eac7fc74fb5d2fd 100644 (file)
@@ -12,7 +12,7 @@
 /**
  * Class CRM_Contribute_Form_UpdateSubscriptionTest
  */
-class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase {
+class CRM_Contribute_Form_UpdateSubscriptionTest extends CRM_Contribute_Form_RecurForms {
 
   /**
    * Test the mail sent on update.
@@ -55,69 +55,4 @@ class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase {
     ];
   }
 
-  /**
-   * 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->paymentProcessorId = $this->processorCreate();
-    $this->callAPISuccess('Order', 'create', [
-      'contact_id' => $this->getContactID(),
-      'contribution_recur_id' => $this->getContributionRecurID(),
-      'financial_type_id' => 'Donation',
-      'total_amount' => 10,
-      'contribution_page_id' => $this->getContributionPageID(),
-      'api.Payment.create' => [
-        'total_amount' => 10,
-        'payment_processor_id' => $this->paymentProcessorId,
-        'is_send_contribution_notification' => FALSE,
-      ],
-    ]);
-  }
-
-  /**
-   * 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];
-  }
-
-  /**
-   * Get a contribution page id.
-   *
-   * @return int
-   */
-  public function getContributionPageID(): int {
-    if (!isset($this->ids['ContributionPage'][0])) {
-      $this->ids['ContributionPage'][0] = $this->callAPISuccess('ContributionPage', 'create', [
-        'receipt_from_name' => 'Bob',
-        'receipt_from_email' => 'bob@example.org',
-        'financial_type_id' => 'Donation',
-      ])['id'];
-    }
-    return $this->ids['ContributionPage'][0];
-  }
-
 }