'User migration', 'description' => 'Test migration of user data', 'group' => 'Migrate', ); } function setUp() { parent::setUp('migrate', 'migrate_example'); // To test timestamps date_default_timezone_set('US/Mountain'); } function testUserImport() { $migration = Migration::getInstance('WineUser'); $result = $migration->processImport(); $this->assertEqual($result, Migration::RESULT_COMPLETED, t('User import returned RESULT_COMPLETED')); $result = db_select('migrate_example_wine_account', 'mea') ->fields('mea', array('accountid', 'status', 'posted', 'name', 'sex', 'password', 'mail', 'last_access', 'last_login', 'sig', 'original_mail')) ->execute(); $uids = db_select('users', 'u') ->fields('u', array('uid')) ->execute() ->fetchCol(); // Index by name $users = array(); foreach ($uids as $uid) { // Skip anon/admin users if ($uid > 1) { $account = user_load($uid); $users[$account->name] = $account; } } $rows = array(); foreach ($result as $row) { $rows[$row->name] = $row; } $this->assertEqual(count($users), count($rows), t('Counts of users and input rows match')); // Test each base user field $this->assert(isset($users['darren']) && isset($rows['darren']), t("Username 'darren' migrated correctly")); $this->assertEqual($users['darren']->mail, $rows['darren']->mail, t('Email addresses match')); $this->assertEqual($users['darren']->status, $rows['darren']->status, t('Statuses match')); $this->assertNotNull($users['darren']->roles[2], t('Authenticated role')); $this->assertEqual($users['darren']->created, strtotime($rows['darren']->posted), t('Created times match')); $this->assertEqual($users['darren']->access, strtotime($rows['darren']->last_access), t('Access times match')); $this->assertEqual($users['darren']->login, strtotime($rows['darren']->last_login), t('Login times match')); $this->assertTrue(user_check_password($rows['darren']->password, $users['darren']), t('Passwords match')); $this->assertEqual($users['darren']->init, $rows['darren']->original_mail, t('Init mails match')); $this->assertEqual($users['darren']->signature, $rows['darren']->sig, t('Signatures match')); $this->assertEqual($users['darren']->signature_format, $migration->basicFormat->format, t('Signature formats match')); $this->assertEqual($users['darren']->field_migrate_example_gender[LANGUAGE_NONE][0]['value'], 0, t('Male gender migrated')); $this->assertEqual($users['emily']->field_migrate_example_gender[LANGUAGE_NONE][0]['value'], 1, t('Female gender migrated')); $this->assert(!isset($users['fonzie']->field_migrate_example_gender[LANGUAGE_NONE][0]['value']), t('Missing gender left unmigrated')); // TODO: Theme, timezone, language, picture // Test updates // Capture original terms $original_users = user_load_multiple(array(), array(), TRUE); $update_migration = Migration::getInstance('WineUserUpdates'); $result = $update_migration->processImport(); $this->assertEqual($result, Migration::RESULT_COMPLETED, t('Wine user updates import returned RESULT_COMPLETED')); $final_users = user_load_multiple(array(), array(), TRUE); foreach ($original_users as $uid => $original_user) { foreach ($original_user as $field => $value) { if ($field == 'field_migrate_example_gender') { if ($value == $final_users[$uid]->$field) { $this->error(t('Field !field should have changed but did not, value=!value', array('!field' => $field, '!value' => print_r($value, TRUE)))); } } else { if ($value != $final_users[$uid]->$field) { $this->error(t('Field !field mismatch: original !value1, result !value2', array('!field' => $field, '!value1' => print_r($value, TRUE), '!value2' => print_r($final_users[$uid]->$field, TRUE)))); } } } } // Test rollback $result = $migration->processRollback(); $this->assertEqual($result, Migration::RESULT_COMPLETED, t('User rollback returned RESULT_COMPLETED')); $count = db_select('users', 'u') ->fields('u', array('uid')) ->countQuery() ->execute() ->fetchField(); // 2 users left - anon and admin $this->assertEqual($count, 2, t('All imported users deleted')); $count = db_select('migrate_map_wineuser', 'map') ->fields('map', array('sourceid1')) ->countQuery() ->execute() ->fetchField(); $this->assertEqual($count, 0, t('Map cleared')); $count = db_select('migrate_message_wineuser', 'msg') ->fields('msg', array('sourceid1')) ->countQuery() ->execute() ->fetchField(); $this->assertEqual($count, 0, t('Messages cleared')); } }