Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserCreateTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\Core\Test\AssertMailTrait;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests the create user administration page.
12  *
13  * @group user
14  */
15 class UserCreateTest extends BrowserTestBase {
16
17   use AssertMailTrait {
18     getMails as drupalGetMails;
19   }
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['image'];
27
28   /**
29    * Create a user through the administration interface and ensure that it
30    * displays in the user list.
31    */
32   public function testUserAdd() {
33     $user = $this->drupalCreateUser(['administer users']);
34     $this->drupalLogin($user);
35
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.');
38
39     // Create a field.
40     $field_name = 'test_field';
41     FieldStorageConfig::create([
42       'field_name' => $field_name,
43       'entity_type' => 'user',
44       'module' => 'image',
45       'type' => 'image',
46       'cardinality' => 1,
47       'locked' => FALSE,
48       'indexes' => ['target_id' => ['target_id']],
49       'settings' => [
50         'uri_scheme' => 'public',
51       ],
52     ])->save();
53
54     FieldConfig::create([
55       'field_name' => $field_name,
56       'entity_type' => 'user',
57       'label' => 'Picture',
58       'bundle' => 'user',
59       'description' => t('Your virtual face or picture.'),
60       'required' => FALSE,
61       'settings' => [
62         'file_extensions' => 'png gif jpg jpeg',
63         'file_directory' => 'pictures',
64         'max_filesize' => '30 KB',
65         'alt_field' => 0,
66         'title_field' => 0,
67         'max_resolution' => '85x85',
68         'min_resolution' => '',
69       ],
70     ])->save();
71
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.');
77
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.');
80
81     // Test that the password strength indicator displays.
82     $config = $this->config('user.settings');
83
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.');
87
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.');
91
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();
96       $edit = [
97         'name' => $name,
98         'mail' => $this->randomMachineName() . '@example.com',
99         'pass[pass1]' => $pass = $this->randomString(),
100         'pass[pass2]' => $pass,
101         'notify' => $notify,
102       ];
103       $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
104
105       if ($notify) {
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');
108       }
109       else {
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');
112       }
113
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');
118     }
119
120     // Test that the password '0' is considered a password.
121     // @see https://www.drupal.org/node/2563751.
122     $name = $this->randomMachineName();
123     $edit = [
124       'name' => $name,
125       'mail' => $this->randomMachineName() . '@example.com',
126       'pass[pass1]' => 0,
127       'pass[pass2]' => 0,
128       'notify' => FALSE,
129     ];
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');
133   }
134
135 }