bbfc41d77d5f2e3b66c09aac2af263a3be4fb95b
[yaffs-website] / web / core / modules / user / tests / src / Kernel / Migrate / d7 / MigrateUserTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Kernel\Migrate\d7;
4
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;
12
13 /**
14  * Users migration.
15  *
16  * @group user
17  */
18 class MigrateUserTest extends MigrateDrupal7TestBase {
19
20   use NodeCommentCombinationTrait;
21
22   /**
23    * {@inheritdoc}
24    */
25   public static $modules = [
26     'comment',
27     'datetime',
28     'file',
29     'image',
30     'language',
31     'link',
32     'node',
33     'system',
34     'taxonomy',
35     'telephone',
36     'text',
37   ];
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
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([
55       'language',
56       'user_picture_field',
57       'user_picture_field_instance',
58       'd7_user_role',
59       'd7_field',
60       'd7_field_instance',
61       'd7_user',
62     ]);
63   }
64
65   /**
66    * Asserts various aspects of a user account.
67    *
68    * @param string $id
69    *   The user ID.
70    * @param string $label
71    *   The username.
72    * @param string $mail
73    *   The user's email address.
74    * @param string $password
75    *   The password for this user.
76    * @param int $created
77    *   The user's creation time.
78    * @param int $access
79    *   The last access time.
80    * @param int $login
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.
88    * @param string $init
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.
98    */
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());
110
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);
122     }
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);
127     }
128     else {
129       $this->assertSame($langcode, $user->langcode->value);
130       $this->assertSame($langcode, $user->preferred_langcode->value);
131       $this->assertSame($langcode, $user->preferred_admin_langcode->value);
132     }
133
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);
141     }
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);
145     }
146   }
147
148   /**
149    * Tests the Drupal 7 user to Drupal 8 migration.
150    */
151   public function testUser() {
152     $users = Database::getConnection('default', 'migrate')
153       ->select('users', 'u')
154       ->fields('u')
155       ->condition('uid', 1, '>')
156       ->execute()
157       ->fetchAll();
158
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)
164         ->execute()
165         ->fetchCol();
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);
171       }
172
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)
177         ->execute()
178         ->fetchCol();
179       $field_integer = !empty($field_integer) ? $field_integer : NULL;
180
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)
185         ->execute()
186         ->fetchField();
187
188       $this->assertEntity(
189         $source->uid,
190         $source->name,
191         $source->mail,
192         $source->pass,
193         $source->created,
194         $source->access,
195         $source->login,
196         $source->status,
197         $source->language,
198         $source->timezone,
199         $source->init,
200         $roles,
201         $field_integer,
202         $field_file
203       );
204
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
209       // Drupal 8.
210       $user = User::load($source->uid);
211       $rehash = $user->getPassword();
212       $this->assertNotEquals($source->pass, $rehash);
213
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());
218     }
219   }
220
221 }