3 namespace Drupal\Tests\user\Kernel\Migrate\d7;
5 use Drupal\Core\Database\Database;
6 use Drupal\taxonomy\Entity\Vocabulary;
7 use Drupal\Tests\migrate\Kernel\NodeCommentCombinationTrait;
8 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
9 use Drupal\user\Entity\User;
10 use Drupal\user\RoleInterface;
11 use Drupal\user\UserInterface;
18 class MigrateUserTest extends MigrateDrupal7TestBase {
20 use NodeCommentCombinationTrait;
25 public static $modules = [
42 protected function setUp() {
45 // Prepare to migrate user pictures as well.
46 $this->installEntitySchema('file');
47 $this->createNodeCommentCombination('page');
48 $this->createNodeCommentCombination('article');
49 $this->createNodeCommentCombination('blog');
50 $this->createNodeCommentCombination('book');
51 $this->createNodeCommentCombination('forum', 'comment_forum');
52 $this->createNodeCommentCombination('test_content_type');
53 Vocabulary::create(['vid' => 'test_vocabulary'])->save();
54 $this->executeMigrations([
57 'user_picture_field_instance',
66 * Asserts various aspects of a user account.
70 * @param string $label
73 * The user's email address.
74 * @param string $password
75 * The password for this user.
77 * The user's creation time.
79 * The last access time.
81 * The last login time.
82 * @param bool $blocked
83 * Whether or not the account is blocked.
84 * @param string $langcode
85 * The user account's language code.
86 * @param string $timezone
87 * The user account's timezone name.
89 * The user's initial email address.
90 * @param string[] $roles
91 * Role IDs the user account is expected to have.
92 * @param int $field_integer
93 * The value of the integer field.
94 * @param int|false $field_file_target_id
95 * (optional) The target ID of the file field.
96 * @param bool $has_picture
97 * (optional) Whether the user is expected to have a picture attached.
99 protected function assertEntity($id, $label, $mail, $password, $created, $access, $login, $blocked, $langcode, $timezone, $init, $roles, $field_integer, $field_file_target_id = FALSE, $has_picture = FALSE) {
100 /** @var \Drupal\user\UserInterface $user */
101 $user = User::load($id);
102 $this->assertTrue($user instanceof UserInterface);
103 $this->assertSame($label, $user->label());
104 $this->assertSame($mail, $user->getEmail());
105 $this->assertSame($password, $user->getPassword());
106 $this->assertSame($created, $user->getCreatedTime());
107 $this->assertSame($access, $user->getLastAccessedTime());
108 $this->assertSame($login, $user->getLastLoginTime());
109 $this->assertNotSame($blocked, $user->isBlocked());
111 // Ensure the user's langcode, preferred_langcode and
112 // preferred_admin_langcode are valid.
113 // $user->getPreferredLangcode() might fallback to default language if the
114 // user preferred language is not configured on the site. We just want to
115 // test if the value was imported correctly.
116 $language_manager = $this->container->get('language_manager');
117 $default_langcode = $language_manager->getDefaultLanguage()->getId();
118 if ($langcode == '') {
119 $this->assertSame('en', $user->langcode->value);
120 $this->assertSame($default_langcode, $user->preferred_langcode->value);
121 $this->assertSame($default_langcode, $user->preferred_admin_langcode->value);
123 elseif ($language_manager->getLanguage($langcode) === NULL) {
124 $this->assertSame($default_langcode, $user->langcode->value);
125 $this->assertSame($default_langcode, $user->preferred_langcode->value);
126 $this->assertSame($default_langcode, $user->preferred_admin_langcode->value);
129 $this->assertSame($langcode, $user->langcode->value);
130 $this->assertSame($langcode, $user->preferred_langcode->value);
131 $this->assertSame($langcode, $user->preferred_admin_langcode->value);
134 $this->assertSame($timezone, $user->getTimeZone());
135 $this->assertSame($init, $user->getInitialEmail());
136 $this->assertSame($roles, $user->getRoles());
137 $this->assertSame($has_picture, !$user->user_picture->isEmpty());
138 if (!is_null($field_integer)) {
139 $this->assertTrue($user->hasField('field_integer'));
140 $this->assertEquals($field_integer[0], $user->field_integer->value);
142 if (!empty($field_file_target_id)) {
143 $this->assertTrue($user->hasField('field_file'));
144 $this->assertSame($field_file_target_id, $user->field_file->target_id);
149 * Tests the Drupal 7 user to Drupal 8 migration.
151 public function testUser() {
152 $users = Database::getConnection('default', 'migrate')
153 ->select('users', 'u')
155 ->condition('uid', 1, '>')
159 foreach ($users as $source) {
160 $rids = Database::getConnection('default', 'migrate')
161 ->select('users_roles', 'ur')
162 ->fields('ur', ['rid'])
163 ->condition('ur.uid', $source->uid)
166 $roles = [RoleInterface::AUTHENTICATED_ID];
167 $id_map = $this->getMigration('d7_user_role')->getIdMap();
168 foreach ($rids as $rid) {
169 $role = $id_map->lookupDestinationId([$rid]);
170 $roles[] = reset($role);
173 $field_integer = Database::getConnection('default', 'migrate')
174 ->select('field_data_field_integer', 'fi')
175 ->fields('fi', ['field_integer_value'])
176 ->condition('fi.entity_id', $source->uid)
179 $field_integer = !empty($field_integer) ? $field_integer : NULL;
181 $field_file = Database::getConnection('default', 'migrate')
182 ->select('field_data_field_file', 'ff')
183 ->fields('ff', ['field_file_fid'])
184 ->condition('ff.entity_id', $source->uid)
205 // Ensure that the user can authenticate.
206 $this->assertEquals($source->uid, $this->container->get('user.auth')->authenticate($source->name, 'a password'));
207 // After authenticating the password will be rehashed because the password
208 // stretching iteration count has changed from 15 in Drupal 7 to 16 in
210 $user = User::load($source->uid);
211 $rehash = $user->getPassword();
212 $this->assertNotEquals($source->pass, $rehash);
214 // Authenticate again and there should be no re-hash.
215 $this->assertEquals($source->uid, $this->container->get('user.auth')->authenticate($source->name, 'a password'));
216 $user = User::load($source->uid);
217 $this->assertEquals($rehash, $user->getPassword());