View source
<?php
namespace Drupal\user\Tests\Migrate\d6;
use Drupal\migrate\Entity\Migration;
use Drupal\migrate\MigrateExecutable;
use Drupal\user\Entity\User;
use Drupal\file\Entity\File;
use Drupal\Core\Database\Database;
use Drupal\user\RoleInterface;
use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase;
class MigrateUserTest extends MigrateDrupal6TestBase {
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installSchema('file', [
'file_usage',
]);
$this
->installEntitySchema('node');
$this
->installSchema('user', [
'users_data',
]);
user_install();
$file = File::create(array(
'fid' => 2,
'uid' => 2,
'filename' => 'image-test.jpg',
'uri' => "public://image-test.jpg",
'filemime' => 'image/jpeg',
'created' => 1,
'changed' => 1,
'status' => FILE_STATUS_PERMANENT,
));
$file
->enforceIsNew();
file_put_contents($file
->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png'));
$file
->save();
$file = File::create(array(
'fid' => 8,
'uid' => 8,
'filename' => 'image-test.png',
'uri' => "public://image-test.png",
'filemime' => 'image/png',
'created' => 1,
'changed' => 1,
'status' => FILE_STATUS_PERMANENT,
));
$file
->enforceIsNew();
file_put_contents($file
->getFileUri(), file_get_contents('core/modules/simpletest/files/image-2.jpg'));
$file
->save();
$this
->migrateUsers();
}
public function testUser() {
$users = Database::getConnection('default', 'migrate')
->select('users', 'u')
->fields('u')
->condition('uid', 0, '>')
->execute()
->fetchAll();
foreach ($users as $source) {
$rids = Database::getConnection('default', 'migrate')
->select('users_roles', 'ur')
->fields('ur', array(
'rid',
))
->condition('ur.uid', $source->uid)
->execute()
->fetchCol();
$roles = array(
RoleInterface::AUTHENTICATED_ID,
);
$id_map = Migration::load('d6_user_role')
->getIdMap();
foreach ($rids as $rid) {
$role = $id_map
->lookupDestinationId(array(
$rid,
));
$roles[] = reset($role);
}
$user = User::load($source->uid);
$this
->assertIdentical($source->uid, $user
->id());
$this
->assertIdentical($source->name, $user
->label());
$this
->assertIdentical($source->mail, $user
->getEmail());
$this
->assertIdentical($source->created, $user
->getCreatedTime());
$this
->assertIdentical($source->access, $user
->getLastAccessedTime());
$this
->assertIdentical($source->login, $user
->getLastLoginTime());
$is_blocked = $source->status == 0;
$this
->assertIdentical($is_blocked, $user
->isBlocked());
$this
->assertIdentical($source->language, $user->preferred_langcode->value);
$expected_timezone_name = $source->timezone_name ?: $this
->config('system.date')
->get('timezone.default');
$this
->assertIdentical($expected_timezone_name, $user
->getTimeZone());
$this
->assertIdentical($source->init, $user
->getInitialEmail());
$this
->assertIdentical($roles, $user
->getRoles());
if (!empty($source->picture)) {
$file = File::load($user->user_picture->target_id);
$this
->assertIdentical(basename($source->picture), $file
->getFilename());
}
if ($user
->id() != 1) {
$this
->assertTrue(\Drupal::service('password')
->check($source->pass_plain, $user
->getPassword()));
}
}
(new MigrateExecutable($this->migration, $this))
->rollback();
$users = Database::getConnection('default', 'migrate')
->select('users', 'u')
->fields('u', [
'uid',
])
->condition('uid', 0, '>')
->execute()
->fetchCol();
foreach ($users as $uid) {
$account = User::load($uid);
if ($uid == 1) {
$this
->assertNotNull($account, 'User 1 was preserved after rollback');
}
else {
$this
->assertNull($account);
}
}
}
}