Merge branch 'aydun-CRM-16203' into 4.6
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / StandaloneAddTest.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_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 'Paid By' => 'Cash',
156 );
157
158 foreach ($expected as $label => $value) {
159 $this->verifyText("xpath=id('ContributionView')/div[2]/table[1]/tbody//tr/td[1][text()='$label']/../td[2]", preg_quote($value));
160 }
161
162 // verify if soft credit was created successfully
163 $expected = array(
164 'Soft Credit To' => "{$softCreditFname} {$softCreditLname}",
165 'Amount' => '100.00',
166 );
167
168 foreach ($expected as $value) {
169 $this->verifyText("css=table.crm-soft-credit-listing", preg_quote($value));
170 }
171
172 // go to first soft creditor contact view page
173 $this->clickLink("css=table.crm-soft-credit-listing tbody tr td a");
174
175 // go to contribution tab
176 $this->waitForElementPresent("css=li#tab_contribute a");
177 $this->click("css=li#tab_contribute a");
178 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
179 $this->verifyText("xpath=id('Search')/div[2]/table[2]/tbody/tr[2]/td[1]/a", preg_quote($contact['display_name']));
180 // verify soft credit details
181 $expected = array(
182 4 => $financialType['name'],
183 2 => '100.00',
184 6 => 'Completed',
185 );
186 foreach ($expected as $value => $label) {
187 $this->verifyText("xpath=id('Search')/div[2]/table[2]/tbody/tr[2]/td[$value]", preg_quote($label));
188 }
189
190 // CRM-17418 fix: Now cancel the contribution
191 $this->openCiviPage("contact/view/contribution", "reset=1&action=update&id=$contriID&cid=$contactID&context=contribution", "_qf_Contribution_upload");
192 $this->select('contribution_status_id', 'label=Cancelled');
193 $this->waitForElementPresent('cancel_reason');
194 $this->click("_qf_Contribution_upload");
195 // Is status message correct?
196 $this->waitForText("crm-notification-container", "The contribution record has been saved.");
197
198 // 1. On first completed contribution the contribution_amount = 100 and Tax Amount = 10
199 // 2. After Cancellation contribution_amount = -100 and Tax Amount = -10
200 // So the sum of all the 4 created financial item's amount would be 0
201 $query = "SELECT SUM( amount ) FROM `civicrm_financial_item` WHERE entity_id = %1";
202 $sum = CRM_Core_DAO::singleValueQuery($query, array(1 => array($contriID, 'Integer')));
203 $this->assertEquals($sum, 0.00);
204 }
205
206 public function testfinancialTypeSearch() {
207 $this->webtestLogin();
208
209 $financialType = array(
210 'name' => 'Financial type' . substr(sha1(rand()), 0, 7),
211 'is_reserved' => FALSE,
212 'is_deductible' => FALSE,
213 );
214
215 $this->addeditFinancialType($financialType);
216 $this->addStandaloneContribution($financialType);
217 $this->addStandaloneContribution($financialType);
218
219 $this->openCiviPage("contribute/search", "reset=1", "_qf_Search_refresh");
220 // select group
221 $this->select("financial_type_id", "label={$financialType['name']}");
222 $this->clickLink("_qf_Search_refresh");
223 $this->assertElementContainsText("xpath=//div[@id='search-status']/table/tbody/tr[1]/td[1]", "2 Result");
224 $this->assertElementContainsText("xpath=//div[@id='search-status']/table/tbody/tr[1]/td[2]", "Financial Type ID In {$financialType['name']}");
225
226 $this->openCiviPage("contact/search/advanced", "reset=1", "_qf_Advanced_refresh-top");
227 $this->clickAjaxLink('CiviContribute', "financial_type_id");
228
229 // select group
230 $this->select("financial_type_id", "label={$financialType['name']}");
231 $this->clickLink("_qf_Advanced_refresh-top");
232 $this->assertElementContainsText("xpath=//div[@id='search-status']//table/tbody/tr[1]/td[1]", "2 Contacts");
233 $this->assertElementContainsText("xpath=//div[@id='search-status']//table/tbody/tr[1]/td[2]", "Financial Type ID In {$financialType['name']}");
234 }
235
236 /**
237 * @param $financialType
238 */
239 public function addStandaloneContribution($financialType) {
240
241 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
242
243 // create new contact using dialog
244 $this->createDialogContact();
245
246 // select financial type
247 $this->select("financial_type_id", "label={$financialType['name']}");
248
249 // fill in Received Date
250 $this->webtestFillDate('receive_date');
251
252 // source
253 $this->type("source", "Mailer 1");
254
255 // total amount
256 $this->type("total_amount", "100");
257
258 // select payment instrument type = Check and enter chk number
259 $this->select("payment_instrument_id", "value=4");
260 $this->waitForElementPresent("check_number");
261 $this->type("check_number", "check #1041");
262
263 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
264
265 //Additional Detail section
266 $this->click("AdditionalDetail");
267 $this->waitForElementPresent("thankyou_date");
268
269 $this->type("note", "This is a test note.");
270 $this->type("non_deductible_amount", "10");
271 $this->type("fee_amount", "0");
272 $this->type("net_amount", "0");
273 $this->type("invoice_id", time());
274 $this->webtestFillDate('thankyou_date');
275
276 // Clicking save.
277 $this->clickLink("_qf_Contribution_upload");
278
279 // Is status message correct?
280 $this->checkCRMAlert("The contribution record has been saved.");
281
282 // verify if Membership is created
283 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
284
285 //click through to the Membership view screen
286 $this->click("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
287 $this->waitForElementPresent("_qf_ContributionView_cancel-bottom");
288
289 $expected = array(
290 'Financial Type' => $financialType['name'],
291 'Total Amount' => '$ 100.00',
292 'Contribution Status' => 'Completed',
293 'Paid By' => 'Check',
294 'Check Number' => 'check #1041',
295 );
296
297 foreach ($expected as $label => $value) {
298 $this->verifyText("xpath=id('ContributionView')/div[2]/table[1]/tbody//tr/td[1][text()='$label']/../td[2]", preg_quote($value));
299 }
300 }
301
302 public function testAjaxCustomGroupLoad() {
303 $this->webtestLogin();
304 $triggerElement = array('name' => 'financial_type_id', 'type' => 'select');
305 $customSets = array(
306 array('entity' => 'Contribution', 'subEntity' => 'Donation', 'triggerElement' => $triggerElement),
307 array('entity' => 'Contribution', 'subEntity' => 'Member Dues', 'triggerElement' => $triggerElement),
308 );
309
310 $pageUrl = array('url' => 'contribute/add', 'args' => 'reset=1&action=add&context=standalone');
311 $this->customFieldSetLoadOnTheFlyCheck($customSets, $pageUrl);
312 }
313
314 }