3 namespace Drupal\Tests\user\Kernel\Migrate;
5 use Drupal\Tests\migrate\Kernel\MigrateTestBase;
6 use Drupal\user\Entity\User;
9 * Tests preservation of root account password.
13 class MigrateUserAdminPassTest extends MigrateTestBase {
16 * The passwords as retrieved from the account entities before migration.
20 protected $originalPasswords = [];
27 public static $modules = ['user'];
32 protected function setUp() {
35 // Make sure the admin user and a regular user are created.
36 $this->container->get('module_handler')->loadInclude('user', 'install');
37 $this->installEntitySchema('user');
39 /** @var \Drupal\user\Entity\User $admin_account */
40 $admin_account = User::load(1);
41 $admin_account->setPassword('original');
42 $admin_account->save();
43 $this->originalPasswords[1] = $admin_account->getPassword();
45 /** @var \Drupal\user\Entity\User $user_account */
46 $user_account = User::create([
48 'name' => 'original_username',
49 'mail' => 'original_email@example.com',
50 'pass' => 'original_password',
52 $user_account->save();
53 $this->originalPasswords[2] = $user_account->getPassword();
57 * Tests preserving the admin user's password.
59 public function testAdminPasswordPreserved() {
63 'username' => 'site_admin',
64 'password' => 'new_password',
65 'email' => 'site_admin@example.com',
69 'username' => 'random_user',
70 'password' => 'random_password',
71 'email' => 'random_user@example.com',
74 $ids = ['id' => ['type' => 'integer']];
77 'migration_tags' => ['Admin password test'],
79 'plugin' => 'embedded_data',
80 'data_rows' => $user_data_rows,
89 'destination' => ['plugin' => 'entity:user'],
91 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
92 $this->executeMigration($migration);
94 // Verify that admin username and email were changed, but password was not.
95 /** @var \Drupal\user\Entity\User $admin_account */
96 $admin_account = User::load(1);
97 $this->assertIdentical($admin_account->getUsername(), 'site_admin');
98 $this->assertIdentical($admin_account->getEmail(), 'site_admin@example.com');
99 $this->assertIdentical($admin_account->getPassword(), $this->originalPasswords[1]);
101 // Verify that everything changed for the regular user.
102 /** @var \Drupal\user\Entity\User $user_account */
103 $user_account = User::load(2);
104 $this->assertIdentical($user_account->getUsername(), 'random_user');
105 $this->assertIdentical($user_account->getEmail(), 'random_user@example.com');
106 $this->assertNotIdentical($user_account->getPassword(), $this->originalPasswords[2]);