Merge pull request #2842 from totten/master-api-rollback-soft-errors
[civicrm-core.git] / tests / phpunit / WebTest / Import / MatchExternalIdTest.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_MatchExternalIdTest
31 */
32 class WebTest_Import_MatchExternalIdTest extends ImportCiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 /*
39 * Test participant import for Individuals matching on external identifier.
40 */
41 function testContributionImport() {
42 $this->webtestLogin();
43
44 // Get sample import data.
45 list($headers, $rows, $fieldMapper) = $this->_contributionIndividualCSVData();
46
47 // Create and import csv from provided data and check imported data.
48 $this->importCSVComponent('Contribution', $headers, $rows, 'Individual', 'Insert new contributions', $fieldMapper);
49 }
50
51 /*
52 * Test membership import for Individuals matching on external identifier.
53 */
54 function testMemberImportIndividual() {
55 $this->webtestLogin();
56
57 // Get membership import data for Individuals.
58 list($headers, $rows, $fieldMapper) = $this->_memberIndividualCSVData();
59
60 // Import participants and check imported data.
61 $this->importCSVComponent('Membership', $headers, $rows, 'Individual', 'Skip', $fieldMapper);
62 }
63
64 /*
65 * Test participant import for Individuals matching on external identifier.
66 */
67 function testParticipantImportIndividual() {
68 // Log in using webtestLogin() method
69 $this->webtestLogin();
70
71 // Get sample import data.
72 list($headers, $rows, $fieldMapper) = $this->_participantIndividualCSVData();
73
74 // Create and import csv from provided data and check imported data.
75 $this->importCSVComponent('Event', $headers, $rows, 'Individual', 'Skip', $fieldMapper);
76 }
77
78 /*
79 * Helper function to provide data for contribution import for Individual.
80 */
81 /**
82 * @return array
83 */
84 function _contributionIndividualCSVData() {
85 $firstName1 = substr(sha1(rand()), 0, 7);
86 $lastName1 = substr(sha1(rand()), 0, 7);
87 $externalId1 = substr(sha1(rand()), 0, 4);
88
89 $this->_addContact($firstName1, $lastName1, $externalId1);
90
91 $firstName2 = substr(sha1(rand()), 0, 7);
92 $lastName2 = substr(sha1(rand()), 0, 7);
93 $externalId2 = substr(sha1(rand()), 0, 4);
94
95 $this->_addContact($firstName2, $lastName2, $externalId2);
96
97 $headers = array(
98 'external_identifier' => 'External Identifier',
99 'fee_amount' => 'Fee Amount',
100 'financial_type' => 'Financial Type',
101 'contribution_status_id' => 'Contribution Status',
102 'total_amount' => 'Total Amount',
103 );
104
105 $rows = array(
106 array(
107 'external_identifier' => $externalId1,
108 'fee_amount' => '200',
109 'financial_type' => 'Donation',
110 'contribution_status_id' => 'Completed',
111 'total_amount' => '200',
112 ),
113 array(
114 'external_identifier' => $externalId2,
115 'fee_amount' => '400',
116 'financial_type' => 'Donation',
117 'contribution_status_id' => 'Completed',
118 'total_amount' => '400',
119 ),
120 );
121 $fieldMapper = array(
122 'mapper[0][0]' => 'external_identifier',
123 'mapper[2][0]' => 'financial_type',
124 'mapper[4][0]' => 'total_amount',
125 );
126 return array($headers, $rows, $fieldMapper);
127 }
128
129 /*
130 * Helper function to provide data for membership import for Individual.
131 */
132 /**
133 * @return array
134 */
135 function _memberIndividualCSVData() {
136 $memTypeParams = $this->webtestAddMembershipType();
137
138 $firstName1 = substr(sha1(rand()), 0, 7);
139 $lastName1 = substr(sha1(rand()), 0, 7);
140 $externalId1 = substr(sha1(rand()), 0, 4);
141
142 $this->_addContact($firstName1, $lastName1, $externalId1);
143 $startDate1 = date('Y-m-d');
144 $year = date('Y') - 1;
145
146 $firstName2 = substr(sha1(rand()), 0, 7);
147 $lastName2 = substr(sha1(rand()), 0, 7);
148 $externalId2 = substr(sha1(rand()), 0, 4);
149
150 $this->_addContact($firstName2, $lastName2, $externalId2);
151 $startDate2 = date('Y-m-d', mktime(0, 0, 0, 9, 10, $year));
152
153 $headers = array(
154 'external_identifier' => 'External Identifier',
155 'membership_type_id' => 'Membership Type',
156 'membership_start_date' => 'Membership Start Date',
157 );
158 $rows = array(
159 array(
160 'external_identifier' => $externalId1,
161 'membership_type_id' => $memTypeParams['membership_type'],
162 'membership_start_date' => $startDate1,
163 ),
164 array(
165 'external_identifier' => $externalId2,
166 'membership_type_id' => $memTypeParams['membership_type'],
167 'membership_start_date' => $startDate2,
168 ),
169 );
170
171 $fieldMapper = array(
172 'mapper[0][0]' => 'external_identifier',
173 'mapper[1][0]' => 'membership_type_id',
174 'mapper[2][0]' => 'membership_start_date',
175 );
176 return array($headers, $rows, $fieldMapper);
177 }
178
179 /*
180 * Helper function to provide data for participant import for Individual.
181 */
182 /**
183 * @return array
184 */
185 function _participantIndividualCSVData() {
186 $eventInfo = $this->_addNewEvent();
187
188 $firstName1 = substr(sha1(rand()), 0, 7);
189 $lastName1 = substr(sha1(rand()), 0, 7);
190 $externalId1 = substr(sha1(rand()), 0, 4);
191
192 $this->_addContact($firstName1, $lastName1, $externalId1);
193
194 $firstName2 = substr(sha1(rand()), 0, 7);
195 $lastName2 = substr(sha1(rand()), 0, 7);
196 $externalId2 = substr(sha1(rand()), 0, 4);
197
198 $this->_addContact($firstName2, $lastName2, $externalId2);
199
200 $headers = array(
201 'external_identifier' => 'External Identifier',
202 'event_id' => 'Event Id',
203 'fee_level' => 'Fee Level',
204 'role' => 'Participant Role',
205 'status' => 'Participant Status',
206 'register_date' => 'Register date',
207 );
208
209 $rows = array(
210 array(
211 'external_identifier' => $externalId1,
212 'event_id' => $eventInfo['event_id'],
213 'fee_level' => 'Member',
214 'role' => 1,
215 'status' => 1,
216 'register_date' => '2011-03-30',
217 ),
218 array(
219 'external_identifier' => $externalId2,
220 'event_id' => $eventInfo['event_id'],
221 'fee_level' => 'Non-Member',
222 'role' => 1,
223 'status' => 1,
224 'register_date' => '2011-03-30',
225 ),
226 );
227
228 $fieldMapper = array(
229 'mapper[0][0]' => 'external_identifier',
230 'mapper[1][0]' => 'event_id',
231 'mapper[2][0]' => 'participant_fee_level',
232 'mapper[4][0]' => 'participant_status_id',
233 );
234
235 return array($headers, $rows, $fieldMapper);
236 }
237
238 /*
239 * Helper function to add new contact
240 *
241 * @params $firstName, $lastName, $externalId
242 *
243 * @return int external id
244 */
245 /**
246 * @param $firstName
247 * @param $lastName
248 * @param $externalId
249 *
250 * @return mixed
251 */
252 function _addContact($firstName, $lastName, $externalId) {
253 $this->openCiviPage('contact/add', 'reset=1&ct=Individual');
254
255 //fill in first name
256 $this->type("first_name", $firstName);
257
258 //fill in last name
259 $this->type("last_name", $lastName);
260
261 //fill in external identifier
262 $this->type("external_identifier", $externalId);
263
264 // Clicking save.
265 $this->click("_qf_Contact_upload_view");
266 $this->waitForPageToLoad($this->getTimeoutMsec());
267 $this->waitForText('crm-notification-container', "Contact Saved");
268
269 return $externalId;
270 }
271
272 /*
273 * Helper function to add new event
274 *
275 * @params array $params parameters to create an event
276 *
277 * @return array $params event details of newly created event
278 */
279 /**
280 * @param array $params
281 *
282 * @return array
283 */
284 function _addNewEvent($params = array(
285 )) {
286 if (empty($params)) {
287
288 // We need a payment processor
289 $processorName = "Webtest Dummy" . substr(sha1(rand()), 0, 7);
290 $this->webtestAddPaymentProcessor($processorName);
291
292 // create an event
293 $eventTitle = 'My Conference - ' . substr(sha1(rand()), 0, 7);
294 $params = array(
295 'title' => $eventTitle,
296 'template_id' => 6,
297 'event_type_id' => 4,
298 'payment_processor' => $processorName,
299 'fee_level' => array(
300 'Member' => "250.00",
301 'Non-Member' => "325.00",
302 ),
303 );
304 }
305
306 $this->openCiviPage('event/add', 'reset=1&action=add', '_qf_EventInfo_upload-bottom');
307
308 $this->select("event_type_id", "value={$params['event_type_id']}");
309
310 // Attendee role s/b selected now.
311 $this->select("default_role_id", "value=1");
312
313 // Enter Event Title, Summary and Description
314 $this->type("title", $params['title']);
315 $this->type("summary", "This is a great conference. Sign up now!");
316 $this->fillRichTextField("description", "Here is a description for this event.", 'CKEditor');
317
318 // Choose Start and End dates.
319 // Using helper webtestFillDate function.
320 $this->webtestFillDateTime("start_date", "+1 week");
321 $this->webtestFillDateTime("end_date", "+1 week 1 day 8 hours ");
322
323 $this->type("max_participants", "50");
324 $this->click("is_map");
325 $this->click("_qf_EventInfo_upload-bottom");
326
327 // Wait for Location tab form to load
328 $this->waitForPageToLoad($this->getTimeoutMsec());
329
330 // Go to Fees tab
331 $this->click("link=Fees");
332 $this->waitForElementPresent("_qf_Fee_upload-bottom");
333 $this->click("CIVICRM_QFID_1_is_monetary");
334 $this->click("xpath=//tr[@class='crm-event-manage-fee-form-block-payment_processor']/td[2]/label[text()='$processorName']");
335 $this->select( "financial_type_id", "value=4" );
336
337 $counter = 1;
338 foreach ($params['fee_level'] as $label => $amount) {
339 $this->type("label_{$counter}", $label);
340 $this->type("value_{$counter}", $amount);
341 $counter++;
342 }
343
344 $this->click("_qf_Fee_upload-bottom");
345 $this->waitForElementPresent("_qf_Fee_upload-bottom");
346
347 // Go to Online Registration tab
348 $this->click("link=Online Registration");
349 $this->waitForElementPresent("_qf_Registration_upload-bottom");
350
351 $this->click("is_online_registration");
352 $this->assertChecked("is_online_registration");
353
354 $this->fillRichTextField("intro_text", "Fill in all the fields below and click Continue.", 'CKEditor', TRUE);
355
356 // enable confirmation email
357 $this->click("CIVICRM_QFID_1_is_email_confirm");
358 $this->type("confirm_from_name", "Jane Doe");
359 $this->type("confirm_from_email", "jane.doe@example.org");
360
361 $this->click("_qf_Registration_upload-bottom");
362 $this->waitForElementPresent("_qf_Registration_upload-bottom");
363 $this->waitForText('crm-notification-container', "'Online Registration' information has been saved");
364
365 // verify event input on info page
366 // start at Manage Events listing
367 $this->openCiviPage('event/manage', 'reset=1');
368 $this->type("xpath=//div[@class='crm-block crm-form-block crm-event-searchevent-form-block']/table/tbody/tr/td/input",$params['title']);
369 $this->click("_qf_SearchEvent_refresh");
370 $this->waitForPageToLoad($this->getTimeoutMsec());
371 $this->clickLink("link=" . $params['title'], NULL);
372
373 $params['event_id'] = $this->urlArg('id');
374
375 return $params;
376 }
377 }
378