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