Global webtest cleanup
[civicrm-core.git] / tests / phpunit / WebTest / Import / CustomDataTest.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 'WebTest/Import/ImportCiviSeleniumTestCase.php';
29class WebTest_Import_CustomDataTest extends ImportCiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testCustomDataImport() {
6a488035
TO
36 $this->webtestLogin();
37
38 $firstName1 = 'Ma_' . substr(sha1(rand()), 0, 7);
39 $firstName2 = 'An_' . substr(sha1(rand()), 0, 7);
40 $customGroupTitle = 'Custom ' . substr(sha1(rand()), 0, 7);
41
42 $firstName3 = 'Ma' . substr(sha1(rand()), 0, 4);
43 $this->webtestAddContact($firstName3, "Anderson", TRUE);
44 $sortName3 = "$firstName3 Anderson";
45 $this->waitForPageToLoad($this->getTimeoutMsec());
46 $url1 = explode('&cid=', $this->getLocation());
47 $id1 = $url1[1];
48
49 $firstName4 = 'Ma' . substr(sha1(rand()), 0, 4);
50 $this->webtestAddContact($firstName4, "Anderson", TRUE);
51 $sortName4 = "$firstName4 Anderson";
52 $this->waitForPageToLoad($this->getTimeoutMsec());
53 $url2 = explode('&cid=', $this->getLocation());
54 $id2 = $url2[1];
55
56 // Get sample import data.
57 list($headers, $rows, $customDataVerify) = $this->_individualCustomCSVData($customGroupTitle, $firstName1, $firstName2,
58 $id1, $id2
59 );
60
61 // Import and check Individual contacts in Skip mode.
62 $other = array(
63 'saveMapping' => TRUE,
64 'createGroup' => TRUE,
65 'createTag' => TRUE,
66 );
67
68 $this->importContacts($headers, $rows, 'Individual', 'Skip', array(), $other);
69
70 // Find the contact
42daf119 71 $this->openCiviPage("contact/search", "reset=1", '_qf_Basic_refresh');
6a488035
TO
72 $this->type('sort_name', $firstName1);
73 $this->click('_qf_Basic_refresh');
74 $this->waitForPageToLoad($this->getTimeoutMsec());
75 $this->click("xpath=//div[@class='crm-search-results']/table/tbody/tr/td[11]/span/a[text()='View']");
76 $this->waitForPageToLoad($this->getTimeoutMsec());
77
78 for ($cnt = 0; $cnt < 2; $cnt++) {
79 foreach ($customDataVerify['rows'][$cnt] as $key => $values) {
80 $rows[$cnt][$key] = $values;
81 }
82 }
83
84 $CGTableId = preg_replace('/\s/', '_', trim($customGroupTitle));
85 if ($this->isElementPresent("xpath=//table[@id='{$CGTableId}_0']")) {
86 $this->click("xpath=//table[@id='{$CGTableId}_0']/tbody/tr[@class='columnheader']/td[@class='grouplabel']/a");
87 }
88 elseif ($this->isElementPresent("xpath=//table[@id='{$CGTableId}_1']")) {
89 $this->click("xpath=//table[@id='{$CGTableId}_1']/tbody/tr[@class='columnheader']/td[@class='grouplabel']/a");
90 }
91
92 // Verify if custom data added
93 $cnt = 1;
94 foreach ($rows[0] as $key => $value) {
95 if ($cnt == 4) {
96 $value = date('F jS, Y');
97 }
98 elseif ($cnt == 7) {
99 $value = $sortName3;
100 }
101 $this->assertTrue($this->isTextPresent($value));
102 $cnt++;
103 }
104 }
105
106 /*
107 * Helper function to provide data for custom data import.
108 */
109 function _individualCustomCSVData($customGroupTitle, $firstName1, $firstName2, $id1, $id2) {
110 list($customDataParams, $customDataVerify) = $this->_addCustomData($customGroupTitle, $id1, $id2);
111
112 $headers = array(
113 'first_name' => 'First Name',
114 'last_name' => 'Last Name',
115 'email' => 'Email',
116 );
117
118 foreach ($customDataParams['headers'] as $key => $values) {
119 $headers[$key] = $values;
120 }
121
122 $rows = array(
123 array(
124 'first_name' => $firstName1,
125 'last_name' => 'Anderson',
126 'email' => substr(sha1(rand()), 0, 7) . '@example.com',
127 ),
128 array(
129 'first_name' => $firstName2,
130 'last_name' => 'Summerson',
131 'email' => substr(sha1(rand()), 0, 7) . '@example.com',
132 ),
133 );
134
135 for ($cnt = 0; $cnt < 2; $cnt++) {
136 foreach ($customDataParams['rows'][$cnt] as $key => $values) {
137 $rows[$cnt][$key] = $values;
138 }
139 }
140
141 return array($headers, $rows, $customDataVerify);
142 }
143
144 function _addCustomData($customGroupTitle, $id1, $id2) {
42daf119 145
6a488035
TO
146 $this->open($this->sboxPath . "civicrm/admin/custom/group?reset=1");
147
148 //add new custom data
149 $this->click("//a[@id='newCustomDataGroup']/span");
150 $this->waitForPageToLoad($this->getTimeoutMsec());
151
152 //fill custom group title
153 $this->click("title");
154 $this->type("title", $customGroupTitle);
155
156 //custom group extends
157 $this->click("extends[0]");
158 $this->select("extends[0]", "value=Contact");
159 $this->click("//option[@value='Contact']");
160 $this->click('_qf_Group_next-bottom');
161 $this->waitForElementPresent('_qf_Field_cancel-bottom');
162
163 //Is custom group created?
164 $this->assertTrue($this->isTextPresent("Your custom field set '{$customGroupTitle}' has been added. You can add custom fields now."));
165 $url = explode('gid=', $this->getLocation());
166 $gid = $url[1];
167
168 // create another custom field - Date
169 $dateFieldLabel = 'custom_field_date_' . substr(sha1(rand()), 0, 4);
170 $this->type('label', $dateFieldLabel);
171 $this->click('data_type[0]');
172 $this->select('data_type[0]', "label=Date");
173 $this->waitForElementPresent('start_date_years');
174
175 // enter years prior to current date
176 $this->type('start_date_years', 3);
177
178 // enter years upto the end year
179 $this->type('end_date_years', 3);
180
181 // select the date and time format
182 $this->select('date_format', "value=yy-mm-dd");
183 $this->select('time_format', "value=2");
184
185 //enter pre help message
186 $this->type("help_pre", "this is field pre help");
187
188 //enter post help message
189 $this->type("help_post", "this field post help");
190
191 //Is searchable?
192 $this->click("is_searchable");
193
194 // clicking save
195 $this->click('_qf_Field_next-bottom');
196 $this->waitForElementPresent('newCustomField');
197
198 $this->assertTrue($this->isTextPresent("Your custom field '{$dateFieldLabel}' has been saved."));
199
200 $dateFieldId = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr/td/span[text()='$dateFieldLabel']/../../td[8]/span/a@href"));
201 $dateFieldId = $dateFieldId[1];
202
203 // create another custom field - Integer Radio
204 $this->click("//a[@id='newCustomField']/span");
205 $this->waitForPageToLoad($this->getTimeoutMsec());
206 $this->click("data_type[0]");
207 $this->select("data_type[0]", "value=1");
208 $this->click("//option[@value='1']");
209 $this->click("data_type[1]");
210 $this->select("data_type[1]", "value=Radio");
211 $this->click("//option[@value='Radio']");
212
213 $radioFieldLabel = 'custom_field_radio' . substr(sha1(rand()), 0, 4);
214 $this->type("label", $radioFieldLabel);
215 $radioOptionLabel1 = 'optionLabel_' . substr(sha1(rand()), 0, 5);
216 $this->type("option_label_1", $radioOptionLabel1);
217 $this->type("option_value_1", "1");
218 $radioOptionLabel2 = 'optionLabel_' . substr(sha1(rand()), 0, 5);
219 $this->type("option_label_2", $radioOptionLabel2);
220 $this->type("option_value_2", "2");
221
222 //select options per line
223 $this->type("options_per_line", "3");
224
225 //enter pre help msg
226 $this->type("help_pre", "this is field pre help");
227
228 //enter post help msg
229 $this->type("help_post", "this is field post help");
230
231 //Is searchable?
232 $this->click("is_searchable");
233
234 //clicking save
235 $this->click("_qf_Field_next");
236 $this->waitForPageToLoad($this->getTimeoutMsec());
237
238 //Is custom field created
239 $this->assertTrue($this->isTextPresent("Your custom field '$radioFieldLabel' has been saved."));
240 $radioFieldId = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr/td/span[text()='$radioFieldLabel']/../../td[8]/span/a@href"));
241 $radioFieldId = $radioFieldId[1];
242
243 // create another custom field - multiselect
244 $this->click("//a[@id='newCustomField']/span");
245 $this->waitForElementPresent('_qf_Field_cancel-bottom');
246 $multiSelectLabel = 'custom_field_multiSelect_' . substr(sha1(rand()), 0, 4);
247 $this->type('label', $multiSelectLabel);
248 $this->click('data_type[1]');
249 $this->select('data_type[1]', "label=Multi-Select");
250 $this->waitForElementPresent('option_label_1');
251
252 // enter multiple choice options
253 $multiSelectOptionLabel1 = 'optionLabel_' . substr(sha1(rand()), 0, 5);
254 $this->type('option_label_1', $multiSelectOptionLabel1);
255 $this->type('option_value_1', 1);
256 $multiSelectOptionLabel2 = 'optionLabel_' . substr(sha1(rand()), 0, 5);
257 $this->type('option_label_2', $multiSelectOptionLabel2);
258 $this->type('option_value_2', 2);
259 $this->click("link=another choice");
260 $multiSelectOptionLabel3 = 'optionLabel_' . substr(sha1(rand()), 0, 5);
261 $this->type('option_label_3', $multiSelectOptionLabel3);
262 $this->type('option_value_3', 3);
263 $this->click("link=another choice");
264
265 //enter pre help msg
266 $this->type("help_pre", "this is field pre help");
267
268 //enter post help msg
269 $this->type("help_post", "this is field post help");
270
271 //Is searchable?
272 $this->click("is_searchable");
273
274 // clicking save
275 $this->click('_qf_Field_next-bottom');
276 $this->waitForPageToLoad($this->getTimeoutMsec());
277 $this->assertTrue($this->isTextPresent("Your custom field '{$multiSelectLabel}' has been saved."));
278 $multiSelectFieldId = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr/td/span[text()='$multiSelectLabel']/../../td[8]/span/a@href"));
279 $multiSelectFieldId = $multiSelectFieldId[1];
280
281 // create another custom field - contact reference
282 $this->click("//a[@id='newCustomField']/span");
283 $this->waitForElementPresent('_qf_Field_cancel-bottom');
284 $contactReferenceLabel = 'custom_field_contactReference_' . substr(sha1(rand()), 0, 4);
285 $this->type('label', $contactReferenceLabel);
286 $this->click('data_type[0]');
287 $this->select('data_type[0]', "label=Contact Reference");
288
289 //enter pre help msg
290 $this->type("help_pre", "this is field pre help");
291
292 //enter post help msg
293 $this->type("help_post", "this is field post help");
294
295 //Is searchable?
296 $this->click("is_searchable");
297
298 // clicking save
299 $this->click('_qf_Field_next-bottom');
300 $this->waitForPageToLoad($this->getTimeoutMsec());
301
302 $this->assertTrue($this->isTextPresent("Your custom field '{$contactReferenceLabel}' has been saved."));
303 $contactReferenceFieldId = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr/td/span[text()='$contactReferenceLabel']/../../td[8]/span/a@href"));
304 $contactReferenceFieldId = $contactReferenceFieldId[1];
305
306 $customDataParams = array(
307 'headers' =>
308 array(
309 "custom_{$dateFieldId}" => "$dateFieldLabel :: $customGroupTitle",
310 "custom_{$radioFieldId}" => "$radioFieldLabel :: $customGroupTitle",
311 "custom_{$multiSelectFieldId}" => "$multiSelectLabel :: $customGroupTitle",
312 "custom_{$contactReferenceFieldId}" => "$contactReferenceLabel :: $customGroupTitle",
313 ),
314 'rows' =>
315 array(0 => array("custom_{$dateFieldId}" => date('Y-m-d'),
316 "custom_{$radioFieldId}" => '2',
317 "custom_{$multiSelectFieldId}" => '3',
318 "custom_{$contactReferenceFieldId}" => $id1,
319 ),
320 1 => array("custom_{$dateFieldId}" => date('Y-m-d', mktime(0, 0, 0, 4, 5, date('Y'))),
321 "custom_{$radioFieldId}" => '1',
322 "custom_{$multiSelectFieldId}" => '2',
323 "custom_{$contactReferenceFieldId}" => $id2,
324 ),
325 ),
326 );
327
328 $customDataVerify = $customDataParams;
329 $customDataVerify['rows'][0]["custom_{$radioFieldId}"] = $radioOptionLabel2;
330 $customDataVerify['rows'][1]["custom_{$radioFieldId}"] = $radioOptionLabel1;
331 $customDataVerify['rows'][0]["custom_{$multiSelectFieldId}"] = $multiSelectOptionLabel3;
332 $customDataVerify['rows'][1]["custom_{$multiSelectFieldId}"] = $multiSelectOptionLabel2;
333
334 return array($customDataParams, $customDataVerify);
335 }
336}
337