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