Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserSaveTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\user\Entity\User;
7
8 /**
9  * Tests account saving for arbitrary new uid.
10  *
11  * @group user
12  */
13 class UserSaveTest extends BrowserTestBase {
14
15   /**
16    * Test creating a user with arbitrary uid.
17    */
18   public function testUserImport() {
19     // User ID must be a number that is not in the database.
20
21     $uids = \Drupal::entityManager()->getStorage('user')->getQuery()
22       ->sort('uid', 'DESC')
23       ->range(0, 1)
24       ->execute();
25     $max_uid = reset($uids);
26     $test_uid = $max_uid + mt_rand(1000, 1000000);
27     $test_name = $this->randomMachineName();
28
29     // Create the base user, based on drupalCreateUser().
30     $user = User::create([
31       'name' => $test_name,
32       'uid' => $test_uid,
33       'mail' => $test_name . '@example.com',
34       'pass' => user_password(),
35       'status' => 1,
36     ]);
37     $user->enforceIsNew();
38     $user->save();
39
40     // Test if created user exists.
41     $user_by_uid = User::load($test_uid);
42     $this->assertTrue($user_by_uid, 'Loading user by uid.');
43
44     $user_by_name = user_load_by_name($test_name);
45     $this->assertTrue($user_by_name, 'Loading user by name.');
46   }
47
48   /**
49    * Ensures that an existing password is unset after the user was saved.
50    */
51   public function testExistingPasswordRemoval() {
52     /** @var \Drupal\user\Entity\User $user */
53     $user = User::create(['name' => $this->randomMachineName()]);
54     $user->save();
55     $user->setExistingPassword('existing password');
56     $this->assertNotNull($user->pass->existing);
57     $user->save();
58     $this->assertNull($user->pass->existing);
59   }
60
61 }