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