3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviSeleniumTestCase.php';
29 class WebTest_Contribute_UpdateContributionTest
extends CiviSeleniumTestCase
{
31 protected function setUp() {
35 function testChangeContributionAmount() {
36 $this->webtestLogin();
37 $firstName = substr(sha1(rand()), 0, 7);
38 $lastName = 'Contributor';
39 $email = $firstName . "@example.com";
41 //Offline Pay Later Contribution
42 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
44 $this->openCiviPage("contribute/search", "reset=1", "contribution_date_low");
46 $this->type("sort_name", "$lastName, $firstName");
47 $this->click("_qf_Search_refresh");
49 $this->waitForPageToLoad($this->getTimeoutMsec());
50 $contriIDOff = explode('&', $this->getAttribute("xpath=//div[@id='contributionSearch']/table/tbody/tr[1]/td[11]/span/a@href"));
51 if (!empty($contriIDOff)) {
52 $contriIDOff = substr($contriIDOff[1], (strrpos($contriIDOff[1], '=') +
1));
55 $this->click("xpath=//tr[@id='rowid{$contriIDOff}']/td[11]/span/a[2]");
56 $this->waitForElementPresent("total_amount");
57 $this->type("total_amount", "90");
58 $this->click('_qf_Contribution_upload');
59 $this->waitForPageToLoad($this->getTimeoutMsec());
61 // Is status message correct?
62 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
64 $searchParams = array('id' => $contriIDOff);
65 $compareParams = array('total_amount' => '90.00');
67 $lineItemSearchParams = array('entity_id' => $contriIDOff);
68 $lineItemCompareParams = array('line_total' => '90.00');
70 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams);
71 $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams);
73 $total = $this->_getTotalContributedAmount($contriIDOff);
74 $compare = array('total_amount' => $total);
75 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
78 $amount = $this->_getFinancialItemAmount($contriIDOff);
79 $compare = array('total_amount' => $amount);
80 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
82 $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff);
83 $compare = array('total_amount' => $financialTrxnAmount);
84 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
87 function testPayLater() {
88 $this->webtestLogin();
89 $firstName = substr(sha1(rand()), 0, 7);
90 $lastName = 'Contributor';
91 $email = $firstName . "@example.com";
93 //Offline Pay Later Contribution
94 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
95 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
96 $this->waitForPageToLoad($this->getTimeoutMsec());
97 $elements = $this->parseURL();
98 $contId = $elements['queryString']['id'];
99 $this->select("contribution_status_id", "label=Completed");
100 $this->click("_qf_Contribution_upload");
101 $this->waitForPageToLoad($this->getTimeoutMsec());
103 $search = array('id' => $contId);
104 $compare = array('contribution_status_id' => 1);
105 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
107 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
108 $search = array( 'entity_id' => $lineItem );
109 $compare = array( 'status_id' => 1 );
110 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
112 $status = $this->_getPremiumActualCost($contId, 'Accounts Receivable', 'Payment Processor Account', NULL, "'civicrm_contribution'", "ft.status_id as status");
113 $this->assertEquals($status, '1', "Verify Completed Status");
116 function testChangePremium() {
117 $this->webtestLogin();
118 $firstName = substr(sha1(rand()), 0, 7);
119 $lastName = 'Contributor';
120 $email = $firstName . "@example.com";
122 $to = 'Premiums inventory';
123 $financialType = array(
124 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
126 'is_deductible' => 1,
128 $this->addeditFinancialType($financialType);
129 $this->select("account_relationship", "label=Cost of Sales Account is");
130 $this->select("financial_account_id", "label=$from");
131 $this->click("_qf_FinancialTypeAccount_next_new-botttom");
132 $this->waitForPageToLoad($this->getTimeoutMsec());
133 $this->select("account_relationship", "label=Premiums Inventory Account is");
134 $this->select("financial_account_id", "label=$to");
135 $this->click("_qf_FinancialTypeAccount_next-botttom");
136 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
141 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
143 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
146 $premiumName2 = 'Premium'.substr(sha1(rand()), 0, 7);
151 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
152 $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']);
154 // add contribution with premium
155 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
157 // create new contact using dialog
158 $this->webtestNewDialogContact($firstName, $lastName, $email);
159 // select financial type
160 $this->select( "financial_type_id", "value=1" );
162 $this->type("total_amount", "100");
163 // fill Premium information
164 $this->click("xpath=//div[@id='Premium']");
165 $this->waitForElementPresent("product_name_0");
166 $this->select('product_name_0', "label=$premiumName ( $sku )");
168 $this->click("_qf_Contribution_upload");
169 $this->waitForPageToLoad($this->getTimeoutMsec());
170 // Is status message correct?
171 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
172 // verify if Membership is created
173 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
174 //click through to the Contribution edit screen
175 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
176 $this->waitForElementPresent("_qf_Contribution_upload-bottom");
177 $elements = $this->parseURL();
178 $contId = $elements['queryString']['id'];
179 $this->waitForElementPresent("product_name_0");
180 $this->select('product_name_0', "label=$premiumName2 ( $sku2 )");
182 $this->click("_qf_Contribution_upload");
183 $this->waitForPageToLoad($this->getTimeoutMsec());
186 $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'");
187 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
189 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
190 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
193 function testDeletePremium() {
194 $this->webtestLogin();
195 $firstName = substr(sha1(rand()), 0, 7);
196 $lastName = 'Contributor';
197 $email = $firstName . "@example.com";
199 $to = 'Premiums inventory';
200 $financialType = array(
201 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
203 'is_deductible' => 1,
205 $this->addeditFinancialType($financialType);
206 $this->select("account_relationship", "label=Cost of Sales Account is");
207 $this->select("financial_account_id", "label=$from");
208 $this->click("_qf_FinancialTypeAccount_next_new-botttom");
209 $this->waitForPageToLoad($this->getTimeoutMsec());
210 $this->select("account_relationship", "label=Premiums Inventory Account is");
211 $this->select("financial_account_id", "label=$to");
212 $this->click("_qf_FinancialTypeAccount_next-botttom");
213 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
218 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
220 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
222 // add contribution with premium
223 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
225 // create new contact using dialog
226 $this->webtestNewDialogContact($firstName, $lastName, $email);
227 // select financial type
228 $this->select("financial_type_id", "value=1");
230 $this->type("total_amount", "100");
231 // fill Premium information
232 $this->click("xpath=//div[@id='Premium']");
233 $this->waitForElementPresent("product_name_0");
234 $this->select('product_name_0', "label=$premiumName ( $sku )");
236 $this->click("_qf_Contribution_upload");
237 $this->waitForPageToLoad($this->getTimeoutMsec());
238 // Is status message correct?
239 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
240 // verify if Membership is created
241 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
242 //click through to the Contribution edit screen
243 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
244 $this->waitForElementPresent("_qf_Contribution_upload-bottom");
245 $elements = $this->parseURL();
246 $contId = $elements['queryString']['id'];
247 $this->waitForElementPresent("product_name_0");
248 $this->select('product_name_0', "value=0");
250 $this->click("_qf_Contribution_upload");
251 $this->waitForPageToLoad($this->getTimeoutMsec());
254 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
255 $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium");
258 function testChangePaymentInstrument() {
259 $this->webtestLogin();
260 $firstName = substr(sha1(rand()), 0, 7);
261 $lastName = 'Contributor';
262 $email = $firstName . "@example.com";
263 $label = 'TEST'.substr(sha1(rand()), 0, 7);
265 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount();
266 $to = array_search('Accounts Receivable', $financialAccount);
267 $from = array_search('Deposit Bank Account', $financialAccount);
268 $this->addPaymentInstrument($label, $to);
269 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
270 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
271 $this->waitForPageToLoad($this->getTimeoutMsec());
272 $elements = $this->parseURL();
273 $contId = $elements['queryString']['id'];
274 //change payment processor to newly created value
275 $this->select("payment_instrument_id", "label=$label");
276 $this->click("_qf_Contribution_upload");
277 $this->waitForPageToLoad($this->getTimeoutMsec());
279 $totalAmount = $this->_getPremiumActualCost($contId, 'Payment Processor Account', 'Accounts Receivable');
280 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
283 function testRefundContribution() {
284 $this->webtestLogin();
285 $firstName = substr(sha1(rand()), 0, 7);
286 $lastName = 'Contributor';
287 $email = $firstName . "@example.com";
288 $label = 'TEST'.substr(sha1(rand()), 0, 7);
290 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
291 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
292 $this->waitForPageToLoad($this->getTimeoutMsec());
293 //Contribution status
294 $this->select("contribution_status_id", "label=Refunded");
295 $elements = $this->parseURL();
296 $contId = $elements['queryString']['id'];
297 $this->click("_qf_Contribution_upload");
298 $this->waitForPageToLoad($this->getTimeoutMsec());
301 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
302 $search = array('entity_id' => $lineItem);
304 'amount' => '100.00',
307 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
308 $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'");
309 $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.');
312 function testCancelPayLater() {
313 $this->webtestLogin();
314 $firstName = substr(sha1(rand()), 0, 7);
315 $lastName = 'Contributor';
316 $email = $firstName . "@example.com";
317 $label = 'TEST'.substr(sha1(rand()), 0, 7);
319 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
320 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
321 $this->waitForPageToLoad($this->getTimeoutMsec());
322 //Contribution status
323 $this->select("contribution_status_id", "label=Cancelled");
324 $elements = $this->parseURL();
325 $contId = $elements['queryString']['id'];
326 $this->click("_qf_Contribution_upload");
327 $this->waitForPageToLoad($this->getTimeoutMsec());
330 $search = array('id' => $contId);
331 $compare = array('contribution_status_id' => 3);
332 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
333 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
335 'amount' => '-100.00',
336 'entity_id' => $lineItem,
339 $items = CRM_Financial_BAO_FinancialItem
::retrieve($itemParams, $defaults);
340 $this->assertEquals($items->amount
, $itemParams['amount'], 'Verify Amount for financial Item');
341 $totalAmount = $this->_getPremiumActualCost($items->id
, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'");
342 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
343 $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'");
344 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
347 function testChangeFinancialType() {
348 $this->webtestLogin();
349 $firstName = substr(sha1(rand()), 0, 7);
350 $lastName = 'Contributor';
351 $email = $firstName . "@example.com";
352 $label = 'TEST'.substr(sha1(rand()), 0, 7);
354 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
355 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
356 $this->waitForPageToLoad($this->getTimeoutMsec());
357 //Contribution status
358 $this->select("financial_type_id", "value=3");
359 $elements = $this->parseURL();
360 $contId = $elements['queryString']['id'];
361 $this->click("_qf_Contribution_upload");
362 $this->waitForPageToLoad($this->getTimeoutMsec());
365 $search = array( 'id' => $contId );
366 $compare = array( 'financial_type_id' => 3 );
367 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
369 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
371 'amount' => '-100.00',
372 'entity_id' => $lineItem,
374 $item1 = $item2 = array();
375 CRM_Financial_BAO_FinancialItem
::retrieve($itemParams, $item1);
376 $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item");
377 $itemParams['amount'] = '100.00';
378 CRM_Financial_BAO_FinancialItem
::retrieve($itemParams, $item2);
379 $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item");
381 $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'");
382 $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'");
383 $this->assertEquals($cValue1, "-100.00", "Verify Amount");
384 $this->assertEquals($fValue1, "-100.00", "Verify Amount");
385 $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'");
386 $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'");
387 $this->assertEquals($cValue2, "100.00", "Verify Amount");
388 $this->assertEquals($fValue2, "100.00", "Verify Amount");
391 function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
392 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount();
395 FROM civicrm_financial_trxn ft
396 INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}";
398 $query .= " AND eft.entity_table = {$entityTable}";
401 $to = array_search($to, $financialAccount);
402 $query .= " AND ft.to_financial_account_id = {$to}";
405 $from = array_search($from, $financialAccount);
406 $query .= " AND ft.from_financial_account_id = {$from}";
409 $query .= " AND eft.amount = {$cost}";
411 $result = CRM_Core_DAO
::singleValueQuery($query);
415 function _getFinancialTrxnAmount($contId) {
417 SUM( ft.total_amount ) AS total
418 FROM civicrm_financial_trxn AS ft
419 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
420 WHERE ceft.entity_table = 'civicrm_contribution'
421 AND ceft.entity_id = {$contId}";
422 $result = CRM_Core_DAO
::singleValueQuery($query);
426 function _getFinancialItemAmount($contId) {
427 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
430 FROM civicrm_financial_item
431 WHERE entity_table = 'civicrm_line_item'
432 AND entity_id = {$lineItem}";
433 $result = CRM_Core_DAO
::singleValueQuery($query);
437 function _getTotalContributedAmount($contId) {
440 FROM civicrm_entity_financial_trxn
441 WHERE entity_table = 'civicrm_contribution'
442 AND entity_id = {$contId}";
443 $result = CRM_Core_DAO
::singleValueQuery($query);
447 function _testOfflineContribution($firstName, $lastName, $email, $amount, $status="Completed") {
449 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
451 // create new contact using dialog
452 $this->webtestNewDialogContact($firstName, $lastName, $email);
454 // select financial type
455 $this->select( "financial_type_id", "value=1" );
457 //Contribution status
458 $this->select("contribution_status_id", "label=$status");
461 $this->type("total_amount", $amount);
463 // select payment instrument type
464 $this->select("payment_instrument_id", "label=Credit Card");
466 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
469 //$this->click('CIVICRM_QFID_3_6');
472 $this->click("_qf_Contribution_upload");
473 $this->waitForPageToLoad($this->getTimeoutMsec());
475 // Is status message correct?
476 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
478 // verify if Membership is created
479 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
481 //click through to the Membership view screen
482 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']");
483 $this->waitForElementPresent("_qf_ContributionView_cancel-bottom");
486 'Financial Type' => 'Donation',
487 'Total Amount' => '100.00',
488 'Contribution Status' => $status,
490 foreach ($expected as $label => $value) {
491 $this->verifyText("xpath=id('ContributionView')/div[2]/table[1]/tbody//tr/td[1][text()='$label']/../td[2]", preg_quote($value));
493 $this->click("_qf_ContributionView_cancel-top");
494 $this->waitForPageToLoad($this->getTimeoutMsec());
495 // Because it tends to cause problems, all uses of sleep() must be justified in comments
496 // Sleep should never be used for wait for anything to load from the server
497 // Justification for this instance: FIXME