-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_import.test
588 lines (556 loc) · 23.5 KB
/
user_import.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<?php
/**
* User Import module base test class.
*/
class UserImportWebTestCase extends DrupalWebTestCase {
protected $admin_user;
protected $user_importer;
/**
* Select CSV file (the included example file)
*/
public function settingsFileSelect() {
$edit = array('file_ftp' => 1);
$this->drupalPost('admin/people/user_import/add', $edit, t('Next'));
/* Check file was selected */
$this->assertText(t('Use Different CSV File'), '[assert] File was selected');
}
public function settingsEmailMatch(&$edit) {
$edit['field_match[5][field_match]'] = 'user-email';
}
public function settingsIgnoreFirstLine(&$edit) {
$edit['first_line_skip'] = 1;
}
public function checkAccountsExist($list_failures = FALSE) {
$failures_list = '';
$users_email = $this->usersList();
$failed = array();
foreach ($users_email as $mail) {
$user = user_load_by_mail($mail);
if (empty($user)) {
$failed[] = $mail;
}
}
if (!empty($failed) && $list_failures) {
$failures_list = t('. Failed accounts: %failures', array('%failures' => implode(', ', $failed)));
}
$this->assertTrue(empty($failed), t('Accounts created for users imported') . $failures_list);
}
/**
* List of users (email addresses) being imported
* To Do - Generate this dynamically, bearing in mind it could be used for stress testing
*/
public function usersList() {
return array(
);
}
/**
* Store import ID
* - set on import settings page, retrieve on later tasks
*/
public function importID($url = NULL) {
static $import_id = 0;
if (empty($import_id) && !empty($url)) {
$args = explode('/', $url);
$import_id = $args[7];
}
return $import_id;
}
/**
* SimpleTest core method: code run after each and every test method.
*/
public function tearDown() {
// delete accounts of users imported
$users_email = $this->usersList();
foreach ($users_email as $mail) {
$account = user_load_by_mail($mail);
if (!empty($account)) {
user_delete($account->uid);
}
}
// delete the import
$import_id = $this->importID();
$this->assertTrue(!empty($import_id), t('Import ID: !id', array('!id' => $import_id)));
_user_import_settings_deletion($import_id);
// Always call the tearDown() function from the parent class.
parent::tearDown();
}
}
/**
* User Import module base test class.
*/
class UserImportBasicsTestCase extends UserImportWebTestCase {
public static function getInfo() {
return array(
'name' => 'Import Users (Basics)',
'description' => 'Import users from a CSV file, test basic functions.',
'group' => 'User Import',
);
}
public function setUp() {
parent::setUp(array('user_import'));
$this->admin_user = $this->drupalCreateUser(array('administer users', 'access administration pages', 'access overlay'));
$this->user_importer = $this->drupalCreateUser(array('import users'));
}
/**
* User with right permissions creates import (with new settings)
* - test core functions
*/
public function testCreateImport() {
// Prepare a user to do testing
$this->drupalLogin($this->user_importer);
// Select CSV file (the included example file)
$this->settingsFileSelect();
// import settings
// store import ID for later
$this->importID($this->getUrl());
$setting_edit = array();
$this->settingsEmailMatch($settings);
$this->settingsIgnoreFirstLine($settings);
$this->drupalPost($this->getUrl(), $settings, 'Import');
// check if users have been imported
$this->checkAccountsExist(TRUE);
}
}
/**
* Test import of user data into Profile module
*/
//class UserImportNodeprofileTestCase extends UserImportWebTestCase {
//
// public static function getInfo() {
// return array(
// 'name' => 'Import Users (Nodeprofile)',
// 'description' => 'Test import of user data into Nodeprofile module.',
// 'group' => 'User Import',
// );
// }
//
// function setUp() {
// parent::setUp('content', 'number', 'optionwidgets', 'text', 'link', 'date_api', 'date', 'node_import', 'content_profile', 'user_import');
// $this->admin_user = $this->drupalCreateUser(array('administer users', 'administer permissions', 'access administration pages', 'administer site configuration', 'administer content types', 'administer taxonomy'));
// $this->user_importer = $this->drupalCreateUser(array('import users'));
// }
//
// /**
// * User with right permissions creates import (with new settings)
// * - test import of user data into Nodeprofile module
// */
// function testCreateImport() {
// $this->drupalLogin($this->admin_user);
// // $this->drupalGet('admin/build/modules');
// // file_put_contents('output.html', $this->drupalGetContent());
//
// $this->nodeprofileConfiguration();
//
// // Prepare a user to do testing
// $this->drupalGet('logout'); // log out first
// $this->drupalLogin($this->user_importer);
//
// // Select CSV file (the included example file)
// $this->settingsFileSelect();
//
// // import settings
// $this->importID($this->getUrl()); // store import ID for later
// $settings = array();
// $this->settingsEmailMatch($settings);
// $this->settingsNodeprofileMatch($settings);
// $this->settingsIgnoreFirstLine($settings);
// $this->drupalPost($this->getUrl(), $settings, 'Import');
//
// // check if users have been imported
// $this->checkNodeprofileExist();
//
// }
//
// /**
// * Configure modules
// */
// function nodeprofileConfiguration() {
//
// // create Identity content type
// $edit = array('name' => 'Identity', 'type' => 'identity', 'title_label' => 'Title', 'body_label' => '', 'node_options[promote]' => 0, 'content_profile_use' => 1);
// $this->drupalPost('admin/content/types/add', $edit, t('Save content type'));
//
// // create First Name field
// $edit = array('_add_new_field[label]' => 'First Name', '_add_new_field[field_name]' => 'first_name', '_add_new_field[type]' => 'text', '_add_new_field[widget_type]' => 'text_textfield');
// $this->drupalPost('admin/content/node-type/identity/fields', $edit, t('Save'));
//
// $edit = array('required' => 1);
// $this->drupalPost('admin/content/node-type/identity/fields/field_first_name', $edit, t('Save field settings'));
//
// // create Last Name field
// $edit = array('_add_new_field[label]' => 'Last Name', '_add_new_field[field_name]' => 'last_name', '_add_new_field[type]' => 'text', '_add_new_field[widget_type]' => 'text_textfield');
// $this->drupalPost('admin/content/node-type/identity/fields', $edit, t('Save'));
//
// $edit = array('required' => 1);
// $this->drupalPost('admin/content/node-type/identity/fields/field_last_name', $edit, t('Save field settings'));
//
// // create Biography content type
// $edit = array('name' => 'Biography', 'type' => 'biography', 'title_label' => 'Title', 'body_label' => '', 'node_options[promote]' => 0, 'content_profile_use' => 1);
// $this->drupalPost('admin/content/types/add', $edit, t('Save content type'));
//
// // create CV field
// $edit = array('_add_new_field[label]' => 'CV', '_add_new_field[field_name]' => 'cv', '_add_new_field[type]' => 'text', '_add_new_field[widget_type]' => 'text_textarea');
// $this->drupalPost('admin/content/node-type/biography/fields', $edit, t('Save'));
//
// $edit = array('required' => 1, 'rows' => 5);
// $this->drupalPost('admin/content/node-type/biography/fields/field_cv', $edit, t('Save field settings'));
//
// // create Blog field (URL)
// $edit = array('_add_new_field[label]' => 'Blog', '_add_new_field[field_name]' => 'blog', '_add_new_field[type]' => 'link', '_add_new_field[widget_type]' => 'link');
// $this->drupalPost('admin/content/node-type/biography/fields', $edit, t('Save'));
//
// // create Birthday field (date)
// $edit = array('_add_new_field[label]' => 'Birthday', '_add_new_field[field_name]' => 'birthday', '_add_new_field[type]' => 'datestamp', '_add_new_field[widget_type]' => 'date_text');
// $this->drupalPost('admin/content/node-type/biography/fields', $edit, t('Save'));
//
// // create Interests Vocabulary
// $edit = array('name' => 'Interests', 'nodes[biography]' => 'biography', 'tags' => 1);
// $this->drupalPost('admin/content/taxonomy/add/vocabulary', $edit, t('Save'));
//
// $vocabularies = taxonomy_get_vocabularies('biography');
//
// foreach ($vocabularies as $vocabulary) {
// $this->vocabulary_id = $vocabulary->vid;
// }
//
// // create Contact Details contact type
// $edit = array('name' => 'Contact Details', 'type' => 'contact_details', 'title_label' => 'Title', 'body_label' => '', 'node_options[promote]' => 0, 'content_profile_use' => 1);
// $this->drupalPost('admin/content/types/add', $edit, t('Save content type'));
//
// // create Can Be Contacted field
// $edit = array('_add_new_field[label]' => 'Contact', '_add_new_field[field_name]' => 'can_be_contacted', '_add_new_field[type]' => 'number_integer', '_add_new_field[widget_type]' => 'optionwidgets_onoff');
// $this->drupalPost('admin/content/node-type/contact-details/fields', $edit, t('Save'));
//
// $edit = array('allowed_values' => "0
// 1|Can be contacted");
// $this->drupalPost('admin/content/node-type/contact-details/fields/field_can_be_contacted', $edit, t('Save field settings'));
//
// // create Contact Preference field
// $edit = array('_add_new_field[label]' => 'Contact Preference', '_add_new_field[field_name]' => 'contact_preference', '_add_new_field[type]' => 'text', '_add_new_field[widget_type]' => 'optionwidgets_select');
// $this->drupalPost('admin/content/node-type/contact-details/fields', $edit, t('Save'));
//
// $edit = array('allowed_values' => 'email|email
// telephone|telephone
// post|post');
// $this->drupalPost('admin/content/node-type/contact-details/fields/field_contact_preference', $edit, t('Save field settings'));
//
// // set access perm for authenticated users to creade profile nodes
// $edit = array('2[create identity content]' => 'create identity content', '2[create biography content]' => 'create biography content', '2[create contact_details content]' => 'create contact_details content');
// $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
// }
//
//
// /**
// * Match CSV columns to Profile fields
// */
// function settingsNodeprofileMatch(&$edit) {
// $edit['field_match[0][field_match]'] = 'content_profile-identity cck:field_first_name:value'; // First Name
// $edit['field_match[1][field_match]'] = 'content_profile-identity cck:field_last_name:value'; // Last Name
// $edit['field_match[10][field_match]'] = 'content_profile-biography cck:field_cv:value'; // CV
// $edit['field_match[7][field_match]'] = 'content_profile-contact_details cck:field_can_be_contacted:value'; // Contact Permision
// $edit['field_match[8][field_match]'] = 'content_profile-contact_details cck:field_contact_preference:value'; // Contact Preference
//// $edit['field_match[9][field_match]'] = 'taxonomy-' . $this->vocabulary_id; // Interests
// $edit['field_match[6][field_match]'] = 'content_profile-biography cck:field_blog:url'; // Blog
// $edit['field_match[11][field_match]'] = 'content_profile-biography cck:field_birthday:value'; // Birthday
// }
//
// /**
// * Check data in CSV file matches data in profiles
// */
// function checkNodeprofileExist() {
// $file_path = drupal_get_path('module', 'user_import') . '/sample.txt';
// $handle = @fopen($file_path, "r");
// $row = 0;
//
// while ($csv = fgetcsv($handle, 1000, ',')) {
//
// if ($row > 0) {
//
// $user = user_load_by_mail($csv[5]);
// // test each data cell against nodeprofile field content
// $identity = node_load(array('type' => 'identity', 'uid' => $user->uid), NULL, TRUE);
// $this->drupalGet("node/$identity->nid");
// $this->assertText(check_plain($csv[0]), "[Compare CSV and Profile data] Row: $row Field: First Name");
// $this->assertText(check_plain($csv[1]), "[Compare CSV and Profile data] Row: $row Field: Last Name " . $csv[1]);
//
// $biography = node_load(array('type' => 'biography', 'uid' => $user->uid), NULL, TRUE);
// $this->drupalGet("node/$biography->nid");
// $this->assertText($csv[6], "[Compare CSV and Profile data] Row: $row Field: Blog");
// $this->assertText($csv[10], "[Compare CSV and Profile data] Row: $row Field: CV");
// $birthday = format_date(strtotime($csv[11]), 'custom', 'D, j/m/Y');
// $this->assertText($birthday, "[Compare CSV and Profile data] Row: $row Field: Birthday " . $birthday);
//
//
// $contact_details = node_load(array('type' => 'contact_details', 'uid' => $user->uid), NULL, TRUE);
// $this->drupalGet("node/$contact_details->nid");
//
// if (isset($csv[7]) && !empty($csv[7])) {
// $this->assertText('Can be contacted', "[Compare CSV and Profile data] Row: $row Field: Contact Permission set");
// }
// else {
// $this->assertNoText('Can be contacted', "[Compare CSV and Profile data] Row: $row Field: Contact Permission not set");
// }
//
// $this->assertText($csv[8], "[Compare CSV and Profile data] Row: $row Field: Contact Preference");
//
// //test interests link on profile page
// if (!empty($user->profile_interests)) {
// $interests = explode(',', $user->profile_interests);
// $this->drupalGet('profile/profile_interests/' . $interests[0]);
// $this->assertWantedRaw('<a title="View user profile." href="/' . url('user/' . $user->uid) . '">' . $user->name . '</a>' , '[Freeform List] User is listed on page about item in list');
// }
//
// }
//
// $row++;
// }
//
// }
//
// /**
// * SimpleTest core method: code run after each and every test method.
// */
// function tearDown() {
//
// // delete accounts of users imported
// $users_email = $this->usersList();
//
// foreach ($users_email as $mail) {
// $account = user_load_by_mail($mail);
// // delete node profile nodes
// if (!empty($account)) {
// $identity = node_load(array('type' => 'identity', 'uid' => $account->uid));
// $biography = node_load(array('type' => 'biography', 'uid' => $account->uid));
// $contact_details = node_load(array('type' => 'contact_details', 'uid' => $account->uid));
// node_delete($identity->nid);
// node_delete($biography->nid);
// node_delete($contact_details->nid);
// user_delete(array(), $account->uid);
// }
// }
//
// // delete the import
// $import_id = $this->importID();
// $this->assertTrue(!empty($import_id), t('Import ID: !id', array('!id' => $import_id)));
// _user_import_settings_deletion($import_id);
//
// // delete vocabulary
// taxonomy_del_vocabulary($this->vocabulary_id);
//
// // uninstall modules
// // - tear down disable doesn't seem to do this
// // foreach($this->modules as $module) {
// // if (function_exists($module . '_uninstall')) {
// // $result = call_user_func_array($module . '_uninstall', array());
// // }
// // }
//
// // delete nodeprofile content types
// node_type_delete('dummy'); // (for some reason) the first node_type_delete is completely ignored
// node_type_delete('identity');
// node_type_delete('biography');
// node_type_delete('contact_details');
//
// // Always call the tearDown() function from the parent class.
// parent::tearDown();
// }
//}
/**
* Test import of user data into Profile module
*/
//class UserImportProfileTestCase extends UserImportWebTestCase {
//
//
// public static function getInfo() {
// return array(
// 'name' => 'Import Users (Profile)',
// 'description' => 'Test import of user data into Profile module.',
// 'group' => 'User Import',
// );
// }
//
// function setUp() {
// parent::setUp('user_import', 'profile');
// $this->admin_user = $this->drupalCreateUser(array('administer users', 'access administration pages', 'administer site configuration'));
// $this->user_importer = $this->drupalCreateUser(array('import users'));
// }
//
// /**
// * User with right permissions creates import (with new settings)
// * - test import of user data into Profile module
// */
// function testCreateImport() {
// $this->drupalLogin($this->admin_user);
// $this->profileFieldsCreate();
//
// // Prepare a user to do testing
// $this->drupalGet('logout'); // log out first
// $this->drupalLogin($this->user_importer);
//
// // Select CSV file (the included example file)
// $this->settingsFileSelect();
//
// // import settings
// $this->importID($this->getUrl()); // store import ID for later
//
// $settings = array();
// $this->settingsEmailMatch($settings);
// $this->settingsProfileMatch($settings);
// $this->settingsIgnoreFirstLine($settings);
// $this->drupalPost($this->getUrl(), $settings, 'Import');
//
// // check if users have been imported
// $this->checkProfileExist();
// }
//
// /**
// * create profile fields
// */
// function profileFieldsCreate() {
//
// // Textfield
// $edit = array('category' => 'Name', 'title' => 'First Name', 'name' => 'profile_first_name');
// $this->drupalPost('admin/people/profile/add/textfield', $edit, t('Save field'));
//
// // Textfield
// $edit = array('category' => 'Name', 'title' => 'Last Name', 'name' => 'profile_last_name');
// $this->drupalPost('admin/people/profile/add/textfield', $edit, t('Save field'));
//
// // Textarea
// $edit = array('category' => 'Biography', 'title' => 'CV', 'name' => 'profile_cv');
// $this->drupalPost('admin/people/profile/add/textarea', $edit, t('Save field'));
//
// // Checkbox
// $edit = array('category' => 'Contact Details', 'title' => 'Can Be Contacted', 'name' => 'profile_contact_permission');
// $this->drupalPost('admin/people/profile/add/checkbox', $edit, t('Save field'));
//
// // List
// $edit = array('category' => 'Contact Details', 'title' => 'Contact Preference', 'name' => 'profile_contact_preference', 'options' => 'email,telephone,post');
// $this->drupalPost('admin/people/profile/add/selection', $edit, t('Save field'));
//
// // Freeform List
// $edit = array('category' => 'Biography', 'title' => 'Interests', 'name' => 'profile_interests');
// $this->drupalPost('admin/people/profile/add/list', $edit, t('Save field'));
//
// // URL
// $edit = array('category' => 'Biography', 'title' => 'Blog', 'name' => 'profile_blog');
// $this->drupalPost('admin/people/profile/add/url', $edit, t('Save field'));
//
// // Date
// $edit = array('category' => 'Biography', 'title' => 'Birthday', 'name' => 'profile_birthday');
// $this->drupalPost('admin/people/profile/add/date', $edit, t('Save field'));
// }
//
// /**
// * Match CSV columns to Profile fields
// */
// function settingsProfileMatch(&$edit) {
// $edit['field_match[0][field_match]'] = 'profile-1'; // First Name
// $edit['field_match[1][field_match]'] = 'profile-2'; // Last Name
// $edit['field_match[10][field_match]'] = 'profile-3'; // CV
// $edit['field_match[7][field_match]'] = 'profile-4'; // Contact Permision
// $edit['field_match[8][field_match]'] = 'profile-5'; // Contact Preference
// $edit['field_match[9][field_match]'] = 'profile-6'; // Interests
// $edit['field_match[6][field_match]'] = 'profile-7'; // Blog
// $edit['field_match[11][field_match]'] = 'profile-8'; // Birthday
// }
//
// /**
// * Check data in CSV file matches data in profiles
// */
// function checkProfileExist() {
//
// $file_path = drupal_get_path('module', 'user_import') . '/sample.txt';
// $handle = @fopen($file_path, "r");
// $row = 0;
//
// while ($csv = fgetcsv($handle, 1000, ',')) {
//
// if ($row > 0) {
// $user = user_load_by_mail($csv[5]);
// // test each data cell against Profile field content
// $profile_first_name = isset($user->profile_first_name) ? $user->profile_first_name : '';
// $this->assertEqual($profile_first_name, $csv[0], "[Compare CSV data to Profile data] Row: $row Field: First Name");
//
// $profile_last_name = isset($user->profile_last_name) ? $user->profile_last_name : '';
// $this->assertEqual($profile_last_name, $csv[1], "[Compare CSV data to Profile data] Row: $row Field: Last Name");
//
// $profile_blog = isset($user->profile_blog) ? $user->profile_blog : '';
// $this->assertEqual($profile_blog, $csv[6], "[Compare CSV data to Profile data] Row: $row Field: Blog");
//
// $profile_contact_permission = isset($user->profile_contact_permission) ? $user->profile_contact_permission : '';
// $file_field_value = (!isset($csv[7]) || empty($csv[7])) ? 0 : 1;
// $this->assertEqual($profile_contact_permission, $file_field_value, "[Compare CSV data to Profile data] Row: $row Field: Contact Permission");
//
// $profile_contact_preference = isset($user->profile_contact_preference) ? $user->profile_contact_preference : '';
// $this->assertEqual($profile_contact_preference, $csv[8], "[Compare CSV data to Profile data] Row: $row Field: Contact Preference");
//
// $profile_interests = isset($user->profile_interests) ? $user->profile_interests : '';
// $this->assertEqual($profile_interests, $csv[9], "[Compare CSV data to Profile data] Row: $row Field: Profile Interests");
//
// $profile_cv = isset($user->profile_cv) ? $user->profile_cv : '';
// $this->assertEqual($profile_cv, $csv[10], "[Compare CSV data to Profile data] Row: $row Field: CV");
//
// $profile_birthday = isset($user->profile_birthday) ? implode('/', $user->profile_birthday) : '';
//
// if (isset($user->profile_birthday)) {
// $profile_birthday = $user->profile_birthday['month'] . '/' . $user->profile_birthday['day'] . '/' . $user->profile_birthday['year'];
// }
// else {
// $profile_birthday = '';
// }
//
// $this->assertEqual($profile_birthday, $csv[11], "[Compare CSV data to Profile data] Row: $row Field: Birthday");
// /**
// * @todo test below fails because it gets an access denied message.
// */
// //test interests link on profile page
// // if (!empty($user->profile_interests)) {
// // $interests = explode(',', $user->profile_interests);
// // $this->drupalGet('profile/profile_interests/' . $interests[0]);
// // $this->assertRaw('<a title="View user profile." href="/'. url('user/'. $user->uid) .'">'. $user->name .'</a>', '[Freeform List] User is listed on page about item in list');
// // }
//
// }
//
// $row++;
// }
// }
//
//}