Version 1
[yaffs-website] / web / core / modules / user / tests / src / Kernel / Migrate / MigrateUserAdminPassTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Kernel\Migrate;
4
5 use Drupal\Tests\migrate\Kernel\MigrateTestBase;
6 use Drupal\user\Entity\User;
7
8 /**
9  * Tests preservation of root account password.
10  *
11  * @group user
12  */
13 class MigrateUserAdminPassTest extends MigrateTestBase {
14
15   /**
16    * The passwords as retrieved from the account entities before migration.
17    *
18    * @var array
19    */
20   protected $originalPasswords = [];
21
22   /**
23    * Modules to enable.
24    *
25    * @var string[]
26    */
27   public static $modules = ['user'];
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
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');
38     user_install();
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();
44
45     /** @var \Drupal\user\Entity\User $user_account */
46     $user_account = User::create([
47       'uid' => 2,
48       'name' => 'original_username',
49       'mail' => 'original_email@example.com',
50       'pass' => 'original_password',
51     ]);
52     $user_account->save();
53     $this->originalPasswords[2] = $user_account->getPassword();
54   }
55
56   /**
57    * Tests preserving the admin user's password.
58    */
59   public function testAdminPasswordPreserved() {
60     $user_data_rows = [
61       [
62         'id' => '1',
63         'username' => 'site_admin',
64         'password' => 'new_password',
65         'email' => 'site_admin@example.com',
66       ],
67       [
68         'id' => '2',
69         'username' => 'random_user',
70         'password' => 'random_password',
71         'email' => 'random_user@example.com',
72       ],
73     ];
74     $ids = ['id' => ['type' => 'integer']];
75     $definition = [
76       'id' => 'users',
77       'migration_tags' => ['Admin password test'],
78       'source' => [
79         'plugin' => 'embedded_data',
80         'data_rows' => $user_data_rows,
81         'ids' => $ids,
82       ],
83       'process' => [
84         'uid' => 'id',
85         'name' => 'username',
86         'mail' => 'email',
87         'pass' => 'password',
88       ],
89       'destination' => ['plugin' => 'entity:user'],
90     ];
91     $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
92     $this->executeMigration($migration);
93
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]);
100
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]);
107   }
108
109 }