Projet

Général

Profil

Paste
Télécharger (39,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_processor_user.test @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for plugins/FeedsUserProcessor.inc.
6
 */
7

    
8
/**
9
 * Test aggregating a feed as data records.
10
 */
11
class FeedsCSVtoUsersTest extends FeedsWebTestCase {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'CSV import to users',
19
      'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import users from a CSV file.',
20
      'group' => 'Feeds',
21
    );
22
  }
23

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function setUp() {
28
    parent::setUp();
29

    
30
    // Include FeedsProcessor.inc to make its constants available.
31
    module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
32

    
33
    // Create an importer.
34
    $this->createImporterConfiguration('User import', 'user_import');
35

    
36
    // Set and configure plugins.
37
    $this->setPlugin('user_import', 'FeedsFileFetcher');
38
    $this->setPlugin('user_import', 'FeedsCSVParser');
39
    $this->setPlugin('user_import', 'FeedsUserProcessor');
40

    
41
    // Go to mapping page and create a couple of mappings.
42
    $mappings = array(
43
      0 => array(
44
        'source' => 'name',
45
        'target' => 'name',
46
        'unique' => FALSE,
47
      ),
48
      1 => array(
49
        'source' => 'mail',
50
        'target' => 'mail',
51
        'unique' => TRUE,
52
      ),
53
      2 => array(
54
        'source' => 'since',
55
        'target' => 'created',
56
      ),
57
      3 => array(
58
        'source' => 'password',
59
        'target' => 'pass',
60
      ),
61
    );
62
    $this->addMappings('user_import', $mappings);
63

    
64
    // Use standalone form.
65
    $edit = array(
66
      'content_type' => '',
67
    );
68
    $this->drupalPost('admin/structure/feeds/user_import/settings', $edit, 'Save');
69
  }
70

    
71
  /**
72
   * Test user creation, refreshing/deleting feeds and feed items.
73
   */
74
  public function test() {
75
    // Create roles and assign one of them to the users to be imported.
76
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
77
    $admin_rid = $this->drupalCreateRole(array('access content'), 'administrator');
78
    $edit = array(
79
      "roles[$manager_rid]" => TRUE,
80
      "roles[$admin_rid]" => FALSE,
81
    );
82
    $this->setSettings('user_import', 'FeedsUserProcessor', $edit);
83

    
84
    // Import CSV file.
85
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
86

    
87
    // Assert result.
88
    $this->assertText('Created 3 users');
89
    // 1 user has an invalid email address, all users should be assigned
90
    // the manager role.
91
    $this->assertText('Failed importing 2 users.');
92
    $this->drupalGet('admin/people');
93
    $this->assertText('Morticia');
94
    $this->assertText('Fester');
95
    $this->assertText('Gomez');
96
    $count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $manager_rid))->fetchField();
97
    $this->assertEqual($count, 3, t('All imported users were assigned the manager role.'));
98
    $count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $admin_rid))->fetchField();
99
    $this->assertEqual($count, 0, t('No imported user was assigned the administrator role.'));
100

    
101
    // Run import again, verify no new users.
102
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
103
    $this->assertText('Failed importing 2 users.');
104

    
105
    // Attempt to log in as one of the imported users.
106
    $account = user_load_by_name('Morticia');
107
    $this->assertTrue($account, 'Imported user account loaded.');
108
    $account->pass_raw = 'mort';
109
    $this->drupalLogin($account);
110

    
111
    // Login as admin.
112
    $this->drupalLogin($this->admin_user);
113

    
114
    // Removing a mapping forces updating without needing a different file.
115
    // We are also testing that if we don't map anything to the user's password
116
    // that it will keep its existing one.
117
    $mappings = array(
118
      3 => array(
119
        'source' => 'password',
120
        'target' => 'pass',
121
      ),
122
    );
123
    $this->removeMappings('user_import', $mappings);
124
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2));
125
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
126
    // Assert result.
127
    $this->assertText('Updated 3 users');
128
    $this->assertText('Failed importing 2 user');
129

    
130
    // Attempt to log in as one of the imported users.
131
    $this->feedsLoginUser('Fester', 'fest');
132

    
133
    // Login as admin.
134
    $this->drupalLogin($this->admin_user);
135

    
136
    // Import modified CSV file, one (valid) user is missing.
137
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2, 'update_non_existent' => 'block'));
138
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users2.csv');
139
    $this->assertText('Blocked 1 user');
140
    $this->assertText('Failed importing 2 user');
141

    
142
    // Import the original CSV file again.
143
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
144
    $this->assertText('Updated 1 user');
145
    $this->assertText('Failed importing 2 user');
146
  }
147

    
148
  /**
149
   * Tests mapping to user ID.
150
   */
151
  public function testUidTarget() {
152
    // Set to update existing users.
153
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
154

    
155
    // Add mapping to user ID.
156
    $this->addMappings('user_import', array(
157
      4 => array(
158
        'source' => 'uid',
159
        'target' => 'uid',
160
        'unique' => TRUE,
161
      ),
162
    ));
163

    
164
    // Create account with uid 202. The username and mail address of this account
165
    // should be updated.
166
    user_save(drupal_anonymous_user(), array(
167
      'uid' => 202,
168
      'name' => 'Joe',
169
      'mail' => 'joe@example.com',
170
      'pass' => 'joe',
171
      'status' => 1,
172
    ));
173

    
174
    // Import CSV file.
175
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
176
    $this->assertText('Created 2 users');
177
    $this->assertText('Updated 1 user');
178

    
179
    // Assert user ID's.
180
    $account = user_load_by_name('Morticia');
181
    $this->assertEqual(201, $account->uid, 'Morticia got user ID 201.');
182
    $account = user_load_by_name('Gomez');
183
    $this->assertEqual(203, $account->uid, 'Gomez got user ID 203.');
184

    
185
    // Assert that the username and mail address of account 202 were changed.
186
    $account = user_load(202);
187
    $values = array(
188
      'name' => array(
189
        'expected' => 'Fester',
190
        'actual' => $account->name,
191
      ),
192
      'mail' => array(
193
        'expected' => 'fester@example.com',
194
        'actual' => $account->mail,
195
      ),
196
    );
197
    $this->assertEqual($values['name']['expected'], $values['name']['actual'], format_string('Username of account 202 changed in @expected (actual: @actual).', array(
198
      '@expected' => $values['name']['expected'],
199
      '@actual' => $values['name']['actual'],
200
    )));
201
    $this->assertEqual($values['mail']['expected'], $values['mail']['actual'], format_string('Mail address of account 202 changed in @expected (actual: @actual).', array(
202
      '@expected' => $values['mail']['expected'],
203
      '@actual' => $values['mail']['actual'],
204
    )));
205

    
206
    // Assert that user Joe no longer exists in the system.
207
    $this->assertFalse(user_load_by_name('Joe'), 'No user with username Joe exists.');
208
    $this->assertFalse(user_load_by_mail('joe@example.com'), 'No user with mail address joe@example.com exists.');
209
  }
210

    
211
  /**
212
   * Tests if user ID's can be changed using the user ID target.
213
   *
214
   * Also checks if a clear error is reported when trying to change the
215
   * user ID to something that is already in use.
216
   */
217
  public function testUidUpdating() {
218
    // Set to update existing users.
219
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
220

    
221
    // Add mapping to user ID, but do not mark target as unique.
222
    $this->addMappings('user_import', array(
223
      4 => array(
224
        'source' => 'uid',
225
        'target' => 'uid',
226
      ),
227
    ));
228

    
229
    // Create an account which user ID should be updated.
230
    user_save(drupal_anonymous_user(), array(
231
      'uid' => 54,
232
      'name' => 'Morticia',
233
      'mail' => 'morticia@example.com',
234
      'pass' => 'mort',
235
      'status' => 1,
236
    ));
237

    
238
    // Create account with uid 202. Importing an other account with uid 202
239
    // should fail.
240
    user_save(drupal_anonymous_user(), array(
241
      'uid' => 202,
242
      'name' => 'Joe',
243
      'mail' => 'joe@example.com',
244
      'pass' => 'joe',
245
      'status' => 1,
246
    ));
247

    
248
    // Import CSV file.
249
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
250
    $this->assertText('Created 1 user');
251
    $this->assertText('Updated 1 user');
252
    $this->assertText('Failed importing 3 users.');
253
    $this->assertText('Could not update user ID to 202 since that ID is already in use.');
254

    
255
    // Assert Morticia's user ID got updated.
256
    $account = user_load_by_name('Morticia');
257
    $this->assertEqual(201, $account->uid, 'Morticia now got user ID 201.');
258
    // Assert that Fester failed to import.
259
    $this->assertFalse(user_load_by_name('Fester'), 'The account for Fester was not imported.');
260
    // Assert that user 202 did not change.
261
    $account = user_load(202);
262
    $this->assertEqual('Joe', $account->name, 'The user name of account 202 is still Joe.');
263
    $this->assertEqual('joe@example.com', $account->mail, 'The mail address of account 202 is still joe@example.com.');
264
  }
265

    
266
  /**
267
   * Tests mapping to role without automatically creating new roles.
268
   */
269
  public function testRoleTargetWithoutRoleCreation() {
270
    // Add mapping to role.
271
    $this->addMappings('user_import', array(
272
      4 => array(
273
        'source' => 'roles',
274
        'target' => 'roles_list',
275
      ),
276
    ));
277

    
278
    // Create manager role.
279
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
280

    
281
    // Import CSV file.
282
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
283

    
284
    // Assert that Morticia did not get the editor role and has one role in
285
    // total.
286
    $account = user_load_by_name('Morticia');
287
    $this->assertFalse(in_array('editor', $account->roles), 'Morticia does not have the editor role.');
288
    $this->assertEqual(1, count($account->roles), 'Morticia has one role.');
289

    
290
    // Assert that Fester got the manager role and two roles in total.
291
    $account = user_load_by_name('Fester');
292
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Fester has the manager role.');
293
    $this->assertEqual(2, count($account->roles), 'Fester has two roles.');
294

    
295
    // Assert that Gomez got the manager role but not the tester role, since
296
    // that role doesn't exist on the system.
297
    $account = user_load_by_name('Gomez');
298
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
299
    $this->assertFalse(in_array('tester', $account->roles), 'Gomez does not have the tester role.');
300
    $this->assertEqual(2, count($account->roles), 'Gomez has two roles.');
301

    
302
    // Assert that Pugsley only has one role.
303
    $account = user_load_by_name('Pugsley');
304
    $this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
305

    
306
    // Assert that only three roles exist:
307
    // - authenticated user
308
    // - role from the admin user
309
    // - manager
310
    $roles = user_roles(TRUE);
311
    $this->assertEqual(3, count($roles), 'Only three roles exist.');
312
  }
313

    
314
  /**
315
   * Tests mapping to role with automatically creating new roles.
316
   */
317
  public function testRoleTargetWithRoleCreation() {
318
    // Add mapping to role.
319
    $this->addMappings('user_import', array(
320
      4 => array(
321
        'source' => 'roles',
322
        'target' => 'roles_list',
323
        'autocreate' => TRUE,
324
      ),
325
    ));
326

    
327
    // Create manager role.
328
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
329

    
330
    // Import CSV file.
331
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
332

    
333
    // Assert that Morticia got the editor role and two roles in total.
334
    $account = user_load_by_name('Morticia');
335
    $this->assertTrue(in_array('editor', $account->roles), 'Morticia has the editor role.');
336
    $this->assertEqual(2, count($account->roles), 'Morticia has two roles.');
337

    
338
    // Assert that Fester got the manager role and two roles in total.
339
    $account = user_load_by_name('Fester');
340
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Fester has the manager role.');
341
    $this->assertEqual(2, count($account->roles), 'Fester has two roles.');
342

    
343
    // Assert that Gomez got the manager, the editor role and three roles in
344
    // total.
345
    $account = user_load_by_name('Gomez');
346
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
347
    $this->assertTrue(in_array('tester', $account->roles), 'Gomez has the tester role.');
348
    $this->assertEqual(3, count($account->roles), 'Gomez has three roles.');
349

    
350
    // Assert that Pugsley only has one role.
351
    $account = user_load_by_name('Pugsley');
352
    $this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
353

    
354
    // Assert that five roles exist:
355
    // - authenticated user
356
    // - role from the admin user
357
    // - manager
358
    // - editor
359
    // - tester
360
    $roles = user_roles(TRUE);
361
    $this->assertEqual(5, count($roles), 'Five roles exist.');
362
  }
363

    
364
  /**
365
   * Tests mapping to role using role ID's.
366
   */
367
  public function testRoleTargetRids() {
368
    // Add mapping to role.
369
    $this->addMappings('user_import', array(
370
      4 => array(
371
        'source' => 'rids',
372
        'target' => 'roles_list',
373
        'role_search' => FeedsUserProcessor::ROLE_SEARCH_RID,
374
      ),
375
    ));
376

    
377
    // Create manager and tester roles.
378
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
379
    $tester_rid = $this->drupalCreateRole(array('access content'), 'tester');
380
    // Ensure expected ID's of these roles.
381
    $this->assertEqual(4, $manager_rid);
382
    $this->assertEqual(5, $tester_rid);
383

    
384
    // Import CSV file.
385
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
386

    
387
    // Assert that Morticia did not get the editor role and has one role in
388
    // total.
389
    $account = user_load_by_name('Morticia');
390
    $this->assertFalse(in_array('editor', $account->roles), 'Morticia does not have the editor role.');
391
    $this->assertEqual(1, count($account->roles), 'Morticia has one role.');
392

    
393
    // Assert that Fester got the manager role and two roles in total.
394
    $account = user_load_by_name('Fester');
395
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Fester has the manager role.');
396
    $this->assertEqual(2, count($account->roles), 'Fester has two roles.');
397

    
398
    // Assert that Gomez got the manager and tester roles.
399
    $account = user_load_by_name('Gomez');
400
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
401
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the tester role.');
402
    $this->assertEqual(3, count($account->roles), 'Gomez has two roles.');
403

    
404
    // Assert that Pugsley only has one role.
405
    $account = user_load_by_name('Pugsley');
406
    $this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
407

    
408
    // Assert that four roles exist:
409
    // - authenticated user
410
    // - role from the admin user
411
    // - manager
412
    // - tester
413
    $roles = user_roles(TRUE);
414
    $this->assertEqual(4, count($roles), 'Four roles exist.');
415
  }
416

    
417
  /**
418
   * Tests mapping to role using only allowed roles.
419
   */
420
  public function testRoleTargetWithAllowedRoles() {
421
    // Create manager role.
422
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
423
    // Create editor role.
424
    $editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
425

    
426
    // Add mapping to role.
427
    // The manager role may not be assigned to the user by the feed.
428
    $this->addMappings('user_import', array(
429
      4 => array(
430
        'source' => 'roles',
431
        'target' => 'roles_list',
432
        'allowed_roles' => array(
433
          $manager_rid => FALSE,
434
          $editor_rid => $editor_rid,
435
        ),
436
        'autocreate' => TRUE,
437
      ),
438
    ));
439

    
440
    // Import CSV file.
441
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
442

    
443
    // Assert that Morticia got the editor role and two roles in total.
444
    $account = user_load_by_name('Morticia');
445
    $this->assertTrue(in_array('editor', $account->roles), 'Morticia has the editor role.');
446
    $this->assertEqual(2, count($account->roles), 'Morticia has two roles.');
447

    
448
    // Assert that Fester did not got the manager role, because that role was
449
    // not an allowed value.
450
    $account = user_load_by_name('Fester');
451
    $this->assertFalse(isset($account->roles[$manager_rid]), 'Fester does not have the manager role.');
452
    $this->assertEqual(1, count($account->roles), 'Fester has one role.');
453

    
454
    // Assert that Gomez only got the tester role and not the manager role.
455
    $account = user_load_by_name('Gomez');
456
    $this->assertFalse(isset($account->roles[$manager_rid]), 'Gomez does not have the manager role.');
457
    $this->assertTrue(in_array('tester', $account->roles), 'Gomez has the tester role.');
458
    $this->assertEqual(2, count($account->roles), 'Gomez has two roles.');
459
  }
460

    
461
  /**
462
   * Tests that roles can be revoked and that only allowed roles are revoked.
463
   */
464
  public function testRoleTargetRevokeRoles() {
465
    // Create manager role.
466
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
467
    // Create editor role.
468
    $editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
469
    // Create tester role.
470
    $tester_rid = $this->drupalCreateRole(array('access content'), 'tester');
471

    
472
    // Set to update existing users.
473
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
474

    
475
    // Add mapping to role.
476
    // The manager role may not be revoked, but the editor role may.
477
    $this->addMappings('user_import', array(
478
      4 => array(
479
        'source' => 'roles',
480
        'target' => 'roles_list',
481
        'allowed_roles' => array(
482
          $manager_rid => FALSE,
483
          $editor_rid => $editor_rid,
484
          $tester_rid => $tester_rid,
485
        ),
486
      ),
487
    ));
488

    
489
    // Create account for Morticia with roles "manager" and "editor". In the
490
    // source only "editor" is specified. Morticia should keep both roles.
491
    user_save(drupal_anonymous_user(), array(
492
      'name' => 'Morticia',
493
      'mail' => 'morticia@example.com',
494
      'pass' => 'mort',
495
      'status' => 1,
496
      'roles' => array(
497
        $manager_rid => $manager_rid,
498
        $editor_rid => $editor_rid,
499
      ),
500
    ));
501
    // Create account for Pugsley with roles "manager", "editor" and "tester".
502
    // Pugsley has no roles in the source so should only keep the "manager"
503
    // role.
504
    user_save(drupal_anonymous_user(), array(
505
      'name' => 'Pugsley',
506
      'mail' => 'pugsley@example.com',
507
      'pass' => 'pugs',
508
      'status' => 1,
509
      'roles' => array(
510
        $manager_rid => $manager_rid,
511
        $editor_rid => $editor_rid,
512
        $tester_rid => $tester_rid,
513
      ),
514
    ));
515
    // Create account for Gomez and give it the "editor" role. Gomez has roles
516
    // "tester" and "manager" in the source, so it should lose the "editor" role
517
    // and gain the "tester" role.
518
    user_save(drupal_anonymous_user(), array(
519
      'name' => 'Gomez',
520
      'mail' => 'gomez@example.com',
521
      'pass' => 'gome',
522
      'status' => 1,
523
      'roles' => array(
524
        $editor_rid => $editor_rid,
525
      ),
526
    ));
527

    
528
    // Import CSV file.
529
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
530

    
531
    // Assert that Morticia kept the manager and editor roles.
532
    $account = user_load_by_name('Morticia');
533
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Morticia still has the manager role.');
534
    $this->assertTrue(isset($account->roles[$editor_rid]), 'Morticia has the editor role.');
535
    $this->assertEqual(3, count($account->roles), 'Morticia has three roles.');
536

    
537
    // Assert that Pugsley only kept the manager role.
538
    $account = user_load_by_name('Pugsley');
539
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Pugsley still has the manager role.');
540
    $this->assertFalse(isset($account->roles[$editor_rid]), 'Pugsley no longer has the editor role.');
541
    $this->assertFalse(isset($account->roles[$tester_rid]), 'Pugsley no longer has the tester role.');
542
    $this->assertEqual(2, count($account->roles), 'Pugsley has two roles.');
543

    
544
    // Assert that Gomez lost the editor role, and gained the tester role.
545
    $account = user_load_by_name('Gomez');
546
    $this->assertFalse(isset($account->roles[$editor_rid]), 'Gomez no longer has the editor role.');
547
    $this->assertTrue(isset($account->roles[$tester_rid]), 'Gomez has the tester role.');
548
    $this->assertEqual(2, count($account->roles), 'Gomez has two roles.');
549
  }
550

    
551
  /**
552
   * Tests if no roles are revoked if the option "Revoke roles" is disabled.
553
   */
554
  public function testRoleTargetNoRevokeRoles() {
555
    // Create manager role.
556
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
557
    // Create editor role.
558
    $editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
559

    
560
    // Set to update existing users.
561
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
562

    
563
    // Add mapping to role. Set option to not revoke roles.
564
    $this->addMappings('user_import', array(
565
      4 => array(
566
        'source' => 'roles',
567
        'target' => 'roles_list',
568
        'allowed_roles' => array(
569
          $manager_rid => FALSE,
570
          $editor_rid => $editor_rid,
571
        ),
572
        'revoke_roles' => FALSE,
573
      ),
574
    ));
575

    
576
    // Create account for Pugsley with roles "manager" and "editor". Pugsley has
577
    // no roles, but roles should not be revoked, so Pugsley should keep all
578
    // roles.
579
    user_save(drupal_anonymous_user(), array(
580
      'name' => 'Pugsley',
581
      'mail' => 'pugsley@example.com',
582
      'pass' => 'pugs',
583
      'status' => 1,
584
      'roles' => array(
585
        $manager_rid => $manager_rid,
586
        $editor_rid => $editor_rid,
587
      ),
588
    ));
589

    
590
    // Import CSV file.
591
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
592

    
593
    // Assert that Pugsley kept all roles.
594
    $account = user_load_by_name('Pugsley');
595
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Pugsley still has the manager role.');
596
    $this->assertTrue(isset($account->roles[$editor_rid]), 'Pugsley still has the editor role.');
597
    $this->assertEqual(3, count($account->roles), 'Pugsley has three roles.');
598
  }
599

    
600
  /**
601
   * Tests if additional roles are assigned when creating or updating users.
602
   */
603
  public function testAdditionalRolesSetting() {
604
    // Create manager role.
605
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
606
    // Create editor role.
607
    $editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
608

    
609
    // Set that the "manager" role should be assigned to every user that is
610
    // imported.
611
    $this->setSettings('user_import', 'FeedsUserProcessor', array(
612
      "roles[$manager_rid]" => TRUE,
613
      'update_existing' => FEEDS_UPDATE_EXISTING,
614
    ));
615

    
616
    // Create account for Gomez and give it the "editor" role. After import
617
    // Gomez should have the roles "editor" and "manager".
618
    user_save(drupal_anonymous_user(), array(
619
      'name' => 'Gomez',
620
      'mail' => 'gomez@example.com',
621
      'pass' => 'gome',
622
      'status' => 1,
623
      'roles' => array(
624
        $editor_rid => $editor_rid,
625
      ),
626
    ));
627

    
628
    // Import CSV file.
629
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
630

    
631
    // Assert that every imported user has gained the "manager" role.
632
    $user_names = array(
633
      'Morticia',
634
      'Fester',
635
      'Pugsley',
636
    );
637
    foreach ($user_names as $user_name) {
638
      $vars = array(
639
        '@user' => $user_name,
640
      );
641
      // Assert that this user has the "manager" role.
642
      $account = user_load_by_name($user_name);
643
      $this->assertTrue(isset($account->roles[$manager_rid]), format_string('@user has the manager role.', $vars));
644
      $this->assertEqual(2, count($account->roles), format_string('@user has two roles.', $vars));
645
    }
646

    
647
    // Assert that Gomez has gained the role "manager" and still has the
648
    // "editor" role.
649
    $account = user_load_by_name('Gomez');
650
    $this->assertTrue(isset($account->roles[$editor_rid]), 'Gomez still has the editor role.');
651
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
652
    $this->assertEqual(3, count($account->roles), 'Gomez has three roles.');
653
  }
654

    
655
  /**
656
   * Tests if additional roles are assigned when also the role mapper is used.
657
   */
658
  public function testAdditionalRolesSettingWithRoleTarget() {
659
    // Create manager role.
660
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
661
    // Create editor role.
662
    $editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
663
    // Create tester role.
664
    $tester_rid = $this->drupalCreateRole(array('access content'), 'tester');
665

    
666
    // Set that the "manager" role should be assigned to every user that is
667
    // imported.
668
    $this->setSettings('user_import', 'FeedsUserProcessor', array(
669
      "roles[$manager_rid]" => TRUE,
670
      'update_existing' => FEEDS_UPDATE_EXISTING,
671
    ));
672
    // Add mapping to role.
673
    $this->addMappings('user_import', array(
674
      4 => array(
675
        'source' => 'roles',
676
        'target' => 'roles_list',
677
      ),
678
    ));
679

    
680
    // Create account for Morticia with roles "manager" and "tester". In the
681
    // source, Morticia does not have the "manager" role, but because on the
682
    // user processor settings that is an additional role to add, that role
683
    // should not be revoked. The "tester" role, on the other hand, should be
684
    // revoked.
685
    user_save(drupal_anonymous_user(), array(
686
      'name' => 'Morticia',
687
      'mail' => 'morticia@example.com',
688
      'pass' => 'mort',
689
      'status' => 1,
690
      'roles' => array(
691
        $manager_rid => $manager_rid,
692
        $tester_rid => $tester_rid,
693
      ),
694
    ));
695

    
696
    // Import CSV file.
697
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
698

    
699
    // Assert that Morticia kept the "manager" role, lost the "tester" role and
700
    // gained the "editor" role.
701
    $account = user_load_by_name('Morticia');
702
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Morticia still has the manager role.');
703
    $this->assertTrue(isset($account->roles[$editor_rid]), 'Morticia has the editor role.');
704
    $this->assertFalse(isset($account->roles[$tester_rid]), 'Morticia no longer has the tester role.');
705
    $this->assertEqual(3, count($account->roles), 'Morticia has three roles.');
706

    
707
    // Assert that all other imported users got the "manager" role as well.
708
    $user_names = array(
709
      'Fester',
710
      'Gomez',
711
      'Pugsley',
712
    );
713
    foreach ($user_names as $user_name) {
714
      $vars = array(
715
        '@user' => $user_name,
716
      );
717
      // Assert that this user has the "manager" role.
718
      $account = user_load_by_name($user_name);
719
      $this->assertTrue(isset($account->roles[$manager_rid]), format_string('@user has the manager role.', $vars));
720
    }
721
  }
722

    
723
  /**
724
   * Tests if roles are replaced when replacing users.
725
   */
726
  public function testAdditionalRolesSettingWhenReplacingUsers() {
727
    // Create manager role.
728
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
729
    // Create editor role.
730
    $editor_rid = $this->drupalCreateRole(array('access content'), 'editor');
731

    
732
    // Set that the "manager" role should be assigned to every user that is
733
    // imported. Other roles should be revoked.
734
    $this->setSettings('user_import', 'FeedsUserProcessor', array(
735
      "roles[$manager_rid]" => TRUE,
736
      'update_existing' => FEEDS_REPLACE_EXISTING,
737
    ));
738

    
739
    // Create account for Morticia with no roles. Morticia should gain the
740
    // "manager" role.
741
    user_save(drupal_anonymous_user(), array(
742
      'name' => 'Morticia',
743
      'mail' => 'morticia@example.com',
744
      'pass' => 'mort',
745
      'status' => 1,
746
    ));
747

    
748
    // Create account for Gomez and give it the "editor" role. After import
749
    // Gomez should have lost the role "editor" and gained the role "manager".
750
    user_save(drupal_anonymous_user(), array(
751
      'name' => 'Gomez',
752
      'mail' => 'gomez@example.com',
753
      'pass' => 'gome',
754
      'status' => 1,
755
      'roles' => array(
756
        $editor_rid => $editor_rid,
757
      ),
758
    ));
759

    
760
    // Import CSV file.
761
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
762

    
763
    // Assert that Morticia has gained the role "manager".
764
    $account = user_load_by_name('Morticia');
765
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Morticia has the manager role.');
766
    $this->assertEqual(2, count($account->roles), 'Morticia has two roles.');
767

    
768
    // Assert that Gomez has gained the role "manager" and but no longer has the
769
    // "editor" role.
770
    $account = user_load_by_name('Gomez');
771
    $this->assertFalse(isset($account->roles[$editor_rid]), 'Gomez no longer has the editor role.');
772
    $this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.');
773
    $this->assertEqual(2, count($account->roles), 'Gomez has two roles.');
774

    
775
    // Now remove all default roles and import again.
776
    $this->setSettings('user_import', 'FeedsUserProcessor', array(
777
      "roles[$manager_rid]" => FALSE,
778
      'skip_hash_check' => TRUE,
779
    ));
780
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
781

    
782
    // Reset loaded users cache.
783
    entity_get_controller('user')->resetCache();
784

    
785
    // Assert that Morticia no longer has the role "manager".
786
    $account = user_load_by_name('Morticia');
787
    $this->assertFalse(isset($account->roles[$manager_rid]), 'Morticia no longer has the manager role.');
788
    $this->assertEqual(1, count($account->roles), 'Morticia has one role.');
789
  }
790

    
791
  /**
792
   * Test if users with md5 passwords can login after import.
793
   */
794
  public function testMD5() {
795
    // Set to update existing users.
796
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
797

    
798
    // Replace password mapper.
799
    $this->removeMappings('user_import', array(
800
      3 => array(
801
        'source' => 'password',
802
        'target' => 'pass',
803
      ),
804
    ));
805
    $this->addMappings('user_import', array(
806
      3 => array(
807
        'source' => 'password_md5',
808
        'target' => 'pass',
809
        'pass_encryption' => 'md5',
810
      ),
811
    ));
812

    
813
    // Create an account for Gomez, to ensure passwords can also be imported for
814
    // existing users. Give Gomez a password different from the one that gets
815
    // imported to ensure that his password gets updated.
816
    user_save(drupal_anonymous_user(), array(
817
      'name' => 'Gomez',
818
      'mail' => 'gomez@example.com',
819
      'pass' => 'temporary',
820
      'status' => 1,
821
    ));
822

    
823
    // Import CSV file.
824
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
825

    
826
    // Assert result.
827
    $this->assertText('Created 2 users');
828
    $this->assertText('Updated 1 user');
829

    
830
    // Try to login as each successful imported user.
831
    $this->feedsLoginUser('Morticia', 'mort');
832
    $this->feedsLoginUser('Fester', 'fest');
833
    $this->feedsLoginUser('Gomez', 'gome');
834
  }
835

    
836
  /**
837
   * Test if users with sha512 passwords can login after import.
838
   */
839
  public function testSha512() {
840
    // Set to update existing users.
841
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
842

    
843
    // Replace password mapper.
844
    $this->removeMappings('user_import', array(
845
      3 => array(
846
        'source' => 'password',
847
        'target' => 'pass',
848
      ),
849
    ));
850
    $this->addMappings('user_import', array(
851
      3 => array(
852
        'source' => 'password_sha512',
853
        'target' => 'pass',
854
        'pass_encryption' => 'sha512',
855
      ),
856
    ));
857

    
858
    // Create an account for Gomez, to ensure passwords can also be imported for
859
    // existing users. Give Gomez a password different from the one that gets
860
    // imported to ensure that his password gets updated.
861
    user_save(drupal_anonymous_user(), array(
862
      'name' => 'Gomez',
863
      'mail' => 'gomez@example.com',
864
      'pass' => 'temporary',
865
      'status' => 1,
866
    ));
867

    
868
    // Import CSV file.
869
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
870

    
871
    // Assert result.
872
    $this->assertText('Created 2 users');
873
    $this->assertText('Updated 1 user');
874

    
875
    // Try to login as each successful imported user.
876
    $this->feedsLoginUser('Morticia', 'mort');
877
    $this->feedsLoginUser('Fester', 'fest');
878
    $this->feedsLoginUser('Gomez', 'gome');
879
  }
880

    
881
  /**
882
   * Tests mapping to timezone.
883
   */
884
  public function testTimezoneTarget() {
885
    // Set to update existing users.
886
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
887

    
888
    // Add mapping to timezone.
889
    $this->addMappings('user_import', array(
890
      4 => array(
891
        'source' => 'timezone',
892
        'target' => 'timezone',
893
      ),
894
    ));
895

    
896
    // Create an account for Fester, to ensure that the timezone can be emptied.
897
    user_save(drupal_anonymous_user(), array(
898
      'name' => 'Fester',
899
      'mail' => 'fester@example.com',
900
      'pass' => 'fest',
901
      'status' => 1,
902
      'timezone' => 'Europe/Lisbon',
903
    ));
904

    
905
    // Import CSV file.
906
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
907

    
908
    // Assert that Morticia got the UTC timezone.
909
    $account = user_load_by_name('Morticia');
910
    $this->assertEqual('UTC', $account->timezone, 'Morticia has the UTC timezone.');
911

    
912
    // Assert that Fester did not get any timezone.
913
    $account = user_load_by_name('Fester');
914
    $this->assertFalse($account->timezone, 'Fester does not have any timezone');
915

    
916
    // Assert that Gomez doesn't exist after import and appropriate message is
917
    // displayed.
918
    $account = user_load_by_name('Gomez');
919
    $this->assertFalse($account, "Gomez doesn't exist after import.");
920
    $this->assertText("Failed importing 'Gomez'. User's timezone is not valid.");
921
  }
922

    
923
  /**
924
   * Tests if user 1 cannot be deleted using the delete non-existing feature.
925
   */
926
  public function testUser1ProtectionWhenDeletingNonExistent() {
927
    // Set to delete non-existing users.
928
    $this->setSettings('user_import', 'FeedsFileFetcher', array());
929
    $this->setSettings('user_import', 'FeedsUserProcessor', array(
930
      'update_existing' => FEEDS_UPDATE_EXISTING,
931
      'update_non_existent' => 'delete',
932
    ));
933

    
934
    // Set mail address of user 1 to "fester@example.com". An user with this
935
    // mail address is missing in the feed later.
936
    $account = user_load(1);
937
    $edit['mail'] = 'fester@example.com';
938
    user_save($account, $edit);
939

    
940
    // Import the first file, which contains the mail address of user 1.
941
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
942
    $this->assertText('Updated 1 user');
943
    // Ensure the username of user 1 was updated.
944
    $account = user_load(1, TRUE);
945
    $this->assertEqual('Fester', $account->name);
946

    
947
    // Now import the second file, where the mail address of user 1 is missing.
948
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users2.csv');
949
    $this->assertNoText('Removed 1 user');
950

    
951
    // Ensure that user 1 still exists.
952
    $account = db_select('users')
953
      ->fields('users')
954
      ->condition('uid', 1)
955
      ->execute()
956
      ->fetch();
957
    $this->assertTrue(is_object($account), 'User 1 still exists.');
958
  }
959

    
960
  /**
961
   * Tests if user 1 cannot be deleted using the delete form.
962
   */
963
  public function testUser1ProtectionWhenDeletingAll() {
964
    // Set to update existing users.
965
    $this->setSettings('user_import', 'FeedsFileFetcher', array());
966
    $this->setSettings('user_import', 'FeedsUserProcessor', array(
967
      'update_existing' => FEEDS_UPDATE_EXISTING,
968
    ));
969

    
970
    // Set mail address of user 1 to "fester@example.com".
971
    $account = user_load(1);
972
    $edit['mail'] = 'fester@example.com';
973
    user_save($account, $edit);
974

    
975
    // Import a file that contains the mail address of user 1.
976
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
977
    $this->assertText('Updated 1 user');
978
    // Ensure the username of user 1 was updated.
979
    $account = user_load(1, TRUE);
980
    $this->assertEqual('Fester', $account->name);
981

    
982
    // Now delete all items. User 1 should not be deleted.
983
    $this->drupalPost('import/user_import/delete-items', array(), 'Delete');
984

    
985
    // Ensure that user 1 still exists.
986
    $account = db_select('users')
987
      ->fields('users')
988
      ->condition('uid', 1)
989
      ->execute()
990
      ->fetch();
991
    $this->assertTrue(is_object($account), 'User 1 still exists.');
992

    
993
    // But ensure that the associated feeds item did got deleted.
994
    $count = db_select('feeds_item')
995
      ->fields('feeds_item')
996
      ->condition('entity_type', 'user')
997
      ->condition('entity_id', 1)
998
      ->countQuery()
999
      ->execute()
1000
      ->fetchField();
1001
    $this->assertEqual(0, $count, 'The feeds item for user 1 was deleted.');
1002
  }
1003

    
1004
  /**
1005
   * Tests if an user with an invalid name is not imported.
1006
   */
1007
  public function testInvalidUsername() {
1008
    // Import a file that contains the mail address of user 1.
1009
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/user_validation/invalid-username.csv');
1010
    $this->assertText('Failed importing 1 user');
1011
    $this->assertText('The username contains an illegal character.');
1012
  }
1013

    
1014
  /**
1015
   * Tests importing an user which name is already taken.
1016
   */
1017
  public function testUserNameAlreadyTaken() {
1018
    // Create an account.
1019
    user_save(drupal_anonymous_user(), array(
1020
      'name' => 'Morticia',
1021
      'mail' => 'morticia2@example.com',
1022
      'pass' => 'mort',
1023
      'status' => 1,
1024
    ));
1025

    
1026
    // Import a file that contains a username that is already taken.
1027
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/user_validation/username-already-taken.csv');
1028
    $this->assertText('Failed importing 1 user');
1029
    $this->assertText("The name 'Morticia' is already taken.");
1030
  }
1031

    
1032
  /**
1033
   * Tests importing an user which mail address is already taken.
1034
   */
1035
  public function testMailAlreadyTaken() {
1036
    // Do not mark mail address as unique.
1037
    $path = 'admin/structure/feeds/user_import/mapping';
1038
    $this->drupalPostAJAX($path, array(), 'mapping_settings_edit_1');
1039
    $edit = array("config[1][settings][unique]" => FALSE);
1040
    $this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_1');
1041
    $this->drupalPost(NULL, array(), t('Save'));
1042

    
1043
    // Create an account.
1044
    user_save(drupal_anonymous_user(), array(
1045
      'name' => 'Morticia2',
1046
      'mail' => 'morticia@example.com',
1047
      'pass' => 'mort',
1048
      'status' => 1,
1049
    ));
1050

    
1051
    // Import a file that contains a mail address that is already taken.
1052
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/user_validation/username-already-taken.csv');
1053
    $this->assertText('Failed importing 1 user');
1054
    $this->assertText("The e-mail address 'morticia@example.com' is already taken.");
1055
  }
1056

    
1057
  /**
1058
   * Log in an imported user.
1059
   *
1060
   * @param string $username
1061
   *   The user's username.
1062
   * @param string $password
1063
   *   The user's password.
1064
   */
1065
  protected function feedsLoginUser($username, $password) {
1066
    $account = user_load_by_name($username);
1067
    $this->assertTrue($account, 'Imported user account loaded.');
1068
    $account->pass_raw = $password;
1069
    $this->drupalLogin($account);
1070
  }
1071

    
1072
}