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->clickLink("_qf_Search_refresh");
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));
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');
58 // Is status message correct?
59 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
62 $searchParams = array('id' => $contriIDOff);
63 $compareParams = array('total_amount' => '90.00');
65 $lineItemSearchParams = array('entity_id' => $contriIDOff);
66 $lineItemCompareParams = array('line_total' => '90.00');
68 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams);
69 $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams);
71 $total = $this->_getTotalContributedAmount($contriIDOff);
72 $compare = array('total_amount' => $total);
73 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
75 $amount = $this->_getFinancialItemAmount($contriIDOff);
76 $compare = array('total_amount' => $amount);
77 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
79 $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff);
80 $compare = array('total_amount' => $financialTrxnAmount);
81 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
84 function testPayLater() {
85 $this->webtestLogin();
86 $firstName = substr(sha1(rand()), 0, 7);
87 $lastName = 'Contributor';
88 $email = $firstName . "@example.com";
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");
98 $search = array('id' => $contId);
99 $compare = array('contribution_status_id' => 1);
100 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
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);
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");
111 function testChangePremium() {
112 $this->webtestLogin();
113 $firstName = substr(sha1(rand()), 0, 7);
114 $lastName = 'Contributor';
115 $email = $firstName . "@example.com";
117 $to = 'Premiums inventory';
118 $financialType = array(
119 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
121 'is_deductible' => 1,
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");
129 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
134 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
136 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
139 $premiumName2 = 'Premium'.substr(sha1(rand()), 0, 7);
144 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
145 $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']);
147 // add contribution with premium
148 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
150 // create new contact using dialog
151 $this->webtestNewDialogContact($firstName, $lastName, $email);
152 // select financial type
153 $this->select( "financial_type_id", "value=1" );
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 )");
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 )");
171 $this->clickLink("_qf_Contribution_upload");
174 $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'");
175 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
177 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
178 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
181 function testDeletePremium() {
182 $this->webtestLogin();
183 $firstName = substr(sha1(rand()), 0, 7);
184 $lastName = 'Contributor';
185 $email = $firstName . "@example.com";
187 $to = 'Premiums inventory';
188 $financialType = array(
189 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
191 'is_deductible' => 1,
193 $this->addeditFinancialType($financialType);
194 $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']");
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);
204 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
206 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
208 // add contribution with premium
209 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
211 // create new contact using dialog
212 $this->webtestNewDialogContact($firstName, $lastName, $email);
213 // select financial type
214 $this->select("financial_type_id", "value=1");
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 )");
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");
233 $this->clickLink("_qf_Contribution_upload");
236 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
237 $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium");
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);
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");
258 $totalAmount = $this->_getPremiumActualCost($contId, 'Payment Processor Account', 'Accounts Receivable');
259 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
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);
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");
277 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
278 $search = array('entity_id' => $lineItem);
280 'amount' => '100.00',
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.');
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);
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");
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'));
308 'amount' => '-100.00',
309 'entity_id' => $lineItem,
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');
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);
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");
335 $search = array( 'id' => $contId );
336 $compare = array( 'financial_type_id' => 3 );
337 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
339 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
341 'amount' => '-100.00',
342 'entity_id' => $lineItem,
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");
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");
361 function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
362 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount();
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}";
368 $query .= " AND eft.entity_table = {$entityTable}";
371 $to = array_search($to, $financialAccount);
372 $query .= " AND ft.to_financial_account_id = {$to}";
375 $from = array_search($from, $financialAccount);
376 $query .= " AND ft.from_financial_account_id = {$from}";
379 $query .= " AND eft.amount = {$cost}";
381 $result = CRM_Core_DAO
::singleValueQuery($query);
385 function _getFinancialTrxnAmount($contId) {
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);
396 function _getFinancialItemAmount($contId) {
397 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
400 FROM civicrm_financial_item
401 WHERE entity_table = 'civicrm_line_item'
402 AND entity_id = {$lineItem}";
403 $result = CRM_Core_DAO
::singleValueQuery($query);
407 function _getTotalContributedAmount($contId) {
410 FROM civicrm_entity_financial_trxn
411 WHERE entity_table = 'civicrm_contribution'
412 AND entity_id = {$contId}";
413 $result = CRM_Core_DAO
::singleValueQuery($query);
417 function _testOfflineContribution($firstName, $lastName, $email, $amount, $status="Completed") {
419 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
421 // create new contact using dialog
422 $this->webtestNewDialogContact($firstName, $lastName, $email);
424 // select financial type
425 $this->select( "financial_type_id", "value=1" );
427 //Contribution status
428 $this->select("contribution_status_id", "label=$status");
431 $this->type("total_amount", $amount);
433 // select payment instrument type
434 $this->select("payment_instrument_id", "label=Credit Card");
437 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
441 //$this->click('CIVICRM_QFID_3_6');
444 $this->clickLink("_qf_Contribution_upload");
446 // Is status message correct?
447 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
449 // verify if Membership is created
450 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
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");
456 'Financial Type' => 'Donation',
457 'Total Amount' => '100.00',
458 'Contribution Status' => $status,
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