Merge pull request #11138 from mattwire/CRM-21312_bootstrap_css_fix
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / CRM19273Test.php
1 <?php
2 /**
3 * Class CRM_Event_BAO_AdditionalPaymentTest
4 * @group headless
5 */
6 class CRM_Event_BAO_CRM19273 extends CiviUnitTestCase {
7
8 protected $_priceSetID;
9 protected $_cheapFee = 80;
10 protected $_expensiveFee = 100;
11 protected $_veryExpensive = 120;
12
13 /**
14 * @var int
15 */
16 protected $contributionID;
17
18 /**
19 * @var int
20 */
21 protected $participantID;
22
23 /**
24 * Price set field id.
25 *
26 * @var int
27 */
28 protected $priceSetFieldID;
29
30 /**
31 * Set up for test.
32 */
33 public function setUp() {
34 parent::setUp();
35 $this->cleanup();
36 $this->_contactId = $this->individualCreate();
37 $event = $this->eventCreate(array('is_monetary' => 1));
38 $this->_eventId = $event['id'];
39 $this->_priceSetID = $this->eventPriceSetCreate();
40 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $this->_priceSetID);
41 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID, TRUE, FALSE);
42 $priceSet = CRM_Utils_Array::value($this->_priceSetID, $priceSet);
43 $this->_feeBlock = CRM_Utils_Array::value('fields', $priceSet);
44 $this->registerParticipantAndPay();
45 }
46
47 /**
48 * Clean up after test.
49 */
50 public function tearDown() {
51 $this->eventDelete($this->_eventId);
52 $this->quickCleanUpFinancialEntities();
53 }
54
55
56 /**
57 * Remove default price field stuff.
58 *
59 * This is not actually good. However resolving this requires
60 * a lot more fixes & we have a bit of work to do on event tests.
61 *
62 * @throws \Exception
63 */
64 protected function cleanup() {
65 $this->quickCleanup(
66 array(
67 'civicrm_price_field_value',
68 'civicrm_price_field',
69 'civicrm_price_set',
70 )
71 );
72 }
73
74 /**
75 * Create an event with a price set.
76 *
77 * @todo resolve this with parent function.
78 *
79 * @param int $feeTotal
80 * @param int $minAmt
81 * @param string $type
82 *
83 * @return int
84 */
85 protected function eventPriceSetCreate($feeTotal = 55, $minAmt = 0, $type = 'Text') {
86 $paramsSet['title'] = 'Two Options';
87 $paramsSet['name'] = CRM_Utils_String::titleToVar('Two Options');
88 $paramsSet['is_active'] = FALSE;
89 $paramsSet['extends'] = 1;
90
91 $priceSet = CRM_Price_BAO_PriceSet::create($paramsSet);
92
93 $paramsField = array(
94 'label' => 'Price Field',
95 'name' => CRM_Utils_String::titleToVar('Two Options'),
96 'html_type' => 'Radio',
97 //'price' => $feeTotal,
98 'option_label' => array('1' => 'Expensive Room', '2' => "Cheap Room", '3' => 'Very Expensive'),
99 'option_value' => array('1' => 'E', '2' => 'C', '3' => 'V'),
100 'option_name' => array('1' => 'Expensive', '2' => "Cheap", "3" => "Very Expensive"),
101 'option_weight' => array('1' => 1, '2' => 2, '3' => 3),
102 'option_amount' => array('1' => $this->_expensiveFee, '2' => $this->_cheapFee, '3' => $this->_veryExpensive),
103 'is_display_amounts' => 1,
104 'weight' => 1,
105 'options_per_line' => 1,
106 'is_active' => array('1' => 1),
107 'price_set_id' => $priceSet->id,
108 'is_enter_qty' => 1,
109 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
110 );
111 $field = CRM_Price_BAO_PriceField::create($paramsField);
112 $this->priceSetFieldID = $field->id;
113 return $priceSet->id;
114 }
115
116 /**
117 * Get the total for the invoice.
118 *
119 * @param int $contributionId
120 * @return mixed
121 */
122 private function contributionInvoice($contributionId) {
123
124 $query = "
125 SELECT SUM(line_total) total
126 FROM civicrm_line_item
127 WHERE entity_table = 'civicrm_participant'
128 AND entity_id = {$contributionId}";
129 $dao = CRM_Core_DAO::executeQuery($query);
130
131 $this->assertTrue($dao->fetch(), "Succeeded retrieving invoicetotal");
132 return $dao->total;
133 }
134
135 /**
136 * Get the total income from the participant record.
137 *
138 * @param int $participantId
139 *
140 * @return mixed
141 */
142 private function totalIncome($participantId) {
143
144 // @todo use INNER JOINS, this is not our style.
145 $query = "
146 SELECT SUM(et.amount) total
147 FROM civicrm_entity_financial_trxn et
148 , civicrm_financial_item fi
149 , civicrm_line_item li
150 WHERE et.entity_table='civicrm_financial_item'
151 AND fi.id = et.entity_id
152 AND fi.entity_table='civicrm_line_item'
153 AND fi.entity_id = li.id
154 AND li.entity_table = 'civicrm_participant'
155 AND li.entity_id = ${participantId}
156 ";
157 $dao = CRM_Core_DAO::executeQuery($query);
158
159 $this->assertTrue($dao->fetch(), "Succeeded retrieving total Income");
160 return $dao->total;
161 }
162
163 /**
164 * Check the relevant entity balances.
165 *
166 * @param float $amount
167 */
168 private function balanceCheck($amount) {
169 $this->assertEquals($this->contributionInvoice($this->contributionID), $amount, "Invoice must a total of $amount");
170 $this->assertEquals($this->totalIncome($this->participantID), $amount, "The recorded income must be $amount ");
171 $this->assertEquals($this->totalIncome($this->contributionID), $amount, "The accumulated assets must be $amount ");
172 }
173
174 /**
175 * Prepare records for editing.
176 */
177 public function registerParticipantAndPay() {
178 $params = array(
179 'send_receipt' => 1,
180 'is_test' => 0,
181 'is_pay_later' => 0,
182 'event_id' => $this->_eventId,
183 'register_date' => date('Y-m-d') . " 00:00:00",
184 'role_id' => 1,
185 'status_id' => 1,
186 'source' => 'Event_' . $this->_eventId,
187 'contact_id' => $this->_contactId,
188 //'fee_level' => CRM_Core_DAO::VALUE_SEPARATOR.'Expensive Room'.CRM_Core_DAO::VALUE_SEPARATOR,
189 );
190 $participant = $this->callAPISuccess('Participant', 'create', $params);
191 $this->_participantId = $participant['id'];
192
193 $actualPaidAmt = $this->_expensiveFee;
194
195 $contributionParams = array(
196 'total_amount' => $actualPaidAmt,
197 'source' => 'Testset with information',
198 'currency' => 'USD',
199 'non_deductible_amount' => 'null',
200 'receipt_date' => date('Y-m-d') . " 00:00:00",
201 'contact_id' => $this->_contactId,
202 'financial_type_id' => 4,
203 'payment_instrument_id' => 4,
204 'contribution_status_id' => 1,
205 'receive_date' => date('Y-m-d') . " 00:00:00",
206 'skipLineItem' => 1,
207 'partial_payment_total' => $this->_expensiveFee,
208 'partial_amount_to_pay' => $actualPaidAmt,
209 );
210
211 $contribution = CRM_Contribute_BAO_Contribution::create($contributionParams);
212 $this->_contributionId = $contribution->id;
213
214 $this->callAPISuccess('participant_payment', 'create', array(
215 'participant_id' => $this->_participantId,
216 'contribution_id' => $this->_contributionId,
217 ));
218
219 $PSparams['price_1'] = 1; // 1 is the option of the expensive room
220 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
221 CRM_Price_BAO_PriceSet::processAmount($this->_feeBlock, $PSparams, $lineItem);
222 $lineItemVal[$this->_priceSetID] = $lineItem;
223 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $contribution, 'civicrm_participant');
224
225 $this->contributionID = $this->callAPISuccessGetValue('Contribution', array('return' => 'id'));
226 $this->participantID = $this->callAPISuccessGetValue('Participant', array('return' => 'id'));
227 $this->balanceCheck($this->_expensiveFee);
228 }
229
230 public function testCRM19273() {
231 $PSparams['price_1'] = 2;
232 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
233 CRM_Price_BAO_LineItem::changeFeeSelections($PSparams, $this->participantID, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
234 $this->balanceCheck($this->_cheapFee);
235
236 $PSparams['price_1'] = 1;
237 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
238 CRM_Price_BAO_LineItem::changeFeeSelections($PSparams, $this->participantID, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
239 $this->balanceCheck($this->_expensiveFee);
240
241 $PSparams['price_1'] = 3;
242 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
243
244 CRM_Price_BAO_LineItem::changeFeeSelections($PSparams, $this->participantID, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
245 $this->balanceCheck($this->_veryExpensive);
246
247 }
248
249 /**
250 * Test that proper financial items are recorded for cancelled line items
251 */
252 public function testCRM20611() {
253 $PSparams['price_1'] = 1;
254 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
255 CRM_Event_BAO_Participant::changeFeeSelections($PSparams, $this->participantID, $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee, $this->_priceSetID);
256 $this->balanceCheck($this->_expensiveFee);
257
258 $PSparams['price_1'] = 2;
259 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
260 CRM_Event_BAO_Participant::changeFeeSelections($PSparams, $this->participantID, $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee, $this->_priceSetID);
261 $this->balanceCheck($this->_cheapFee);
262
263 //Complete the refund payment.
264 $submittedValues = array(
265 'total_amount' => 120,
266 'payment_instrument_id' => 3,
267 );
268 CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, 'refund', $this->participantID);
269
270 // retrieve the cancelled line-item information
271 $cancelledLineItem = $this->callAPISuccessGetSingle('LineItem', array(
272 'entity_table' => 'civicrm_participant',
273 'entity_id' => $this->participantID,
274 'qty' => 0,
275 ));
276 // retrieve the related financial lin-items
277 $financialItems = $this->callAPISuccess('FinancialItem', 'Get', array(
278 'entity_id' => $cancelledLineItem['id'],
279 'entity_table' => 'civicrm_line_item',
280 ));
281 $this->assertEquals($financialItems['count'], 2, 'Financial Items for Cancelled fee is not proper');
282
283 $contributionCompletedStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
284 $expectedAmount = 100.00;
285 foreach ($financialItems['values'] as $id => $financialItem) {
286 $this->assertEquals($expectedAmount, $financialItem['amount']);
287 $this->assertNotEmpty($financialItem['financial_account_id']);
288 $this->assertEquals($contributionCompletedStatusID, $financialItem['status_id']);
289 $expectedAmount = -$expectedAmount;
290 }
291 }
292
293 }