Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-10-28-14-52-15
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / UpdateContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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
28 require_once 'CiviTest/CiviSeleniumTestCase.php';
29 class 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");
108 $this->assertEquals($status, '1', "Verify Completed Status");
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->clickLink("xpath=//a[@id='newfinancialTypeAccount']");
125 $this->select("account_relationship", "label=Premiums Inventory Account is");
126 $this->select("financial_account_id", "label=$to");
127 $this->clickLink("_qf_FinancialTypeAccount_next-botttom");
128
129 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
130 $amount = 500;
131 $sku = 'SKU';
132 $price = 300;
133 $cost = 3.00;
134 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
135 // add premium
136 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
137
138 //add second premium
139 $premiumName2 = 'Premium'.substr(sha1(rand()), 0, 7);
140 $amount2 = 600;
141 $sku2 = 'SKU';
142 $price2 = 200;
143 $cost2 = 2.00;
144 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
145 $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']);
146
147 // add contribution with premium
148 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
149
150 // create new contact using dialog
151 $this->webtestNewDialogContact($firstName, $lastName, $email);
152 // select financial type
153 $this->select( "financial_type_id", "value=1" );
154 // total amount
155 $this->type("total_amount", "100");
156 // fill Premium information
157 $this->clickLink("xpath=//div[@id='Premium']", "product_name_0", FALSE);
158 $this->select('product_name_0', "label=$premiumName ( $sku )");
159 // Clicking save.
160 $this->clickLink("_qf_Contribution_upload");
161 // Is status message correct?
162 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
163 // verify if Membership is created
164 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
165 //click through to the Contribution edit screen
166 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']", "_qf_Contribution_upload-bottom");
167 $contId = $this->urlArg('id');
168 $this->waitForElementPresent("product_name_0");
169 $this->select('product_name_0', "label=$premiumName2 ( $sku2 )");
170 // Clicking save.
171 $this->clickLink("_qf_Contribution_upload");
172
173 //Assertions
174 $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'");
175 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
176
177 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
178 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
179 }
180
181 function testDeletePremium() {
182 $this->webtestLogin();
183 $firstName = substr(sha1(rand()), 0, 7);
184 $lastName = 'Contributor';
185 $email = $firstName . "@example.com";
186 $from = 'Premiums';
187 $to = 'Premiums inventory';
188 $financialType = array(
189 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
190 'is_reserved' => 1,
191 'is_deductible' => 1,
192 );
193 $this->addeditFinancialType($financialType);
194 $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']");
195
196 $this->select("account_relationship", "label=Premiums Inventory Account is");
197 $this->select("financial_account_id", "label=$to");
198 $this->clickLink("_qf_FinancialTypeAccount_next-botttom");
199 $premiumName = 'Premium' . substr(sha1(rand()), 0, 7);
200 $amount = 500;
201 $sku = 'SKU';
202 $price = 300;
203 $cost = 3.00;
204 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
205 // add premium
206 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
207
208 // add contribution with premium
209 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
210
211 // create new contact using dialog
212 $this->webtestNewDialogContact($firstName, $lastName, $email);
213 // select financial type
214 $this->select("financial_type_id", "value=1");
215 // total amount
216 $this->type("total_amount", "100");
217 // fill Premium information
218 $this->click("xpath=//div[@id='Premium']");
219 $this->waitForElementPresent("product_name_0");
220 $this->select('product_name_0', "label=$premiumName ( $sku )");
221 // Clicking save.
222 $this->clickLink("_qf_Contribution_upload");
223 // Is status message correct?
224 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
225 // verify if Membership is created
226 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
227 //click through to the Contribution edit screen
228 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']", "_qf_Contribution_upload-bottom");
229 $contId = $this->urlArg('id');
230 $this->waitForElementPresent("product_name_0");
231 $this->select('product_name_0', "value=0");
232 // Clicking save.
233 $this->clickLink("_qf_Contribution_upload");
234
235 //Assertions
236 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
237 $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium");
238 }
239
240 function testChangePaymentInstrument() {
241 $this->webtestLogin();
242 $firstName = substr(sha1(rand()), 0, 7);
243 $lastName = 'Contributor';
244 $email = $firstName . "@example.com";
245 $label = 'TEST'.substr(sha1(rand()), 0, 7);
246 $amount = 100.00;
247 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
248 $to = array_search('Accounts Receivable', $financialAccount);
249 $from = array_search('Deposit Bank Account', $financialAccount);
250 $this->addPaymentInstrument($label, $to);
251 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
252 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
253 $contId = $this->urlArg('id');
254 //change payment processor to newly created value
255 $this->select("payment_instrument_id", "label=$label");
256 $this->clickLink("_qf_Contribution_upload");
257 //Assertions
258 $totalAmount = $this->_getPremiumActualCost($contId, 'Payment Processor Account', 'Accounts Receivable');
259 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
260 }
261
262 function testRefundContribution() {
263 $this->webtestLogin();
264 $firstName = substr(sha1(rand()), 0, 7);
265 $lastName = 'Contributor';
266 $email = $firstName . "@example.com";
267 $label = 'TEST'.substr(sha1(rand()), 0, 7);
268 $amount = 100.00;
269 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
270 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
271 //Contribution status
272 $this->select("contribution_status_id", "label=Refunded");
273 $contId = $this->urlArg('id');
274 $this->clickLink("_qf_Contribution_upload");
275
276 //Assertions
277 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
278 $search = array('entity_id' => $lineItem);
279 $compare = array(
280 'amount' => '100.00',
281 'status_id' => 1,
282 );
283 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
284 $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'");
285 $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.');
286 }
287
288 function testCancelPayLater() {
289 $this->webtestLogin();
290 $firstName = substr(sha1(rand()), 0, 7);
291 $lastName = 'Contributor';
292 $email = $firstName . "@example.com";
293 $label = 'TEST'.substr(sha1(rand()), 0, 7);
294 $amount = 100.00;
295 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
296 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
297 //Contribution status
298 $this->select("contribution_status_id", "label=Cancelled");
299 $contId = $this->urlArg('id');
300 $this->clickLink("_qf_Contribution_upload");
301
302 //Assertions
303 $search = array('id' => $contId);
304 $compare = array('contribution_status_id' => 3);
305 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
306 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
307 $itemParams = array(
308 'amount' => '-100.00',
309 'entity_id' => $lineItem,
310 );
311 $defaults = array();
312 $items = CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $defaults);
313 $this->assertEquals($items->amount, $itemParams['amount'], 'Verify Amount for financial Item');
314 $totalAmount = $this->_getPremiumActualCost($items->id, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'");
315 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
316 $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'");
317 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
318 }
319
320 function testChangeFinancialType() {
321 $this->webtestLogin();
322 $firstName = substr(sha1(rand()), 0, 7);
323 $lastName = 'Contributor';
324 $email = $firstName . "@example.com";
325 $label = 'TEST'.substr(sha1(rand()), 0, 7);
326 $amount = 100.00;
327 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
328 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
329 //Contribution status
330 $this->select("financial_type_id", "value=3");
331 $contId = $this->urlArg('id');
332 $this->clickLink("_qf_Contribution_upload");
333
334 //Assertions
335 $search = array( 'id' => $contId );
336 $compare = array( 'financial_type_id' => 3 );
337 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
338
339 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
340 $itemParams = array(
341 'amount' => '-100.00',
342 'entity_id' => $lineItem,
343 );
344 $item1 = $item2 = array();
345 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item1);
346 $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item");
347 $itemParams['amount'] = '100.00';
348 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item2);
349 $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item");
350
351 $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'");
352 $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'");
353 $this->assertEquals($cValue1, "-100.00", "Verify Amount");
354 $this->assertEquals($fValue1, "-100.00", "Verify Amount");
355 $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'");
356 $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'");
357 $this->assertEquals($cValue2, "100.00", "Verify Amount");
358 $this->assertEquals($fValue2, "100.00", "Verify Amount");
359 }
360
361 function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
362 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
363 $query = "SELECT
364 {$select}
365 FROM civicrm_financial_trxn ft
366 INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}";
367 if ($entityTable) {
368 $query .= " AND eft.entity_table = {$entityTable}";
369 }
370 if (!empty($to)) {
371 $to = array_search($to, $financialAccount);
372 $query .= " AND ft.to_financial_account_id = {$to}";
373 }
374 if (!empty($from)) {
375 $from = array_search($from, $financialAccount);
376 $query .= " AND ft.from_financial_account_id = {$from}";
377 }
378 if (!empty($cost)) {
379 $query .= " AND eft.amount = {$cost}";
380 }
381 $result = CRM_Core_DAO::singleValueQuery($query);
382 return $result;
383 }
384
385 function _getFinancialTrxnAmount($contId) {
386 $query = "SELECT
387 SUM( ft.total_amount ) AS total
388 FROM civicrm_financial_trxn AS ft
389 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
390 WHERE ceft.entity_table = 'civicrm_contribution'
391 AND ceft.entity_id = {$contId}";
392 $result = CRM_Core_DAO::singleValueQuery($query);
393 return $result;
394 }
395
396 function _getFinancialItemAmount($contId) {
397 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
398 $query = "SELECT
399 SUM(amount)
400 FROM civicrm_financial_item
401 WHERE entity_table = 'civicrm_line_item'
402 AND entity_id = {$lineItem}";
403 $result = CRM_Core_DAO::singleValueQuery($query);
404 return $result;
405 }
406
407 function _getTotalContributedAmount($contId) {
408 $query = "SELECT
409 SUM(amount)
410 FROM civicrm_entity_financial_trxn
411 WHERE entity_table = 'civicrm_contribution'
412 AND entity_id = {$contId}";
413 $result = CRM_Core_DAO::singleValueQuery($query);
414 return $result;
415 }
416
417 function _testOfflineContribution($firstName, $lastName, $email, $amount, $status="Completed") {
418
419 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
420
421 // create new contact using dialog
422 $this->webtestNewDialogContact($firstName, $lastName, $email);
423
424 // select financial type
425 $this->select( "financial_type_id", "value=1" );
426
427 //Contribution status
428 $this->select("contribution_status_id", "label=$status");
429
430 // total amount
431 $this->type("total_amount", $amount);
432
433 // select payment instrument type
434 $this->select("payment_instrument_id", "label=Credit Card");
435
436
437 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
438
439
440 //Custom Data
441 //$this->click('CIVICRM_QFID_3_6');
442
443 // Clicking save.
444 $this->clickLink("_qf_Contribution_upload");
445
446 // Is status message correct?
447 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
448
449 // verify if Membership is created
450 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
451
452 //click through to the Membership view screen
453 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']", "_qf_ContributionView_cancel-bottom");
454
455 $expected = array(
456 'Financial Type' => 'Donation',
457 'Total Amount' => '100.00',
458 'Contribution Status' => $status,
459 );
460 $this->webtestVerifyTabularData($expected);
461 $this->clickLink("_qf_ContributionView_cancel-top");
462 // Because it tends to cause problems, all uses of sleep() must be justified in comments
463 // Sleep should never be used for wait for anything to load from the server
464 // Justification for this instance: FIXME
465 sleep(4);
466 }
467 }
468