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