webtest cleanup and code improvement
[civicrm-core.git] / tests / phpunit / WebTest / Campaign / OnlineContributionTest.php
CommitLineData
6a488035
TO
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
28require_once 'CiviTest/CiviSeleniumTestCase.php';
29class WebTest_Campaign_OnlineContributionTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testCreateCampaign() {
36 // This is the path where our testing install resides.
37 // The rest of URL is defined in CiviSeleniumTestCase base class, in
38 // class attributes.
39 $this->open($this->sboxPath);
40
41 // Logging in. Remember to wait for page to load. In most cases,
42 // you can rely on 30000 as the value that allows your test to pass, however,
43 // sometimes your test might fail because of this. In such cases, it's better to pick one element
44 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
45 // page contents loaded and you can continue your test execution.
46 $this->webtestLogin();
47
48 // Create new group
49 $title = substr(sha1(rand()), 0, 7);
50 $groupName = $this->WebtestAddGroup();
51
52 // Adding contact
53 // We're using Quick Add block on the main page for this.
54 $firstName1 = substr(sha1(rand()), 0, 7);
55 $this->webtestAddContact($firstName1, "Smith", "$firstName1.smith@example.org");
56
57 // add contact to group
58 // visit group tab
59 $this->click("css=li#tab_group a");
60 $this->waitForElementPresent("group_id");
61
62 // add to group
63 $this->select("group_id", "label=$groupName");
64 $this->click("_qf_GroupContact_next");
65 $this->waitForPageToLoad($this->getTimeoutMsec());
66
67 $firstName2 = substr(sha1(rand()), 0, 7);
68 $this->webtestAddContact($firstName2, "John", "$firstName2.john@example.org");
69
70 // add contact to group
71 // visit group tab
72 $this->click("css=li#tab_group a");
73 $this->waitForElementPresent("group_id");
74
75 // add to group
76 $this->select("group_id", "label=$groupName");
77 $this->click("_qf_GroupContact_next");
78 $this->waitForPageToLoad($this->getTimeoutMsec());
79
80 // Enable CiviCampaign module if necessary
81 $this->open($this->sboxPath . "civicrm/admin/setting/component?reset=1");
82 $this->waitForPageToLoad($this->getTimeoutMsec());
83 $this->waitForElementPresent("_qf_Component_next-bottom");
84 $enabledComponents = $this->getSelectOptions("enableComponents-t");
85 if (!in_array("CiviCampaign", $enabledComponents)) {
86 $this->addSelection("enableComponents-f", "label=CiviCampaign");
87 $this->click("//option[@value='CiviCampaign']");
88 $this->click("add");
89 $this->click("_qf_Component_next-bottom");
90 $this->waitForPageToLoad($this->getTimeoutMsec());
91 $this->assertTrue($this->isTextPresent("Changes Saved."));
92 }
93
94 // add the required Drupal permission
95 $permissions = array(
96 'edit-2-administer-civicampaign',
97 'edit-1-make-online-contributions',
98 'edit-1-profile-listings-and-forms',
99 );
100 $this->changePermissions($permissions);
101
102 // Go directly to the URL of the screen that you will be testing
103 $this->open($this->sboxPath . "civicrm/campaign/add?reset=1");
104
105 // As mentioned before, waitForPageToLoad is not always reliable. Below, we're waiting for the submit
106 // button at the end of this page to show up, to make sure it's fully loaded.
107 $this->waitForElementPresent("_qf_Campaign_upload-bottom");
108
109 // Let's start filling the form with values.
110 $campaignTitle = "Campaign $title";
111 $this->type("title", $campaignTitle);
112
113 // select the campaign type
114 $this->select("campaign_type_id", "value=2");
115
116 // fill in the description
117 $this->type("description", "This is a test campaign");
118
119 // include groups for the campaign
120 $this->addSelection("includeGroups-f", "label=$groupName");
121 $this->click("//option[@value=4]");
122 $this->click("add");
123
124 // fill the end date for campaign
125 $this->webtestFillDate("end_date", "+1 year");
126
127 // select campaign status
128 $this->select("status_id", "value=2");
129
130 // click save
131 $this->click("_qf_Campaign_upload-bottom");
132 $this->waitForPageToLoad($this->getTimeoutMsec());
133
134 $this->assertTrue($this->isTextPresent("Campaign Campaign $title has been saved."),
135 "Status message didn't show up after saving campaign!"
136 );
137
138 $this->waitForElementPresent("//div[@id='campaignList']/div[@class='dataTables_wrapper']/table/tbody/tr/td[text()='{$campaignTitle}']/../td[1]");
139 $id = (int) $this->getText("//div[@id='campaignList']/div[@class='dataTables_wrapper']/table/tbody/tr/td[text()='{$campaignTitle}']/../td[1]");
140
141 $this->onlineContributionAddTest($campaignTitle, $id);
142 }
143
144 function onlineContributionAddTest($campaignTitle, $id) {
145 // We need a payment processor
146 $processorName = "Webtest Dummy" . substr(sha1(rand()), 0, 7);
147 $paymentProcessorId = $this->webtestAddPaymentProcessor($processorName);
148
149 $this->open($this->sboxPath . "civicrm/admin/contribute/add?reset=1&action=add");
150
151 $contributionTitle = substr(sha1(rand()), 0, 7);
152 $rand = 2 * rand(2, 50);
153
154 // fill in step 1 (Title and Settings)
155 $contributionPageTitle = "Title $contributionTitle";
156 $this->type('title', $contributionPageTitle);
157 $this->select( 'financial_type_id', 'value=1' );
158
159 // select campaign
160 $this->click("campaign_id");
161 $this->select("campaign_id", "value=$id");
162
163 $this->fillRichTextField('intro_text', 'This is Test Introductory Message', 'CKEditor');
164 $this->fillRichTextField('footer_text', 'This is Test Footer Message', 'CKEditor');
165
166 // go to step 2
167 $this->click('_qf_Settings_next');
168 $this->waitForElementPresent("_qf_Amount_next-bottom");
169
170 //this contribution page for online contribution
171 $this->check("payment_processor[{$paymentProcessorId}]");
172 $this->isTextPresent("Contribution Amounts section enabled");
173 $this->type("label_1", "amount 1");
174 $this->type("value_1", "100");
175 $this->type("label_2", "amount 2");
176 $this->type("value_2", "200");
177 $this->click("CIVICRM_QFID_1_2");
178
179 $this->click("_qf_Amount_next-bottom");
180 $this->waitForPageToLoad($this->getTimeoutMsec());
181
182 // go to step 4
183 $this->click("//div[@id='mainTabContainer']/ul//li/a[text()='Receipt']");
184 $this->waitForElementPresent('_qf_ThankYou_next-bottom');
185
186 // fill in step 4 (Thanks and Receipt)
187 $this->type('thankyou_title', "Thank-you Page Title $contributionTitle");
188 $this->type('receipt_from_name', "Receipt From Name $contributionTitle");
189 $this->type('receipt_from_email', "$contributionTitle@example.org");
190 $this->type('receipt_text', "Receipt Message $contributionTitle");
191 $this->type('cc_receipt', "$contributionTitle@example.net");
192 $this->type('bcc_receipt', "$contributionTitle@example.com");
193
194 $this->click('_qf_ThankYou_next-bottom');
195 $this->waitForPageToLoad($this->getTimeoutMsec());
196
197 // go to step 5
198 $this->click("//div[@id='mainTabContainer']/ul//li/a[text()='Tell a Friend']");
199 $this->waitForElementPresent("_qf_Contribute_next-bottom");
200
201 // fill in step 5 (Tell a Friend)
202 $this->click('tf_is_active');
203 $this->type('tf_title', "TaF Title $contributionTitle");
204 $this->type('intro', "TaF Introduction $contributionTitle");
205 $this->type('suggested_message', "TaF Suggested Message $contributionTitle");
206 $this->type('general_link', "TaF Info Page Link $contributionTitle");
207 $this->type('tf_thankyou_title', "TaF Thank-you Title $contributionTitle");
208 $this->type('tf_thankyou_text', "TaF Thank-you Message $contributionTitle");
209
210 $this->click('_qf_Contribute_next-bottom');
211 $this->waitForPageToLoad($this->getTimeoutMsec());
212
213 // go to step 6
214 $this->click("//div[@id='mainTabContainer']/ul//li/a[text()='Profiles']");
215 $this->waitForElementPresent("_qf_Custom_next-bottom");
216
217 // fill in step 6 (Include Profiles)
218 $this->select('custom_pre_id', 'value=1');
219
220 $this->click('_qf_Custom_next-bottom');
221 $this->waitForPageToLoad($this->getTimeoutMsec());
222
223 // go to step 7
224 $this->click("//div[@id='mainTabContainer']/ul//li/a[text()='Premiums']");
225 $this->waitForElementPresent("_qf_Premium_next-bottom");
226
227 // fill in step 7 (Premiums)
228 $this->click('premiums_active');
229 $this->type('premiums_intro_title', "Prem Title $contributionTitle");
230 $this->type('premiums_intro_text', "Prem Introductory Message $contributionTitle");
231 $this->type('premiums_contact_email', "$contributionTitle@example.info");
232 $this->type('premiums_contact_phone', rand(100000000, 999999999));
233 $this->click('premiums_display_min_contribution');
234 $this->type('premiums_nothankyou_label', "No Thank you ");
235
236 $this->click('_qf_Premium_next-bottom');
237 $this->waitForPageToLoad($this->getTimeoutMsec());
238
239 // go to step 8
240 $this->click("//div[@id='mainTabContainer']/ul//li/a[text()='Widgets']");
241 $this->waitForElementPresent("_qf_Widget_next-bottom");
242
243 // fill in step 8 (Widget Settings)
244 $this->click('is_active');
245 $this->type('url_logo', "URL to Logo Image $contributionTitle");
246 $this->type('button_title', "Button Title $contributionTitle");
247 $this->type('about', "About $contributionTitle");
248
249 $this->click('_qf_Widget_next-bottom');
250 $this->waitForPageToLoad($this->getTimeoutMsec());
251
252 // go to step 9
253 $this->click("//div[@id='mainTabContainer']/ul//li/a[text()='Personal Campaigns']");
254 $this->waitForElementPresent("_qf_Contribute_next-bottom");
255
256 // fill in step 9 (Enable Personal Campaign Pages)
257 $this->click('pcp_active');
258 $this->click('is_approval_needed');
259 $this->type('notify_email', "$contributionTitle@example.name");
260 $this->select('supporter_profile_id', 'value=2');
261 $this->type('tellfriend_limit', 7);
262 $this->type('link_text', "'Create Personal Campaign Page' link text $contributionTitle");
263
264 // submit new contribution page
265 $this->click('_qf_Contribute_next-bottom');
266 $this->waitForPageToLoad($this->getTimeoutMsec());
267
268 //get Url for Live Contribution Page
269 $registerUrl = $this->_testVerifyRegisterPage($contributionPageTitle);
270
271 //logout
272 $this->open($this->sboxPath . "civicrm/logout?reset=1");
273 $this->waitForPageToLoad($this->getTimeoutMsec());
274
275 //Open Live Contribution Page
276 $this->open($this->sboxPath . $registerUrl);
277 $this->waitForElementPresent("_qf_Main_upload-bottom");
278
279 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
280 $lastName = 'An' . substr(sha1(rand()), 0, 7);
281
282 $this->type("email-5", $firstName . "@example.com");
283
284 $this->type("first_name", $firstName);
285 $this->type("last_name", $lastName);
286
287 $streetAddress = "100 Main Street";
288 $this->type("street_address-1", $streetAddress);
289 $this->type("city-1", "San Francisco");
290 $this->type("postal_code-1", "94117");
291 $this->select("country-1", "value=1228");
292 $this->select("state_province-1", "value=1001");
293
294 //Credit Card Info
295 $this->select("credit_card_type", "value=Visa");
296 $this->type("credit_card_number", "4111111111111111");
297 $this->type("cvv2", "000");
298 $this->select("credit_card_exp_date[M]", "value=1");
299 $this->select("credit_card_exp_date[Y]", "value=2020");
300
301 //Billing Info
302 $this->type("billing_first_name", $firstName . "billing");
303 $this->type("billing_last_name", $lastName . "billing");
304 $this->type("billing_street_address-5", "15 Main St.");
305 $this->type(" billing_city-5", "San Jose");
306 $this->select("billing_country_id-5", "value=1228");
307 $this->select("billing_state_province_id-5", "value=1004");
308 $this->type("billing_postal_code-5", "94129");
309 $this->click("_qf_Main_upload-bottom");
310
311 $this->waitForPageToLoad($this->getTimeoutMsec());
312 $this->waitForElementPresent("_qf_Confirm_next-bottom");
313
314 $this->click("_qf_Confirm_next-bottom");
315 $this->waitForPageToLoad($this->getTimeoutMsec());
316
317 //login to check contribution
318 $this->open($this->sboxPath);
319
320 // Log in using webtestLogin() method
321 $this->webtestLogin();
322
323 //Find Contribution
324 $this->open($this->sboxPath . "civicrm/contribute/search?reset=1");
325
326 $this->waitForElementPresent("contribution_date_low");
327
328 $this->type("sort_name", "$firstName $lastName");
329 $this->click("_qf_Search_refresh");
330
331 $this->waitForPageToLoad($this->getTimeoutMsec());
332
333 $this->waitForElementPresent("xpath=//div[@id='contributionSearch']//table//tbody/tr[1]/td[11]/span/a[text()='View']");
334 $this->click("xpath=//div[@id='contributionSearch']//table//tbody/tr[1]/td[11]/span/a[text()='View']");
335 $this->waitForPageToLoad($this->getTimeoutMsec());
336 $this->waitForElementPresent("_qf_ContributionView_cancel-bottom");
337
338 //View Contribution Record
339 $this->verifyText("xpath=id('ContributionView')/div[2]/table[1]/tbody/tr[10]/td[2]", preg_quote($campaignTitle));
340 }
341
342 function _testVerifyRegisterPage($contributionPageTitle) {
343 $this->open($this->sboxPath . "civicrm/admin/contribute?reset=1");
344 $this->waitForElementPresent("_qf_SearchContribution_refresh");
345 $this->type('title', $contributionPageTitle);
346 $this->click("_qf_SearchContribution_refresh");
347 $this->waitForPageToLoad('50000');
348 $id = $this->getAttribute("//div[@id='configure_contribution_page']//table/tbody/tr/td/strong[text()='$contributionPageTitle']/../../td[5]/div/span/ul/li/a[text()='Title and Settings']@href");
349 $id = explode('id=', $id);
350 $registerUrl = "civicrm/contribute/transact?reset=1&id=$id[1]";
351 return $registerUrl;
352 }
353}
354