3 namespace Drupal\Tests\user\Kernel\Migrate\d7;
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\Core\Database\Database;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\taxonomy\Entity\Vocabulary;
9 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
10 use Drupal\user\Entity\User;
11 use Drupal\user\RoleInterface;
12 use Drupal\user\UserInterface;
19 class MigrateUserTest extends MigrateDrupal7TestBase {
24 public static $modules = [
41 protected function setUp() {
44 // Prepare to migrate user pictures as well.
45 $this->installEntitySchema('file');
46 $this->createType('page');
47 $this->createType('article');
48 $this->createType('blog');
49 $this->createType('book');
50 $this->createType('forum');
51 $this->createType('test_content_type');
52 Vocabulary::create(['vid' => 'test_vocabulary'])->save();
53 $this->executeMigrations([
56 'user_picture_field_instance',
65 * Creates a node type with a corresponding comment type.
70 protected function createType($id) {
73 'label' => $this->randomString(),
77 'id' => 'comment_node_' . $id,
78 'label' => $this->randomString(),
79 'target_entity_type_id' => 'node',
84 * Asserts various aspects of a user account.
88 * @param string $label
91 * The user's email address.
92 * @param string $password
93 * The password for this user.
95 * The user's creation time.
97 * The last access time.
99 * The last login time.
100 * @param bool $blocked
101 * Whether or not the account is blocked.
102 * @param string $langcode
103 * The user account's language code.
104 * @param string $timezone
105 * The user account's timezone name.
106 * @param string $init
107 * The user's initial email address.
108 * @param string[] $roles
109 * Role IDs the user account is expected to have.
110 * @param int $field_integer
111 * The value of the integer field.
112 * @param int|false $field_file_target_id
113 * (optional) The target ID of the file field.
114 * @param bool $has_picture
115 * (optional) Whether the user is expected to have a picture attached.
117 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) {
118 /** @var \Drupal\user\UserInterface $user */
119 $user = User::load($id);
120 $this->assertTrue($user instanceof UserInterface);
121 $this->assertSame($label, $user->label());
122 $this->assertSame($mail, $user->getEmail());
123 $this->assertSame($password, $user->getPassword());
124 $this->assertSame($created, $user->getCreatedTime());
125 $this->assertSame($access, $user->getLastAccessedTime());
126 $this->assertSame($login, $user->getLastLoginTime());
127 $this->assertNotSame($blocked, $user->isBlocked());
129 // Ensure the user's langcode, preferred_langcode and
130 // preferred_admin_langcode are valid.
131 // $user->getPreferredLangcode() might fallback to default language if the
132 // user preferred language is not configured on the site. We just want to
133 // test if the value was imported correctly.
134 $language_manager = $this->container->get('language_manager');
135 $default_langcode = $language_manager->getDefaultLanguage()->getId();
136 if ($langcode == '') {
137 $this->assertSame('en', $user->langcode->value);
138 $this->assertSame($default_langcode, $user->preferred_langcode->value);
139 $this->assertSame($default_langcode, $user->preferred_admin_langcode->value);
141 elseif ($language_manager->getLanguage($langcode) === NULL) {
142 $this->assertSame($default_langcode, $user->langcode->value);
143 $this->assertSame($default_langcode, $user->preferred_langcode->value);
144 $this->assertSame($default_langcode, $user->preferred_admin_langcode->value);
147 $this->assertSame($langcode, $user->langcode->value);
148 $this->assertSame($langcode, $user->preferred_langcode->value);
149 $this->assertSame($langcode, $user->preferred_admin_langcode->value);
152 $this->assertSame($timezone, $user->getTimeZone());
153 $this->assertSame($init, $user->getInitialEmail());
154 $this->assertSame($roles, $user->getRoles());
155 $this->assertSame($has_picture, !$user->user_picture->isEmpty());
156 if (!is_null($field_integer)) {
157 $this->assertTrue($user->hasField('field_integer'));
158 $this->assertEquals($field_integer[0], $user->field_integer->value);
160 if (!empty($field_file_target_id)) {
161 $this->assertTrue($user->hasField('field_file'));
162 $this->assertSame($field_file_target_id, $user->field_file->target_id);
167 * Tests the Drupal 7 user to Drupal 8 migration.
169 public function testUser() {
170 $users = Database::getConnection('default', 'migrate')
171 ->select('users', 'u')
173 ->condition('uid', 1, '>')
177 foreach ($users as $source) {
178 $rids = Database::getConnection('default', 'migrate')
179 ->select('users_roles', 'ur')
180 ->fields('ur', ['rid'])
181 ->condition('ur.uid', $source->uid)
184 $roles = [RoleInterface::AUTHENTICATED_ID];
185 $id_map = $this->getMigration('d7_user_role')->getIdMap();
186 foreach ($rids as $rid) {
187 $role = $id_map->lookupDestinationId([$rid]);
188 $roles[] = reset($role);
191 $field_integer = Database::getConnection('default', 'migrate')
192 ->select('field_data_field_integer', 'fi')
193 ->fields('fi', ['field_integer_value'])
194 ->condition('fi.entity_id', $source->uid)
197 $field_integer = !empty($field_integer) ? $field_integer : NULL;
199 $field_file = Database::getConnection('default', 'migrate')
200 ->select('field_data_field_file', 'ff')
201 ->fields('ff', ['field_file_fid'])
202 ->condition('ff.entity_id', $source->uid)
223 // Ensure that the user can authenticate.
224 $this->assertEquals($source->uid, $this->container->get('user.auth')->authenticate($source->name, 'a password'));
225 // After authenticating the password will be rehashed because the password
226 // stretching iteration count has changed from 15 in Drupal 7 to 16 in
228 $user = User::load($source->uid);
229 $rehash = $user->getPassword();
230 $this->assertNotEquals($source->pass, $rehash);
232 // Authenticate again and there should be no re-hash.
233 $this->assertEquals($source->uid, $this->container->get('user.auth')->authenticate($source->name, 'a password'));
234 $user = User::load($source->uid);
235 $this->assertEquals($rehash, $user->getPassword());