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