3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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->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"));
51 if (!empty($contriIDOff)) {
52 $contriIDOff = substr($contriIDOff[1], (strrpos($contriIDOff[1], '=') +
1));
55 $this->clickLink("xpath=//tr[@id='rowid{$contriIDOff}']/td[11]/span/a[2]", "total_amount", FALSE);
56 $this->type("total_amount", "90");
57 $this->clickLink('_qf_Contribution_upload');
59 // Is status message correct?
60 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
63 $searchParams = array('id' => $contriIDOff);
64 $compareParams = array('total_amount' => '90.00');
66 $lineItemSearchParams = array('entity_id' => $contriIDOff);
67 $lineItemCompareParams = array('line_total' => '90.00');
69 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams);
70 $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams);
72 $total = $this->_getTotalContributedAmount($contriIDOff);
73 $compare = array('total_amount' => $total);
74 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
76 $amount = $this->_getFinancialItemAmount($contriIDOff);
77 $compare = array('total_amount' => $amount);
78 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
80 $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff);
81 $compare = array('total_amount' => $financialTrxnAmount);
82 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
85 function testPayLater() {
86 $this->webtestLogin();
87 $firstName = substr(sha1(rand()), 0, 7);
88 $lastName = 'Contributor';
89 $email = $firstName . "@example.com";
91 //Offline Pay Later Contribution
92 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
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']");
95 $contId = $this->urlArg('id');
96 $this->select("contribution_status_id", "label=Completed");
97 $this->clickLink("_qf_Contribution_upload");
100 $search = array('id' => $contId);
101 $compare = array('contribution_status_id' => 1);
102 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
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);
109 $status = $this->_getPremiumActualCost($contId, 'Accounts Receivable', 'Payment Processor Account', NULL, "'civicrm_contribution'", "ft.status_id as status");
110 $this->assertEquals($status, '1', "Verify Completed Status");
113 function testChangePremium() {
114 $this->webtestLogin();
115 $firstName = substr(sha1(rand()), 0, 7);
116 $lastName = 'Contributor';
117 $email = $firstName . "@example.com";
119 $to = 'Premiums inventory';
120 $financialType = array(
121 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
123 'is_deductible' => 1,
125 $this->addeditFinancialType($financialType);
126 $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']");
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");
131 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
136 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
138 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
141 $premiumName2 = 'Premium'.substr(sha1(rand()), 0, 7);
146 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
147 $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']);
149 // add contribution with premium
150 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
152 // create new contact using dialog
153 $this->webtestNewDialogContact($firstName, $lastName, $email);
154 // select financial type
155 $this->select( "financial_type_id", "value=1" );
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 )");
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
166 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
167 //click through to the Contribution edit screen
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");
169 $contId = $this->urlArg('id');
170 $this->waitForElementPresent("product_name_0");
171 $this->select('product_name_0', "label=$premiumName2 ( $sku2 )");
173 $this->clickLink("_qf_Contribution_upload");
176 $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'");
177 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
179 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
180 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
183 function testDeletePremium() {
184 $this->webtestLogin();
185 $firstName = substr(sha1(rand()), 0, 7);
186 $lastName = 'Contributor';
187 $email = $firstName . "@example.com";
189 $to = 'Premiums inventory';
190 $financialType = array(
191 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
193 'is_deductible' => 1,
195 $this->addeditFinancialType($financialType);
196 $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']");
198 $this->select("account_relationship", "label=Premiums Inventory Account is");
199 $this->select("financial_account_id", "label=$to");
200 $this->clickLink("_qf_FinancialTypeAccount_next-botttom");
201 $premiumName = 'Premium' . substr(sha1(rand()), 0, 7);
206 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
208 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
210 // add contribution with premium
211 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
213 // create new contact using dialog
214 $this->webtestNewDialogContact($firstName, $lastName, $email);
215 // select financial type
216 $this->select("financial_type_id", "value=1");
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 )");
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[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
229 //click through to the Contribution edit screen
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");
231 $contId = $this->urlArg('id');
232 $this->waitForElementPresent("product_name_0");
233 $this->select('product_name_0', "value=0");
235 $this->clickLink("_qf_Contribution_upload");
238 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
239 $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium");
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);
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->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']");
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");
261 $totalAmount = $this->_getPremiumActualCost($contId, 'Payment Processor Account', 'Accounts Receivable');
262 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
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);
272 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
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']");
275 //Contribution status
276 $this->select("contribution_status_id", "label=Refunded");
277 $contId = $this->urlArg('id');
278 $this->clickLink("_qf_Contribution_upload");
281 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
282 $search = array('entity_id' => $lineItem);
284 'amount' => '100.00',
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.');
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);
299 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
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']");
302 //Contribution status
303 $this->select("contribution_status_id", "label=Cancelled");
304 $contId = $this->urlArg('id');
305 $this->clickLink("_qf_Contribution_upload");
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'));
313 'amount' => '-100.00',
314 'entity_id' => $lineItem,
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');
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);
332 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
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']");
335 //Contribution status
336 $this->select("financial_type_id", "value=3");
337 $contId = $this->urlArg('id');
338 $this->clickLink("_qf_Contribution_upload");
341 $search = array( 'id' => $contId );
342 $compare = array( 'financial_type_id' => 3 );
343 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
345 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
347 'amount' => '-100.00',
348 'entity_id' => $lineItem,
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");
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");
367 function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
368 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount();
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}";
374 $query .= " AND eft.entity_table = {$entityTable}";
377 $to = array_search($to, $financialAccount);
378 $query .= " AND ft.to_financial_account_id = {$to}";
381 $from = array_search($from, $financialAccount);
382 $query .= " AND ft.from_financial_account_id = {$from}";
385 $query .= " AND eft.amount = {$cost}";
387 $result = CRM_Core_DAO
::singleValueQuery($query);
391 function _getFinancialTrxnAmount($contId) {
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);
402 function _getFinancialItemAmount($contId) {
403 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
406 FROM civicrm_financial_item
407 WHERE entity_table = 'civicrm_line_item'
408 AND entity_id = {$lineItem}";
409 $result = CRM_Core_DAO
::singleValueQuery($query);
413 function _getTotalContributedAmount($contId) {
416 FROM civicrm_entity_financial_trxn
417 WHERE entity_table = 'civicrm_contribution'
418 AND entity_id = {$contId}";
419 $result = CRM_Core_DAO
::singleValueQuery($query);
423 function _testOfflineContribution($firstName, $lastName, $email, $amount, $status="Completed") {
425 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
427 // create new contact using dialog
428 $this->webtestNewDialogContact($firstName, $lastName, $email);
430 // select financial type
431 $this->select( "financial_type_id", "value=1" );
433 //Contribution status
434 $this->select("contribution_status_id", "label=$status");
437 $this->type("total_amount", $amount);
439 // select payment instrument type
440 $this->select("payment_instrument_id", "label=Credit Card");
443 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
447 //$this->click('CIVICRM_QFID_3_6');
450 $this->clickLink("_qf_Contribution_upload");
452 // Is status message correct?
453 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
455 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
456 // verify if Membership is created
458 //click through to the Membership view screen
459 $this->click("xpath=//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='View']");
462 'Financial Type' => 'Donation',
463 'Total Amount' => '100.00',
464 'Contribution Status' => $status,
466 $this->webtestVerifyTabularData($expected);
467 $this->click("_qf_ContributionView_cancel-bottom");