Always use cancelSubscription form for cancelling recurring contributions
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / CancelSubscriptionTest.php
CommitLineData
e73cf7de
EM
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | Use of this source code is governed by the AGPL license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Class CRM_Contribute_Form_UpdateSubscriptionTest
14 */
9bb5c4bd 15class CRM_Contribute_Form_CancelSubscriptionTest extends CiviUnitTestCase {
16
17 use CRMTraits_Contribute_RecurFormsTrait;
e73cf7de
EM
18
19 /**
20 * Test the mail sent on update.
21 *
22 * @throws \CRM_Core_Exception|\API_Exception
23 */
24 public function testMail(): void {
25 $mut = new CiviMailUtils($this, TRUE);
26 $this->addContribution();
27 /* @var CRM_Contribute_Form_CancelSubscription $form */
28 $form = $this->getFormObject('CRM_Contribute_Form_CancelSubscription', ['is_notify' => TRUE]);
29 $form->set('crid', $this->getContributionRecurID());
30 $form->buildForm();
31 try {
32 $form->postProcess();
33 }
34 catch (CRM_Core_Exception_PrematureExitException $e) {
35 $mut->checkMailLog($this->getExpectedMailStrings());
36 return;
37 }
38 $this->fail('should not be reachable');
39 }
40
41 /**
42 * Get the strings to check for.
43 *
44 * @return string[]
45 */
46 public function getExpectedMailStrings(): array {
47 return [
48 'MIME-Version: 1.0',
49 'From: "Bob" <bob@example.org>',
50 'To: Anthony Anderson <anthony_anderson@civicrm.org>',
51 "Subject: Recurring Contribution Cancellation Notification - Mr. Anthony\n Anderson II",
52 'Return-Path: bob@example.org',
53 'Dear Anthony,',
e589a77b 54 'Your recurring contribution of $10.00, every 1 month has been cancelled as requested',
e73cf7de
EM
55 ];
56 }
57
7403f6a2 58 /**
ca574dc9
EM
59 * Test if the full fledged form is displayed on cancelling the Recurring
60 * Contribution with a payment processor which does not support cancelling a
61 * Recurring Contribution
7403f6a2 62 *
ca574dc9
EM
63 * @throws \CRM_Core_Exception
64 * @throws \CiviCRM_API3_Exception
7403f6a2
KK
65 */
66 public function testCancelSubscriptionForm(): void {
67 $this->addContribution();
68 /* @var CRM_Contribute_Form_CancelSubscription $form */
69 $form = $this->getFormObject('CRM_Contribute_Form_CancelSubscription', ['is_notify' => TRUE]);
70 $form->set('crid', $this->getContributionRecurID());
71 $form->buildForm();
72
73 /* Set the Payment processor to not support 'Cancel Recurring' */
74 $paymentProcessorObj = Civi\Payment\System::singleton()->getById(CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorID($this->getContributionRecurID()));
75 $paymentProcessorObj->setSupports([
76 'CancelRecurring' => FALSE,
77 ]);
78
79 $actions = CRM_Contribute_Page_Tab::recurLinks($this->getContributionRecurID());
7403f6a2 80 // Using "Cancel Recurring" form
ca574dc9 81 $this->assertEquals('civicrm/contribute/unsubscribe', $actions[CRM_Core_Action::DISABLE]['url']);
7403f6a2
KK
82 }
83
e73cf7de 84}