Merge pull request #801 from totten/migrate-tweaks
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / UpdateContributionTest.php
CommitLineData
855be98e
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25*/
26
27
28require_once 'CiviTest/CiviSeleniumTestCase.php';
29class WebTest_Contribute_UpdateContributionTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testChangeContributionAmount() {
36 $this->webtestLogin();
37 $firstName = substr(sha1(rand()), 0, 7);
38 $lastName = 'Contributor';
39 $email = $firstName . "@example.com";
40 $amount = 100;
41 //Offline Pay Later Contribution
42 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
43
44 $this->openCiviPage("contribute/search", "reset=1", "contribution_date_low");
45
46 $this->type("sort_name", "$lastName, $firstName");
47 $this->clickLink("_qf_Search_refresh");
48
49 $contriIDOff = explode('&', $this->getAttribute("xpath=//div[@id='contributionSearch']/table/tbody/tr[1]/td[11]/span/a@href"));
50 if (!empty($contriIDOff)) {
51 $contriIDOff = substr($contriIDOff[1], (strrpos($contriIDOff[1], '=') + 1));
52 }
53
54 $this->clickLink("xpath=//tr[@id='rowid{$contriIDOff}']/td[11]/span/a[2]", "total_amount");
55 $this->type("total_amount", "90");
56 $this->clickLink('_qf_Contribution_upload');
57
58 // Is status message correct?
59 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
60
61 //For Contribution
62 $searchParams = array('id' => $contriIDOff);
63 $compareParams = array('total_amount' => '90.00');
64 //For LineItem
65 $lineItemSearchParams = array('entity_id' => $contriIDOff);
66 $lineItemCompareParams = array('line_total' => '90.00');
67
68 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams);
69 $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams);
70
71 $total = $this->_getTotalContributedAmount($contriIDOff);
72 $compare = array('total_amount' => $total);
73 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
74
75 $amount = $this->_getFinancialItemAmount($contriIDOff);
76 $compare = array('total_amount' => $amount);
77 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
78
79 $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff);
80 $compare = array('total_amount' => $financialTrxnAmount);
81 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
82 }
83
84 function testPayLater() {
85 $this->webtestLogin();
86 $firstName = substr(sha1(rand()), 0, 7);
87 $lastName = 'Contributor';
88 $email = $firstName . "@example.com";
89 $amount = 100.00;
90 //Offline Pay Later Contribution
91 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
92 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
93 $contId = $this->urlArg('id');
94 $this->select("contribution_status_id", "label=Completed");
95 $this->clickLink("_qf_Contribution_upload");
96
97 //Assertions
98 $search = array('id' => $contId);
99 $compare = array('contribution_status_id' => 1);
100 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
101
102 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
103 $search = array( 'entity_id' => $lineItem );
104 $compare = array( 'status_id' => 1 );
105 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
106
107 $status = $this->_getPremiumActualCost($contId, 'Accounts Receivable', 'Payment Processor Account', NULL, "'civicrm_contribution'", "ft.status_id as status");
76e86fd8 108 $this->assertEquals($status, '1', "Verify Completed Status");
855be98e
PJ
109 }
110
111 function testChangePremium() {
112 $this->webtestLogin();
113 $firstName = substr(sha1(rand()), 0, 7);
114 $lastName = 'Contributor';
115 $email = $firstName . "@example.com";
116 $from = 'Premiums';
117 $to = 'Premiums inventory';
118 $financialType = array(
119 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
120 'is_reserved' => 1,
121 'is_deductible' => 1,
122 );
123 $this->addeditFinancialType($financialType);
124 $this->select("account_relationship", "label=Cost of Sales Account is");
125 $this->select("financial_account_id", "label=$from");
126 $this->clickLink("_qf_FinancialTypeAccount_next_new-botttom");
127 $this->select("account_relationship", "label=Premiums Inventory Account is");
128 $this->select("financial_account_id", "label=$to");
129 $this->clickLink("_qf_FinancialTypeAccount_next-botttom");
130 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
131 $amount = 500;
132 $sku = 'SKU';
133 $price = 300;
134 $cost = 3.00;
135 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
136 // add premium
137 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
138
139 //add second premium
140 $premiumName2 = 'Premium'.substr(sha1(rand()), 0, 7);
141 $amount2 = 600;
142 $sku2 = 'SKU';
143 $price2 = 200;
144 $cost2 = 2.00;
145 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
146 $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']);
147
148 // add contribution with premium
149 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
150
151 // create new contact using dialog
152 $this->webtestNewDialogContact($firstName, $lastName, $email);
153 // select financial type
154 $this->select( "financial_type_id", "value=1" );
155 // total amount
156 $this->type("total_amount", "100");
157 // fill Premium information
158 $this->clickLink("xpath=//div[@id='Premium']", "product_name_0", FALSE);
159 $this->select('product_name_0', "label=$premiumName ( $sku )");
160 // Clicking save.
161 $this->clickLink("_qf_Contribution_upload");
162 // Is status message correct?
163 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
164 // verify if Membership is created
165 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
166 //click through to the Contribution edit screen
167 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']", "_qf_Contribution_upload-bottom");
168 $contId = $this->urlArg('id');
169 $this->waitForElementPresent("product_name_0");
170 $this->select('product_name_0', "label=$premiumName2 ( $sku2 )");
171 // Clicking save.
172 $this->clickLink("_qf_Contribution_upload");
173
174 //Assertions
175 $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'");
176 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
177
178 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
179 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
180 }
181
182 function testDeletePremium() {
183 $this->webtestLogin();
184 $firstName = substr(sha1(rand()), 0, 7);
185 $lastName = 'Contributor';
186 $email = $firstName . "@example.com";
187 $from = 'Premiums';
188 $to = 'Premiums inventory';
189 $financialType = array(
190 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
191 'is_reserved' => 1,
192 'is_deductible' => 1,
193 );
194 $this->addeditFinancialType($financialType);
195 $this->select("account_relationship", "label=Cost of Sales Account is");
196 $this->select("financial_account_id", "label=$from");
197 $this->clickLink("_qf_FinancialTypeAccount_next_new-botttom");
198 $this->select("account_relationship", "label=Premiums Inventory Account is");
199 $this->select("financial_account_id", "label=$to");
377afa39 200 $this->clickLink("_qf_FinancialTypeAccount_next-botttom");
855be98e
PJ
201 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
202 $amount = 500;
203 $sku = 'SKU';
204 $price = 300;
205 $cost = 3.00;
206 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
207 // add premium
208 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
209
210 // add contribution with premium
211 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
212
213 // create new contact using dialog
214 $this->webtestNewDialogContact($firstName, $lastName, $email);
215 // select financial type
216 $this->select("financial_type_id", "value=1");
217 // total amount
218 $this->type("total_amount", "100");
219 // fill Premium information
220 $this->click("xpath=//div[@id='Premium']");
221 $this->waitForElementPresent("product_name_0");
222 $this->select('product_name_0', "label=$premiumName ( $sku )");
223 // Clicking save.
224 $this->clickLink("_qf_Contribution_upload");
225 // Is status message correct?
226 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
227 // verify if Membership is created
228 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
229 //click through to the Contribution edit screen
230 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']", "_qf_Contribution_upload-bottom");
231 $contId = $this->urlArg('id');
232 $this->waitForElementPresent("product_name_0");
233 $this->select('product_name_0', "value=0");
234 // Clicking save.
235 $this->clickLink("_qf_Contribution_upload");
236
237 //Assertions
238 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
239 $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium");
240 }
241
242 function testChangePaymentInstrument() {
243 $this->webtestLogin();
244 $firstName = substr(sha1(rand()), 0, 7);
245 $lastName = 'Contributor';
246 $email = $firstName . "@example.com";
247 $label = 'TEST'.substr(sha1(rand()), 0, 7);
248 $amount = 100.00;
249 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
250 $to = array_search('Accounts Receivable', $financialAccount);
251 $from = array_search('Deposit Bank Account', $financialAccount);
252 $this->addPaymentInstrument($label, $to);
253 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
254 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
255 $contId = $this->urlArg('id');
256 //change payment processor to newly created value
257 $this->select("payment_instrument_id", "label=$label");
258 $this->clickLink("_qf_Contribution_upload");
259 //Assertions
260 $totalAmount = $this->_getPremiumActualCost($contId, 'Payment Processor Account', 'Accounts Receivable');
261 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
262 }
263
264 function testRefundContribution() {
265 $this->webtestLogin();
266 $firstName = substr(sha1(rand()), 0, 7);
267 $lastName = 'Contributor';
268 $email = $firstName . "@example.com";
269 $label = 'TEST'.substr(sha1(rand()), 0, 7);
270 $amount = 100.00;
271 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
272 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
273 //Contribution status
274 $this->select("contribution_status_id", "label=Refunded");
275 $contId = $this->urlArg('id');
276 $this->clickLink("_qf_Contribution_upload");
277
278 //Assertions
279 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
280 $search = array('entity_id' => $lineItem);
281 $compare = array(
282 'amount' => '100.00',
283 'status_id' => 1,
284 );
285 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
286 $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'");
287 $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.');
288 }
289
290 function testCancelPayLater() {
291 $this->webtestLogin();
292 $firstName = substr(sha1(rand()), 0, 7);
293 $lastName = 'Contributor';
294 $email = $firstName . "@example.com";
295 $label = 'TEST'.substr(sha1(rand()), 0, 7);
296 $amount = 100.00;
297 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
298 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
299 //Contribution status
300 $this->select("contribution_status_id", "label=Cancelled");
301 $contId = $this->urlArg('id');
302 $this->clickLink("_qf_Contribution_upload");
303
304 //Assertions
305 $search = array('id' => $contId);
306 $compare = array('contribution_status_id' => 3);
307 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
308 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
309 $itemParams = array(
310 'amount' => '-100.00',
311 'entity_id' => $lineItem,
312 );
313 $defaults = array();
314 $items = CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $defaults);
315 $this->assertEquals($items->amount, $itemParams['amount'], 'Verify Amount for financial Item');
316 $totalAmount = $this->_getPremiumActualCost($items->id, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'");
317 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
318 $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'");
319 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
320 }
321
322 function testChangeFinancialType() {
323 $this->webtestLogin();
324 $firstName = substr(sha1(rand()), 0, 7);
325 $lastName = 'Contributor';
326 $email = $firstName . "@example.com";
327 $label = 'TEST'.substr(sha1(rand()), 0, 7);
328 $amount = 100.00;
329 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
330 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
331 //Contribution status
332 $this->select("financial_type_id", "value=3");
333 $contId = $this->urlArg('id');
334 $this->clickLink("_qf_Contribution_upload");
335
336 //Assertions
337 $search = array( 'id' => $contId );
338 $compare = array( 'financial_type_id' => 3 );
339 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
340
341 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
342 $itemParams = array(
343 'amount' => '-100.00',
344 'entity_id' => $lineItem,
345 );
346 $item1 = $item2 = array();
347 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item1);
348 $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item");
349 $itemParams['amount'] = '100.00';
350 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item2);
351 $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item");
352
353 $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'");
354 $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'");
355 $this->assertEquals($cValue1, "-100.00", "Verify Amount");
356 $this->assertEquals($fValue1, "-100.00", "Verify Amount");
357 $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'");
358 $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'");
359 $this->assertEquals($cValue2, "100.00", "Verify Amount");
360 $this->assertEquals($fValue2, "100.00", "Verify Amount");
361 }
362
363 function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
364 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
365 $query = "SELECT
366 {$select}
367 FROM civicrm_financial_trxn ft
368 INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}";
369 if ($entityTable) {
370 $query .= " AND eft.entity_table = {$entityTable}";
371 }
372 if (!empty($to)) {
373 $to = array_search($to, $financialAccount);
374 $query .= " AND ft.to_financial_account_id = {$to}";
375 }
376 if (!empty($from)) {
377 $from = array_search($from, $financialAccount);
378 $query .= " AND ft.from_financial_account_id = {$from}";
379 }
380 if (!empty($cost)) {
381 $query .= " AND eft.amount = {$cost}";
382 }
383 $result = CRM_Core_DAO::singleValueQuery($query);
384 return $result;
385 }
386
387 function _getFinancialTrxnAmount($contId) {
388 $query = "SELECT
389 SUM( ft.total_amount ) AS total
390 FROM civicrm_financial_trxn AS ft
391 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
392 WHERE ceft.entity_table = 'civicrm_contribution'
393 AND ceft.entity_id = {$contId}";
394 $result = CRM_Core_DAO::singleValueQuery($query);
395 return $result;
396 }
397
398 function _getFinancialItemAmount($contId) {
399 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
400 $query = "SELECT
401 SUM(amount)
402 FROM civicrm_financial_item
403 WHERE entity_table = 'civicrm_line_item'
404 AND entity_id = {$lineItem}";
405 $result = CRM_Core_DAO::singleValueQuery($query);
406 return $result;
407 }
408
409 function _getTotalContributedAmount($contId) {
410 $query = "SELECT
411 SUM(amount)
412 FROM civicrm_entity_financial_trxn
413 WHERE entity_table = 'civicrm_contribution'
414 AND entity_id = {$contId}";
415 $result = CRM_Core_DAO::singleValueQuery($query);
416 return $result;
417 }
418
42daf119 419 function _testOfflineContribution($firstName, $lastName, $email, $amount, $status="Completed") {
855be98e 420
42daf119 421 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
855be98e
PJ
422
423 // create new contact using dialog
424 $this->webtestNewDialogContact($firstName, $lastName, $email);
425
426 // select financial type
427 $this->select( "financial_type_id", "value=1" );
428
429 //Contribution status
430 $this->select("contribution_status_id", "label=$status");
431
432 // total amount
433 $this->type("total_amount", $amount);
434
435 // select payment instrument type
436 $this->select("payment_instrument_id", "label=Credit Card");
437
438
439 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
440
441
442 //Custom Data
443 //$this->click('CIVICRM_QFID_3_6');
444
445 // Clicking save.
446 $this->clickLink("_qf_Contribution_upload");
447
448 // Is status message correct?
449 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
450
451 // verify if Membership is created
452 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
453
454 //click through to the Membership view screen
455 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']", "_qf_ContributionView_cancel-bottom");
456
457 $expected = array(
458 'Financial Type' => 'Donation',
459 'Total Amount' => '100.00',
460 'Contribution Status' => $status,
461 );
462 $this->webtestVerifyTabularData($expected);
463 $this->clickLink("_qf_ContributionView_cancel-top");
efb29358
CW
464 // Because it tends to cause problems, all uses of sleep() must be justified in comments
465 // Sleep should never be used for wait for anything to load from the server
466 // Justification for this instance: FIXME
855be98e
PJ
467 sleep(4);
468 }
469}
470