3 namespace Drupal\Tests\user\Functional;
5 use Drupal\Core\Test\AssertMailTrait;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests the create user administration page.
15 class UserCreateTest extends BrowserTestBase {
18 getMails as drupalGetMails;
26 public static $modules = ['image'];
29 * Create a user through the administration interface and ensure that it
30 * displays in the user list.
32 public function testUserAdd() {
33 $user = $this->drupalCreateUser(['administer users']);
34 $this->drupalLogin($user);
36 $this->assertEqual($user->getCreatedTime(), REQUEST_TIME, 'Creating a user sets default "created" timestamp.');
37 $this->assertEqual($user->getChangedTime(), REQUEST_TIME, 'Creating a user sets default "changed" timestamp.');
40 $field_name = 'test_field';
41 FieldStorageConfig::create([
42 'field_name' => $field_name,
43 'entity_type' => 'user',
48 'indexes' => ['target_id' => ['target_id']],
50 'uri_scheme' => 'public',
55 'field_name' => $field_name,
56 'entity_type' => 'user',
59 'description' => t('Your virtual face or picture.'),
62 'file_extensions' => 'png gif jpg jpeg',
63 'file_directory' => 'pictures',
64 'max_filesize' => '30 KB',
67 'max_resolution' => '85x85',
68 'min_resolution' => '',
72 // Test user creation page for valid fields.
73 $this->drupalGet('admin/people/create');
74 $this->assertFieldbyId('edit-status-0', 0, 'The user status option Blocked exists.', 'User login');
75 $this->assertFieldbyId('edit-status-1', 1, 'The user status option Active exists.', 'User login');
76 $this->assertFieldByXPath('//input[@type="radio" and @id="edit-status-1" and @checked="checked"]', NULL, 'Default setting for user status is active.');
78 // Test that browser autocomplete behavior does not occur.
79 $this->assertNoRaw('data-user-info-from-browser', 'Ensure form attribute, data-user-info-from-browser, does not exist.');
81 // Test that the password strength indicator displays.
82 $config = $this->config('user.settings');
84 $config->set('password_strength', TRUE)->save();
85 $this->drupalGet('admin/people/create');
86 $this->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.');
88 $config->set('password_strength', FALSE)->save();
89 $this->drupalGet('admin/people/create');
90 $this->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.');
92 // We create two users, notifying one and not notifying the other, to
93 // ensure that the tests work in both cases.
94 foreach ([FALSE, TRUE] as $notify) {
95 $name = $this->randomMachineName();
98 'mail' => $this->randomMachineName() . '@example.com',
99 'pass[pass1]' => $pass = $this->randomString(),
100 'pass[pass2]' => $pass,
103 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
106 $this->assertText(t('A welcome message with further instructions has been emailed to the new user @name.', ['@name' => $edit['name']]), 'User created');
107 $this->assertEqual(count($this->drupalGetMails()), 1, 'Notification email sent');
110 $this->assertText(t('Created a new user account for @name. No email has been sent.', ['@name' => $edit['name']]), 'User created');
111 $this->assertEqual(count($this->drupalGetMails()), 0, 'Notification email not sent');
114 $this->drupalGet('admin/people');
115 $this->assertText($edit['name'], 'User found in list of users');
116 $user = user_load_by_name($name);
117 $this->assertTrue($user->isActive(), 'User is not blocked');
120 // Test that the password '0' is considered a password.
121 // @see https://www.drupal.org/node/2563751.
122 $name = $this->randomMachineName();
125 'mail' => $this->randomMachineName() . '@example.com',
130 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
131 $this->assertText("Created a new user account for $name. No email has been sent");
132 $this->assertNoText('Password field is required');