Test tearDown fixes
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / Contribution / ThankYouTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 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 * Test CRM_Contribute_Form_Contribution_ThankYou
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
17 * @group headless
18 */
19 class CRM_Contribute_Form_Contribution_ThankYouTest extends CiviUnitTestCase {
20
21 /**
22 * Clean up DB.
23 *
24 * @throws \CRM_Core_Exception|\API_Exception
25 */
26 public function tearDown(): void {
27 $this->quickCleanUpFinancialEntities();
28 parent::tearDown();
29 }
30
31 /**
32 * Test that correct contribution status is fetched for both live and test contributions.
33 */
34 public function testLiveAndTestContributionStatus() {
35 $paymentProcessorID = $this->paymentProcessorCreate(['payment_processor_type_id' => 'Dummy']);
36
37 $form = $this->getThankYouFormWithContribution($paymentProcessorID, FALSE, FALSE);
38 $form->buildQuickForm();
39 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
40
41 $this->assertEquals(FALSE, $isPendingOutcome, 'Outcome should not be pending.');
42
43 $form = $this->getThankYouFormWithContribution($paymentProcessorID, TRUE, FALSE);
44 $form->buildQuickForm();
45 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
46
47 $this->assertEquals(TRUE, $isPendingOutcome, 'Outcome should be pending.');
48
49 $form = $this->getThankYouFormWithContribution($paymentProcessorID, FALSE, TRUE);
50 $form->buildQuickForm();
51 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
52
53 $this->assertEquals(FALSE, $isPendingOutcome, 'Outcome should not be pending.');
54
55 $form = $this->getThankYouFormWithContribution($paymentProcessorID, TRUE, TRUE);
56 $form->buildQuickForm();
57 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
58
59 $this->assertEquals(TRUE, $isPendingOutcome, 'Outcome should be pending.');
60 }
61
62 /**
63 * Get CRM_Contribute_Form_Contribution_ThankYou form with attached contribution.
64 *
65 * @param $paymentProcessorID
66 * @param bool $withPendingContribution
67 * @param bool $isTestContribution
68 * @return CRM_Contribute_Form_Contribution_ThankYou
69 */
70 private function getThankYouFormWithContribution($paymentProcessorID, $withPendingContribution = FALSE, $isTestContribution = FALSE) {
71 $pageContribution = $this->getPageContribution((($withPendingContribution) ? 2 : 1), $isTestContribution);
72 $form = $this->getThankYouForm();
73 $form->_lineItem = [];
74 $form->_bltID = 5;
75
76 $form->_params['contributionID'] = $pageContribution['contribution_id'];
77 $form->_params['invoiceID'] = $pageContribution['invoice_id'];
78 $form->_params['email-5'] = 'demo@example.com';
79 $form->_params['payment_processor_id'] = $paymentProcessorID;
80 if ($isTestContribution) {
81 $form->_mode = 'test';
82 }
83 $form->_values = [
84 'custom_pre_id' => NULL,
85 'custom_post_id' => NULL,
86 ];
87
88 return $form;
89 }
90
91 /**
92 * Get Contribution and Invoice ID.
93 *
94 * @param $contributionStatus
95 * @param bool $isTest
96 * @return array
97 */
98 private function getPageContribution($contributionStatus, $isTest = FALSE) {
99 $individualId = $this->individualCreate();
100 $invoiceId = rand(100000, 999999);
101
102 $contributionId = $this->contributionCreate([
103 'contact_id' => $individualId,
104 'invoice_id' => $invoiceId,
105 'contribution_status_id' => $contributionStatus,
106 'is_test' => ($isTest) ? 1 : 0,
107 ]);
108
109 return [
110 'contribution_id' => $contributionId,
111 'invoice_id' => $invoiceId,
112 ];
113 }
114
115 /**
116 * Get CRM_Contribute_Form_Contribution_ThankYou Form
117 *
118 * @return CRM_Contribute_Form_Contribution_ThankYou
119 */
120 private function getThankYouForm() {
121 $form = new CRM_Contribute_Form_Contribution_ThankYou();
122 $_SERVER['REQUEST_METHOD'] = 'GET';
123 $form->controller = new CRM_Contribute_Controller_Contribution();
124 return $form;
125 }
126
127 }