Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / Tests / UserCreateTest.php
1 <?php
2
3 namespace Drupal\user\Tests;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10  * Tests the create user administration page.
11  *
12  * @group user
13  */
14 class UserCreateTest extends WebTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['image'];
22
23   /**
24    * Create a user through the administration interface and ensure that it
25    * displays in the user list.
26    */
27   public function testUserAdd() {
28     $user = $this->drupalCreateUser(['administer users']);
29     $this->drupalLogin($user);
30
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.');
33
34     // Create a field.
35     $field_name = 'test_field';
36     FieldStorageConfig::create([
37       'field_name' => $field_name,
38       'entity_type' => 'user',
39       'module' => 'image',
40       'type' => 'image',
41       'cardinality' => 1,
42       'locked' => FALSE,
43       'indexes' => ['target_id' => ['target_id']],
44       'settings' => [
45         'uri_scheme' => 'public',
46       ],
47     ])->save();
48
49     FieldConfig::create([
50       'field_name' => $field_name,
51       'entity_type' => 'user',
52       'label' => 'Picture',
53       'bundle' => 'user',
54       'description' => t('Your virtual face or picture.'),
55       'required' => FALSE,
56       'settings' => [
57         'file_extensions' => 'png gif jpg jpeg',
58         'file_directory' => 'pictures',
59         'max_filesize' => '30 KB',
60         'alt_field' => 0,
61         'title_field' => 0,
62         'max_resolution' => '85x85',
63         'min_resolution' => '',
64       ],
65     ])->save();
66
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.');
72
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.');
75
76     // Test that the password strength indicator displays.
77     $config = $this->config('user.settings');
78
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.');
82
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.');
86
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();
91       $edit = [
92         'name' => $name,
93         'mail' => $this->randomMachineName() . '@example.com',
94         'pass[pass1]' => $pass = $this->randomString(),
95         'pass[pass2]' => $pass,
96         'notify' => $notify,
97       ];
98       $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
99
100       if ($notify) {
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');
103       }
104       else {
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');
107       }
108
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');
113     }
114
115     // Test that the password '0' is considered a password.
116     // @see https://www.drupal.org/node/2563751.
117     $name = $this->randomMachineName();
118     $edit = [
119       'name' => $name,
120       'mail' => $this->randomMachineName() . '@example.com',
121       'pass[pass1]' => 0,
122       'pass[pass2]' => 0,
123       'notify' => FALSE,
124     ];
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');
128   }
129
130 }