Global webtest cleanup
[civicrm-core.git] / tests / phpunit / WebTest / Member / OfflineMembershipAddPricesetTest.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
28 require_once 'CiviTest/CiviSeleniumTestCase.php';
29 class WebTest_Member_OfflineMembershipAddPricesetTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testAddPriceSet() {
36 // Log in using webtestLogin() method
37 $this->webtestLogin();
38
39 $title = substr(sha1(rand()), 0, 7);
40 $setTitle = "Membership Fees - $title";
41 $usedFor = 'Membership';
42 $contributionType = 'Donation';
43 $setHelp = 'Select your membership options.';
44 $this->_testAddSet($setTitle, $usedFor, $contributionType, $setHelp);
45
46 // Get the price set id ($sid) by retrieving and parsing the URL of the New Price Field form
47 // which is where we are after adding Price Set.
48 $elements = $this->parseURL();
49 $sid = $elements['queryString']['sid'];
50 $this->assertType('numeric', $sid);
51
52 $fields = array(
53 "National Membership $title" => 'Radio',
54 "Local Chapter $title" => 'CheckBox',
55 );
56
57 list($memTypeTitle1, $memTypeTitle2) = $this->_testAddPriceFields($fields, $validateStrings, FALSE, $title, $sid, $contributionType);
58
59 // load the Price Set Preview and check for expected values
60 $this->_testVerifyPriceSet($validateStrings, $sid);
61
62 // Sign up for membership
63 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
64 $lastName = 'Anderson_' . substr(sha1(rand()), 0, 7);
65 $email = "{$firstName}.{$lastName}@example.com";
66 $contactParams = array(
67 'first_name' => $firstName,
68 'last_name' => $lastName,
69 'email-5' => $email,
70 );
71
72 // Add a contact from the quick add block
73 $this->webtestAddContact($firstName, $lastName, $email);
74
75 $this->_testSignUpOrRenewMembership($sid, $contactParams, $memTypeTitle1, $memTypeTitle2);
76
77 // Renew this membership
78 $this->_testSignUpOrRenewMembership($sid, $contactParams, $memTypeTitle1, $memTypeTitle2, $renew = TRUE);
79 }
80
81 function testAddPriceSetWithMultipleTerms() {
82 // Log in using webtestLogin() method
83 $this->webtestLogin();
84
85 $title = substr(sha1(rand()), 0, 7);
86 $setTitle = "Membership Fees - $title";
87 $usedFor = 'Membership';
88 $contributionType = 'Member Dues';
89 $setHelp = 'Select your membership options.';
90 $memTypeParams1 = $this->webtestAddMembershipType();
91 $memTypeTitle1 = $memTypeParams1['membership_type'];
92 $memTypeId1 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[12]/span/a[3]@href"));
93 $memTypeId1 = $memTypeId1[1];
94 $this->_testAddSet($setTitle, $usedFor, $contributionType, $setHelp);
95
96 // Get the price set id ($sid) by retrieving and parsing the URL of the New Price Field form
97 // which is where we are after adding Price Set.
98 $elements = $this->parseURL();
99 $sid = $elements['queryString']['sid'];
100 $this->assertType('numeric', $sid);
101
102 $fields = array("National Membership $title", "Radio");
103 $this->openCiviPage("admin/price/field", "reset=1&action=add&sid={$sid}");
104
105 $validateStrings[] = $fields[0];
106 $this->type('label', $fields[0]);
107 $this->select('html_type', "value={$fields[1]}");
108 $options = array(
109 1 => array('label' => $memTypeTitle1."_1",
110 'membership_type_id' => $memTypeId1,
111 'amount' => 50.00,
112 'membership_num_terms' => 1,
113 ),
114 2 => array(
115 'label' => $memTypeTitle1."_2",
116 'membership_type_id' => $memTypeId1,
117 'amount' => 90.00,
118 'membership_num_terms' => 2,
119 ),
120 3 => array(
121 'label' => $memTypeTitle1."_3",
122 'membership_type_id' => $memTypeId1,
123 'amount' => 120.00,
124 'membership_num_terms' => 3,
125 ),
126 );
127 $i = 2;
128 foreach($options as $index => $values){
129 $this->select("membership_type_id_{$index}", "value={$values['membership_type_id']}");
130 sleep(1);
131 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[4]/input",$values['membership_num_terms']);
132 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[5]/input",$values['label']);
133 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[6]/input",$values['amount']);
134 if($i > 3){
135 $this->click('link=another choice');
136 }
137 $i++;
138 }
139 $this->waitForElementPresent( 'financial_type_id' );
140 $this->select("financial_type_id", "label={$contributionType}");
141 $this->waitForElementPresent('_qf_Field_next-bottom');
142 $this->click('_qf_Field_next-bottom');
143 $this->waitForPageToLoad($this->getTimeoutMsec());
144 $this->assertElementContainsText('crm-notification-container', "Price Field '{$fields[0]}' has been saved.");
145
146 // load the Price Set Preview and check for expected values
147 $this->_testVerifyPriceSet($validateStrings, $sid);
148
149 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
150 $lastName = 'Anderson_' . substr(sha1(rand()), 0, 7);
151 $email = "{$firstName}.{$lastName}@example.com";
152
153 $contactParams = array(
154 'first_name' => $firstName,
155 'last_name' => $lastName,
156 'email-5' => $email,
157 );
158 $this->webtestAddContact($firstName, $lastName, $email);
159 //membership with number of terms as 3
160 $this->_testMultilpeTermsMembershipRegistration($sid, $contactParams, $memTypeTitle1, 3);
161 //membership with number of terms as 2
162 $this->_testMultilpeTermsMembershipRegistration($sid, $contactParams, $memTypeTitle1, 2);
163
164 }
165
166
167 function _testAddSet($setTitle, $usedFor, $contributionType = NULL, $setHelp) {
168 $this->openCiviPage('admin/price', 'reset=1&action=add', '_qf_Set_next-bottom');
169
170 // Enter Priceset fields (Title, Used For ...)
171 $this->type('title', $setTitle);
172 if ($usedFor == 'Event') {
173 $this->check('extends_1');
174 }
175 elseif ($usedFor == 'Contribution') {
176 $this->check('extends_2');
177 }
178 elseif ($usedFor == 'Membership') {
179 $this->click('extends_3');
180 $this->waitForElementPresent('financial_type_id');
181 $this->select("css=select.form-select", "label={$contributionType}");
182 }
183 $this->type('help_pre', $setHelp);
184 $this->assertChecked('is_active', 'Verify that Is Active checkbox is set.');
185
186 $this->click('_qf_Set_next-bottom');
187 $this->waitForElementPresent('_qf_Field_next-bottom');
188 $this->assertElementContainsText('crm-notification-container', "Your Set '{$setTitle}' has been added. You can add fields to this set now.");
189 }
190
191 function _testAddPriceFields(&$fields, &$validateString, $dateSpecificFields = FALSE, $title, $sid, $contributionType) {
192 $memTypeParams1 = $this->webtestAddMembershipType();
193 $memTypeTitle1 = $memTypeParams1['membership_type'];
194
195 $memTypeId1 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[12]/span/a[3]@href"));
196 $memTypeId1 = $memTypeId1[1];
197
198 $memTypeParams2 = $this->webtestAddMembershipType();
199 $memTypeTitle2 = $memTypeParams2['membership_type'];
200 $memTypeId2 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle2}']/../td[12]/span/a[3]@href"));
201 $memTypeId2 = $memTypeId2[1];
202
203 $this->openCiviPage("admin/price/field", "reset=1&action=add&sid={$sid}");
204
205 foreach ($fields as $label => $type) {
206 $validateStrings[] = $label;
207
208 $this->type('label', $label);
209 $this->select('html_type', "value={$type}");
210
211 switch ($type) {
212 case 'Radio':
213 $options = array(
214 1 => array('label' => "$memTypeTitle1",
215 'membership_type_id' => $memTypeId1,
216 'amount' => 100.00,
217 ),
218 2 => array(
219 'label' => "$memTypeTitle2",
220 'membership_type_id' => $memTypeId2,
221 'amount' => 50.00,
222 ),
223 );
224 $this->addMultipleChoiceOptions($options, $validateStrings);
225 break;
226
227 case 'CheckBox':
228 $options = array(
229 1 => array('label' => "$memTypeTitle1",
230 'membership_type_id' => $memTypeId1,
231 'amount' => 100.00,
232 ),
233 2 => array(
234 'label' => "$memTypeTitle2",
235 'membership_type_id' => $memTypeId2,
236 'amount' => 50.00,
237 ),
238 );
239 $this->addMultipleChoiceOptions($options, $validateStrings);
240 break;
241
242 default:
243 break;
244 }
245 $this->select("financial_type_id", "label={$contributionType}");
246 $this->click('_qf_Field_next_new-bottom');
247 $this->waitForPageToLoad($this->getTimeoutMsec());
248 $this->assertElementContainsText('crm-notification-container', "Price Field '{$label}' has been saved.");
249 }
250 return array($memTypeTitle1, $memTypeTitle2);
251 }
252
253 function _testVerifyPriceSet($validateStrings, $sid) {
254 // verify Price Set at Preview page
255 // start at Manage Price Sets listing
256 $this->openCiviPage('admin/price', 'reset=1');
257
258 // Use the price set id ($sid) to pick the correct row
259 $this->click("css=tr#row_{$sid} a[title='Preview Price Set']");
260
261 // Look for Register button
262 $this->waitForElementPresent('_qf_Preview_cancel-bottom');
263
264 // Check for expected price set field strings
265 $this->assertStringsPresent($validateStrings);
266 }
267
268 function _testSignUpOrRenewMembership($sid, $contactParams, $memTypeTitle1, $memTypeTitle2, $renew = FALSE) {
269 //build the membership dates.
270 require_once 'CRM/Core/Config.php';
271 require_once 'CRM/Utils/Array.php';
272 require_once 'CRM/Utils/Date.php';
273 $currentYear = date('Y');
274 $currentMonth = date('m');
275 $previousDay = date('d') - 1;
276 $endYear = ($renew) ? $currentYear + 2 : $currentYear + 1;
277 $joinDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
278 $startDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
279 $endDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, $previousDay, $endYear));
280 $configVars = new CRM_Core_Config_Variables();
281 foreach (array(
282 'joinDate', 'startDate', 'endDate') as $date) {
283 $$date = CRM_Utils_Date::customFormat($$date, $configVars->dateformatFull);
284 }
285
286 if (!$renew) {
287
288 $this->click('css=li#tab_member a');
289 $this->waitForElementPresent('link=Add Membership');
290
291 $this->click('link=Add Membership');
292 $this->waitForElementPresent('_qf_Membership_cancel-bottom');
293
294 $this->select('price_set_id', "value={$sid}");
295 $this->waitForElementPresent('pricesetTotal');
296
297 $this->click("xpath=//div[@id='priceset']/div[2]/div[2]/div/span/input");
298 $this->click("xpath=//div[@id='priceset']/div[3]/div[2]/div[2]/span/input");
299
300 $this->type('source', 'Offline membership Sign Up Test Text');
301 $this->click('_qf_Membership_upload-bottom');
302 }
303 else {
304 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[9]/span[2][text()='more']/ul/li/a[text()='Renew']");
305 $this->waitForElementPresent('_qf_MembershipRenewal_cancel-bottom');
306 $this->click('_qf_MembershipRenewal_upload-bottom');
307
308 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
309 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$memTypeTitle2}']/../td[9]/span[2][text()='more']/ul/li/a[text()='Renew']");
310 $this->waitForElementPresent('_qf_MembershipRenewal_cancel-bottom');
311 $this->click('_qf_MembershipRenewal_upload-bottom');
312 }
313 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
314 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[9]/span/a[text()='View']");
315 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
316
317 //View Membership Record
318 $verifyData = array(
319 'Membership Type' => "{$memTypeTitle1}",
320 'Status' => 'New',
321 'Member Since' => $joinDate,
322 'Start date' => $startDate,
323 'End date' => $endDate,
324 );
325 $this->webtestVerifyTabularData($verifyData);
326
327 $this->click('_qf_MembershipView_cancel-bottom');
328 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
329 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$memTypeTitle2}']/../td[9]/span/a[text()='View']");
330 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
331
332 //View Membership Record
333 $verifyData = array(
334 'Membership Type' => "{$memTypeTitle2}",
335 'Status' => 'New',
336 'Member Since' => $joinDate,
337 'Start date' => $startDate,
338 'End date' => $endDate,
339 );
340 $this->webtestVerifyTabularData($verifyData);
341
342 $this->click("_qf_MembershipView_cancel-bottom");
343 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
344 }
345
346 function _testMultilpeTermsMembershipRegistration($sid, $contactParams, $memTypeTitle1, $term){
347 //build the membership dates.
348 require_once 'CRM/Core/Config.php';
349 require_once 'CRM/Utils/Array.php';
350 require_once 'CRM/Utils/Date.php';
351 $currentYear = date('Y');
352 $currentMonth = date('m');
353 $previousDay = date('d') - 1;
354 $endYear = ($term == 3) ? $currentYear + 3 : (($term == 2) ? $currentYear + 2 : $currentYear + 1);
355 $joinDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
356 $startDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
357 $endDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, $previousDay, $endYear));
358 $configVars = new CRM_Core_Config_Variables();
359 foreach (array(
360 'joinDate', 'startDate', 'endDate') as $date) {
361 $$date = CRM_Utils_Date::customFormat($$date, $configVars->dateformatFull);
362 }
363
364 $this->click('css=li#tab_member a');
365 $this->waitForElementPresent('link=Add Membership');
366
367 $this->click('link=Add Membership');
368 $this->waitForElementPresent('_qf_Membership_cancel-bottom');
369
370 $this->select('price_set_id', "value={$sid}");
371 $this->waitForElementPresent('pricesetTotal');
372
373 $i = ($term == 3) ? 3 : (($term == 2) ? 2 : 1 );
374 $this->waitForElementPresent("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/input");
375 $this->click("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/input");
376 $amount = $this->getText("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/label/span[@class='crm-price-amount-amount']");
377
378 $this->type('source', 'Offline membership Sign Up Test Text');
379 $this->click('_qf_Membership_upload-bottom');
380
381 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
382 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$endDate}']/../td[9]/span/a[text()='View']");
383 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
384 //View Membership Record
385 $verifyData = array(
386 'Membership Type' => "$memTypeTitle1",
387 'Status' => 'New',
388 'Member Since' => $joinDate,
389 'Start date' => $startDate,
390 'End date' => $endDate,
391 );
392 $this->webtestVerifyTabularData($verifyData);
393
394 //check if the membership amount is correct
395 $this->assertTrue($this->isElementPresent("xpath=//form[@id='MembershipView']/div[2]/div/table[2]/tbody/tr/td/span[text()='{$amount}']"));
396 $this->click("_qf_MembershipView_cancel-bottom");
397 $this->waitForPageToLoad($this->getTimeoutMsec());
398 }
399 }