3 namespace Drupal\user\Tests;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\field\Entity\FieldStorageConfig;
10 * Tests the create user administration page.
14 class UserCreateTest extends WebTestBase {
21 public static $modules = ['image'];
24 * Create a user through the administration interface and ensure that it
25 * displays in the user list.
27 public function testUserAdd() {
28 $user = $this->drupalCreateUser(['administer users']);
29 $this->drupalLogin($user);
31 $this->assertEqual($user->getCreatedTime(), REQUEST_TIME, 'Creating a user sets default "created" timestamp.');
32 $this->assertEqual($user->getChangedTime(), REQUEST_TIME, 'Creating a user sets default "changed" timestamp.');
35 $field_name = 'test_field';
36 FieldStorageConfig::create([
37 'field_name' => $field_name,
38 'entity_type' => 'user',
43 'indexes' => ['target_id' => ['target_id']],
45 'uri_scheme' => 'public',
50 'field_name' => $field_name,
51 'entity_type' => 'user',
54 'description' => t('Your virtual face or picture.'),
57 'file_extensions' => 'png gif jpg jpeg',
58 'file_directory' => 'pictures',
59 'max_filesize' => '30 KB',
62 'max_resolution' => '85x85',
63 'min_resolution' => '',
67 // Test user creation page for valid fields.
68 $this->drupalGet('admin/people/create');
69 $this->assertFieldbyId('edit-status-0', 0, 'The user status option Blocked exists.', 'User login');
70 $this->assertFieldbyId('edit-status-1', 1, 'The user status option Active exists.', 'User login');
71 $this->assertFieldByXPath('//input[@type="radio" and @id="edit-status-1" and @checked="checked"]', NULL, 'Default setting for user status is active.');
73 // Test that browser autocomplete behavior does not occur.
74 $this->assertNoRaw('data-user-info-from-browser', 'Ensure form attribute, data-user-info-from-browser, does not exist.');
76 // Test that the password strength indicator displays.
77 $config = $this->config('user.settings');
79 $config->set('password_strength', TRUE)->save();
80 $this->drupalGet('admin/people/create');
81 $this->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.');
83 $config->set('password_strength', FALSE)->save();
84 $this->drupalGet('admin/people/create');
85 $this->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.');
87 // We create two users, notifying one and not notifying the other, to
88 // ensure that the tests work in both cases.
89 foreach ([FALSE, TRUE] as $notify) {
90 $name = $this->randomMachineName();
93 'mail' => $this->randomMachineName() . '@example.com',
94 'pass[pass1]' => $pass = $this->randomString(),
95 'pass[pass2]' => $pass,
98 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
101 $this->assertText(t('A welcome message with further instructions has been emailed to the new user @name.', ['@name' => $edit['name']]), 'User created');
102 $this->assertEqual(count($this->drupalGetMails()), 1, 'Notification email sent');
105 $this->assertText(t('Created a new user account for @name. No email has been sent.', ['@name' => $edit['name']]), 'User created');
106 $this->assertEqual(count($this->drupalGetMails()), 0, 'Notification email not sent');
109 $this->drupalGet('admin/people');
110 $this->assertText($edit['name'], 'User found in list of users');
111 $user = user_load_by_name($name);
112 $this->assertEqual($user->isActive(), 'User is not blocked');
115 // Test that the password '0' is considered a password.
116 // @see https://www.drupal.org/node/2563751.
117 $name = $this->randomMachineName();
120 'mail' => $this->randomMachineName() . '@example.com',
125 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
126 $this->assertText("Created a new user account for $name. No email has been sent");
127 $this->assertNoText('Password field is required');