3 namespace Drupal\Tests\user\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\user\Entity\User;
9 * Tests account saving for arbitrary new uid.
13 class UserSaveTest extends BrowserTestBase {
16 * Test creating a user with arbitrary uid.
18 public function testUserImport() {
19 // User ID must be a number that is not in the database.
21 $uids = \Drupal::entityManager()->getStorage('user')->getQuery()
25 $max_uid = reset($uids);
26 $test_uid = $max_uid + mt_rand(1000, 1000000);
27 $test_name = $this->randomMachineName();
29 // Create the base user, based on drupalCreateUser().
30 $user = User::create([
33 'mail' => $test_name . '@example.com',
34 'pass' => user_password(),
37 $user->enforceIsNew();
40 // Test if created user exists.
41 $user_by_uid = User::load($test_uid);
42 $this->assertTrue($user_by_uid, 'Loading user by uid.');
44 $user_by_name = user_load_by_name($test_name);
45 $this->assertTrue($user_by_name, 'Loading user by name.');
49 * Ensures that an existing password is unset after the user was saved.
51 public function testExistingPasswordRemoval() {
52 /** @var \Drupal\user\Entity\User $user */
53 $user = User::create(['name' => $this->randomMachineName()]);
55 $user->setExistingPassword('existing password');
56 $this->assertNotNull($user->pass->existing);
58 $this->assertNull($user->pass->existing);