Merge pull request #15314 from jitendrapurohit/dev-1255
[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() {
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
81 return $form;
82 }
83
84 /**
85 * Get Contribution and Invoice ID.
86 *
87 * @param $contributionStatus
88 * @param bool $isTest
89 * @return array
90 */
91 private function getPageContribution($contributionStatus, $isTest = FALSE) {
92 $individualId = $this->individualCreate();
93 $invoiceId = rand(100000, 999999);
94
95 $contributionId = $this->contributionCreate([
96 'contact_id' => $individualId,
97 'invoice_id' => $invoiceId,
98 'contribution_status_id' => $contributionStatus,
99 'is_test' => ($isTest) ? 1 : 0,
100 ]);
101
102 return [
103 'contribution_id' => $contributionId,
104 'invoice_id' => $invoiceId,
105 ];
106 }
107
108 /**
109 * Get CRM_Contribute_Form_Contribution_ThankYou Form
110 *
111 * @return CRM_Contribute_Form_Contribution_ThankYou
112 */
113 private function getThankYouForm() {
114 $form = new CRM_Contribute_Form_Contribution_ThankYou();
115 $_SERVER['REQUEST_METHOD'] = 'GET';
116 $form->controller = new CRM_Contribute_Controller_Contribution();
117 return $form;
118 }
119
120 }