0e177fb7c70f5afaef24381e3970a288e9d2aec7
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / OfflineContributionTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
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 $captureScreenshotOnFailure = TRUE;
33 protected $screenshotPath = '/var/www/api.dev.civicrm.org/public/sc';
34 protected $screenshotUrl = 'http://api.dev.civicrm.org/sc/';
35
36 protected function setUp() {
37 parent::setUp();
38 }
39
40 function testStandaloneContributeAdd() {
41 $this->webtestLogin();
42
43 // Create a contact to be used as soft creditor
44 $softCreditFname = substr(sha1(rand()), 0, 7);
45 $softCreditLname = substr(sha1(rand()), 0, 7);
46 $this->webtestAddContact( $softCreditFname, $softCreditLname, false );
47
48 //financial account for check
49 $this->openCiviPage("admin/options/payment_instrument", "group=payment_instrument&reset=1");
50 $financialAccount = $this->getText("xpath=//div[@id='payment_instrument']/div[2]/table/tbody//tr/td[1][text()='Check']/../td[3]");
51
52 // Add new Financial Account
53 $orgName = 'Alberta '.substr(sha1(rand()), 0, 7);
54 $financialAccountTitle = 'Financial Account '.substr(sha1(rand()), 0, 4);
55 $financialAccountDescription = "{$financialAccountTitle} Description";
56 $accountingCode = 1033;
57 $financialAccountType = 'Asset';
58 $taxDeductible = FALSE;
59 $isActive = FALSE;
60 $isTax = TRUE;
61 $taxRate = 9;
62 $isDefault = FALSE;
63
64 //Add new organisation
65 if($orgName) {
66 $this->webtestAddOrganization($orgName);
67 }
68
69 $this->_testAddFinancialAccount($financialAccountTitle,
70 $financialAccountDescription,
71 $accountingCode,
72 $orgName,
73 $financialAccountType,
74 $taxDeductible,
75 $isActive,
76 $isTax,
77 $taxRate,
78 $isDefault
79 );
80
81 $firstName = 'John'.substr(sha1(rand()), 0, 7);
82 $lastName = 'Dsouza'.substr(sha1(rand()), 0, 7);
83 $this->webtestAddContact($firstName, $lastName);
84
85 $this->waitForElementPresent("css=li#tab_contribute a");
86 $this->click("css=li#tab_contribute a");
87 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
88 $this->click("link=Record Contribution (Check, Cash, EFT ...)");
89 $this->waitForPageToLoad($this->getTimeoutMsec());
90
91 // select financial type
92 $this->select("financial_type_id", "value=1");
93
94 // fill in Received Date
95 $this->webtestFillDate('receive_date');
96
97 // source
98 $this->type("source", "Mailer 1");
99
100 // total amount
101 $this->type("total_amount", "100");
102
103 // select payment instrument type = Check and enter chk number
104 $this->select("payment_instrument_id", "value=4");
105 $this->waitForElementPresent("check_number");
106 $this->type("check_number", "check #1041");
107
108 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
109
110 // soft credit
111 $this->click("soft_credit_to");
112 $this->type("soft_credit_to", $softCreditFname);
113 $this->typeKeys("soft_credit_to", $softCreditFname);
114
115 $this->waitForElementPresent("css=div.ac_results-inner li");
116 $this->click("css=div.ac_results-inner li");
117
118 //Custom Data
119 // $this->click('CIVICRM_QFID_3_6');
120
121 //Additional Detail section
122 $this->click("AdditionalDetail");
123 $this->waitForElementPresent("thankyou_date");
124
125 $this->type("note", "This is a test note.");
126 $this->type("non_deductible_amount", "10.00");
127 $this->type("fee_amount", "0");
128 $this->type("net_amount", "0");
129 $this->type("invoice_id", time());
130 $this->webtestFillDate('thankyou_date');
131
132 //Honoree section
133 $this->click("Honoree");
134 $this->waitForElementPresent("honor_email");
135
136 $this->click("CIVICRM_QFID_1_2");
137 $this->select("honor_prefix_id", "label=Ms.");
138 $this->type("honor_first_name", "Foo");
139 $this->type("honor_last_name", "Bar");
140 $this->type("honor_email", "foo@bar.com");
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 $this->waitForPageToLoad($this->getTimeoutMsec());
152
153 // Is status message correct?
154 $this->assertTrue($this->isTextPresent("The contribution record has been saved."), "Status message didn't show up after saving!");
155
156 // verify if Membership is created
157 $this->waitForElementPresent( "xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']" );
158
159 //click through to the Membership view screen
160 $this->click( "xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']" );
161 $this->waitForElementPresent("_qf_ContributionView_cancel-bottom");
162
163 $expected = array('Financial Type' => 'Donation',
164
165 'Total Amount' => '100.00',
166 'Contribution Status' => 'Completed',
167 'Paid By' => 'Check',
168 'Check Number' => 'check #1041',
169 'Non-deductible Amount' => '10.00',
170 'Received Into' => $financialAccount,
171 'Soft Credit To' => "{$softCreditFname} {$softCreditLname}"
172 );
173 foreach($expected as $label => $value) {
174 $this->verifyText("xpath=id('ContributionView')/div[2]/table[1]/tbody//tr/td[1][text()='$label']/../td[2]", preg_quote($value));
175 }
176
177 // go to soft creditor contact view page
178 $this->click("xpath=id('ContributionView')/div[2]/table[1]/tbody//tr/td[1][text()='Soft Credit To']/../td[2]/a[text()='{$softCreditFname} {$softCreditLname}']");
179
180 // go to contribution tab
181 $this->waitForElementPresent("css=li#tab_contribute a");
182 $this->click("css=li#tab_contribute a");
183 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
184
185 // verify soft credit details
186 $expected = array( 3 => 'Donation',
187
188 2 => '100.00',
189 5 => 'Completed',
190 1 => "{$firstName} {$lastName}"
191 );
192 foreach($expected as $value => $label) {
193 $this->verifyText("xpath=id('Search')/div[2]/table[2]/tbody/tr[2]/td[$value]", preg_quote($label));
194 }
195 }
196
197 function testDeductibleAmount() {
198 $this->webtestLogin();
199
200 //add authorize .net payment processor
201 $processorName = 'Webtest AuthNet' . substr(sha1(rand()), 0, 7);
202 $this->webtestAddPaymentProcessor($processorName, 'AuthNet');
203
204 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
205 $premiumName = 'test Premium' . substr(sha1(rand()), 0, 7);
206 $this->addPremium($premiumName, 'SKU', 3, 12, NULL, NULL);
207
208 $firstName = 'John'.substr(sha1(rand()), 0, 7);
209 $lastName = 'Dsouza'.substr(sha1(rand()), 0, 7);
210 $this->webtestAddContact($firstName, $lastName);
211
212 //scenario 1 : is_deductible = 0 and non deductible amount is entered
213 $scenario1 = array(
214 'financial_type' => 'Campaign Contribution',
215 'total_amount' => 111,
216 'non_deductible_amount' => 15
217 );
218 $this->_doOfflineContribution($scenario1, $firstName, $lastName, $processorName);
219
220 $checkScenario1 = array(
221 'From' => "{$firstName} {$lastName}",
222 'Financial Type' => 'Campaign Contribution',
223 'Total Amount' => 111,
224 'Non-deductible Amount' => 15
225 );
226 $this->_verifyAmounts($checkScenario1);
227
228 //scenario 2 : is_deductible = TRUE and premium is set and premium is greater than total amount
229 $scenario2 = array(
230 'financial_type' => 'Donation',
231 'total_amount' => 10,
232 'premium' => "{$premiumName} ( SKU )"
233 );
234 $this->_doOfflineContribution($scenario2, $firstName, $lastName, $processorName);
235
236 $checkScenario2 = array(
237 'From' => "{$firstName} {$lastName}",
238 'Financial Type' => 'Donation',
239 'Total Amount' => 10,
240 'Non-deductible Amount' => 10
241 );
242 $this->_verifyAmounts($checkScenario2);
243
244 //scenario 3 : is_deductible = TRUE and premium is set and premium is less than total amount
245 $scenario3 = array(
246 'financial_type' => 'Donation',
247 'total_amount' => 123,
248 'premium' => "{$premiumName} ( SKU )"
249 );
250 $this->_doOfflineContribution($scenario3, $firstName, $lastName, $processorName);
251
252 $checkScenario3 = array(
253 'From' => "{$firstName} {$lastName}",
254 'Financial Type' => 'Donation',
255 'Total Amount' => 123,
256 'Non-deductible Amount' => 12
257 );
258 $this->_verifyAmounts($checkScenario3);
259
260 //scenario 4 : is_deductible = TRUE and premium is not set
261 $scenario4 = array(
262 'financial_type' => 'Donation',
263 'total_amount' => 123,
264 );
265 $this->_doOfflineContribution($scenario4, $firstName, $lastName, $processorName);
266
267 $checkScenario4 = array(
268 'From' => "{$firstName} {$lastName}",
269 'Financial Type' => 'Donation',
270 'Total Amount' => 123,
271 'Non-deductible Amount' => '0.00'
272 );
273 $this->_verifyAmounts($checkScenario4);
274
275 //scenario 5 : is_deductible = FALSE, non_deductible_amount = the total amount
276 $scenario5 = array(
277 'financial_type' => 'Campaign Contribution',
278 'total_amount' => 555,
279 );
280 $this->_doOfflineContribution($scenario5, $firstName, $lastName, $processorName);
281
282 $checkScenario5 = array(
283 'From' => "{$firstName} {$lastName}",
284 'Financial Type' => 'Campaign Contribution',
285 'Total Amount' => 555,
286 'Non-deductible Amount' => 555
287 );
288 $this->_verifyAmounts($checkScenario5);
289 }
290
291 //common function for doing offline contribution
292 function _doOfflineContribution($params, $firstName, $lastName, $processorName) {
293
294 $this->waitForElementPresent("css=li#tab_contribute a");
295 $this->click("css=li#tab_contribute a");
296 $this->waitForElementPresent("link=Submit Credit Card Contribution");
297 $this->click("link=Submit Credit Card Contribution");
298 $this->waitForPageToLoad($this->getTimeoutMsec());
299
300 // since we don't have live credentials we will switch to test mode
301 $url = $this->getLocation();
302 $url = str_replace('mode=live', 'mode=test', $url);
303 $this->open($url);
304 $this->waitForPageToLoad($this->getTimeoutMsec());
305
306 // start filling out contribution form
307 $this->waitForElementPresent('payment_processor_id');
308 $this->select('payment_processor_id', "label={$processorName}");
309
310 // select financial type
311 $this->select("financial_type_id", "label={$params['financial_type']}");
312
313 // total amount
314 $this->type("total_amount", "{$params['total_amount']}");
315
316 // enter credit card info on form
317 $this->webtestAddCreditCardDetails();
318
319 // billing address
320 $this->webtestAddBillingDetails($firstName, NULL, $lastName);
321
322 if ($nonDeductibleAmt = CRM_Utils_Array::value('non_deductible_amount', $params)) {
323 $this->click("AdditionalDetail");
324 $this->waitForElementPresent("thankyou_date");
325 $this->type("note", "This is a test note.");
326 $this->type("non_deductible_amount", "{$nonDeductibleAmt}");
327 }
328
329 if (CRM_Utils_Array::value('premium', $params)) {
330 //Premium section
331 $this->click("Premium");
332 $this->waitForElementPresent("fulfilled_date");
333 $this->select("product_name[0]", "label={$params['premium']}");
334 }
335 // Clicking save.
336 $this->click("_qf_Contribution_upload");
337 $this->waitForPageToLoad($this->getTimeoutMsec());
338
339 // Is status message correct?
340 $this->assertTrue($this->isTextPresent("The contribution record has been processed."), "Status message didn't show up after saving!");
341 }
342
343 //common function for verifing total_amount, and non_deductible_amount
344 function _verifyAmounts($verifyData) {
345 $this->waitForElementPresent( "xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']" );
346 $this->click( "xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']" );
347 $this->waitForPageToLoad($this->getTimeoutMsec());
348
349 foreach ($verifyData as $label => $value) {
350 $this->verifyText("xpath=//form[@id='ContributionView']//table/tbody/tr/td[text()='{$label}']/following-sibling::td",
351 preg_quote($value)
352 );
353 }
354
355 $this->click("_qf_ContributionView_cancel-top");
356 $this->waitForPageToLoad($this->getTimeoutMsec());
357 }
358 }