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