7d73dafecd1bfde5d0a42fd908d704d49573a1e5
[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\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;
13
14 /**
15  * Users migration.
16  *
17  * @group user
18  */
19 class MigrateUserTest extends MigrateDrupal7TestBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public static $modules = [
25     'comment',
26     'datetime',
27     'file',
28     'image',
29     'language',
30     'link',
31     'node',
32     'system',
33     'taxonomy',
34     'telephone',
35     'text',
36   ];
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43
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([
54       'language',
55       'user_picture_field',
56       'user_picture_field_instance',
57       'd7_user_role',
58       'd7_field',
59       'd7_field_instance',
60       'd7_user',
61     ]);
62   }
63
64   /**
65    * Creates a node type with a corresponding comment type.
66    *
67    * @param string $id
68    *   The node type ID.
69    */
70   protected function createType($id) {
71     NodeType::create([
72       'type' => $id,
73       'label' => $this->randomString(),
74     ])->save();
75
76     CommentType::create([
77       'id' => 'comment_node_' . $id,
78       'label' => $this->randomString(),
79       'target_entity_type_id' => 'node',
80     ])->save();
81   }
82
83   /**
84    * Asserts various aspects of a user account.
85    *
86    * @param string $id
87    *   The user ID.
88    * @param string $label
89    *   The username.
90    * @param string $mail
91    *   The user's email address.
92    * @param string $password
93    *   The password for this user.
94    * @param int $created
95    *   The user's creation time.
96    * @param int $access
97    *   The last access time.
98    * @param int $login
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.
116    */
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());
128
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);
140     }
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);
145     }
146     else {
147       $this->assertSame($langcode, $user->langcode->value);
148       $this->assertSame($langcode, $user->preferred_langcode->value);
149       $this->assertSame($langcode, $user->preferred_admin_langcode->value);
150     }
151
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);
159     }
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);
163     }
164   }
165
166   /**
167    * Tests the Drupal 7 user to Drupal 8 migration.
168    */
169   public function testUser() {
170     $users = Database::getConnection('default', 'migrate')
171       ->select('users', 'u')
172       ->fields('u')
173       ->condition('uid', 1, '>')
174       ->execute()
175       ->fetchAll();
176
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)
182         ->execute()
183         ->fetchCol();
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);
189       }
190
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)
195         ->execute()
196         ->fetchCol();
197       $field_integer = !empty($field_integer) ? $field_integer : NULL;
198
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)
203         ->execute()
204         ->fetchField();
205
206       $this->assertEntity(
207         $source->uid,
208         $source->name,
209         $source->mail,
210         $source->pass,
211         $source->created,
212         $source->access,
213         $source->login,
214         $source->status,
215         $source->language,
216         $source->timezone,
217         $source->init,
218         $roles,
219         $field_integer,
220         $field_file
221       );
222
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
227       // Drupal 8.
228       $user = User::load($source->uid);
229       $rehash = $user->getPassword();
230       $this->assertNotEquals($source->pass, $rehash);
231
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());
236     }
237   }
238
239 }