Merge pull request #7271 from jitendrapurohit/CRM-17613
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / StandaloneAddTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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_StandaloneAddTest
31 */
32 class WebTest_Contribute_StandaloneAddTest 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 // Add new Financial Account
47 $orgName = 'Alberta ' . substr(sha1(rand()), 0, 7);
48 $financialAccountTitle = 'Financial Account ' . substr(sha1(rand()), 0, 4);
49 $financialAccountDescription = "{$financialAccountTitle} Description";
50 $accountingCode = 1033;
51 $financialAccountType = 'Liability';
52 $taxDeductible = TRUE;
53 $isActive = FALSE;
54 $isTax = TRUE;
55 $taxRate = 10.00;
56 $isDefault = FALSE;
57
58 //Add new organisation
59 $this->webtestAddOrganization($orgName);
60
61 $this->_testAddFinancialAccount($financialAccountTitle,
62 $financialAccountDescription,
63 $accountingCode,
64 $orgName,
65 $financialAccountType,
66 $taxDeductible,
67 $isActive,
68 $isTax,
69 $taxRate,
70 $isDefault
71 );
72
73 //Add new Financial Type
74 $financialType['name'] = 'Taxable FinancialType ' . substr(sha1(rand()), 0, 4);
75 $financialType['is_deductible'] = TRUE;
76 $financialType['is_reserved'] = FALSE;
77 $this->addeditFinancialType($financialType);
78
79 // Assign the created Financial Account $financialAccountTitle to $financialType
80 $this->click("xpath=id('ltype')/div/table/tbody/tr/td[1]/div[text()='$financialType[name]']/../../td[7]/span/a[text()='Accounts']");
81 $this->waitForElementPresent("xpath=//div[@class='ui-dialog-buttonset']/button/span[text()=' Assign Account']");
82 $this->click("xpath=//div[@class='ui-dialog-buttonset']/button/span[text()=' Assign Account']");
83 $this->waitForElementPresent("xpath=//div[@class='ui-dialog-buttonset']/button/span[text()='Save']");
84 $this->select('account_relationship', "label=Sales Tax Account is");
85 $this->select('financial_account_id', "label=" . $financialAccountTitle);
86 $this->click("xpath=//div[@class='ui-dialog-buttonset']/button/span[text()='Save']");
87 $this->waitForElementPresent("xpath=//div[@class='ui-dialog-buttonset']/button/span[text()=' Assign Account']");
88
89 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
90
91 // create new contact using dialog
92 $contact = $this->createDialogContact();
93
94 // select financial type
95 $this->select("financial_type_id", "label=" . $financialType['name']);
96
97 // fill in Received Date
98 $this->webtestFillDate('receive_date');
99
100 // source
101 $this->type("source", "Mailer 1");
102
103 // total amount
104 $this->type("total_amount", "100");
105
106 // select payment instrument type = Check and enter chk number
107 $this->select("payment_instrument_id", "label=Cash");
108 $this->click("is_email_receipt");
109 $this->assertTrue($this->isChecked("is_email_receipt"), 'Send Receipt checkbox should be checked.');
110 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
111
112 // soft credit
113 $this->webtestFillAutocomplete("{$softCreditLname}, {$softCreditFname}", 's2id_soft_credit_contact_id_1');
114 $this->type("soft_credit_amount_1", "100");
115
116 //Additional Detail section
117 $this->click("AdditionalDetail");
118 $this->waitForElementPresent("thankyou_date");
119
120 $this->type("note", "This is a test note.");
121 $this->type("invoice_id", time());
122 $this->webtestFillDate('thankyou_date');
123
124 //Premium section
125 $this->click("Premium");
126 $this->waitForElementPresent("fulfilled_date");
127 $this->select("product_name[0]", "label=Coffee Mug ( MUG-101 )");
128 $this->select("product_name[1]", "label=Black");
129 $this->webtestFillDate('fulfilled_date');
130
131 // Clicking save.
132 $this->click("_qf_Contribution_upload");
133 // Ask for confirmation to send a receipt to the contributor on 'is_email_reciept' check
134 $this->assertTrue((bool) preg_match("/^Click OK to save this contribution record AND send a receipt to the contributor now./", $this->getConfirmation()));
135 $this->chooseOkOnNextConfirmation();
136 $this->waitForPageToLoad($this->getTimeoutMsec());
137
138 // Is status message correct?
139 $this->waitForText("crm-notification-container", "The contribution record has been saved.");
140
141 // verify if Membership is created
142 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
143
144 $contriID = $this->urlArg('id', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
145 $contactID = $this->urlArg('cid', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
146
147 //click through to the Membership view screen
148 $this->click("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
149 $this->waitForElementPresent("_qf_ContributionView_cancel-bottom");
150
151 $expected = array(
152 'Financial Type' => $financialType['name'],
153 'Total Amount' => '$ 110.00',
154 'Contribution Status' => 'Completed',
155 'Payment Method' => 'Check',
156 'Check Number' => 'check #1041',
157 'Paid By' => 'Cash',
158 );
159
160 foreach ($expected as $label => $value) {
161 $this->verifyText("xpath=id('ContributionView')/div[2]/table[1]/tbody//tr/td[1][text()='$label']/../td[2]", preg_quote($value));
162 }
163
164 // verify if soft credit was created successfully
165 $expected = array(
166 'Soft Credit To' => "{$softCreditFname} {$softCreditLname}",
167 'Amount' => '100.00',
168 );
169
170 foreach ($expected as $value) {
171 $this->verifyText("css=table.crm-soft-credit-listing", preg_quote($value));
172 }
173
174 // go to first soft creditor contact view page
175 $this->clickLink("css=table.crm-soft-credit-listing tbody tr td a");
176
177 // go to contribution tab
178 $this->waitForElementPresent("css=li#tab_contribute a");
179 $this->click("css=li#tab_contribute a");
180 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
181 $this->verifyText("xpath=id('Search')/div[2]/table[2]/tbody/tr[2]/td[1]/a", preg_quote($contact['display_name']));
182 // verify soft credit details
183 $expected = array(
184 4 => $financialType['name'],
185 2 => '100.00',
186 6 => 'Completed',
187 );
188 foreach ($expected as $value => $label) {
189 $this->verifyText("xpath=id('Search')/div[2]/table[2]/tbody/tr[2]/td[$value]", preg_quote($label));
190 }
191
192 // CRM-17418 fix: Now cancel the contribution
193 $this->openCiviPage("contact/view/contribution", "reset=1&action=update&id=$contriID&cid=$contactID&context=contribution", "_qf_Contribution_upload");
194 $this->select('contribution_status_id', 'label=Cancelled');
195 $this->waitForElementPresent('cancel_reason');
196 $this->click("_qf_Contribution_upload");
197 // Is status message correct?
198 $this->waitForText("crm-notification-container", "The contribution record has been saved.");
199
200 // 1. On first completed contribution the contribution_amount = 100 and Tax Amount = 10
201 // 2. After Cancellation contribution_amount = -100 and Tax Amount = -10
202 // So the sum of all the 4 created financial item's amount would be 0
203 $query = "SELECT SUM( amount ) FROM `civicrm_financial_item` WHERE entity_id = %1";
204 $sum = CRM_Core_DAO::singleValueQuery($query, array(1 => array($contriID, 'Integer')));
205 $this->assertEquals($sum, 0.00);
206 }
207
208 public function testfinancialTypeSearch() {
209 $this->webtestLogin();
210
211 $financialType = array(
212 'name' => 'Financial type' . substr(sha1(rand()), 0, 7),
213 'is_reserved' => FALSE,
214 'is_deductible' => FALSE,
215 );
216
217 $this->addeditFinancialType($financialType);
218 $this->addStandaloneContribution($financialType);
219 $this->addStandaloneContribution($financialType);
220
221 $this->openCiviPage("contribute/search", "reset=1", "_qf_Search_refresh");
222 // select group
223 $this->select("financial_type_id", "label={$financialType['name']}");
224 $this->clickLink("_qf_Search_refresh");
225 $this->assertElementContainsText("xpath=//div[@id='search-status']/table/tbody/tr[1]/td[1]", "2 Result");
226 $this->assertElementContainsText("xpath=//div[@id='search-status']/table/tbody/tr[1]/td[2]", "Financial Type ID In {$financialType['name']}");
227
228 $this->openCiviPage("contact/search/advanced", "reset=1", "_qf_Advanced_refresh-top");
229 $this->clickAjaxLink('CiviContribute', "financial_type_id");
230
231 // select group
232 $this->select("financial_type_id", "label={$financialType['name']}");
233 $this->clickLink("_qf_Advanced_refresh-top");
234 $this->assertElementContainsText("xpath=//div[@id='search-status']//table/tbody/tr[1]/td[1]", "2 Contacts");
235 $this->assertElementContainsText("xpath=//div[@id='search-status']//table/tbody/tr[1]/td[2]", "Financial Type ID In {$financialType['name']}");
236 }
237
238 /**
239 * @param $financialType
240 */
241 public function addStandaloneContribution($financialType) {
242
243 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
244
245 // create new contact using dialog
246 $this->createDialogContact();
247
248 // select financial type
249 $this->select("financial_type_id", "label={$financialType['name']}");
250
251 // fill in Received Date
252 $this->webtestFillDate('receive_date');
253
254 // source
255 $this->type("source", "Mailer 1");
256
257 // total amount
258 $this->type("total_amount", "100");
259
260 // select payment instrument type = Check and enter chk number
261 $this->select("payment_instrument_id", "value=4");
262 $this->waitForElementPresent("check_number");
263 $this->type("check_number", "check #1041");
264
265 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
266
267 //Additional Detail section
268 $this->click("AdditionalDetail");
269 $this->waitForElementPresent("thankyou_date");
270
271 $this->type("note", "This is a test note.");
272 $this->type("non_deductible_amount", "10");
273 $this->type("fee_amount", "0");
274 $this->type("net_amount", "0");
275 $this->type("invoice_id", time());
276 $this->webtestFillDate('thankyou_date');
277
278 // Clicking save.
279 $this->clickLink("_qf_Contribution_upload");
280
281 // Is status message correct?
282 $this->checkCRMAlert("The contribution record has been saved.");
283
284 // verify if Membership is created
285 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
286
287 //click through to the Membership view screen
288 $this->click("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
289 $this->waitForElementPresent("_qf_ContributionView_cancel-bottom");
290
291 $expected = array(
292 'Financial Type' => $financialType['name'],
293 'Total Amount' => '$ 100.00',
294 'Contribution Status' => 'Completed',
295 'Payment Method' => 'Check',
296 'Check Number' => 'check #1041',
297 );
298
299 foreach ($expected as $label => $value) {
300 $this->verifyText("xpath=id('ContributionView')/div[2]/table[1]/tbody//tr/td[1][text()='$label']/../td[2]", preg_quote($value));
301 }
302 }
303
304 public function testAjaxCustomGroupLoad() {
305 $this->webtestLogin();
306 $triggerElement = array('name' => 'financial_type_id', 'type' => 'select');
307 $customSets = array(
308 array('entity' => 'Contribution', 'subEntity' => 'Donation', 'triggerElement' => $triggerElement),
309 array('entity' => 'Contribution', 'subEntity' => 'Member Dues', 'triggerElement' => $triggerElement),
310 );
311
312 $pageUrl = array('url' => 'contribute/add', 'args' => 'reset=1&action=add&context=standalone');
313 $this->customFieldSetLoadOnTheFlyCheck($customSets, $pageUrl);
314 }
315
316 }