webtest fixes for WebTest_Campaign_ActivityTest
[civicrm-core.git] / tests / phpunit / WebTest / Campaign / OfflineContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 class WebTest_Campaign_OfflineContributionTest extends CiviSeleniumTestCase {
29
30 protected function setUp() {
31 parent::setUp();
32 }
33
34 function testCreateCampaign() {
35 $this->webtestLogin('admin');
36
37 // Create new group
38 $title = substr(sha1(rand()), 0, 7);
39 $groupName = $this->WebtestAddGroup();
40
41 // Adding contact
42 // We're using Quick Add block on the main page for this.
43 $firstName1 = substr(sha1(rand()), 0, 7);
44 $this->webtestAddContact($firstName1, "Smith", "$firstName1.smith@example.org");
45
46 // add contact to group
47 // visit group tab
48 $this->click("css=li#tab_group a");
49 $this->waitForElementPresent("group_id");
50
51 // add to group
52 $this->select("group_id", "label=$groupName");
53 $this->click("_qf_GroupContact_next");
54 $this->waitForPageToLoad($this->getTimeoutMsec());
55
56 $firstName2 = substr(sha1(rand()), 0, 7);
57 $this->webtestAddContact($firstName2, "John", "$firstName2.john@example.org");
58
59 // add contact to group
60 // visit group tab
61 $this->click("css=li#tab_group a");
62 $this->waitForElementPresent("group_id");
63
64 // add to group
65 $this->select("group_id", "label=$groupName");
66 $this->click("_qf_GroupContact_next");
67 $this->waitForPageToLoad($this->getTimeoutMsec());
68
69 // Enable CiviCampaign module if necessary
70 $this->enableComponents(array('CiviCampaign'));
71
72 // add the required permission
73 $this->changePermissions('edit-2-administer-civicampaign');
74
75 // Log in as normal user
76 $this->webtestLogin();
77
78 $this->openCiviPage('campaign', 'reset=1', "link=Add Campaign");
79 if ($this->isTextPresent('No campaigns found.')) {
80
81 $this->openCiviPage('contribute/add', 'reset=1&action=add&context=standalone', '_qf_Contribution_cancel-bottom');
82 $this->assertElementContainsText('crm-container', 'There are currently no active Campaigns.');
83 }
84 $this->openCiviPage('campaign/add', 'reset=1');
85
86 $campaignTitle = "Campaign $title";
87 $this->type("title", $campaignTitle);
88
89 // select the campaign type
90 $this->select("campaign_type_id", "value=2");
91
92 // fill in the description
93 $this->type("description", "This is a test campaign");
94
95 // include groups for the campaign
96 $this->addSelection("includeGroups-f", "label=$groupName");
97 $this->click("//option[@value=4]");
98 $this->click("add");
99
100 // fill the end date for campaign
101 $this->webtestFillDate("end_date", "+1 year");
102
103 // select campaign status
104 $this->select("status_id", "value=2");
105
106 // click save
107 $this->click("_qf_Campaign_upload-bottom");
108 $this->waitForPageToLoad($this->getTimeoutMsec());
109
110 $this->waitForText('crm-notification-container', "Campaign $title");
111
112 $this->waitForElementPresent("xpath=//div[@id='campaignList']/div[@id='campaigns_wrapper']/table[@id='campaigns']/tbody//tr/td[text()='$campaignTitle']");
113 $url = explode('id=', $this->getAttribute("xpath=//div[@id='campaignList']/div[@id='campaigns_wrapper']/table[@id='campaigns']/tbody//tr/td[text()='$campaignTitle']/../td[13]/span/a[text()='Edit']@href"));
114 $campaignId = $url[1];
115
116 $this->offlineContributionTest($campaignTitle, $campaignId);
117
118 $this->pastCampaignsTest($groupName);
119 }
120
121 function offlineContributionTest($campaignTitle, $id, $past = FALSE) {
122 // Create a contact to be used as soft creditor
123 $softCreditFname = substr(sha1(rand()), 0, 7);
124 $softCreditLname = substr(sha1(rand()), 0, 7);
125 $this->webtestAddContact($softCreditFname, $softCreditLname, FALSE);
126
127 // Adding contact with randomized first name (so we can then select that contact when creating contribution.)
128 // We're using Quick Add block on the main page for this.
129 $firstName = substr(sha1(rand()), 0, 7);
130 $this->webtestAddContact($firstName, "Summerson", $firstName . "@summerson.name");
131
132 // go to contribution tab and add contribution.
133 $this->click("css=li#tab_contribute a");
134
135 // wait for Record Contribution elenment.
136 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
137 $this->click("link=Record Contribution (Check, Cash, EFT ...)");
138
139 $this->waitForElementPresent("_qf_Contribution_cancel-bottom");
140 // fill financial type.
141 $this->select("financial_type_id", "Donation");
142
143 // fill in Received Date
144 $this->webtestFillDate('receive_date');
145
146 // source
147 $this->type("source", "Mailer 1");
148
149 if ($past) {
150 $this->click("include-past-campaigns");
151 // Because it tends to cause problems, all uses of sleep() must be justified in comments
152 // Sleep should never be used for wait for anything to load from the server
153 // FIXME: Use a better method for waiting for this AJAX call - sleep is not going to work!
154 sleep(2);
155 }
156
157 $this->click("campaign_id");
158 $this->select("campaign_id", "value=$id");
159
160 // total amount
161 $this->type("total_amount", "100");
162
163 // select payment instrument type = Check and enter chk number
164 $this->select("payment_instrument_id", "value=4");
165 $this->waitForElementPresent("check_number");
166 $this->type("check_number", "check #1041");
167
168 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
169
170 // soft credit
171 $this->click("soft_credit_to");
172 $this->type("soft_credit_to", $softCreditFname);
173 $this->typeKeys("soft_credit_to", $softCreditFname);
174 $this->waitForElementPresent("css=div.ac_results-inner li");
175 $this->click("css=div.ac_results-inner li");
176
177 //Additional Detail section
178 $this->click("AdditionalDetail");
179 $this->waitForElementPresent("thankyou_date");
180
181 $this->type("note", "Test note for {$firstName}.");
182 $this->type("non_deductible_amount", "10");
183 $this->type("fee_amount", "0");
184 $this->type("net_amount", "0");
185 $this->type("invoice_id", time());
186 $this->webtestFillDate('thankyou_date');
187
188 //Honoree section
189 $this->click("Honoree");
190 $this->waitForElementPresent("honor_email");
191
192 $this->click("CIVICRM_QFID_1_2");
193 $this->select("honor_prefix_id", "label=Ms.");
194 $this->type("honor_first_name", "Foo");
195 $this->type("honor_last_name", "Bar");
196 $this->type("honor_email", "foo@bar.com");
197
198 //Premium section
199 $this->click("Premium");
200 $this->waitForElementPresent("fulfilled_date");
201 $this->select("product_name[0]", "label=Coffee Mug ( MUG-101 )");
202 $this->select("product_name[1]", "label=Black");
203 $this->webtestFillDate('fulfilled_date');
204
205 // Clicking save.
206 $this->click("_qf_Contribution_upload-bottom");
207 $this->waitForPageToLoad($this->getTimeoutMsec());
208
209 // Is status message correct?
210 $this->waitForText('crm-notification-container', "The contribution record has been saved");
211
212 $this->waitForElementPresent("xpath=//div[@id='Contributions']//table/tbody/tr/td[8]/span/a[text()='View']");
213
214 // click through to the Contribution view screen
215 $this->click("xpath=//div[@id='Contributions']//table/tbody/tr/td[8]/span/a[text()='View']");
216 $this->waitForElementPresent('_qf_ContributionView_cancel-bottom');
217
218 // verify Contribution created
219 $this->webtestVerifyTabularData(array('Campaign' => $campaignTitle));
220
221 if ($past) {
222 // when campaign component is disabled
223 $this->openCiviPage('admin/setting/component', 'reset=1', '_qf_Component_next-bottom');
224 $this->addSelection("enableComponents-t", "label=CiviCampaign");
225 $this->click("//option[@value='CiviCampaign']");
226 $this->click("remove");
227 $this->click("_qf_Component_next-bottom");
228 $this->waitForPageToLoad($this->getTimeoutMsec());
229 $this->assertTrue($this->isTextPresent("Changes Saved."));
230
231 $this->openCiviPage('contribute/search', 'reset=1', '_qf_Search_refresh');
232
233 $this->type('sort_name', $firstName);
234 $this->click("_qf_Search_refresh");
235 $this->waitForElementPresent("_qf_Search_next_print");
236 $this->click("xpath=//div[@id='contributionSearch']/table/tbody/tr/td[11]/span/a[text()='Edit']");
237 $this->waitForElementPresent("_qf_Contribution_cancel-bottom");
238 $this->assertTrue($this->isTextPresent("$campaignTitle"));
239 }
240 }
241
242 function pastCampaignsTest($groupName) {
243 $this->openCiviPage('campaign/add', 'reset=1', '_qf_Campaign_upload-bottom');
244
245 $pastTitle = substr(sha1(rand()), 0, 7);
246 $pastCampaignTitle = "Past Campaign $pastTitle";
247 $this->type("title", $pastCampaignTitle);
248
249 // select the campaign type
250 $this->select("campaign_type_id", "value=2");
251
252 // fill in the description
253 $this->type("description", "This is a test for past campaign");
254
255 // include groups for the campaign
256 $this->addSelection("includeGroups-f", "label=$groupName");
257 $this->click("//option[@value=4]");
258 $this->click("add");
259
260 // fill the start date for campaign
261 $this->webtestFillDate("start_date", "1 January 2011");
262
263 // fill the end date for campaign
264 $this->webtestFillDate("end_date", "31 January 2011");
265
266 // select campaign status
267 $this->select("status_id", "value=3");
268
269 // click save
270 $this->click("_qf_Campaign_upload-bottom");
271 $this->waitForPageToLoad($this->getTimeoutMsec());
272
273 $this->assertTrue($this->isTextPresent("Campaign $pastCampaignTitle has been saved."),
274 "Status message didn't show up after saving campaign!"
275 );
276
277 $this->waitForElementPresent("link=Add Campaign");
278
279 $this->waitForElementPresent("Campaigns");
280 $this->click("search_form_campaign");
281 $this->type("campaign_title", $pastCampaignTitle);
282 $this->click("xpath=//div[@class='crm-accordion-body']/table/tbody/tr[4]/td/a[text()='Search']");
283
284 $this->waitForElementPresent("xpath=//div[@id='campaignList']/div[@id='campaigns_wrapper']/table[@id='campaigns']/tbody//tr/td[text()='$pastCampaignTitle']");
285 $url = explode('id=', $this->getAttribute("xpath=//div[@id='campaignList']/div[@id='campaigns_wrapper']/table[@id='campaigns']/tbody//tr/td[text()='$pastCampaignTitle']/../td[13]/span/a[text()='Edit']@href"));
286 $campaignId = $url[1];
287
288 $this->offlineContributionTest($pastCampaignTitle, $campaignId, TRUE);
289 }
290 }
291