Merge pull request #3113 from deepak-srivastava/CRM-14431
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / UpdateContributionTest.php
CommitLineData
855be98e
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
855be98e 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
855be98e
PJ
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");
d17bd806 47 $this->click("_qf_Search_refresh");
855be98e 48
d17bd806 49 $this->waitForElementPresent("xpath=//div[@class='crm-content-block']//div[@id='contributionSearch']");
50 $contriIDOff = explode('&', $this->getAttribute("xpath=//div[@id='contributionSearch']//table[@class='selector row-highlight']/tbody/tr[1]/td[11]/span/a[1]@href"));
855be98e
PJ
51 if (!empty($contriIDOff)) {
52 $contriIDOff = substr($contriIDOff[1], (strrpos($contriIDOff[1], '=') + 1));
53 }
54
d17bd806 55 $this->clickLink("xpath=//tr[@id='rowid{$contriIDOff}']/td[11]/span/a[2]", "total_amount", FALSE);
855be98e
PJ
56 $this->type("total_amount", "90");
57 $this->clickLink('_qf_Contribution_upload');
58
59 // Is status message correct?
60 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
61
62 //For Contribution
63 $searchParams = array('id' => $contriIDOff);
64 $compareParams = array('total_amount' => '90.00');
65 //For LineItem
66 $lineItemSearchParams = array('entity_id' => $contriIDOff);
67 $lineItemCompareParams = array('line_total' => '90.00');
68
69 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams);
70 $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams);
71
72 $total = $this->_getTotalContributedAmount($contriIDOff);
73 $compare = array('total_amount' => $total);
74 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
75
76 $amount = $this->_getFinancialItemAmount($contriIDOff);
77 $compare = array('total_amount' => $amount);
78 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
79
80 $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff);
81 $compare = array('total_amount' => $financialTrxnAmount);
82 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
83 }
84
85 function testPayLater() {
86 $this->webtestLogin();
87 $firstName = substr(sha1(rand()), 0, 7);
88 $lastName = 'Contributor';
89 $email = $firstName . "@example.com";
90 $amount = 100.00;
91 //Offline Pay Later Contribution
92 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
d17bd806 93 $this->waitForElementPresent("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
94 $this->clickLink("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
855be98e
PJ
95 $contId = $this->urlArg('id');
96 $this->select("contribution_status_id", "label=Completed");
97 $this->clickLink("_qf_Contribution_upload");
98
99 //Assertions
100 $search = array('id' => $contId);
101 $compare = array('contribution_status_id' => 1);
102 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
103
104 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
105 $search = array( 'entity_id' => $lineItem );
106 $compare = array( 'status_id' => 1 );
107 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
108
109 $status = $this->_getPremiumActualCost($contId, 'Accounts Receivable', 'Payment Processor Account', NULL, "'civicrm_contribution'", "ft.status_id as status");
76e86fd8 110 $this->assertEquals($status, '1', "Verify Completed Status");
855be98e
PJ
111 }
112
113 function testChangePremium() {
114 $this->webtestLogin();
115 $firstName = substr(sha1(rand()), 0, 7);
116 $lastName = 'Contributor';
117 $email = $firstName . "@example.com";
118 $from = 'Premiums';
119 $to = 'Premiums inventory';
120 $financialType = array(
121 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
122 'is_reserved' => 1,
123 'is_deductible' => 1,
124 );
125 $this->addeditFinancialType($financialType);
f17d75bb 126 $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']");
855be98e
PJ
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");
f17d75bb 130
855be98e
PJ
131 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
132 $amount = 500;
133 $sku = 'SKU';
134 $price = 300;
135 $cost = 3.00;
136 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
137 // add premium
138 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
139
140 //add second premium
141 $premiumName2 = 'Premium'.substr(sha1(rand()), 0, 7);
142 $amount2 = 600;
143 $sku2 = 'SKU';
144 $price2 = 200;
145 $cost2 = 2.00;
146 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
147 $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']);
148
149 // add contribution with premium
150 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
151
152 // create new contact using dialog
153 $this->webtestNewDialogContact($firstName, $lastName, $email);
154 // select financial type
155 $this->select( "financial_type_id", "value=1" );
156 // total amount
157 $this->type("total_amount", "100");
158 // fill Premium information
159 $this->clickLink("xpath=//div[@id='Premium']", "product_name_0", FALSE);
160 $this->select('product_name_0', "label=$premiumName ( $sku )");
161 // Clicking save.
162 $this->clickLink("_qf_Contribution_upload");
163 // Is status message correct?
164 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
165 // verify if Membership is created
d17bd806 166 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e 167 //click through to the Contribution edit screen
d17bd806 168 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", "_qf_Contribution_upload-bottom");
855be98e
PJ
169 $contId = $this->urlArg('id');
170 $this->waitForElementPresent("product_name_0");
171 $this->select('product_name_0', "label=$premiumName2 ( $sku2 )");
172 // Clicking save.
173 $this->clickLink("_qf_Contribution_upload");
174
175 //Assertions
176 $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'");
177 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
178
179 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
180 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
181 }
182
183 function testDeletePremium() {
184 $this->webtestLogin();
185 $firstName = substr(sha1(rand()), 0, 7);
186 $lastName = 'Contributor';
187 $email = $firstName . "@example.com";
188 $from = 'Premiums';
189 $to = 'Premiums inventory';
190 $financialType = array(
f17d75bb 191 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
855be98e
PJ
192 'is_reserved' => 1,
193 'is_deductible' => 1,
194 );
195 $this->addeditFinancialType($financialType);
f17d75bb
PN
196 $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']");
197
855be98e
PJ
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");
f17d75bb 201 $premiumName = 'Premium' . substr(sha1(rand()), 0, 7);
855be98e
PJ
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
d17bd806 228 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e 229 //click through to the Contribution edit screen
d17bd806 230 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", "_qf_Contribution_upload-bottom");
855be98e
PJ
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);
d17bd806 254 $this->waitForElementPresent("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
255 $this->clickLink("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
855be98e
PJ
256 $contId = $this->urlArg('id');
257 //change payment processor to newly created value
258 $this->select("payment_instrument_id", "label=$label");
259 $this->clickLink("_qf_Contribution_upload");
260 //Assertions
261 $totalAmount = $this->_getPremiumActualCost($contId, 'Payment Processor Account', 'Accounts Receivable');
262 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
263 }
264
265 function testRefundContribution() {
266 $this->webtestLogin();
267 $firstName = substr(sha1(rand()), 0, 7);
268 $lastName = 'Contributor';
269 $email = $firstName . "@example.com";
270 $label = 'TEST'.substr(sha1(rand()), 0, 7);
271 $amount = 100.00;
272 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
d17bd806 273 $this->waitForElementPresent("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
274 $this->clickLink("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
855be98e
PJ
275 //Contribution status
276 $this->select("contribution_status_id", "label=Refunded");
277 $contId = $this->urlArg('id');
278 $this->clickLink("_qf_Contribution_upload");
279
280 //Assertions
281 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
282 $search = array('entity_id' => $lineItem);
283 $compare = array(
284 'amount' => '100.00',
285 'status_id' => 1,
286 );
287 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
288 $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'");
289 $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.');
290 }
291
292 function testCancelPayLater() {
293 $this->webtestLogin();
294 $firstName = substr(sha1(rand()), 0, 7);
295 $lastName = 'Contributor';
296 $email = $firstName . "@example.com";
297 $label = 'TEST'.substr(sha1(rand()), 0, 7);
298 $amount = 100.00;
299 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
d17bd806 300 $this->waitForElementPresent("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
301 $this->clickLink("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
855be98e
PJ
302 //Contribution status
303 $this->select("contribution_status_id", "label=Cancelled");
304 $contId = $this->urlArg('id');
305 $this->clickLink("_qf_Contribution_upload");
306
307 //Assertions
308 $search = array('id' => $contId);
309 $compare = array('contribution_status_id' => 3);
310 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
311 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
312 $itemParams = array(
313 'amount' => '-100.00',
314 'entity_id' => $lineItem,
315 );
316 $defaults = array();
317 $items = CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $defaults);
318 $this->assertEquals($items->amount, $itemParams['amount'], 'Verify Amount for financial Item');
319 $totalAmount = $this->_getPremiumActualCost($items->id, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'");
320 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
321 $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'");
322 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
323 }
324
325 function testChangeFinancialType() {
326 $this->webtestLogin();
327 $firstName = substr(sha1(rand()), 0, 7);
328 $lastName = 'Contributor';
329 $email = $firstName . "@example.com";
330 $label = 'TEST'.substr(sha1(rand()), 0, 7);
331 $amount = 100.00;
332 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
d17bd806 333 $this->waitForElementPresent("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
334 $this->clickLink("xpath=//div[@id='contributionSearch']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
855be98e
PJ
335 //Contribution status
336 $this->select("financial_type_id", "value=3");
337 $contId = $this->urlArg('id');
338 $this->clickLink("_qf_Contribution_upload");
339
340 //Assertions
341 $search = array( 'id' => $contId );
342 $compare = array( 'financial_type_id' => 3 );
343 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
344
345 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
346 $itemParams = array(
347 'amount' => '-100.00',
348 'entity_id' => $lineItem,
349 );
350 $item1 = $item2 = array();
351 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item1);
352 $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item");
353 $itemParams['amount'] = '100.00';
354 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item2);
355 $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item");
356
357 $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'");
358 $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'");
359 $this->assertEquals($cValue1, "-100.00", "Verify Amount");
360 $this->assertEquals($fValue1, "-100.00", "Verify Amount");
361 $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'");
362 $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'");
363 $this->assertEquals($cValue2, "100.00", "Verify Amount");
364 $this->assertEquals($fValue2, "100.00", "Verify Amount");
365 }
366
367 function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
368 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
369 $query = "SELECT
370 {$select}
371 FROM civicrm_financial_trxn ft
372 INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}";
373 if ($entityTable) {
374 $query .= " AND eft.entity_table = {$entityTable}";
375 }
376 if (!empty($to)) {
377 $to = array_search($to, $financialAccount);
378 $query .= " AND ft.to_financial_account_id = {$to}";
379 }
380 if (!empty($from)) {
381 $from = array_search($from, $financialAccount);
382 $query .= " AND ft.from_financial_account_id = {$from}";
383 }
384 if (!empty($cost)) {
385 $query .= " AND eft.amount = {$cost}";
386 }
387 $result = CRM_Core_DAO::singleValueQuery($query);
388 return $result;
389 }
390
391 function _getFinancialTrxnAmount($contId) {
392 $query = "SELECT
393 SUM( ft.total_amount ) AS total
394 FROM civicrm_financial_trxn AS ft
395 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
396 WHERE ceft.entity_table = 'civicrm_contribution'
397 AND ceft.entity_id = {$contId}";
398 $result = CRM_Core_DAO::singleValueQuery($query);
399 return $result;
400 }
401
402 function _getFinancialItemAmount($contId) {
403 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
404 $query = "SELECT
405 SUM(amount)
406 FROM civicrm_financial_item
407 WHERE entity_table = 'civicrm_line_item'
408 AND entity_id = {$lineItem}";
409 $result = CRM_Core_DAO::singleValueQuery($query);
410 return $result;
411 }
412
413 function _getTotalContributedAmount($contId) {
414 $query = "SELECT
415 SUM(amount)
416 FROM civicrm_entity_financial_trxn
417 WHERE entity_table = 'civicrm_contribution'
418 AND entity_id = {$contId}";
419 $result = CRM_Core_DAO::singleValueQuery($query);
420 return $result;
421 }
422
42daf119 423 function _testOfflineContribution($firstName, $lastName, $email, $amount, $status="Completed") {
855be98e 424
42daf119 425 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
855be98e
PJ
426
427 // create new contact using dialog
428 $this->webtestNewDialogContact($firstName, $lastName, $email);
429
430 // select financial type
431 $this->select( "financial_type_id", "value=1" );
432
433 //Contribution status
434 $this->select("contribution_status_id", "label=$status");
435
436 // total amount
437 $this->type("total_amount", $amount);
438
439 // select payment instrument type
440 $this->select("payment_instrument_id", "label=Credit Card");
441
442
443 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
444
445
446 //Custom Data
447 //$this->click('CIVICRM_QFID_3_6');
448
449 // Clicking save.
450 $this->clickLink("_qf_Contribution_upload");
451
452 // Is status message correct?
453 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
454
d17bd806 455 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e 456 // verify if Membership is created
855be98e
PJ
457
458 //click through to the Membership view screen
d17bd806 459 $this->click("xpath=//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e
PJ
460
461 $expected = array(
462 'Financial Type' => 'Donation',
463 'Total Amount' => '100.00',
464 'Contribution Status' => $status,
465 );
466 $this->webtestVerifyTabularData($expected);
d17bd806 467 $this->click("_qf_ContributionView_cancel-bottom");
855be98e
PJ
468 }
469}
470