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