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