user.test 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the user destination plugin.
  5. */
  6. /**
  7. * Test user migration.
  8. */
  9. class MigrateUserUnitTest extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'User migration',
  13. 'description' => 'Test migration of user data',
  14. 'group' => 'Migrate',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('migrate', 'migrate_example');
  19. // To test timestamps
  20. date_default_timezone_set('US/Mountain');
  21. }
  22. function testUserImport() {
  23. $migration = Migration::getInstance('WineFileCopy');
  24. $result = $migration->processImport();
  25. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  26. t('File import returned RESULT_COMPLETED'));
  27. $migration = Migration::getInstance('WineRole');
  28. $result = $migration->processImport();
  29. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  30. t('Role import returned RESULT_COMPLETED'));
  31. // Confirm both roles were successfully imported
  32. $result = db_select('role', 'r')
  33. ->fields('r', array('rid', 'name'))
  34. ->condition('name', array('Taster', 'Vintner'), 'IN')
  35. ->execute();
  36. $roles = array();
  37. foreach ($result as $row) {
  38. $roles[$row->name] = $row->rid;
  39. }
  40. $this->assertEqual(count($roles), 2, t('Both roles imported'));
  41. $migration = Migration::getInstance('WineUser');
  42. $result = $migration->processImport();
  43. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  44. t('User import returned RESULT_COMPLETED'));
  45. $result = db_select('migrate_example_wine_account', 'mea')
  46. ->fields('mea', array('accountid', 'status', 'posted', 'name',
  47. 'sex', 'password', 'mail', 'last_access', 'last_login',
  48. 'sig', 'original_mail'))
  49. ->execute();
  50. $uids = db_select('users', 'u')
  51. ->fields('u', array('uid'))
  52. ->execute()
  53. ->fetchCol();
  54. // Index by name
  55. $users = array();
  56. foreach ($uids as $uid) {
  57. // Skip anon/admin users
  58. if ($uid > 1) {
  59. $account = user_load($uid);
  60. $users[$account->name] = $account;
  61. }
  62. }
  63. $rows = array();
  64. foreach ($result as $row) {
  65. $rows[$row->name] = $row;
  66. }
  67. $this->assertEqual(count($users), count($rows),
  68. t('Counts of users and input rows match'));
  69. // Test each base user field
  70. $this->assert(isset($users['darren']) && isset($rows['darren']),
  71. t("Username 'darren' migrated correctly"));
  72. $this->assertEqual($users['darren']->mail, $rows['darren']->mail,
  73. t('Email addresses match'));
  74. $this->assertEqual($users['darren']->status, $rows['darren']->status,
  75. t('Statuses match'));
  76. $this->assertNotNull($users['darren']->roles[2], t('Authenticated role'));
  77. $this->assertNotNull($users['darren']->roles[$roles['Taster']], t('Taster role'));
  78. $this->assertFalse(isset($users['darren']->roles[$roles['Vintner']]), t('No Vintner role'));
  79. $this->assertEqual($users['darren']->created, strtotime($rows['darren']->posted),
  80. t('Created times match'));
  81. $this->assertEqual($users['darren']->access, strtotime($rows['darren']->last_access),
  82. t('Access times match'));
  83. $this->assertEqual($users['darren']->login, strtotime($rows['darren']->last_login),
  84. t('Login times match'));
  85. $this->assertTrue(user_check_password($rows['darren']->password, $users['darren']),
  86. t('Passwords match'));
  87. $this->assertEqual($users['darren']->init, $rows['darren']->original_mail,
  88. t('Init mails match'));
  89. $this->assertEqual($users['darren']->signature, $rows['darren']->sig,
  90. t('Signatures match'));
  91. $this->assertEqual($users['darren']->signature_format, $migration->basicFormat->format,
  92. t('Signature formats match'));
  93. $this->assertEqual($users['darren']->field_migrate_example_gender[LANGUAGE_NONE][0]['value'],
  94. 0, t('Male gender migrated'));
  95. $this->assertEqual($users['emily']->field_migrate_example_gender[LANGUAGE_NONE][0]['value'],
  96. 1, t('Female gender migrated'));
  97. $this->assert(!isset($users['fonzie']->field_migrate_example_gender[LANGUAGE_NONE][0]['value']),
  98. t('Missing gender left unmigrated'));
  99. $this->assert(is_object($users['fonzie']->picture) &&
  100. $users['fonzie']->picture->filename == 'association-individual.png',
  101. t('Picture migrated'));
  102. $this->assertNotNull($users['fonzie']->roles[$roles['Taster']], t('Taster role'));
  103. $this->assertNotNull($users['fonzie']->roles[$roles['Vintner']], t('Vintner role'));
  104. // TODO: Theme, timezone, language
  105. // Test updates
  106. // Capture original users
  107. $query = new EntityFieldQuery;
  108. $result = $query
  109. ->entityCondition('entity_type', 'user')
  110. ->propertyCondition('uid', 1, '>')
  111. ->execute();
  112. $uids = array_keys($result['user']);
  113. $original_users = user_load_multiple($uids, array(), TRUE);
  114. $update_migration = Migration::getInstance('WineUserUpdates');
  115. $result = $update_migration->processImport();
  116. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  117. t('Wine user updates import returned RESULT_COMPLETED'));
  118. $final_users = user_load_multiple($uids, array(), TRUE);
  119. foreach ($original_users as $uid => $original_user) {
  120. foreach ($original_user as $field => $value) {
  121. if ($field == 'field_migrate_example_gender') {
  122. if ($value == $final_users[$uid]->$field) {
  123. $this->error(t('For user !name, field !field should have changed but did not, value=!value',
  124. array('!name' => $final_users[$uid]->name, '!field' => $field,
  125. '!value' => print_r($value, TRUE))));
  126. }
  127. }
  128. else {
  129. if ($value != $final_users[$uid]->$field) {
  130. // Core bug http://drupal.org/node/935592 causes picture mismatches, ignore until it's fixed
  131. if ($field != 'picture') {
  132. $this->error(t('For user !name, field !field mismatch: original !value1, result !value2',
  133. array('!name' => $final_users[$uid]->name, '!field' => $field,
  134. '!value1' => print_r($value, TRUE),
  135. '!value2' => print_r($final_users[$uid]->$field, TRUE))));
  136. }
  137. }
  138. }
  139. }
  140. }
  141. // Test rollback
  142. $result = $migration->processRollback(array('force' => TRUE));
  143. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  144. t('User rollback returned RESULT_COMPLETED'));
  145. $count = db_select('users', 'u')
  146. ->fields('u', array('uid'))
  147. ->countQuery()
  148. ->execute()
  149. ->fetchField();
  150. // 2 users left - anon and admin
  151. $this->assertEqual($count, 2, t('All imported users deleted'));
  152. $count = db_select('migrate_map_wineuser', 'map')
  153. ->fields('map', array('sourceid1'))
  154. ->countQuery()
  155. ->execute()
  156. ->fetchField();
  157. $this->assertEqual($count, 0, t('Map cleared'));
  158. $count = db_select('migrate_message_wineuser', 'msg')
  159. ->fields('msg', array('sourceid1'))
  160. ->countQuery()
  161. ->execute()
  162. ->fetchField();
  163. $this->assertEqual($count, 0, t('Messages cleared'));
  164. // Test deduping
  165. // First, do the original import
  166. $migration = Migration::getInstance('BeerUser');
  167. $result = $migration->processImport();
  168. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  169. t('User import returned RESULT_COMPLETED'));
  170. $accounts = db_select('users', 'u')
  171. ->fields('u', array('mail', 'name'))
  172. ->execute()
  173. ->fetchAllKeyed();
  174. if (!$this->assertEqual($accounts['alice@example.com'], 'alice', t('alice found'))) {
  175. $this->error(t('Expected alice, found !name', array('!name' => $accounts['alice@example.com'])));
  176. }
  177. if (!$this->assertEqual($accounts['alice2@example.com'], 'alice_2', t('alice_2 found'))) {
  178. $this->error(t('Expected alice_2, found !name', array('!name' => $accounts['alice2@example.com'])));
  179. }
  180. // Then, update in place and make sure the usernames did not change
  181. $migration->prepareUpdate();
  182. $result = $migration->processImport();
  183. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  184. t('User import returned RESULT_COMPLETED'));
  185. $accounts = db_select('users', 'u')
  186. ->fields('u', array('mail', 'name'))
  187. ->execute()
  188. ->fetchAllKeyed();
  189. if (!$this->assertEqual($accounts['alice@example.com'], 'alice', t('alice found'))) {
  190. $this->error(t('Expected alice, found !name', array('!name' => $accounts['alice@example.com'])));
  191. }
  192. if (!$this->assertEqual($accounts['alice2@example.com'], 'alice_2', t('alice_2 found'))) {
  193. $this->error(t('Expected alice_2, found !name', array('!name' => $accounts['alice2@example.com'])));
  194. }
  195. }
  196. }