Import from SVN (r45945, r596)
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / UpdateContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
28 require_once 'CiviTest/CiviSeleniumTestCase.php';
29 class WebTest_Contribute_UpdateContributionTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testChangeContributionAmount() {
36 // This is the path where our testing install resides.
37 // The rest of URL is defined in CiviSeleniumTestCase base class, in
38 // class attributes.
39 $this->open($this->sboxPath);
40 // Logging in. Remember to wait for page to load. In most cases,
41 // you can rely on 30000 as the value that allows your test to pass, however,
42 // sometimes your test might fail because of this. In such cases, it's better to pick one element
43 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
44 // page contents loaded and you can continue your test execution.
45 $this->webtestLogin();
46 $firstName = substr(sha1(rand()), 0, 7);
47 $lastName = 'Contributor';
48 $email = $firstName . "@example.com";
49 $amount = 100;
50 //Offline Pay Later Contribution
51 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
52
53 $this->open($this->sboxPath . "civicrm/contribute/search?reset=1");
54
55 $this->waitForElementPresent("contribution_date_low");
56
57 $this->type("sort_name", "$lastName, $firstName");
58 $this->click("_qf_Search_refresh");
59
60 $this->waitForPageToLoad($this->getTimeoutMsec());
61 $contriIDOff = explode('&', $this->getAttribute("xpath=//div[@id='contributionSearch']/table/tbody/tr[1]/td[11]/span/a@href"));
62 if (!empty($contriIDOff)) {
63 $contriIDOff = substr($contriIDOff[1], (strrpos($contriIDOff[1], '=') + 1));
64 }
65
66 $this->click("xpath=//tr[@id='rowid{$contriIDOff}']/td[11]/span/a[2]");
67 $this->waitForElementPresent("total_amount");
68 $this->type("total_amount", "90");
69 $this->click('_qf_Contribution_upload');
70 $this->waitForPageToLoad($this->getTimeoutMsec());
71
72 // Is status message correct?
73 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
74 //For Contribution
75 $searchParams = array('id' => $contriIDOff);
76 $compareParams = array('total_amount' => '90.00');
77 //For LineItem
78 $lineItemSearchParams = array('entity_id' => $contriIDOff);
79 $lineItemCompareParams = array('line_total' => '90.00');
80
81 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams);
82 $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams);
83
84 $total = $this->_getTotalContributedAmount($contriIDOff);
85 $compare = array('total_amount' => $total);
86 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
87
88
89 $amount = $this->_getFinancialItemAmount($contriIDOff);
90 $compare = array('total_amount' => $amount);
91 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
92
93 $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff);
94 $compare = array('total_amount' => $financialTrxnAmount);
95 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
96 }
97
98 function testPayLater() {
99 // This is the path where our testing install resides.
100 // The rest of URL is defined in CiviSeleniumTestCase base class, in
101 // class attributes.
102 $this->open($this->sboxPath);
103 // Logging in. Remember to wait for page to load. In most cases,
104 // you can rely on 30000 as the value that allows your test to pass, however,
105 // sometimes your test might fail because of this. In such cases, it's better to pick one element
106 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
107 // page contents loaded and you can continue your test execution.
108 $this->webtestLogin();
109 $firstName = substr(sha1(rand()), 0, 7);
110 $lastName = 'Contributor';
111 $email = $firstName . "@example.com";
112 $amount = 100.00;
113 //Offline Pay Later Contribution
114 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
115 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
116 $this->waitForPageToLoad($this->getTimeoutMsec());
117 $elements = $this->parseURL();
118 $contId = $elements['queryString']['id'];
119 $this->select("contribution_status_id", "label=Completed");
120 $this->click("_qf_Contribution_upload");
121 $this->waitForPageToLoad($this->getTimeoutMsec());
122 //Assertions
123 $search = array('id' => $contId);
124 $compare = array('contribution_status_id' => 1);
125 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
126
127 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
128 $search = array( 'entity_id' => $lineItem );
129 $compare = array( 'status_id' => 1 );
130 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
131
132 $status = $this->_getPremiumActualCost($contId, 'Accounts Receivable', 'Payment Processor Account', NULL, "'civicrm_contribution'", "ft.status_id as status");
133 $this->assertEquals($status, '1', "Verify Completed Status");
134 }
135
136 function testChangePremium() {
137 // This is the path where our testing install resides.
138 // The rest of URL is defined in CiviSeleniumTestCase base class, in
139 // class attributes.
140 $this->open($this->sboxPath);
141 // Logging in. Remember to wait for page to load. In most cases,
142 // you can rely on 30000 as the value that allows your test to pass, however,
143 // sometimes your test might fail because of this. In such cases, it's better to pick one element
144 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
145 // page contents loaded and you can continue your test execution.
146 $this->webtestLogin();
147 $firstName = substr(sha1(rand()), 0, 7);
148 $lastName = 'Contributor';
149 $email = $firstName . "@example.com";
150 $from = 'Premiums';
151 $to = 'Premiums inventory';
152 $financialType = array(
153 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
154 'is_reserved' => 1,
155 'is_deductible' => 1,
156 );
157 $this->addeditFinancialType($financialType);
158 $this->select("account_relationship", "label=Cost of Sales Account is");
159 $this->select("financial_account_id", "label=$from");
160 $this->click("_qf_FinancialTypeAccount_next_new-botttom");
161 $this->waitForPageToLoad($this->getTimeoutMsec());
162 $this->select("account_relationship", "label=Premiums Inventory Account is");
163 $this->select("financial_account_id", "label=$to");
164 $this->click("_qf_FinancialTypeAccount_next-botttom");
165 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
166 $amount = 500;
167 $sku = 'SKU';
168 $price = 300;
169 $cost = 3.00;
170 $this->open($this->sboxPath . "civicrm/admin/contribute/managePremiums?action=add&reset=1");
171 // add premium
172 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
173
174 //add second premium
175 $premiumName2 = 'Premium'.substr(sha1(rand()), 0, 7);
176 $amount2 = 600;
177 $sku2 = 'SKU';
178 $price2 = 200;
179 $cost2 = 2.00;
180 $this->open($this->sboxPath . "civicrm/admin/contribute/managePremiums?action=add&reset=1");
181 $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']);
182
183 // add contribution with premium
184 $this->open($this->sboxPath . "civicrm/contribute/add?reset=1&action=add&context=standalone");
185 $this->waitForPageToLoad($this->getTimeoutMsec());
186
187 // create new contact using dialog
188 $this->webtestNewDialogContact($firstName, $lastName, $email);
189 // select financial type
190 $this->select( "financial_type_id", "value=1" );
191 // total amount
192 $this->type("total_amount", "100");
193 // fill Premium information
194 $this->click("xpath=//div[@id='Premium']");
195 $this->waitForElementPresent("product_name_0");
196 $this->select('product_name_0', "label=$premiumName ( $sku )");
197 // Clicking save.
198 $this->click("_qf_Contribution_upload");
199 $this->waitForPageToLoad($this->getTimeoutMsec());
200 // Is status message correct?
201 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
202 // verify if Membership is created
203 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
204 //click through to the Contribution edit screen
205 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
206 $this->waitForElementPresent("_qf_Contribution_upload-bottom");
207 $elements = $this->parseURL();
208 $contId = $elements['queryString']['id'];
209 $this->waitForElementPresent("product_name_0");
210 $this->select('product_name_0', "label=$premiumName2 ( $sku2 )");
211 // Clicking save.
212 $this->click("_qf_Contribution_upload");
213 $this->waitForPageToLoad($this->getTimeoutMsec());
214
215 //Assertions
216 $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'");
217 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
218
219 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
220 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
221 }
222
223 function testDeletePremium() {
224 // This is the path where our testing install resides.
225 // The rest of URL is defined in CiviSeleniumTestCase base class, in
226 // class attributes.
227 $this->open($this->sboxPath);
228 // Logging in. Remember to wait for page to load. In most cases,
229 // you can rely on 30000 as the value that allows your test to pass, however,
230 // sometimes your test might fail because of this. In such cases, it's better to pick one element
231 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
232 // page contents loaded and you can continue your test execution.
233 $this->webtestLogin();
234 $firstName = substr(sha1(rand()), 0, 7);
235 $lastName = 'Contributor';
236 $email = $firstName . "@example.com";
237 $from = 'Premiums';
238 $to = 'Premiums inventory';
239 $financialType = array(
240 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7),
241 'is_reserved' => 1,
242 'is_deductible' => 1,
243 );
244 $this->addeditFinancialType($financialType);
245 $this->select("account_relationship", "label=Cost of Sales Account is");
246 $this->select("financial_account_id", "label=$from");
247 $this->click("_qf_FinancialTypeAccount_next_new-botttom");
248 $this->waitForPageToLoad($this->getTimeoutMsec());
249 $this->select("account_relationship", "label=Premiums Inventory Account is");
250 $this->select("financial_account_id", "label=$to");
251 $this->click("_qf_FinancialTypeAccount_next-botttom");
252 $premiumName = 'Premium'.substr(sha1(rand()), 0, 7);
253 $amount = 500;
254 $sku = 'SKU';
255 $price = 300;
256 $cost = 3.00;
257 $this->open($this->sboxPath . "civicrm/admin/contribute/managePremiums?action=add&reset=1");
258 // add premium
259 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
260
261 // add contribution with premium
262 $this->open($this->sboxPath . "civicrm/contribute/add?reset=1&action=add&context=standalone");
263 $this->waitForPageToLoad($this->getTimeoutMsec());
264
265 // create new contact using dialog
266 $this->webtestNewDialogContact($firstName, $lastName, $email);
267 // select financial type
268 $this->select( "financial_type_id", "value=1" );
269 // total amount
270 $this->type("total_amount", "100");
271 // fill Premium information
272 $this->click("xpath=//div[@id='Premium']");
273 $this->waitForElementPresent("product_name_0");
274 $this->select('product_name_0', "label=$premiumName ( $sku )");
275 // Clicking save.
276 $this->click("_qf_Contribution_upload");
277 $this->waitForPageToLoad($this->getTimeoutMsec());
278 // Is status message correct?
279 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
280 // verify if Membership is created
281 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
282 //click through to the Contribution edit screen
283 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
284 $this->waitForElementPresent("_qf_Contribution_upload-bottom");
285 $elements = $this->parseURL();
286 $contId = $elements['queryString']['id'];
287 $this->waitForElementPresent("product_name_0");
288 $this->select('product_name_0', "value=0");
289 // Clicking save.
290 $this->click("_qf_Contribution_upload");
291 $this->waitForPageToLoad($this->getTimeoutMsec());
292
293 //Assertions
294 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
295 $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium");
296 }
297
298 function testChangePaymentInstrument() {
299 // This is the path where our testing install resides.
300 // The rest of URL is defined in CiviSeleniumTestCase base class, in
301 // class attributes.
302 $this->open($this->sboxPath);
303 // Logging in. Remember to wait for page to load. In most cases,
304 // you can rely on 30000 as the value that allows your test to pass, however,
305 // sometimes your test might fail because of this. In such cases, it's better to pick one element
306 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
307 // page contents loaded and you can continue your test execution.
308 $this->webtestLogin();
309 $firstName = substr(sha1(rand()), 0, 7);
310 $lastName = 'Contributor';
311 $email = $firstName . "@example.com";
312 $label = 'TEST'.substr(sha1(rand()), 0, 7);
313 $amount = 100.00;
314 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
315 $to = array_search('Accounts Receivable', $financialAccount);
316 $from = array_search('Deposit Bank Account', $financialAccount);
317 $this->addPaymentInstrument($label, $to);
318 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
319 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
320 $this->waitForPageToLoad($this->getTimeoutMsec());
321 $elements = $this->parseURL();
322 $contId = $elements['queryString']['id'];
323 //change payment processor to newly created value
324 $this->select("payment_instrument_id", "label=$label");
325 $this->click("_qf_Contribution_upload");
326 $this->waitForPageToLoad($this->getTimeoutMsec());
327 //Assertions
328 $totalAmount = $this->_getPremiumActualCost($contId, 'Payment Processor Account', 'Accounts Receivable');
329 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
330 }
331
332 function testRefundContribution() {
333 // This is the path where our testing install resides.
334 // The rest of URL is defined in CiviSeleniumTestCase base class, in
335 // class attributes.
336 $this->open($this->sboxPath);
337 // Logging in. Remember to wait for page to load. In most cases,
338 // you can rely on 30000 as the value that allows your test to pass, however,
339 // sometimes your test might fail because of this. In such cases, it's better to pick one element
340 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
341 // page contents loaded and you can continue your test execution.
342 $this->webtestLogin();
343 $firstName = substr(sha1(rand()), 0, 7);
344 $lastName = 'Contributor';
345 $email = $firstName . "@example.com";
346 $label = 'TEST'.substr(sha1(rand()), 0, 7);
347 $amount = 100.00;
348 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
349 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
350 $this->waitForPageToLoad($this->getTimeoutMsec());
351 //Contribution status
352 $this->select("contribution_status_id", "label=Refunded");
353 $elements = $this->parseURL();
354 $contId = $elements['queryString']['id'];
355 $this->click("_qf_Contribution_upload");
356 $this->waitForPageToLoad($this->getTimeoutMsec());
357
358 //Assertions
359 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
360 $search = array('entity_id' => $lineItem);
361 $compare = array(
362 'amount' => '100.00',
363 'status_id' => 1,
364 );
365 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
366 $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'");
367 $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.');
368 }
369
370 function testCancelPayLater() {
371 // This is the path where our testing install resides.
372 // The rest of URL is defined in CiviSeleniumTestCase base class, in
373 // class attributes.
374 $this->open($this->sboxPath);
375 // Logging in. Remember to wait for page to load. In most cases,
376 // you can rely on 30000 as the value that allows your test to pass, however,
377 // sometimes your test might fail because of this. In such cases, it's better to pick one element
378 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
379 // page contents loaded and you can continue your test execution.
380 $this->webtestLogin();
381 $firstName = substr(sha1(rand()), 0, 7);
382 $lastName = 'Contributor';
383 $email = $firstName . "@example.com";
384 $label = 'TEST'.substr(sha1(rand()), 0, 7);
385 $amount = 100.00;
386 $this->_testOfflineContribution($firstName, $lastName, $email, $amount, "Pending");
387 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
388 $this->waitForPageToLoad($this->getTimeoutMsec());
389 //Contribution status
390 $this->select("contribution_status_id", "label=Cancelled");
391 $elements = $this->parseURL();
392 $contId = $elements['queryString']['id'];
393 $this->click("_qf_Contribution_upload");
394 $this->waitForPageToLoad($this->getTimeoutMsec());
395
396 //Assertions
397 $search = array('id' => $contId);
398 $compare = array('contribution_status_id' => 3);
399 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
400 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
401 $itemParams = array(
402 'amount' => '-100.00',
403 'entity_id' => $lineItem,
404 );
405 $defaults = array();
406 $items = CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $defaults);
407 $this->assertEquals($items->amount, $itemParams['amount'], 'Verify Amount for financial Item');
408 $totalAmount = $this->_getPremiumActualCost($items->id, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'");
409 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
410 $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'");
411 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
412 }
413
414 function testChangeFinancialType() {
415 // This is the path where our testing install resides.
416 // The rest of URL is defined in CiviSeleniumTestCase base class, in
417 // class attributes.
418 $this->open($this->sboxPath);
419 // Logging in. Remember to wait for page to load. In most cases,
420 // you can rely on 30000 as the value that allows your test to pass, however,
421 // sometimes your test might fail because of this. In such cases, it's better to pick one element
422 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
423 // page contents loaded and you can continue your test execution.
424 $this->webtestLogin();
425 $firstName = substr(sha1(rand()), 0, 7);
426 $lastName = 'Contributor';
427 $email = $firstName . "@example.com";
428 $label = 'TEST'.substr(sha1(rand()), 0, 7);
429 $amount = 100.00;
430 $this->_testOfflineContribution($firstName, $lastName, $email, $amount);
431 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='Edit']");
432 $this->waitForPageToLoad($this->getTimeoutMsec());
433 //Contribution status
434 $this->select("financial_type_id", "value=3");
435 $elements = $this->parseURL();
436 $contId = $elements['queryString']['id'];
437 $this->click("_qf_Contribution_upload");
438 $this->waitForPageToLoad($this->getTimeoutMsec());
439
440 //Assertions
441 $search = array( 'id' => $contId );
442 $compare = array( 'financial_type_id' => 3 );
443 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
444
445 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
446 $itemParams = array(
447 'amount' => '-100.00',
448 'entity_id' => $lineItem,
449 );
450 $item1 = $item2 = array();
451 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item1);
452 $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item");
453 $itemParams['amount'] = '100.00';
454 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item2);
455 $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item");
456
457 $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'");
458 $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'");
459 $this->assertEquals($cValue1, "-100.00", "Verify Amount");
460 $this->assertEquals($fValue1, "-100.00", "Verify Amount");
461 $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'");
462 $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'");
463 $this->assertEquals($cValue2, "100.00", "Verify Amount");
464 $this->assertEquals($fValue2, "100.00", "Verify Amount");
465 }
466
467 function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
468 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
469 $query = "SELECT
470 {$select}
471 FROM civicrm_financial_trxn ft
472 INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}";
473 if ($entityTable) {
474 $query .= " AND eft.entity_table = {$entityTable}";
475 }
476 if (!empty($to)) {
477 $to = array_search($to, $financialAccount);
478 $query .= " AND ft.to_financial_account_id = {$to}";
479 }
480 if (!empty($from)) {
481 $from = array_search($from, $financialAccount);
482 $query .= " AND ft.from_financial_account_id = {$from}";
483 }
484 if (!empty($cost)) {
485 $query .= " AND eft.amount = {$cost}";
486 }
487 $result = CRM_Core_DAO::singleValueQuery($query);
488 return $result;
489 }
490
491 function _getFinancialTrxnAmount($contId) {
492 $query = "SELECT
493 SUM( ft.total_amount ) AS total
494 FROM civicrm_financial_trxn AS ft
495 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
496 WHERE ceft.entity_table = 'civicrm_contribution'
497 AND ceft.entity_id = {$contId}";
498 $result = CRM_Core_DAO::singleValueQuery($query);
499 return $result;
500 }
501
502 function _getFinancialItemAmount($contId) {
503 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
504 $query = "SELECT
505 SUM(amount)
506 FROM civicrm_financial_item
507 WHERE entity_table = 'civicrm_line_item'
508 AND entity_id = {$lineItem}";
509 $result = CRM_Core_DAO::singleValueQuery($query);
510 return $result;
511 }
512
513 function _getTotalContributedAmount($contId) {
514 $query = "SELECT
515 SUM(amount)
516 FROM civicrm_entity_financial_trxn
517 WHERE entity_table = 'civicrm_contribution'
518 AND entity_id = {$contId}";
519 $result = CRM_Core_DAO::singleValueQuery($query);
520 return $result;
521 }
522
523 function _testOfflineContribution($firstName, $lastName, $email, $amount, $status="Completed") {
524 // Go directly to the URL of the screen that you will be testing (New Contribution-standalone).
525 $this->open($this->sboxPath . "civicrm/contribute/add?reset=1&context=standalone");
526
527 // As mentioned before, waitForPageToLoad is not always reliable. Below, we're waiting for the submit
528 // button at the end of this page to show up, to make sure it's fully loaded.
529 $this->waitForElementPresent("_qf_Contribution_upload");
530
531 // Let's start filling the form with values.
532
533 // create new contact using dialog
534 $this->webtestNewDialogContact($firstName, $lastName, $email);
535
536 // select financial type
537 $this->select( "financial_type_id", "value=1" );
538
539 //Contribution status
540 $this->select("contribution_status_id", "label=$status");
541
542 // total amount
543 $this->type("total_amount", $amount);
544
545 // select payment instrument type
546 $this->select("payment_instrument_id", "label=Credit Card");
547
548 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
549
550 //Custom Data
551 //$this->click('CIVICRM_QFID_3_6');
552
553 // Clicking save.
554 $this->click("_qf_Contribution_upload");
555 $this->waitForPageToLoad($this->getTimeoutMsec());
556
557 // Is status message correct?
558 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
559
560 // verify if Membership is created
561 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
562
563 //click through to the Membership view screen
564 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']");
565 $this->waitForElementPresent("_qf_ContributionView_cancel-bottom");
566
567 $expected = array(
568 'Financial Type' => 'Donation',
569 'Total Amount' => '100.00',
570 'Contribution Status' => $status,
571 );
572 foreach ($expected as $label => $value) {
573 $this->verifyText("xpath=id('ContributionView')/div[2]/table[1]/tbody//tr/td[1][text()='$label']/../td[2]", preg_quote($value));
574 }
575 $this->click("_qf_ContributionView_cancel-top");
576 $this->waitForPageToLoad($this->getTimeoutMsec());
577 sleep(4);
578 }
579 }
580