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