Projet

Général

Profil

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

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

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
  public static function getInfo() {
13
    return array(
14
      'name' => 'CSV import to users',
15
      'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import users from a CSV file.',
16
      'group' => 'Feeds',
17
    );
18
  }
19

    
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function setUp() {
24
    parent::setUp();
25

    
26
    // Include FeedsProcessor.inc to make its constants available.
27
    module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
28

    
29
    // Create an importer.
30
    $this->createImporterConfiguration('User import', 'user_import');
31

    
32
    // Set and configure plugins.
33
    $this->setPlugin('user_import', 'FeedsFileFetcher');
34
    $this->setPlugin('user_import', 'FeedsCSVParser');
35
    $this->setPlugin('user_import', 'FeedsUserProcessor');
36

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

    
60
    // Use standalone form.
61
    $edit = array(
62
      'content_type' => '',
63
    );
64
    $this->drupalPost('admin/structure/feeds/user_import/settings', $edit, 'Save');
65
  }
66

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

    
80
    // Import CSV file.
81
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
82

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

    
97
    // Run import again, verify no new users.
98
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
99
    $this->assertText('Failed importing 2 users.');
100

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

    
107
    // Login as admin.
108
    $this->drupalLogin($this->admin_user);
109

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

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

    
129
    // Login as admin.
130
    $this->drupalLogin($this->admin_user);
131

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

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

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

    
151
    // Add mapping to user ID.
152
    $this->addMappings('user_import', array(
153
      4 => array(
154
        'source' => 'uid',
155
        'target' => 'uid',
156
        'unique' => TRUE,
157
      ),
158
    ));
159

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

    
170
    // Import CSV file.
171
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
172
    $this->assertText('Created 2 users');
173
    $this->assertText('Updated 1 user');
174

    
175
    // Assert user ID's.
176
    $account = user_load_by_name('Morticia');
177
    $this->assertEqual(201, $account->uid, 'Morticia got user ID 201.');
178
    $account = user_load_by_name('Gomez');
179
    $this->assertEqual(203, $account->uid, 'Gomez got user ID 203.');
180

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

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

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

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

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

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

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

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

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

    
274
    // Create manager role.
275
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
276

    
277
    // Import CSV file.
278
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
279

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

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

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

    
298
    // Assert that Pugsley only has one role.
299
    $account = user_load_by_name('Pugsley');
300
    $this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
301

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

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

    
323
    // Create manager role.
324
    $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
325

    
326
    // Import CSV file.
327
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
328

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

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

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

    
346
    // Assert that Pugsley only has one role.
347
    $account = user_load_by_name('Pugsley');
348
    $this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
349

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

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

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

    
380
    // Import CSV file.
381
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
382

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

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

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

    
400
    // Assert that Pugsley only has one role.
401
    $account = user_load_by_name('Pugsley');
402
    $this->assertEqual(1, count($account->roles), 'Pugsley has one role.');
403

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

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

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

    
436
    // Import CSV file.
437
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
438

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

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

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

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

    
468
    // Set to update existing users.
469
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
470

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

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

    
524
    // Import CSV file.
525
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
526

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

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

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

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

    
556
    // Set to update existing users.
557
    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
558

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

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

    
586
    // Import CSV file.
587
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
588

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

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

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

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

    
624
    // Import CSV file.
625
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
626

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

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

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

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

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

    
692
    // Import CSV file.
693
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
694

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

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

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

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

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

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

    
756
    // Import CSV file.
757
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv');
758

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

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

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

    
778
    // Reset loaded users cache.
779
    entity_get_controller('user')->resetCache();
780

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

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

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

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

    
819
    // Import CSV file.
820
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
821

    
822
    // Assert result.
823
    $this->assertText('Created 2 users');
824
    $this->assertText('Updated 1 user');
825

    
826
    // Try to login as each successful imported user.
827
    $this->feedsLoginUser('Morticia', 'mort');
828
    $this->feedsLoginUser('Fester', 'fest');
829
    $this->feedsLoginUser('Gomez', 'gome');
830
  }
831

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

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

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

    
864
    // Import CSV file.
865
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
866

    
867
    // Assert result.
868
    $this->assertText('Created 2 users');
869
    $this->assertText('Updated 1 user');
870

    
871
    // Try to login as each successful imported user.
872
    $this->feedsLoginUser('Morticia', 'mort');
873
    $this->feedsLoginUser('Fester', 'fest');
874
    $this->feedsLoginUser('Gomez', 'gome');
875
  }
876

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

    
884
    // Add mapping to timezone.
885
    $this->addMappings('user_import', array(
886
      4 => array(
887
        'source' => 'timezone',
888
        'target' => 'timezone',
889
      ),
890
    ));
891

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

    
901
    // Import CSV file.
902
    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
903

    
904
    // Assert that Morticia got the UTC timezone.
905
    $account = user_load_by_name('Morticia');
906
    $this->assertEqual('UTC', $account->timezone, 'Morticia has the UTC timezone.');
907

    
908
    // Assert that Fester did not get any timezone.
909
    $account = user_load_by_name('Fester');
910
    $this->assertFalse($account->timezone, 'Fester does not have any timezone');
911

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

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

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

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

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

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

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

    
966
    // Set mail address of user 1 to "fester@example.com".
967
    $account = user_load(1);
968
    $edit['mail'] = 'fester@example.com';
969
    user_save($account, $edit);
970

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

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

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

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

    
1000
  /**
1001
   * Log in an imported user.
1002
   *
1003
   * @param string $username
1004
   *   The user's username.
1005
   * @param string $password
1006
   *   The user's password.
1007
   */
1008
  protected function feedsLoginUser($username, $password) {
1009
    $account = user_load_by_name($username);
1010
    $this->assertTrue($account, 'Imported user account loaded.');
1011
    $account->pass_raw = $password;
1012
    $this->drupalLogin($account);
1013
  }
1014
}