INFRA-132 - settings/ - phpcbf
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / UpdateContributionTest.php
CommitLineData
855be98e
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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';
e9479dcf
EM
29
30/**
31 * Class WebTest_Contribute_UpdateContributionTest
32 */
855be98e
PJ
33class WebTest_Contribute_UpdateContributionTest extends CiviSeleniumTestCase {
34
35 protected function setUp() {
36 parent::setUp();
37 }
38
00be9182 39 public function testChangeContributionAmount() {
855be98e 40 $this->webtestLogin();
855be98e
PJ
41 $amount = 100;
42 //Offline Pay Later Contribution
80f3b91d 43 $contact = $this->_testOfflineContribution($amount, "Pending");
855be98e
PJ
44
45 $this->openCiviPage("contribute/search", "reset=1", "contribution_date_low");
46
80f3b91d 47 $this->type("sort_name", $contact['sort_name']);
d17bd806 48 $this->click("_qf_Search_refresh");
855be98e 49
215ed9aa 50 $this->waitForElementPresent("xpath=//*[@id='Search']//div[@id='contributionSearch']");
d17bd806 51 $contriIDOff = explode('&', $this->getAttribute("xpath=//div[@id='contributionSearch']//table[@class='selector row-highlight']/tbody/tr[1]/td[11]/span/a[1]@href"));
855be98e
PJ
52 if (!empty($contriIDOff)) {
53 $contriIDOff = substr($contriIDOff[1], (strrpos($contriIDOff[1], '=') + 1));
54 }
55
d17bd806 56 $this->clickLink("xpath=//tr[@id='rowid{$contriIDOff}']/td[11]/span/a[2]", "total_amount", FALSE);
855be98e 57 $this->type("total_amount", "90");
215ed9aa 58 $this->clickLink('_qf_Contribution_upload','',FALSE);
855be98e
PJ
59
60 // Is status message correct?
61 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
62
63 //For Contribution
64 $searchParams = array('id' => $contriIDOff);
65 $compareParams = array('total_amount' => '90.00');
66 //For LineItem
67 $lineItemSearchParams = array('entity_id' => $contriIDOff);
68 $lineItemCompareParams = array('line_total' => '90.00');
69
70 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams);
71 $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams);
72
73 $total = $this->_getTotalContributedAmount($contriIDOff);
74 $compare = array('total_amount' => $total);
75 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
76
77 $amount = $this->_getFinancialItemAmount($contriIDOff);
78 $compare = array('total_amount' => $amount);
79 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
80
81 $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff);
82 $compare = array('total_amount' => $financialTrxnAmount);
83 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
84 }
85
00be9182 86 public function testPayLater() {
855be98e 87 $this->webtestLogin();
855be98e
PJ
88 $amount = 100.00;
89 //Offline Pay Later Contribution
80f3b91d 90 $this->_testOfflineContribution($amount, "Pending");
9445497e 91 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
80f3b91d
CW
92 $contId = $this->urlArg('id', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
93 $this->clickAjaxLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom');
855be98e 94 $this->select("contribution_status_id", "label=Completed");
80f3b91d
CW
95 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
96 $this->checkCRMAlert("The contribution record has been saved.");
855be98e
PJ
97
98 //Assertions
99 $search = array('id' => $contId);
100 $compare = array('contribution_status_id' => 1);
101 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
102
103 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
104 $search = array( 'entity_id' => $lineItem );
105 $compare = array( 'status_id' => 1 );
106 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
107
108 $status = $this->_getPremiumActualCost($contId, 'Accounts Receivable', 'Payment Processor Account', NULL, "'civicrm_contribution'", "ft.status_id as status");
76e86fd8 109 $this->assertEquals($status, '1', "Verify Completed Status");
855be98e
PJ
110 }
111
00be9182 112 public function testChangePremium() {
855be98e 113 $this->webtestLogin();
855be98e
PJ
114 $from = 'Premiums';
115 $to = 'Premiums inventory';
116 $financialType = array(
117 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
118 'is_reserved' => 1,
119 'is_deductible' => 1,
120 );
121 $this->addeditFinancialType($financialType);
9445497e 122 $this->waitForElementPresent("xpath=//div[@id='ltype']/div/table/tbody//tr/td[text()='".$financialType['name']."']/../td[7]/span/a[text()='Accounts']");
123 $this->click("xpath=//div[@id='ltype']/div/table/tbody//tr/td[text()='".$financialType['name']."']/../td[7]/span/a[text()='Accounts']");
124 $this->waitForElementPresent("xpath=//a[@id='newfinancialTypeAccount']");
9c5c0056 125 $this->clickAjaxLink("xpath=//a[@id='newfinancialTypeAccount']", "_qf_FinancialTypeAccount_cancel-botttom");
855be98e 126 $this->select("account_relationship", "label=Premiums Inventory Account is");
93fc1656 127 $this->waitForElementPresent("xpath=//select[@id='financial_account_id']/option[3][text()='Deposit Bank Account']");
855be98e 128 $this->select("financial_account_id", "label=$to");
9c5c0056 129 $this->clickAjaxLink("_qf_FinancialTypeAccount_next-botttom", "xpath=//a[@id='newfinancialTypeAccount']");
4cbe18b8 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
80f3b91d 153 $this->createDialogContact();
855be98e
PJ
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
9c5c0056 159 $this->clickAjaxLink("xpath=//div[@id='Premium']", "product_name_0");
855be98e
PJ
160 $this->select('product_name_0', "label=$premiumName ( $sku )");
161 // Clicking save.
9c5c0056 162 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e 163 // Is status message correct?
9c5c0056 164 $this->checkCRMAlert("The contribution record has been saved.");
dcba1580 165 // verify if Contribution is created
855be98e 166 //click through to the Contribution edit screen
80f3b91d
CW
167 $contId = $this->urlArg('id', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
168 $this->clickAjaxLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", "product_name_0");
66cae705 169
855be98e
PJ
170 $this->select('product_name_0', "label=$premiumName2 ( $sku2 )");
171 // Clicking save.
9c5c0056 172 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[7][text()='$premiumName2']");
5c88df60 173 $this->checkCRMAlert("The contribution record has been saved.");
855be98e
PJ
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
00be9182 183 public function testDeletePremium() {
855be98e 184 $this->webtestLogin();
855be98e
PJ
185 $from = 'Premiums';
186 $to = 'Premiums inventory';
187 $financialType = array(
f17d75bb 188 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
855be98e
PJ
189 'is_reserved' => 1,
190 'is_deductible' => 1,
191 );
192 $this->addeditFinancialType($financialType);
9445497e 193 $this->waitForElementPresent("xpath=//div[@id='ltype']/div/table/tbody//tr/td[text()='".$financialType['name']."']/../td[7]/span/a[text()='Accounts']");
194 $this->click("xpath=//div[@id='ltype']/div/table/tbody//tr/td[text()='".$financialType['name']."']/../td[7]/span/a[text()='Accounts']");
195 $this->waitForElementPresent("xpath=//a[@id='newfinancialTypeAccount']");
196 $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']", "_qf_FinancialTypeAccount_cancel-botttom", FALSE);
4cbe18b8 197
855be98e 198 $this->select("account_relationship", "label=Premiums Inventory Account is");
93fc1656 199 $this->waitForElementPresent("xpath=//select[@id='financial_account_id']/option[3][text()='Deposit Bank Account']");
855be98e 200 $this->select("financial_account_id", "label=$to");
9445497e 201 $this->clickLink("_qf_FinancialTypeAccount_next-botttom", "xpath=//a[@id='newfinancialTypeAccount']", FALSE);
f17d75bb 202 $premiumName = 'Premium' . substr(sha1(rand()), 0, 7);
855be98e
PJ
203 $amount = 500;
204 $sku = 'SKU';
205 $price = 300;
206 $cost = 3.00;
207 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
208 // add premium
209 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
210
211 // add contribution with premium
212 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
213
214 // create new contact using dialog
80f3b91d 215 $this->createDialogContact();
855be98e
PJ
216 // select financial type
217 $this->select("financial_type_id", "value=1");
218 // total amount
219 $this->type("total_amount", "100");
220 // fill Premium information
221 $this->click("xpath=//div[@id='Premium']");
222 $this->waitForElementPresent("product_name_0");
223 $this->select('product_name_0', "label=$premiumName ( $sku )");
224 // Clicking save.
225 $this->clickLink("_qf_Contribution_upload");
226 // Is status message correct?
227 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
dcba1580 228 // verify if Contribution is created
d17bd806 229 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e 230 //click through to the Contribution edit screen
9445497e 231 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
232 $contId = explode('&', $contId[1]);
233 $contId = $contId[0];
234 $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", FALSE);
855be98e
PJ
235 $this->waitForElementPresent("product_name_0");
236 $this->select('product_name_0', "value=0");
237 // Clicking save.
93fc1656 238 $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[7][not(text())]", FALSE);
c3d82091 239
855be98e
PJ
240 //Assertions
241 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
242 $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium");
243 }
244
00be9182 245 public function testChangePaymentInstrument() {
855be98e 246 $this->webtestLogin();
855be98e
PJ
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);
80f3b91d 253 $this->_testOfflineContribution($amount);
9445497e 254 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
255 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE);
256 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
257 $contId = explode('&', $contId[1]);
258 $contId = $contId[0];
855be98e
PJ
259 //change payment processor to newly created value
260 $this->select("payment_instrument_id", "label=$label");
9445497e 261 $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE);
2913f26f 262 $this->waitForAjaxContent();
855be98e 263 //Assertions
4ddaee72
M
264 $subtractedTotal = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account');
265 $this->assertEquals($subtractedTotal, -$amount, "Verify amount deleted from old account");
266 $totalAmount = $this->_getPremiumActualCost($contId, NULL, 'Accounts Receivable');
855be98e
PJ
267 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
268 }
269
00be9182 270 public function testRefundContribution() {
855be98e 271 $this->webtestLogin();
855be98e 272 $amount = 100.00;
80f3b91d 273 $this->_testOfflineContribution($amount);
9445497e 274 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
9c5c0056 275 $this->clickAjaxLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom');
855be98e
PJ
276 //Contribution status
277 $this->select("contribution_status_id", "label=Refunded");
9445497e 278 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
279 $contId = explode('&', $contId[1]);
280 $contId = $contId[0];
9c5c0056 281 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[6][text()='Refunded']");
855be98e
PJ
282
283 //Assertions
284 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
285 $search = array('entity_id' => $lineItem);
286 $compare = array(
287 'amount' => '100.00',
288 'status_id' => 1,
289 );
290 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
291 $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'");
292 $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.');
293 }
294
00be9182 295 public function testCancelPayLater() {
855be98e 296 $this->webtestLogin();
855be98e 297 $amount = 100.00;
80f3b91d 298 $this->_testOfflineContribution($amount, "Pending");
9445497e 299 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
300 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE);
855be98e
PJ
301 //Contribution status
302 $this->select("contribution_status_id", "label=Cancelled");
9445497e 303 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
304 $contId = explode('&', $contId[1]);
305 $contId = $contId[0];
306 $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE);
5dac229e 307 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
855be98e
PJ
308
309 //Assertions
310 $search = array('id' => $contId);
311 $compare = array('contribution_status_id' => 3);
312 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
313 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
314 $itemParams = array(
315 'amount' => '-100.00',
316 'entity_id' => $lineItem,
317 );
318 $defaults = array();
319 $items = CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $defaults);
320 $this->assertEquals($items->amount, $itemParams['amount'], 'Verify Amount for financial Item');
321 $totalAmount = $this->_getPremiumActualCost($items->id, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'");
322 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
323 $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'");
324 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
325 }
326
00be9182 327 public function testChangeFinancialType() {
855be98e 328 $this->webtestLogin();
855be98e 329 $amount = 100.00;
80f3b91d 330 $this->_testOfflineContribution($amount);
9445497e 331 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
332 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE);
855be98e
PJ
333 //Contribution status
334 $this->select("financial_type_id", "value=3");
9445497e 335 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
336 $contId = explode('&', $contId[1]);
337 $contId = $contId[0];
338 $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE);
5dac229e 339 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
855be98e
PJ
340
341 //Assertions
342 $search = array( 'id' => $contId );
343 $compare = array( 'financial_type_id' => 3 );
344 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
345
346 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
347 $itemParams = array(
348 'amount' => '-100.00',
349 'entity_id' => $lineItem,
350 );
351 $item1 = $item2 = array();
352 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item1);
353 $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item");
354 $itemParams['amount'] = '100.00';
355 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item2);
356 $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item");
357
358 $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'");
359 $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'");
360 $this->assertEquals($cValue1, "-100.00", "Verify Amount");
361 $this->assertEquals($fValue1, "-100.00", "Verify Amount");
362 $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'");
363 $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'");
364 $this->assertEquals($cValue2, "100.00", "Verify Amount");
365 $this->assertEquals($fValue2, "100.00", "Verify Amount");
366 }
367
4cbe18b8 368 /**
100fef9d 369 * @param int $entityId
4cbe18b8
EM
370 * @param null $from
371 * @param null $to
372 * @param null $cost
373 * @param null $entityTable
374 * @param string $select
375 *
376 * @return null|string
95ea96be
EM
377 */
378 function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
855be98e
PJ
379 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
380 $query = "SELECT
381 {$select}
382 FROM civicrm_financial_trxn ft
383 INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}";
384 if ($entityTable) {
385 $query .= " AND eft.entity_table = {$entityTable}";
386 }
387 if (!empty($to)) {
388 $to = array_search($to, $financialAccount);
389 $query .= " AND ft.to_financial_account_id = {$to}";
390 }
391 if (!empty($from)) {
392 $from = array_search($from, $financialAccount);
393 $query .= " AND ft.from_financial_account_id = {$from}";
394 }
395 if (!empty($cost)) {
396 $query .= " AND eft.amount = {$cost}";
397 }
4ddaee72 398 $query .= " ORDER BY ft.id DESC LIMIT 1";
855be98e
PJ
399 $result = CRM_Core_DAO::singleValueQuery($query);
400 return $result;
401 }
402
4cbe18b8 403 /**
100fef9d 404 * @param int $contId
4cbe18b8
EM
405 *
406 * @return null|string
95ea96be
EM
407 */
408 function _getFinancialTrxnAmount($contId) {
855be98e
PJ
409 $query = "SELECT
410 SUM( ft.total_amount ) AS total
411 FROM civicrm_financial_trxn AS ft
412 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
413 WHERE ceft.entity_table = 'civicrm_contribution'
414 AND ceft.entity_id = {$contId}";
415 $result = CRM_Core_DAO::singleValueQuery($query);
416 return $result;
417 }
418
4cbe18b8 419 /**
100fef9d 420 * @param int $contId
4cbe18b8
EM
421 *
422 * @return null|string
95ea96be
EM
423 */
424 function _getFinancialItemAmount($contId) {
855be98e
PJ
425 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
426 $query = "SELECT
427 SUM(amount)
428 FROM civicrm_financial_item
429 WHERE entity_table = 'civicrm_line_item'
430 AND entity_id = {$lineItem}";
431 $result = CRM_Core_DAO::singleValueQuery($query);
432 return $result;
433 }
434
4cbe18b8 435 /**
100fef9d 436 * @param int $contId
4cbe18b8
EM
437 *
438 * @return null|string
95ea96be
EM
439 */
440 function _getTotalContributedAmount($contId) {
855be98e
PJ
441 $query = "SELECT
442 SUM(amount)
443 FROM civicrm_entity_financial_trxn
444 WHERE entity_table = 'civicrm_contribution'
445 AND entity_id = {$contId}";
446 $result = CRM_Core_DAO::singleValueQuery($query);
447 return $result;
448 }
449
4cbe18b8 450 /**
4cbe18b8
EM
451 * @param $amount
452 * @param string $status
80f3b91d 453 * @return array
4cbe18b8 454 */
00be9182 455 public function _testOfflineContribution($amount, $status="Completed") {
855be98e 456
42daf119 457 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
855be98e
PJ
458
459 // create new contact using dialog
80f3b91d 460 $contact = $this->createDialogContact();
855be98e
PJ
461
462 // select financial type
463 $this->select( "financial_type_id", "value=1" );
464
465 //Contribution status
466 $this->select("contribution_status_id", "label=$status");
467
468 // total amount
469 $this->type("total_amount", $amount);
470
471 // select payment instrument type
472 $this->select("payment_instrument_id", "label=Credit Card");
473
474
475 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
476
477
478 //Custom Data
479 //$this->click('CIVICRM_QFID_3_6');
480
481 // Clicking save.
482 $this->clickLink("_qf_Contribution_upload");
483
484 // Is status message correct?
80f3b91d 485 $this->checkCRMAlert("The contribution record has been saved.");
855be98e 486
d17bd806 487 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e 488 // verify if Membership is created
855be98e
PJ
489
490 //click through to the Membership view screen
d17bd806 491 $this->click("xpath=//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e
PJ
492
493 $expected = array(
494 'Financial Type' => 'Donation',
495 'Total Amount' => '100.00',
496 'Contribution Status' => $status,
497 );
498 $this->webtestVerifyTabularData($expected);
d17bd806 499 $this->click("_qf_ContributionView_cancel-bottom");
80f3b91d
CW
500
501 return $contact;
855be98e
PJ
502 }
503}