feeds_processor_user.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @file
  4. * Tests for plugins/FeedsUserProcessor.inc
  5. */
  6. /**
  7. * Test aggregating a feed as data records.
  8. */
  9. class FeedsCSVtoUsersTest extends FeedsWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'CSV import to users',
  13. 'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import users from a CSV file.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. /**
  18. * Test node creation, refreshing/deleting feeds and feed items.
  19. */
  20. public function test() {
  21. // Create an importer.
  22. $this->createImporterConfiguration('User import', 'user_import');
  23. // Set and configure plugins.
  24. $this->setPlugin('user_import', 'FeedsFileFetcher');
  25. $this->setPlugin('user_import', 'FeedsCSVParser');
  26. $this->setPlugin('user_import', 'FeedsUserProcessor');
  27. // Go to mapping page and create a couple of mappings.
  28. $mappings = array(
  29. 0 => array(
  30. 'source' => 'name',
  31. 'target' => 'name',
  32. 'unique' => FALSE,
  33. ),
  34. 1 => array(
  35. 'source' => 'mail',
  36. 'target' => 'mail',
  37. 'unique' => TRUE,
  38. ),
  39. 2 => array(
  40. 'source' => 'since',
  41. 'target' => 'created',
  42. ),
  43. 3 => array(
  44. 'source' => 'password',
  45. 'target' => 'pass',
  46. ),
  47. );
  48. $this->addMappings('user_import', $mappings);
  49. // Use standalone form.
  50. $edit = array(
  51. 'content_type' => '',
  52. );
  53. $this->drupalPost('admin/structure/feeds/user_import/settings', $edit, 'Save');
  54. // Create roles and assign one of them to the users to be imported.
  55. $manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
  56. $admin_rid = $this->drupalCreateRole(array('access content'), 'administrator');
  57. $edit = array(
  58. "roles[$manager_rid]" => TRUE,
  59. "roles[$admin_rid]" => FALSE,
  60. );
  61. $this->setSettings('user_import', 'FeedsUserProcessor', $edit);
  62. // Import CSV file.
  63. $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
  64. // Assert result.
  65. $this->assertText('Created 3 users');
  66. // 1 user has an invalid email address, all users should be assigned
  67. // the manager role.
  68. $this->assertText('Failed importing 2 users.');
  69. $this->drupalGet('admin/people');
  70. $this->assertText('Morticia');
  71. $this->assertText('Fester');
  72. $this->assertText('Gomez');
  73. $count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $manager_rid))->fetchField();
  74. $this->assertEqual($count, 3, t('All imported users were assigned the manager role.'));
  75. $count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $admin_rid))->fetchField();
  76. $this->assertEqual($count, 0, t('No imported user was assigned the administrator role.'));
  77. // Run import again, verify no new users.
  78. $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
  79. $this->assertText('Failed importing 2 users.');
  80. // Attempt to log in as one of the imported users.
  81. $account = user_load_by_name('Morticia');
  82. $this->assertTrue($account, 'Imported user account loaded.');
  83. $account->pass_raw = 'mort';
  84. $this->drupalLogin($account);
  85. // Login as admin.
  86. $this->drupalLogin($this->admin_user);
  87. // Removing a mapping forces updating without needing a different file.
  88. // We are also testing that if we don't map anything to the user's password
  89. // that it will keep its existing one.
  90. $mappings = array(
  91. 3 => array(
  92. 'source' => 'password',
  93. 'target' => 'pass',
  94. ),
  95. );
  96. $this->removeMappings('user_import', $mappings);
  97. $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2));
  98. $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
  99. // Assert result.
  100. $this->assertText('Updated 3 users');
  101. $this->assertText('Failed importing 2 user');
  102. // Attempt to log in as one of the imported users.
  103. $account = user_load_by_name('Fester');
  104. $this->assertTrue($account, 'Imported user account loaded.');
  105. $account->pass_raw = 'fest';
  106. $this->drupalLogin($account);
  107. // Login as admin.
  108. $this->drupalLogin($this->admin_user);
  109. // Import modified CSV file, one (valid) user is missing.
  110. $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2, 'update_non_existent' => 'block'));
  111. $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users2.csv');
  112. $this->assertText('Blocked 1 user');
  113. $this->assertText('Failed importing 2 user');
  114. // Import the original CSV file again.
  115. $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
  116. $this->assertText('Updated 1 user');
  117. $this->assertText('Failed importing 2 user');
  118. }
  119. }