separate method for email assignment
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / Contribution / ThankYouTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test CRM_Contribute_Form_Contribution_ThankYou
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 class CRM_Contribute_Form_Contribution_ThankYouTest extends CiviUnitTestCase {
36
37 /**
38 * Clean up DB.
39 */
40 public function tearDown() {
41 $this->quickCleanUpFinancialEntities();
42 }
43
44 /**
45 * Test that correct contribution status is fetched for both live and test contributions.
46 */
47 public function testLiveAndTestContributionStatus() {
48 $paymentProcessorID = $this->paymentProcessorCreate(['payment_processor_type_id' => 'Dummy']);
49
50 $form = $this->getThankYouFormWithContribution($paymentProcessorID, FALSE, FALSE);
51 $form->buildQuickForm();
52 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
53
54 $this->assertEquals(FALSE, $isPendingOutcome, 'Outcome should not be pending.');
55
56 $form = $this->getThankYouFormWithContribution($paymentProcessorID, TRUE, FALSE);
57 $form->buildQuickForm();
58 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
59
60 $this->assertEquals(TRUE, $isPendingOutcome, 'Outcome should be pending.');
61
62 $form = $this->getThankYouFormWithContribution($paymentProcessorID, FALSE, TRUE);
63 $form->buildQuickForm();
64 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
65
66 $this->assertEquals(FALSE, $isPendingOutcome, 'Outcome should not be pending.');
67
68 $form = $this->getThankYouFormWithContribution($paymentProcessorID, TRUE, TRUE);
69 $form->buildQuickForm();
70 $isPendingOutcome = $form->get_template_vars('isPendingOutcome');
71
72 $this->assertEquals(TRUE, $isPendingOutcome, 'Outcome should be pending.');
73 }
74
75 /**
76 * Get CRM_Contribute_Form_Contribution_ThankYou form with attached contribution.
77 *
78 * @param $paymentProcessorID
79 * @param bool $withPendingContribution
80 * @param bool $isTestContribution
81 * @return CRM_Contribute_Form_Contribution_ThankYou
82 */
83 private function getThankYouFormWithContribution($paymentProcessorID, $withPendingContribution = FALSE, $isTestContribution = FALSE) {
84 $pageContribution = $this->getPageContribution((($withPendingContribution) ? 2 : 1), $isTestContribution);
85 $form = $this->getThankYouForm();
86 $form->_lineItem = [];
87 $form->_bltID = 5;
88
89 $form->_params['contributionID'] = $pageContribution['contribution_id'];
90 $form->_params['invoiceID'] = $pageContribution['invoice_id'];
91 $form->_params['email-5'] = 'demo@example.com';
92 $form->_params['payment_processor_id'] = $paymentProcessorID;
93 if ($isTestContribution) {
94 $form->_mode = 'test';
95 }
96
97 return $form;
98 }
99
100 /**
101 * Get Contribution and Invoice ID.
102 *
103 * @param $contributionStatus
104 * @param bool $isTest
105 * @return array
106 */
107 private function getPageContribution($contributionStatus, $isTest = FALSE) {
108 $individualId = $this->individualCreate();
109 $invoiceId = rand(100000, 999999);
110
111 $contributionId = $this->contributionCreate([
112 'contact_id' => $individualId,
113 'invoice_id' => $invoiceId,
114 'contribution_status_id' => $contributionStatus,
115 'is_test' => ($isTest) ? 1 : 0,
116 ]);
117
118 return [
119 'contribution_id' => $contributionId,
120 'invoice_id' => $invoiceId,
121 ];
122 }
123
124 /**
125 * Get CRM_Contribute_Form_Contribution_ThankYou Form
126 *
127 * @return CRM_Contribute_Form_Contribution_ThankYou
128 */
129 private function getThankYouForm() {
130 $form = new CRM_Contribute_Form_Contribution_ThankYou();
131 $_SERVER['REQUEST_METHOD'] = 'GET';
132 $form->controller = new CRM_Contribute_Controller_Contribution();
133 return $form;
134 }
135
136 }