You are here

user.test in Migrate 6.2

Same filename and directory in other branches
  1. 7.2 tests/plugins/destinations/user.test

File

tests/plugins/destinations/user.test
View source
<?php

/**
 * Test user migration.
 */
class MigrateUserUnitTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'User migration',
      'description' => 'Test migration of user data',
      'group' => 'Migrate',
    );
  }
  function setUp() {
    parent::setUp('autoload', 'dbtng', 'taxonomy', 'content', 'text', 'number', 'date_api', 'date_timezone', 'date', 'filefield', 'imagefield', 'migrate', 'migrate_extras', 'migrate_example');

    // Make sure the migrations are registered.
    migrate_get_module_apis();

    // To test timestamps
    date_default_timezone_set('US/Mountain');
  }
  function testUserImport() {
    $migration = Migration::getInstance('WineRole');
    $result = $migration
      ->processImport();
    $this
      ->assertEqual($result, Migration::RESULT_COMPLETED, t('Role import returned RESULT_COMPLETED'));

    // Confirm both roles were successfully imported
    $result = db_select('role', 'r')
      ->fields('r', array(
      'rid',
      'name',
    ))
      ->condition('name', array(
      'Taster',
      'Vintner',
    ), 'IN')
      ->execute();
    $roles = array();
    foreach ($result as $row) {
      $roles[$row->name] = $row->rid;
    }
    $this
      ->assertEqual(count($roles), 2, t('Both roles imported'));
    $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',
    ))
      ->condition('uid', 1, '>')
      ->execute()
      ->fetchCol();

    // Index by name
    $users = array();
    foreach ($uids as $uid) {
      $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
      ->assertNotNull($users['darren']->roles[$roles['Taster']], t('Taster role'));
    $this
      ->assertFalse(isset($users['darren']->roles[$roles['Vintner']]), t('No Vintner 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
      ->assertEqual(md5($rows['darren']->password), $users['darren']->pass, 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, t('Signature formats match'));
    $this
      ->assertNotNull($users['fonzie']->roles[$roles['Taster']], t('Taster role'));
    $this
      ->assertNotNull($users['fonzie']->roles[$roles['Vintner']], t('Vintner role'));

    // TODO: Theme, timezone, language, picture
    // Test updates. Well, since we don't have fields on users in D6, we're only
    // testing that updates don't destroy data.
    // Capture original users
    foreach ($uids as $uid) {
      $original_users[$uid] = user_load($uid);
    }
    $update_migration = Migration::getInstance('WineUserUpdates');
    $result = $update_migration
      ->processImport();
    $this
      ->assertEqual($result, Migration::RESULT_COMPLETED, t('Wine user updates import returned RESULT_COMPLETED'));
    foreach ($uids as $uid) {
      $final_users[$uid] = user_load($uid);
    }
    foreach ($original_users as $uid => $original_user) {
      foreach ($original_user as $field => $value) {

        /* Not applicable on D6
           if ($field == 'field_migrate_example_gender') {
             if ($value == $final_users[$uid]->$field) {
               $this->error(t('For user !name, field !field should have changed but did not, value=!value',
                 array('!name' => $final_users[$uid]->name, '!field' => $field,
                   '!value' => print_r($value, TRUE))));
             }
           }
           else {*/
        if ($value != $final_users[$uid]->{$field}) {

          // Note we will get an error here until core bug http://drupal.org/node/935592
          // is fixed.
          $this
            ->error(t('For user !name, field !field mismatch: original !value1, result !value2', array(
            '!name' => $final_users[$uid]->name,
            '!field' => $field,
            '!value1' => print_r($value, TRUE),
            '!value2' => print_r($final_users[$uid]->{$field}, TRUE),
          )));
        }

        //        }
      }
    }

    // Test rollback
    $result = $migration
      ->processRollback(array(
      'force' => TRUE,
    ));
    $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'));

    // Test deduping
    // First, do the original import
    $migration = Migration::getInstance('BeerUser');
    $result = $migration
      ->processImport();
    $this
      ->assertEqual($result, Migration::RESULT_COMPLETED, t('User import returned RESULT_COMPLETED'));
    $accounts = db_select('users', 'u')
      ->fields('u', array(
      'mail',
      'name',
    ))
      ->execute()
      ->fetchAllKeyed();
    if (!$this
      ->assertEqual($accounts['alice@example.com'], 'alice', t('alice found'))) {
      $this
        ->error(t('Expected alice, found !name', array(
        '!name' => $accounts['alice@example.com'],
      )));
    }
    if (!$this
      ->assertEqual($accounts['alice2@example.com'], 'alice_2', t('alice_2 found'))) {
      $this
        ->error(t('Expected alice_2, found !name', array(
        '!name' => $accounts['alice2@example.com'],
      )));
    }

    // Then, update in place and make sure the usernames did not change
    $migration
      ->prepareUpdate();
    $result = $migration
      ->processImport();
    $this
      ->assertEqual($result, Migration::RESULT_COMPLETED, t('User import returned RESULT_COMPLETED'));
    $accounts = db_select('users', 'u')
      ->fields('u', array(
      'mail',
      'name',
    ))
      ->execute()
      ->fetchAllKeyed();
    if (!$this
      ->assertEqual($accounts['alice@example.com'], 'alice', t('alice found'))) {
      $this
        ->error(t('Expected alice, found !name', array(
        '!name' => $accounts['alice@example.com'],
      )));
    }
    if (!$this
      ->assertEqual($accounts['alice2@example.com'], 'alice_2', t('alice_2 found'))) {
      $this
        ->error(t('Expected alice_2, found !name', array(
        '!name' => $accounts['alice2@example.com'],
      )));
    }
  }

}

Classes

Namesort descending Description
MigrateUserUnitTest Test user migration.