Newer
Older
<?php
/**
* @file
* Tests for plugins/FeedsUserProcessor.inc
*/
/**
* Test aggregating a feed as data records.
*/
class FeedsCSVtoUsersTest extends FeedsWebTestCase {
Dave Reid
committed
public static function getInfo() {
return array(
Dave Reid
committed
'name' => 'CSV import to users',
'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import users from a CSV file.',
'group' => 'Feeds',
);
}
/**
* {@inheritdoc}
public function setUp() {
parent::setUp();
// Include FeedsProcessor.inc to make its constants available.
module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
// Create an importer.
$this->createImporterConfiguration('User import', 'user_import');
// Set and configure plugins.
$this->setPlugin('user_import', 'FeedsFileFetcher');
$this->setPlugin('user_import', 'FeedsCSVParser');
$this->setPlugin('user_import', 'FeedsUserProcessor');
// Go to mapping page and create a couple of mappings.
$mappings = array(
niklas
committed
0 => array(
'source' => 'name',
'target' => 'name',
Chris Leppanen
committed
'unique' => FALSE,
niklas
committed
1 => array(
'source' => 'mail',
'target' => 'mail',
Chris Leppanen
committed
'unique' => TRUE,
niklas
committed
2 => array(
'source' => 'since',
'target' => 'created',
),
niklas
committed
3 => array(
'source' => 'password',
'target' => 'pass',
),
);
$this->addMappings('user_import', $mappings);
// Use standalone form.
$edit = array(
'content_type' => '',
);
twistor
committed
$this->drupalPost('admin/structure/feeds/user_import/settings', $edit, 'Save');
}
/**
* Test user creation, refreshing/deleting feeds and feed items.
*/
public function test() {
// Create roles and assign one of them to the users to be imported.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
$admin_rid = $this->drupalCreateRole(array('access content'), 'administrator');
$edit = array(
"roles[$manager_rid]" => TRUE,
"roles[$admin_rid]" => FALSE,
);
$this->setSettings('user_import', 'FeedsUserProcessor', $edit);
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
// Assert result.
// 1 user has an invalid email address, all users should be assigned
// the manager role.
Chris Leppanen
committed
$this->assertText('Failed importing 2 users.');
$this->drupalGet('admin/people');
$this->assertText('Morticia');
$this->assertText('Fester');
$this->assertText('Gomez');
$count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $manager_rid))->fetchField();
$this->assertEqual($count, 3, t('All imported users were assigned the manager role.'));
$count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $admin_rid))->fetchField();
$this->assertEqual($count, 0, t('No imported user was assigned the administrator role.'));
Chris Leppanen
committed
// Run import again, verify no new users.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Failed importing 2 users.');
// Attempt to log in as one of the imported users.
$account = user_load_by_name('Morticia');
$this->assertTrue($account, 'Imported user account loaded.');
$account->pass_raw = 'mort';
$this->drupalLogin($account);
Chris Leppanen
committed
// Login as admin.
$this->drupalLogin($this->admin_user);
// Removing a mapping forces updating without needing a different file.
// We are also testing that if we don't map anything to the user's password
// that it will keep its existing one.
$mappings = array(
3 => array(
'source' => 'password',
'target' => 'pass',
),
);
$this->removeMappings('user_import', $mappings);
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2));
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
// Assert result.
$this->assertText('Updated 3 users');
$this->assertText('Failed importing 2 user');
// Attempt to log in as one of the imported users.
$this->feedsLoginUser('Fester', 'fest');
// Login as admin.
$this->drupalLogin($this->admin_user);
// Import modified CSV file, one (valid) user is missing.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2, 'update_non_existent' => 'block'));
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users2.csv');
$this->assertText('Blocked 1 user');
$this->assertText('Failed importing 2 user');
// Import the original CSV file again.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Updated 1 user');
$this->assertText('Failed importing 2 user');
/**
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
* Tests mapping to user ID.
*/
public function testUidTarget() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Add mapping to user ID.
$this->addMappings('user_import', array(
4 => array(
'source' => 'uid',
'target' => 'uid',
'unique' => TRUE,
),
));
// Create account with uid 202. The username and mail address of this account
// should be updated.
user_save(drupal_anonymous_user(), array(
'uid' => 202,
'name' => 'Joe',
'mail' => 'joe@example.com',
'pass' => 'joe',
'status' => 1,
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Created 2 users');
$this->assertText('Updated 1 user');
// Assert user ID's.
$account = user_load_by_name('Morticia');
$this->assertEqual(201, $account->uid, 'Morticia got user ID 201.');
$account = user_load_by_name('Gomez');
$this->assertEqual(203, $account->uid, 'Gomez got user ID 203.');
// Assert that the username and mail address of account 202 were changed.
$account = user_load(202);
$values = array(
'name' => array(
'expected' => 'Fester',
'actual' => $account->name,
),
'mail' => array(
'expected' => 'fester@example.com',
'actual' => $account->mail,
),
);
$this->assertEqual($values['name']['expected'], $values['name']['actual'], format_string('Username of account 202 changed in @expected (actual: @actual).', array(
'@expected' => $values['name']['expected'],
'@actual' => $values['name']['actual'],
)));
$this->assertEqual($values['mail']['expected'], $values['mail']['actual'], format_string('Mail address of account 202 changed in @expected (actual: @actual).', array(
'@expected' => $values['mail']['expected'],
'@actual' => $values['mail']['actual'],
)));
// Assert that user Joe no longer exists in the system.
$this->assertFalse(user_load_by_name('Joe'), 'No user with username Joe exists.');
$this->assertFalse(user_load_by_mail('joe@example.com'), 'No user with mail address joe@example.com exists.');
}
/**
* Tests if user ID's can be changed using the user ID target.
*
* Also checks if a clear error is reported when trying to change the
* user ID to something that is already in use.
*/
public function testUidUpdating() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Add mapping to user ID, but do not mark target as unique.
$this->addMappings('user_import', array(
4 => array(
'source' => 'uid',
'target' => 'uid',
),
));
// Create an account which user ID should be updated.
user_save(drupal_anonymous_user(), array(
'uid' => 54,
'name' => 'Morticia',
'mail' => 'morticia@example.com',
'pass' => 'mort',
'status' => 1,
));
// Create account with uid 202. Importing an other account with uid 202
// should fail.
user_save(drupal_anonymous_user(), array(
'uid' => 202,
'name' => 'Joe',
'mail' => 'joe@example.com',
'pass' => 'joe',
'status' => 1,
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Created 1 user');
$this->assertText('Updated 1 user');
$this->assertText('Failed importing 3 users.');
$this->assertText('Could not update user ID to 202 since that ID is already in use.');
// Assert Morticia's user ID got updated.
$account = user_load_by_name('Morticia');
$this->assertEqual(201, $account->uid, 'Morticia now got user ID 201.');
// Assert that Fester failed to import.
$this->assertFalse(user_load_by_name('Fester'), 'The account for Fester was not imported.');
// Assert that user 202 did not change.
$account = user_load(202);
$this->assertEqual('Joe', $account->name, 'The user name of account 202 is still Joe.');
$this->assertEqual('joe@example.com', $account->mail, 'The mail address of account 202 is still joe@example.com.');
}
/**
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
* Tests mapping to role without automatically creating new roles.
*/
public function testRoleTargetWithoutRoleCreation() {
// Add mapping to role.
$this->addMappings('user_import', array(
4 => array(
'source' => 'roles',
'target' => 'roles_list',
),
));
// Create manager role.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that Morticia did not get the editor role and has one role in
// total.
$account = user_load_by_name('Morticia');
$this->assertFalse(in_array('editor', $account->roles), 'Morticia does not have the editor role.');
$this->assertEqual(1, count($account->roles), 'Morticia has one role.');
// Assert that Fester got the manager role and two roles in total.
$account = user_load_by_name('Fester');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Fester has the manager role.');
$this->assertEqual(2, count($account->roles), 'Fester has two roles.');
// Assert that Gomez got the manager role but not the tester role, since
// that role doesn't exist on the system.
$account = user_load_by_name('Gomez');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
$this->assertFalse(in_array('tester', $account->roles), 'Gomez does not have the tester role.');
$this->assertEqual(2, count($account->roles), 'Gomez has two roles.');
// Assert that Pugsley only has one role.
$account = user_load_by_name('Pugsley');
$this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
// Assert that only three roles exist:
// - authenticated user
// - role from the admin user
// - manager
$roles = user_roles(TRUE);
$this->assertEqual(3, count($roles), 'Only three roles exist.');
}
/**
* Tests mapping to role with automatically creating new roles.
*/
public function testRoleTargetWithRoleCreation() {
// Add mapping to role.
$this->addMappings('user_import', array(
4 => array(
'source' => 'roles',
'target' => 'roles_list',
'autocreate' => TRUE,
),
));
// Create manager role.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that Morticia got the editor role and two roles in total.
$account = user_load_by_name('Morticia');
$this->assertTrue(in_array('editor', $account->roles), 'Morticia has the editor role.');
$this->assertEqual(2, count($account->roles), 'Morticia has two roles.');
// Assert that Fester got the manager role and two roles in total.
$account = user_load_by_name('Fester');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Fester has the manager role.');
$this->assertEqual(2, count($account->roles), 'Fester has two roles.');
// Assert that Gomez got the manager, the editor role and three roles in
// total.
$account = user_load_by_name('Gomez');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
$this->assertTrue(in_array('tester', $account->roles), 'Gomez has the tester role.');
$this->assertEqual(3, count($account->roles), 'Gomez has three roles.');
// Assert that Pugsley only has one role.
$account = user_load_by_name('Pugsley');
$this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
// Assert that five roles exist:
// - authenticated user
// - role from the admin user
// - manager
// - editor
// - tester
$roles = user_roles(TRUE);
$this->assertEqual(5, count($roles), 'Five roles exist.');
}
/**
* Tests mapping to role using role ID's.
*/
public function testRoleTargetRids() {
// Add mapping to role.
$this->addMappings('user_import', array(
4 => array(
'source' => 'rids',
'target' => 'roles_list',
'role_search' => FeedsUserProcessor::ROLE_SEARCH_RID,
),
));
// Create manager and tester roles.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
$tester_rid = $this->drupalCreateRole(array('access content'), 'tester');
// Ensure expected ID's of these roles.
$this->assertEqual(4, $manager_rid);
$this->assertEqual(5, $tester_rid);
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that Morticia did not get the editor role and has one role in
// total.
$account = user_load_by_name('Morticia');
$this->assertFalse(in_array('editor', $account->roles), 'Morticia does not have the editor role.');
$this->assertEqual(1, count($account->roles), 'Morticia has one role.');
// Assert that Fester got the manager role and two roles in total.
$account = user_load_by_name('Fester');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Fester has the manager role.');
$this->assertEqual(2, count($account->roles), 'Fester has two roles.');
// Assert that Gomez got the manager and tester roles.
$account = user_load_by_name('Gomez');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the tester role.');
$this->assertEqual(3, count($account->roles), 'Gomez has two roles.');
// Assert that Pugsley only has one role.
$account = user_load_by_name('Pugsley');
$this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
// Assert that four roles exist:
// - authenticated user
// - role from the admin user
// - manager
// - tester
$roles = user_roles(TRUE);
$this->assertEqual(4, count($roles), 'Four roles exist.');
}
/**
* Tests mapping to role using only allowed roles.
*/
public function testRoleTargetWithAllowedRoles() {
// Create manager role.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
// Create editor role.
$editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
// Add mapping to role.
// The manager role may not be assigned to the user by the feed.
$this->addMappings('user_import', array(
4 => array(
'source' => 'roles',
'target' => 'roles_list',
'allowed_roles' => array(
$manager_rid => FALSE,
$editor_rid => $editor_rid,
),
'autocreate' => TRUE,
),
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that Morticia got the editor role and two roles in total.
$account = user_load_by_name('Morticia');
$this->assertTrue(in_array('editor', $account->roles), 'Morticia has the editor role.');
$this->assertEqual(2, count($account->roles), 'Morticia has two roles.');
// Assert that Fester did not got the manager role, because that role was
// not an allowed value.
$account = user_load_by_name('Fester');
$this->assertFalse(isset($account->roles[$manager_rid]), 'Fester does not have the manager role.');
$this->assertEqual(1, count($account->roles), 'Fester has one role.');
// Assert that Gomez only got the tester role and not the manager role.
$account = user_load_by_name('Gomez');
$this->assertFalse(isset($account->roles[$manager_rid]), 'Gomez does not have the manager role.');
$this->assertTrue(in_array('tester', $account->roles), 'Gomez has the tester role.');
$this->assertEqual(2, count($account->roles), 'Gomez has two roles.');
}
/**
* Tests that roles can be revoked and that only allowed roles are revoked.
*/
public function testRoleTargetRevokeRoles() {
// Create manager role.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
// Create editor role.
$editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
// Create tester role.
$tester_rid = $this->drupalCreateRole(array('access content'), 'tester');
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Add mapping to role.
// The manager role may not be revoked, but the editor role may.
$this->addMappings('user_import', array(
4 => array(
'source' => 'roles',
'target' => 'roles_list',
'allowed_roles' => array(
$manager_rid => FALSE,
$editor_rid => $editor_rid,
$tester_rid => $tester_rid,
),
),
));
// Create account for Morticia with roles "manager" and "editor". In the
// source only "editor" is specified. Morticia should keep both roles.
user_save(drupal_anonymous_user(), array(
'name' => 'Morticia',
'mail' => 'morticia@example.com',
'pass' => 'mort',
'status' => 1,
'roles' => array(
$manager_rid => $manager_rid,
$editor_rid => $editor_rid,
),
));
// Create account for Pugsley with roles "manager", "editor" and "tester".
// Pugsley has no roles in the source so should only keep the "manager"
// role.
user_save(drupal_anonymous_user(), array(
'name' => 'Pugsley',
'mail' => 'pugsley@example.com',
'pass' => 'pugs',
'status' => 1,
'roles' => array(
$manager_rid => $manager_rid,
$editor_rid => $editor_rid,
$tester_rid => $tester_rid,
),
));
// Create account for Gomez and give it the "editor" role. Gomez has roles
// "tester" and "manager" in the source, so it should lose the "editor" role
// and gain the "tester" role.
user_save(drupal_anonymous_user(), array(
'name' => 'Gomez',
'mail' => 'gomez@example.com',
'pass' => 'gome',
'status' => 1,
'roles' => array(
$editor_rid => $editor_rid,
),
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that Morticia kept the manager and editor roles.
$account = user_load_by_name('Morticia');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Morticia still has the manager role.');
$this->assertTrue(isset($account->roles[$editor_rid]), 'Morticia has the editor role.');
$this->assertEqual(3, count($account->roles), 'Morticia has three roles.');
// Assert that Pugsley only kept the manager role.
$account = user_load_by_name('Pugsley');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Pugsley still has the manager role.');
$this->assertFalse(isset($account->roles[$editor_rid]), 'Pugsley no longer has the editor role.');
$this->assertFalse(isset($account->roles[$tester_rid]), 'Pugsley no longer has the tester role.');
$this->assertEqual(2, count($account->roles), 'Pugsley has two roles.');
// Assert that Gomez lost the editor role, and gained the tester role.
$account = user_load_by_name('Gomez');
$this->assertFalse(isset($account->roles[$editor_rid]), 'Gomez no longer has the editor role.');
$this->assertTrue(isset($account->roles[$tester_rid]), 'Gomez has the tester role.');
$this->assertEqual(2, count($account->roles), 'Gomez has two roles.');
}
/**
* Tests if no roles are revoked if the option "Revoke roles" is disabled.
*/
public function testRoleTargetNoRevokeRoles() {
// Create manager role.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
// Create editor role.
$editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Add mapping to role. Set option to not revoke roles.
$this->addMappings('user_import', array(
4 => array(
'source' => 'roles',
'target' => 'roles_list',
'allowed_roles' => array(
$manager_rid => FALSE,
$editor_rid => $editor_rid,
),
'revoke_roles' => FALSE,
),
));
// Create account for Pugsley with roles "manager" and "editor". Pugsley has
// no roles, but roles should not be revoked, so Pugsley should keep all
// roles.
user_save(drupal_anonymous_user(), array(
'name' => 'Pugsley',
'mail' => 'pugsley@example.com',
'pass' => 'pugs',
'status' => 1,
'roles' => array(
$manager_rid => $manager_rid,
$editor_rid => $editor_rid,
),
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that Pugsley kept all roles.
$account = user_load_by_name('Pugsley');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Pugsley still has the manager role.');
$this->assertTrue(isset($account->roles[$editor_rid]), 'Pugsley still has the editor role.');
$this->assertEqual(3, count($account->roles), 'Pugsley has three roles.');
}
/**
* Tests if additional roles are assigned when creating or updating users.
*/
public function testAdditionalRolesSetting() {
// Create manager role.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
// Create editor role.
$editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
// Set that the "manager" role should be assigned to every user that is
// imported.
$this->setSettings('user_import', 'FeedsUserProcessor', array(
"roles[$manager_rid]" => TRUE,
'update_existing' => FEEDS_UPDATE_EXISTING,
));
// Create account for Gomez and give it the "editor" role. After import
// Gomez should have the roles "editor" and "manager".
user_save(drupal_anonymous_user(), array(
'name' => 'Gomez',
'mail' => 'gomez@example.com',
'pass' => 'gome',
'status' => 1,
'roles' => array(
$editor_rid => $editor_rid,
),
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that every imported user has gained the "manager" role.
$user_names = array(
'Morticia',
'Fester',
'Pugsley',
);
foreach ($user_names as $user_name) {
$vars = array(
'@user' => $user_name,
);
// Assert that this user has the "manager" role.
$account = user_load_by_name($user_name);
$this->assertTrue(isset($account->roles[$manager_rid]), format_string('@user has the manager role.', $vars));
$this->assertEqual(2, count($account->roles), format_string('@user has two roles.', $vars));
}
// Assert that Gomez has gained the role "manager" and still has the
// "editor" role.
$account = user_load_by_name('Gomez');
$this->assertTrue(isset($account->roles[$editor_rid]), 'Gomez still has the editor role.');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
$this->assertEqual(3, count($account->roles), 'Gomez has three roles.');
}
/**
* Tests if additional roles are assigned when also the role mapper is used.
*/
public function testAdditionalRolesSettingWithRoleTarget() {
// Create manager role.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
// Create editor role.
$editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
// Create tester role.
$tester_rid = $this->drupalCreateRole(array('access content'), 'tester');
// Set that the "manager" role should be assigned to every user that is
// imported.
$this->setSettings('user_import', 'FeedsUserProcessor', array(
"roles[$manager_rid]" => TRUE,
'update_existing' => FEEDS_UPDATE_EXISTING,
));
// Add mapping to role.
$this->addMappings('user_import', array(
4 => array(
'source' => 'roles',
'target' => 'roles_list',
),
));
// Create account for Morticia with roles "manager" and "tester". In the
// source, Morticia does not have the "manager" role, but because on the
// user processor settings that is an additional role to add, that role
// should not be revoked. The "tester" role, on the other hand, should be
// revoked.
user_save(drupal_anonymous_user(), array(
'name' => 'Morticia',
'mail' => 'morticia@example.com',
'pass' => 'mort',
'status' => 1,
'roles' => array(
$manager_rid => $manager_rid,
$tester_rid => $tester_rid,
),
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that Morticia kept the "manager" role, lost the "tester" role and
// gained the "editor" role.
$account = user_load_by_name('Morticia');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Morticia still has the manager role.');
$this->assertTrue(isset($account->roles[$editor_rid]), 'Morticia has the editor role.');
$this->assertFalse(isset($account->roles[$tester_rid]), 'Morticia no longer has the tester role.');
$this->assertEqual(3, count($account->roles), 'Morticia has three roles.');
// Assert that all other imported users got the "manager" role as well.
$user_names = array(
'Fester',
'Gomez',
'Pugsley',
);
foreach ($user_names as $user_name) {
$vars = array(
'@user' => $user_name,
);
// Assert that this user has the "manager" role.
$account = user_load_by_name($user_name);
$this->assertTrue(isset($account->roles[$manager_rid]), format_string('@user has the manager role.', $vars));
}
}
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/**
* Tests if roles are replaced when replacing users.
*/
public function testAdditionalRolesSettingWhenReplacingUsers() {
// Create manager role.
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
// Create editor role.
$editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
// Set that the "manager" role should be assigned to every user that is
// imported. Other roles should be revoked.
$this->setSettings('user_import', 'FeedsUserProcessor', array(
"roles[$manager_rid]" => TRUE,
'update_existing' => FEEDS_REPLACE_EXISTING,
));
// Create account for Morticia with no roles. Morticia should gain the
// "manager" role.
user_save(drupal_anonymous_user(), array(
'name' => 'Morticia',
'mail' => 'morticia@example.com',
'pass' => 'mort',
'status' => 1,
));
// Create account for Gomez and give it the "editor" role. After import
// Gomez should have lost the role "editor" and gained the role "manager".
user_save(drupal_anonymous_user(), array(
'name' => 'Gomez',
'mail' => 'gomez@example.com',
'pass' => 'gome',
'status' => 1,
'roles' => array(
$editor_rid => $editor_rid,
),
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Assert that Morticia has gained the role "manager".
$account = user_load_by_name('Morticia');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Morticia has the manager role.');
$this->assertEqual(2, count($account->roles), 'Morticia has two roles.');
// Assert that Gomez has gained the role "manager" and but no longer has the
// "editor" role.
$account = user_load_by_name('Gomez');
$this->assertFalse(isset($account->roles[$editor_rid]), 'Gomez no longer has the editor role.');
$this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
$this->assertEqual(2, count($account->roles), 'Gomez has two roles.');
// Now remove all default roles and import again.
$this->setSettings('user_import', 'FeedsUserProcessor', array(
"roles[$manager_rid]" => FALSE,
'skip_hash_check' => TRUE,
));
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
// Reset loaded users cache.
entity_get_controller('user')->resetCache();
// Assert that Morticia no longer has the role "manager".
$account = user_load_by_name('Morticia');
$this->assertFalse(isset($account->roles[$manager_rid]), 'Morticia no longer has the manager role.');
$this->assertEqual(1, count($account->roles), 'Morticia has one role.');
}
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/**
* Test if users with md5 passwords can login after import.
*/
public function testMD5() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Replace password mapper.
$this->removeMappings('user_import', array(
3 => array(
'source' => 'password',
'target' => 'pass',
),
));
$this->addMappings('user_import', array(
3 => array(
'source' => 'password_md5',
'target' => 'pass',
'pass_encryption' => 'md5',
),
));
// Create an account for Gomez, to ensure passwords can also be imported for
// existing users. Give Gomez a password different from the one that gets
// imported to ensure that his password gets updated.
user_save(drupal_anonymous_user(), array(
'name' => 'Gomez',
'mail' => 'gomez@example.com',
'pass' => 'temporary',
'status' => 1,
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
// Assert result.
$this->assertText('Created 2 users');
$this->assertText('Updated 1 user');
// Try to login as each successful imported user.
$this->feedsLoginUser('Morticia', 'mort');
$this->feedsLoginUser('Fester', 'fest');
$this->feedsLoginUser('Gomez', 'gome');
}
/**
* Test if users with sha512 passwords can login after import.
*/
public function testSha512() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Replace password mapper.
$this->removeMappings('user_import', array(
3 => array(
'source' => 'password',
'target' => 'pass',
),
));
$this->addMappings('user_import', array(
3 => array(
'source' => 'password_sha512',
'target' => 'pass',
'pass_encryption' => 'sha512',
),
));
// Create an account for Gomez, to ensure passwords can also be imported for
// existing users. Give Gomez a password different from the one that gets
// imported to ensure that his password gets updated.
user_save(drupal_anonymous_user(), array(
'name' => 'Gomez',
'mail' => 'gomez@example.com',
'pass' => 'temporary',
'status' => 1,
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
// Assert result.
$this->assertText('Created 2 users');
$this->assertText('Updated 1 user');
// Try to login as each successful imported user.
$this->feedsLoginUser('Morticia', 'mort');
$this->feedsLoginUser('Fester', 'fest');
$this->feedsLoginUser('Gomez', 'gome');
}
git
committed
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/**
* Tests mapping to timezone.
*/
public function testTimezoneTarget() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Add mapping to timezone.
$this->addMappings('user_import', array(
4 => array(
'source' => 'timezone',
'target' => 'timezone',
),
));
// Create an account for Fester, to ensure that the timezone can be emptied.
user_save(drupal_anonymous_user(), array(
'name' => 'Fester',
'mail' => 'fester@example.com',
'pass' => 'fest',
'status' => 1,
'timezone' => 'Europe/Lisbon',
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
// Assert that Morticia got the UTC timezone.
$account = user_load_by_name('Morticia');
$this->assertEqual('UTC', $account->timezone, 'Morticia has the UTC timezone.');
// Assert that Fester did not get any timezone.
$account = user_load_by_name('Fester');
$this->assertFalse($account->timezone, 'Fester does not have any timezone');
// Assert that Gomez doesn't exist after import and appropriate message is
// displayed.
$account = user_load_by_name('Gomez');
$this->assertFalse($account, "Gomez doesn't exist after import.");
$this->assertText("Failed importing 'Gomez'. User's timezone is not valid.");
}
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
/**
* Tests if user 1 cannot be deleted using the delete non-existing feature.
*/
public function testUser1ProtectionWhenDeletingNonExistent() {
// Set to delete non-existing users.
$this->setSettings('user_import', 'FeedsFileFetcher', array());
$this->setSettings('user_import', 'FeedsUserProcessor', array(
'update_existing' => FEEDS_UPDATE_EXISTING,
'update_non_existent' => 'delete',
));
// Set mail address of user 1 to "fester@example.com". An user with this
// mail address is missing in the feed later.
$account = user_load(1);
$edit['mail'] = 'fester@example.com';
user_save($account, $edit);
// Import the first file, which contains the mail address of user 1.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Updated 1 user');
// Ensure the username of user 1 was updated.
$account = user_load(1, TRUE);
$this->assertEqual('Fester', $account->name);
// Now import the second file, where the mail address of user 1 is missing.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users2.csv');
$this->assertNoText('Removed 1 user');
// Ensure that user 1 still exists.
$account = db_select('users')
->fields('users')
->condition('uid', 1)
->execute()
->fetch();
$this->assertTrue(is_object($account), 'User 1 still exists.');
}
/**
* Tests if user 1 cannot be deleted using the delete form.
*/
public function testUser1ProtectionWhenDeletingAll() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsFileFetcher', array());
$this->setSettings('user_import', 'FeedsUserProcessor', array(
'update_existing' => FEEDS_UPDATE_EXISTING,
));
// Set mail address of user 1 to "fester@example.com".
$account = user_load(1);
$edit['mail'] = 'fester@example.com';
user_save($account, $edit);
// Import a file that contains the mail address of user 1.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Updated 1 user');
// Ensure the username of user 1 was updated.
$account = user_load(1, TRUE);
$this->assertEqual('Fester', $account->name);
// Now delete all items. User 1 should not be deleted.
$this->drupalPost('import/user_import/delete-items', array(), 'Delete');
// Ensure that user 1 still exists.
$account = db_select('users')
->fields('users')
->condition('uid', 1)
->execute()
->fetch();
$this->assertTrue(is_object($account), 'User 1 still exists.');
// But ensure that the associated feeds item did got deleted.
$count = db_select('feeds_item')
->fields('feeds_item')
->condition('entity_type', 'user')
->condition('entity_id', 1)
->countQuery()
->execute()
->fetchField();
$this->assertEqual(0, $count, 'The feeds item for user 1 was deleted.');
}