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