Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-10-28-14-52-15
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / OfflineContributionTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License along with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviSeleniumTestCase.php';
29
30 class WebTest_Contribute_OfflineContributionTest extends CiviSeleniumTestCase {
31
32 protected function setUp() {
33 parent::setUp();
34 }
35
36 function testStandaloneContributeAdd() {
37 $this->webtestLogin();
38
39 // Create a contact to be used as soft creditor
40 $softCreditFname = substr(sha1(rand()), 0, 7);
41 $softCreditLname = substr(sha1(rand()), 0, 7);
42 $this->webtestAddContact($softCreditFname, $softCreditLname, FALSE);
43
44 //financial account for check
45 $this->openCiviPage("admin/options/payment_instrument", "group=payment_instrument&reset=1");
46 $financialAccount = $this->getText("xpath=//div[@id='payment_instrument']/div[2]/table/tbody//tr/td[1][text()='Check']/../td[3]");
47
48 // Add new Financial Account
49 $orgName = 'Alberta ' . substr(sha1(rand()), 0, 7);
50 $financialAccountTitle = 'Financial Account ' . substr(sha1(rand()), 0, 4);
51 $financialAccountDescription = "{$financialAccountTitle} Description";
52 $accountingCode = 1033;
53 $financialAccountType = 'Asset';
54 $taxDeductible = FALSE;
55 $isActive = FALSE;
56 $isTax = TRUE;
57 $taxRate = 9;
58 $isDefault = FALSE;
59
60 //Add new organisation
61 if ($orgName) {
62 $this->webtestAddOrganization($orgName);
63 }
64
65 $this->_testAddFinancialAccount($financialAccountTitle,
66 $financialAccountDescription,
67 $accountingCode,
68 $orgName,
69 $financialAccountType,
70 $taxDeductible,
71 $isActive,
72 $isTax,
73 $taxRate,
74 $isDefault
75 );
76
77 $firstName = 'John' . substr(sha1(rand()), 0, 7);
78 $lastName = 'Dsouza' . substr(sha1(rand()), 0, 7);
79 $this->webtestAddContact($firstName, $lastName);
80
81 $this->waitForElementPresent("css=li#tab_contribute a");
82 $this->click("css=li#tab_contribute a");
83 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
84 $this->click("link=Record Contribution (Check, Cash, EFT ...)");
85 $this->waitForPageToLoad($this->getTimeoutMsec());
86
87 // select financial type
88 $this->select("financial_type_id", "value=1");
89
90 // fill in Received Date
91 $this->webtestFillDate('receive_date');
92
93 // source
94 $this->type("source", "Mailer 1");
95
96 // total amount
97 $this->type("total_amount", "100");
98
99 // select payment instrument type = Check and enter chk number
100 $this->select("payment_instrument_id", "value=4");
101 $this->waitForElementPresent("check_number");
102 $this->type("check_number", "check #1041");
103
104 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
105
106 // create first soft credit
107 $this->webtestFillAutocomplete("{$softCreditLname}, {$softCreditFname}", 'soft_credit_contact_1');
108 $this->type("soft_credit_amount_1", "50");
109
110 // add second soft credit field
111 $this->click("addMoreSoftCredit");
112 $this->waitForElementPresent("soft_credit_amount_2");
113 // create new individual via soft credit
114 $softCreditSecondFname = substr(sha1(rand()), 0, 7);
115 $softCreditSecondLname = substr(sha1(rand()), 0, 7);
116 $this->webtestNewDialogContact($softCreditSecondFname, $softCreditSecondLname, NULL, 4, 'soft_credit_profiles_2', 'soft_credit_1');
117 // enter the second soft credit
118 $this->verifyText("soft_credit_amount_2", ""); // it should be blank cause first soft credit != total_amount
119 $this->type("soft_credit_amount_2", "100"); //the sum of the soft credit amounts can exceed total_amount
120
121 //Custom Data
122 // $this->click('CIVICRM_QFID_3_6');
123
124 //Additional Detail section
125 $this->click("AdditionalDetail");
126 $this->waitForElementPresent("thankyou_date");
127
128 $this->type("note", "This is a test note.");
129 $this->type("non_deductible_amount", "10.00");
130 $this->type("fee_amount", "0");
131 $this->type("net_amount", "0");
132 $this->type("invoice_id", time());
133 $this->webtestFillDate('thankyou_date');
134
135 //Honoree section
136 $this->click("Honoree");
137 $this->waitForElementPresent("honor_email");
138
139 $this->click("CIVICRM_QFID_1_2");
140 $this->select("honor_prefix_id", "label=Ms.");
141 $this->type("honor_first_name", "Foo");
142 $this->type("honor_last_name", "Bar");
143 $this->type("honor_email", "foo@bar.com");
144
145 //Premium section
146 $this->click("Premium");
147 $this->waitForElementPresent("fulfilled_date");
148 $this->select("product_name[0]", "label=Coffee Mug ( MUG-101 )");
149 $this->select("product_name[1]", "label=Black");
150 $this->webtestFillDate('fulfilled_date');
151
152 // Clicking save.
153 $this->click("_qf_Contribution_upload");
154 $this->waitForPageToLoad($this->getTimeoutMsec());
155
156 // Is status message correct?
157 //$this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
158
159 // verify if Contribution is created
160 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
161
162 //click through to the Contribution view screen
163 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']");
164 $this->waitForElementPresent("_qf_ContributionView_cancel-bottom");
165
166 $expected = array(
167 'Financial Type' => 'Donation',
168 'Total Amount' => '100.00',
169 'Contribution Status' => 'Completed',
170 'Paid By' => 'Check',
171 'Check Number' => 'check #1041',
172 'Non-deductible Amount' => '10.00',
173 'Received Into' => $financialAccount,
174 );
175
176 $this->waitForElementPresent("xpath=/html/body/div[3]/div/div[2]/div/div[3]/div/div[2]/div/div/div/form/div[2]");
177 foreach ($expected as $value) {
178 $this->verifyText("xpath=/html/body/div[3]/div/div[2]/div/div[3]/div/div[2]/div/div/div/form/div[2]", preg_quote($value));
179 }
180
181 // verify if soft credit was created successfully
182 $expected = array(
183 'Soft Credit To 1' => "{$softCreditFname} {$softCreditLname}",
184 'Soft Credit To 2' => "{$softCreditSecondFname} {$softCreditSecondLname}",
185 'Amount' => '50.00',
186 'Amount' => '100.00',
187 );
188
189 foreach ($expected as $value) {
190 $this->verifyText("css=table.crm-soft-credit-listing", preg_quote($value));
191 }
192
193 // go to first soft creditor contact view page
194 $this->click("css=table.crm-soft-credit-listing tbody tr td a");
195
196 // go to contribution tab
197 $this->waitForElementPresent("css=li#tab_contribute a");
198 $this->click("css=li#tab_contribute a");
199 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
200
201 // verify soft credit details
202 $expected = array(
203 3 => 'Donation',
204 2 => '50.00',
205 5 => 'Completed',
206 1 => "{$firstName} {$lastName}"
207 );
208 foreach ($expected as $value => $label) {
209 $this->verifyText("xpath=id('Search')/div[2]/table[2]/tbody/tr[2]/td[$value]", preg_quote($label));
210 }
211 }
212
213 function testDeductibleAmount() {
214 $this->webtestLogin();
215
216 //add authorize .net payment processor
217 $processorName = 'Webtest AuthNet' . substr(sha1(rand()), 0, 7);
218 $this->webtestAddPaymentProcessor($processorName, 'AuthNet');
219
220 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
221 $premiumName = 'test Premium' . substr(sha1(rand()), 0, 7);
222 $this->addPremium($premiumName, 'SKU', 3, 12, NULL, NULL);
223
224 $firstName = 'John' . substr(sha1(rand()), 0, 7);
225 $lastName = 'Dsouza' . substr(sha1(rand()), 0, 7);
226 $this->webtestAddContact($firstName, $lastName);
227
228 //scenario 1 : is_deductible = 0 and non deductible amount is entered
229 $scenario1 = array(
230 'financial_type' => 'Campaign Contribution',
231 'total_amount' => 111,
232 'non_deductible_amount' => 15,
233 'sort_name' => "$lastName, $firstName",
234 );
235 $this->_doOfflineContribution($scenario1, $firstName, $lastName, $processorName);
236
237 $checkScenario1 = array(
238 'From' => "{$firstName} {$lastName}",
239 'Financial Type' => 'Campaign Contribution',
240 'Total Amount' => 111,
241 'Non-deductible Amount' => 15,
242 'sort_name' => "$lastName, $firstName",
243 );
244 $this->_verifyAmounts($checkScenario1);
245
246 //scenario 2 : is_deductible = TRUE and premium is set and premium is greater than total amount
247 $scenario2 = array(
248 'financial_type' => 'Donation',
249 'total_amount' => 10,
250 'premium' => "{$premiumName} ( SKU )",
251 'sort_name' => "$lastName, $firstName",
252 );
253 $this->_doOfflineContribution($scenario2, $firstName, $lastName, $processorName);
254
255 $checkScenario2 = array(
256 'From' => "{$firstName} {$lastName}",
257 'Financial Type' => 'Donation',
258 'Total Amount' => 10,
259 'Non-deductible Amount' => 10,
260 'sort_name' => "$lastName, $firstName",
261 );
262 $this->_verifyAmounts($checkScenario2);
263
264 //scenario 3 : is_deductible = TRUE and premium is set and premium is less than total amount
265 $scenario3 = array(
266 'financial_type' => 'Donation',
267 'total_amount' => 123,
268 'premium' => "{$premiumName} ( SKU )",
269 'sort_name' => "$lastName, $firstName",
270 );
271 $this->_doOfflineContribution($scenario3, $firstName, $lastName, $processorName);
272
273 $checkScenario3 = array(
274 'From' => "{$firstName} {$lastName}",
275 'Financial Type' => 'Donation',
276 'Total Amount' => 123,
277 'Non-deductible Amount' => 12,
278 'sort_name' => "$lastName, $firstName",
279 );
280 $this->_verifyAmounts($checkScenario3);
281
282 //scenario 4 : is_deductible = TRUE and premium is not set
283 $scenario4 = array(
284 'financial_type' => 'Donation',
285 'total_amount' => 123,
286 'sort_name' => "$lastName, $firstName",
287 );
288 $this->_doOfflineContribution($scenario4, $firstName, $lastName, $processorName);
289
290 $checkScenario4 = array(
291 'From' => "{$firstName} {$lastName}",
292 'Financial Type' => 'Donation',
293 'Total Amount' => 123,
294 'Non-deductible Amount' => '0.00',
295 'sort_name' => "$lastName, $firstName",
296 );
297 $this->_verifyAmounts($checkScenario4);
298
299 //scenario 5 : is_deductible = FALSE, non_deductible_amount = the total amount
300 $scenario5 = array(
301 'financial_type' => 'Campaign Contribution',
302 'total_amount' => 555,
303 'sort_name' => "$lastName, $firstName",
304 );
305 $this->_doOfflineContribution($scenario5, $firstName, $lastName, $processorName);
306
307 $checkScenario5 = array(
308 'From' => "{$firstName} {$lastName}",
309 'Financial Type' => 'Campaign Contribution',
310 'Total Amount' => 555,
311 'Non-deductible Amount' => 555,
312 'sort_name' => "$lastName, $firstName",
313 );
314 $this->_verifyAmounts($checkScenario5);
315 }
316
317 //common function for doing offline contribution
318 function _doOfflineContribution($params, $firstName, $lastName, $processorName) {
319
320 $this->waitForElementPresent("css=li#tab_contribute a");
321 $this->click("css=li#tab_contribute a");
322 $this->waitForElementPresent("link=Submit Credit Card Contribution");
323 $this->click("link=Submit Credit Card Contribution");
324 $this->waitForPageToLoad($this->getTimeoutMsec());
325
326 // since we don't have live credentials we will switch to test mode
327 $url = $this->getLocation();
328 $url = str_replace('mode=live', 'mode=test', $url);
329 $this->open($url);
330 $this->waitForPageToLoad($this->getTimeoutMsec());
331
332 // start filling out contribution form
333 $this->waitForElementPresent('payment_processor_id');
334 $this->select('payment_processor_id', "label={$processorName}");
335
336 // select financial type
337 $this->select("financial_type_id", "label={$params['financial_type']}");
338
339 // total amount
340 $this->type("total_amount", "{$params['total_amount']}");
341
342 // enter credit card info on form
343 $this->webtestAddCreditCardDetails();
344
345 // billing address
346 $this->webtestAddBillingDetails($firstName, NULL, $lastName);
347
348 if ($nonDeductibleAmt = CRM_Utils_Array::value('non_deductible_amount', $params)) {
349 $this->click("AdditionalDetail");
350 $this->waitForElementPresent("thankyou_date");
351 $this->type("note", "This is a test note.");
352 $this->type("non_deductible_amount", "{$nonDeductibleAmt}");
353 }
354
355 if (CRM_Utils_Array::value('premium', $params)) {
356 //Premium section
357 $this->click("Premium");
358 $this->waitForElementPresent("fulfilled_date");
359 $this->select("product_name[0]", "label={$params['premium']}");
360 }
361 // Clicking save.
362 $this->click("_qf_Contribution_upload");
363 $this->waitForPageToLoad($this->getTimeoutMsec());
364
365 // Is status message correct?
366 $this->assertTrue($this->isTextPresent("The contribution record has been processed."), "Status message didn't show up after saving!");
367 }
368
369 //common function for verifing total_amount, and non_deductible_amount
370 function _verifyAmounts($verifyData) {
371 // since we are doing test contributions we need to search for test contribution and select first contribution
372 // record for the contact
373 $this->openCiviPage("contribute/search", "reset=1", "contribution_date_low");
374 $this->type("sort_name", $verifyData['sort_name']);
375
376 // select show test contributions
377 $this->click("contribution_test", "value=1");
378 $this->clickLink("_qf_Search_refresh", "xpath=//div[@id='contributionSearch']//table//tbody/tr[1]/td[11]/span/a[text()='View']");
379 $this->clickLink("xpath=//div[@id='contributionSearch']//table//tbody/tr[1]/td[11]/span/a[text()='View']", "_qf_ContributionView_cancel-bottom");
380
381 foreach ($verifyData as $label => $value) {
382 if ( $label == 'sort_name' ) {
383 continue;
384 }
385 $this->verifyText("xpath=//form[@id='ContributionView']//table/tbody/tr/td[text()='{$label}']/following-sibling::td",
386 preg_quote($value)
387 );
388 }
389
390 // now find contact and go back to contact summary
391 $this->openCiviPage("contact/search", "reset=1", "sort_name");
392 $this->type("sort_name", $verifyData['sort_name']);
393 $this->clickLink("_qf_Basic_refresh",
394 "xpath=//form[@id='Basic']/div[3]/div[1]/div[2]/table/tbody/tr[1]/td[11]/span/a[text()='View']");
395
396 $this->clickLink("xpath=//form[@id='Basic']/div[3]/div[1]/div[2]/table/tbody/tr[1]/td[11]/span/a[text()='View']",
397 'crm-contact-actions-link');
398 }
399
400 function testOnlineContributionWithZeroAmount() {
401 $this->webtestLogin();
402
403 // Create a contact to be used as soft creditor
404 $firstName = 'John' . substr(sha1(rand()), 0, 7);
405 $lastName = 'Peterson' . substr(sha1(rand()), 0, 7);
406 $this->webtestAddContact($firstName, $lastName);
407 $this->waitForElementPresent("css=li#tab_contribute a");
408 $this->click("css=li#tab_contribute a");
409 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
410 $this->clickLink("link=Record Contribution (Check, Cash, EFT ...)");
411
412 // select financial type
413 $this->select("financial_type_id", "value=1");
414
415 // total amount
416 $this->type("total_amount", "0.00");
417
418 // select payment instrument
419 $this->select("payment_instrument_id", "value=1");
420
421 $this->type("trxn_id", "X20901X1" . rand(100, 10000));
422 $this->clickLink('_qf_Contribution_upload-bottom');
423 $this->waitForText("crm-notification-container", "The contribution record has been saved.");
424
425 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']");
426 $this->clickLink("xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']");
427 $expected = array(
428 'Financial Type' => 'Donation',
429 'Total Amount' => '0.00',
430 'Contribution Status' => 'Completed',
431 'Paid By' => 'Credit Card'
432 );
433 $this->webtestVerifyTabularData($expected);
434 }
435 }