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