Merge pull request #21561 from civicrm/5.42
[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 public function tearDown(): void {
25 $this->quickCleanUpFinancialEntities();
26 parent::tearDown();
27 }
28
29 /**
30 * Test that correct contribution status is fetched for both live and test contributions.
31 */
32 public function testLiveAndTestContributionStatus(): void {
33 $paymentProcessorID = $this->paymentProcessorCreate(['payment_processor_type_id' => 'Dummy']);
34
35 $form = $this->getThankYouFormWithContribution($paymentProcessorID, FALSE, FALSE);
36 $form->buildQuickForm();
37 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
38
39 $this->assertEquals(FALSE, $isPendingOutcome, 'Outcome should not be pending.');
40
41 $form = $this->getThankYouFormWithContribution($paymentProcessorID, TRUE, FALSE);
42 $form->buildQuickForm();
43 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
44
45 $this->assertEquals(TRUE, $isPendingOutcome, 'Outcome should be pending.');
46
47 $form = $this->getThankYouFormWithContribution($paymentProcessorID, FALSE, TRUE);
48 $form->buildQuickForm();
49 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
50
51 $this->assertEquals(FALSE, $isPendingOutcome, 'Outcome should not be pending.');
52
53 $form = $this->getThankYouFormWithContribution($paymentProcessorID, TRUE, TRUE);
54 $form->buildQuickForm();
55 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
56
57 $this->assertEquals(TRUE, $isPendingOutcome, 'Outcome should be pending.');
58 }
59
60 /**
61 * Get CRM_Contribute_Form_Contribution_ThankYou form with attached contribution.
62 *
63 * @param $paymentProcessorID
64 * @param bool $withPendingContribution
65 * @param bool $isTestContribution
66 * @return CRM_Contribute_Form_Contribution_ThankYou
67 */
68 private function getThankYouFormWithContribution($paymentProcessorID, $withPendingContribution = FALSE, $isTestContribution = FALSE) {
69 $pageContribution = $this->getPageContribution((($withPendingContribution) ? 2 : 1), $isTestContribution);
70 $form = $this->getThankYouForm();
71 $form->_lineItem = [];
72 $form->_bltID = 5;
73
74 $form->_params['contributionID'] = $pageContribution['contribution_id'];
75 $form->_params['invoiceID'] = $pageContribution['invoice_id'];
76 $form->_params['email-5'] = 'demo@example.com';
77 $form->_params['payment_processor_id'] = $paymentProcessorID;
78 if ($isTestContribution) {
79 $form->_mode = 'test';
80 }
81 $form->_values = [
82 'custom_pre_id' => NULL,
83 'custom_post_id' => NULL,
84 ];
85
86 return $form;
87 }
88
89 /**
90 * Get Contribution and Invoice ID.
91 *
92 * @param $contributionStatus
93 * @param bool $isTest
94 * @return array
95 */
96 private function getPageContribution($contributionStatus, $isTest = FALSE) {
97 $individualId = $this->individualCreate();
98 $invoiceId = rand(100000, 999999);
99
100 $contributionId = $this->contributionCreate([
101 'contact_id' => $individualId,
102 'invoice_id' => $invoiceId,
103 'contribution_status_id' => $contributionStatus,
104 'is_test' => ($isTest) ? 1 : 0,
105 ]);
106
107 return [
108 'contribution_id' => $contributionId,
109 'invoice_id' => $invoiceId,
110 ];
111 }
112
113 /**
114 * Get CRM_Contribute_Form_Contribution_ThankYou Form
115 *
116 * @return CRM_Contribute_Form_Contribution_ThankYou
117 */
118 private function getThankYouForm() {
119 $form = new CRM_Contribute_Form_Contribution_ThankYou();
120 $_SERVER['REQUEST_METHOD'] = 'GET';
121 $form->controller = new CRM_Contribute_Controller_Contribution();
122 return $form;
123 }
124
125 }