Update copyright date for 2020
[civicrm-core.git] / tests / phpunit / CRM / Pledge / BAO / PledgeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 class for CRM_Pledge_BAO_Pledge BAO
30 *
31 * @package CiviCRM
32 * @group headless
33 */
34 class CRM_Pledge_BAO_PledgeTest extends CiviUnitTestCase {
35
36 /**
37 * Sets up the fixture, for example, opens a network connection.
38 * This method is called before a test is executed.
39 */
40 protected function setUp() {
41 parent::setUp();
42 $this->_contactId = $this->individualCreate();
43 $this->_params = [
44 'contact_id' => $this->_contactId,
45 'frequency_unit' => 'month',
46 'original_installment_amount' => 25.00,
47 'frequency_interval' => 1,
48 'frequency_day' => 1,
49 'installments' => 12,
50 'financial_type_id' => 1,
51 'create_date' => '20100513000000',
52 'acknowledge_date' => '20100513000000',
53 'start_date' => '20100513000000',
54 'status_id' => 2,
55 'currency' => 'USD',
56 'amount' => 300,
57 ];
58 }
59
60 /**
61 * Tears down the fixture, for example, closes a network connection.
62 * This method is called after a test is executed.
63 */
64 protected function tearDown() {
65 }
66
67 /**
68 * Test for Add/Update Pledge.
69 */
70 public function testAdd() {
71 //do test for normal add.
72 $pledge = CRM_Pledge_BAO_Pledge::add($this->_params);
73
74 foreach ($this->_params as $param => $value) {
75 $this->assertEquals($value, $pledge->$param);
76 }
77 }
78
79 /**
80 * Test Pledge Payment Status with 1 installment
81 * and not passing status id.
82 */
83 public function testPledgePaymentStatus() {
84 $scheduledDate = date('Ymd', mktime(0, 0, 0, date("m"), date("d") + 2, date("y")));
85 $this->_params['installments'] = 1;
86 $this->_params['scheduled_date'] = $scheduledDate;
87
88 unset($this->_params['status_id']);
89 $pledge = CRM_Pledge_BAO_Pledge::create($this->_params);
90 $pledgePayment = CRM_Pledge_BAO_PledgePayment::getPledgePayments($pledge->id);
91
92 $this->assertEquals(count($pledgePayment), 1);
93 $payment = array_pop($pledgePayment);
94 // Assert that we actually have no pledge Payments
95 $this->assertEquals(0, CRM_Pledge_BAO_Pledge::pledgeHasFinancialTransactions($pledge->id, array_search('Pending', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'))));
96 $this->assertEquals($payment['status'], 'Pending');
97 $this->assertEquals($payment['scheduled_date'], date('Y-m-d 00:00:00', strtotime($scheduledDate)));
98 }
99
100 /**
101 * Retrieve a pledge based on a pledge id = 0
102 */
103 public function testRetrieveZeroPledeID() {
104 $defaults = [];
105 $params = ['pledge_id' => 0];
106 $pledgeId = CRM_Pledge_BAO_Pledge::retrieve($params, $defaults);
107
108 $this->assertEquals(is_null($pledgeId), 1, "Pledge Id must be greater than 0");
109 }
110
111 /**
112 * Retrieve a payment based on a Null pledge id random string.
113 */
114 public function testRetrieveStringPledgeID() {
115 $defaults = [];
116 $params = ['pledge_id' => 'random text'];
117 $pledgeId = CRM_Pledge_BAO_Pledge::retrieve($params, $defaults);
118
119 $this->assertEquals(is_null($pledgeId), 1, "Pledge Id must be a string");
120 }
121
122 /**
123 * Test that payment retrieve wrks based on known pledge id.
124 */
125 public function testRetrieveKnownPledgeID() {
126 $params = [
127 'contact_id' => $this->_contactId,
128 'frequency_unit' => 'month',
129 'frequency_interval' => 1,
130 'frequency_day' => 1,
131 'original_installment_amount' => 25.00,
132 'installments' => 12,
133 'financial_type_id' => 1,
134 'create_date' => '20100513000000',
135 'acknowledge_date' => '20100513000000',
136 'start_date' => '20100513000000',
137 'status_id' => 2,
138 'currency' => 'USD',
139 'amount' => 300,
140 ];
141
142 $pledge = CRM_Pledge_BAO_Pledge::add($params);
143
144 $defaults = [];
145 $pledgeParams = ['pledge_id' => $pledge->id];
146
147 $pledgeId = CRM_Pledge_BAO_Pledge::retrieve($pledgeParams, $defaults);
148
149 $this->assertEquals($pledgeId->N, 1, "Pledge was retrieved");
150 }
151
152 /**
153 * Test build recur params.
154 */
155 public function testGetPledgeStartDate() {
156 $startDate = json_encode(['calendar_month' => 6]);
157
158 $params = [
159 'pledge_start_date' => $startDate,
160 'is_pledge_start_date_editable' => TRUE,
161 'is_pledge_start_date_visible' => TRUE,
162 ];
163
164 // Try with relative date
165 $date = CRM_Pledge_BAO_Pledge::getPledgeStartDate(6, $params);
166 $paymentDate = CRM_Pledge_BAO_Pledge::getPaymentDate(6);
167
168 $this->assertEquals(date('m/d/Y', strtotime($date)), $paymentDate, "The two dates do not match");
169
170 // Try with fixed date
171 $date = NULL;
172 $params = [
173 'pledge_start_date' => json_encode(['calendar_date' => '06/10/2016']),
174 'is_pledge_start_date_visible' => FALSE,
175 ];
176
177 $date = CRM_Pledge_BAO_Pledge::getPledgeStartDate($date, $params);
178 $this->assertEquals(date('m/d/Y', strtotime($date)), '06/10/2016', "The two dates do not match");
179 }
180
181 }