annotate core/modules/user/tests/src/Functional/UserRegistrationTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@4 1 <?php
Chris@4 2
Chris@4 3 namespace Drupal\Tests\user\Functional;
Chris@4 4
Chris@4 5 use Drupal\Component\Render\FormattableMarkup;
Chris@4 6 use Drupal\Core\Entity\Entity\EntityFormDisplay;
Chris@4 7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
Chris@4 8 use Drupal\field\Entity\FieldConfig;
Chris@4 9 use Drupal\field\Entity\FieldStorageConfig;
Chris@4 10 use Drupal\Tests\BrowserTestBase;
Chris@5 11 use Drupal\user\UserInterface;
Chris@4 12
Chris@4 13 /**
Chris@4 14 * Tests registration of user under different configurations.
Chris@4 15 *
Chris@4 16 * @group user
Chris@4 17 */
Chris@4 18 class UserRegistrationTest extends BrowserTestBase {
Chris@4 19
Chris@4 20 /**
Chris@4 21 * Modules to enable.
Chris@4 22 *
Chris@4 23 * @var array
Chris@4 24 */
Chris@4 25 public static $modules = ['field_test'];
Chris@4 26
Chris@4 27 public function testRegistrationWithEmailVerification() {
Chris@4 28 $config = $this->config('user.settings');
Chris@4 29 // Require email verification.
Chris@4 30 $config->set('verify_mail', TRUE)->save();
Chris@4 31
Chris@4 32 // Set registration to administrator only.
Chris@5 33 $config->set('register', UserInterface::REGISTER_ADMINISTRATORS_ONLY)->save();
Chris@4 34 $this->drupalGet('user/register');
Chris@4 35 $this->assertResponse(403, 'Registration page is inaccessible when only administrators can create accounts.');
Chris@4 36
Chris@4 37 // Allow registration by site visitors without administrator approval.
Chris@5 38 $config->set('register', UserInterface::REGISTER_VISITORS)->save();
Chris@4 39 $edit = [];
Chris@4 40 $edit['name'] = $name = $this->randomMachineName();
Chris@4 41 $edit['mail'] = $mail = $edit['name'] . '@example.com';
Chris@4 42 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 43 $this->assertText(t('A welcome message with further instructions has been sent to your email address.'), 'User registered successfully.');
Chris@4 44
Chris@4 45 /** @var EntityStorageInterface $storage */
Chris@4 46 $storage = $this->container->get('entity_type.manager')->getStorage('user');
Chris@4 47 $accounts = $storage->loadByProperties(['name' => $name, 'mail' => $mail]);
Chris@4 48 $new_user = reset($accounts);
Chris@4 49 $this->assertTrue($new_user->isActive(), 'New account is active after registration.');
Chris@4 50 $resetURL = user_pass_reset_url($new_user);
Chris@4 51 $this->drupalGet($resetURL);
Chris@4 52 $this->assertTitle(t('Set password | Drupal'), 'Page title is "Set password".');
Chris@4 53
Chris@4 54 // Allow registration by site visitors, but require administrator approval.
Chris@5 55 $config->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)->save();
Chris@4 56 $edit = [];
Chris@4 57 $edit['name'] = $name = $this->randomMachineName();
Chris@4 58 $edit['mail'] = $mail = $edit['name'] . '@example.com';
Chris@4 59 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 60 $this->container->get('entity.manager')->getStorage('user')->resetCache();
Chris@4 61 $accounts = $storage->loadByProperties(['name' => $name, 'mail' => $mail]);
Chris@4 62 $new_user = reset($accounts);
Chris@4 63 $this->assertFalse($new_user->isActive(), 'New account is blocked until approved by an administrator.');
Chris@4 64 }
Chris@4 65
Chris@4 66 public function testRegistrationWithoutEmailVerification() {
Chris@4 67 $config = $this->config('user.settings');
Chris@4 68 // Don't require email verification and allow registration by site visitors
Chris@4 69 // without administrator approval.
Chris@4 70 $config
Chris@4 71 ->set('verify_mail', FALSE)
Chris@5 72 ->set('register', UserInterface::REGISTER_VISITORS)
Chris@4 73 ->save();
Chris@4 74
Chris@4 75 $edit = [];
Chris@4 76 $edit['name'] = $name = $this->randomMachineName();
Chris@4 77 $edit['mail'] = $mail = $edit['name'] . '@example.com';
Chris@4 78
Chris@4 79 // Try entering a mismatching password.
Chris@4 80 $edit['pass[pass1]'] = '99999.0';
Chris@4 81 $edit['pass[pass2]'] = '99999';
Chris@4 82 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 83 $this->assertText(t('The specified passwords do not match.'), 'Typing mismatched passwords displays an error message.');
Chris@4 84
Chris@4 85 // Enter a correct password.
Chris@4 86 $edit['pass[pass1]'] = $new_pass = $this->randomMachineName();
Chris@4 87 $edit['pass[pass2]'] = $new_pass;
Chris@4 88 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 89 $this->container->get('entity.manager')->getStorage('user')->resetCache();
Chris@4 90 $accounts = $this->container->get('entity_type.manager')->getStorage('user')
Chris@4 91 ->loadByProperties(['name' => $name, 'mail' => $mail]);
Chris@4 92 $new_user = reset($accounts);
Chris@4 93 $this->assertNotNull($new_user, 'New account successfully created with matching passwords.');
Chris@4 94 $this->assertText(t('Registration successful. You are now logged in.'), 'Users are logged in after registering.');
Chris@4 95 $this->drupalLogout();
Chris@4 96
Chris@4 97 // Allow registration by site visitors, but require administrator approval.
Chris@5 98 $config->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)->save();
Chris@4 99 $edit = [];
Chris@4 100 $edit['name'] = $name = $this->randomMachineName();
Chris@4 101 $edit['mail'] = $mail = $edit['name'] . '@example.com';
Chris@4 102 $edit['pass[pass1]'] = $pass = $this->randomMachineName();
Chris@4 103 $edit['pass[pass2]'] = $pass;
Chris@4 104 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 105 $this->assertText(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'), 'Users are notified of pending approval');
Chris@4 106
Chris@4 107 // Try to log in before administrator approval.
Chris@4 108 $auth = [
Chris@4 109 'name' => $name,
Chris@4 110 'pass' => $pass,
Chris@4 111 ];
Chris@4 112 $this->drupalPostForm('user/login', $auth, t('Log in'));
Chris@4 113 $this->assertText(t('The username @name has not been activated or is blocked.', ['@name' => $name]), 'User cannot log in yet.');
Chris@4 114
Chris@4 115 // Activate the new account.
Chris@4 116 $accounts = $this->container->get('entity_type.manager')->getStorage('user')
Chris@4 117 ->loadByProperties(['name' => $name, 'mail' => $mail]);
Chris@4 118 $new_user = reset($accounts);
Chris@4 119 $admin_user = $this->drupalCreateUser(['administer users']);
Chris@4 120 $this->drupalLogin($admin_user);
Chris@4 121 $edit = [
Chris@4 122 'status' => 1,
Chris@4 123 ];
Chris@4 124 $this->drupalPostForm('user/' . $new_user->id() . '/edit', $edit, t('Save'));
Chris@4 125 $this->drupalLogout();
Chris@4 126
Chris@4 127 // Log in after administrator approval.
Chris@4 128 $this->drupalPostForm('user/login', $auth, t('Log in'));
Chris@4 129 $this->assertText(t('Member for'), 'User can log in after administrator approval.');
Chris@4 130 }
Chris@4 131
Chris@4 132 public function testRegistrationEmailDuplicates() {
Chris@4 133 // Don't require email verification and allow registration by site visitors
Chris@4 134 // without administrator approval.
Chris@4 135 $this->config('user.settings')
Chris@4 136 ->set('verify_mail', FALSE)
Chris@5 137 ->set('register', UserInterface::REGISTER_VISITORS)
Chris@4 138 ->save();
Chris@4 139
Chris@4 140 // Set up a user to check for duplicates.
Chris@4 141 $duplicate_user = $this->drupalCreateUser();
Chris@4 142
Chris@4 143 $edit = [];
Chris@4 144 $edit['name'] = $this->randomMachineName();
Chris@4 145 $edit['mail'] = $duplicate_user->getEmail();
Chris@4 146
Chris@4 147 // Attempt to create a new account using an existing email address.
Chris@4 148 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 149 $this->assertText(t('The email address @email is already taken.', ['@email' => $duplicate_user->getEmail()]), 'Supplying an exact duplicate email address displays an error message');
Chris@4 150
Chris@4 151 // Attempt to bypass duplicate email registration validation by adding spaces.
Chris@4 152 $edit['mail'] = ' ' . $duplicate_user->getEmail() . ' ';
Chris@4 153
Chris@4 154 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 155 $this->assertText(t('The email address @email is already taken.', ['@email' => $duplicate_user->getEmail()]), 'Supplying a duplicate email address with added whitespace displays an error message');
Chris@4 156 }
Chris@4 157
Chris@4 158 /**
Chris@4 159 * Tests that UUID isn't cached in form state on register form.
Chris@4 160 *
Chris@4 161 * This is a regression test for https://www.drupal.org/node/2500527 to ensure
Chris@4 162 * that the form is not cached on GET requests.
Chris@4 163 */
Chris@4 164 public function testUuidFormState() {
Chris@4 165 \Drupal::service('module_installer')->install(['image']);
Chris@4 166 \Drupal::service('router.builder')->rebuild();
Chris@4 167
Chris@4 168 // Add a picture field in order to ensure that no form cache is written,
Chris@4 169 // which breaks registration of more than 1 user every 6 hours.
Chris@4 170 $field_storage = FieldStorageConfig::create([
Chris@4 171 'field_name' => 'user_picture',
Chris@4 172 'entity_type' => 'user',
Chris@4 173 'type' => 'image',
Chris@4 174 ]);
Chris@4 175 $field_storage->save();
Chris@4 176
Chris@4 177 $field = FieldConfig::create([
Chris@4 178 'field_name' => 'user_picture',
Chris@4 179 'entity_type' => 'user',
Chris@4 180 'bundle' => 'user',
Chris@4 181 ]);
Chris@4 182 $field->save();
Chris@4 183
Chris@4 184 $form_display = EntityFormDisplay::create([
Chris@4 185 'targetEntityType' => 'user',
Chris@4 186 'bundle' => 'user',
Chris@4 187 'mode' => 'default',
Chris@4 188 'status' => TRUE,
Chris@4 189 ]);
Chris@4 190 $form_display->setComponent('user_picture', [
Chris@4 191 'type' => 'image_image',
Chris@4 192 ]);
Chris@4 193 $form_display->save();
Chris@4 194
Chris@4 195 // Don't require email verification and allow registration by site visitors
Chris@4 196 // without administrator approval.
Chris@4 197 $this->config('user.settings')
Chris@4 198 ->set('verify_mail', FALSE)
Chris@5 199 ->set('register', UserInterface::REGISTER_VISITORS)
Chris@4 200 ->save();
Chris@4 201
Chris@4 202 $edit = [];
Chris@4 203 $edit['name'] = $this->randomMachineName();
Chris@4 204 $edit['mail'] = $edit['name'] . '@example.com';
Chris@4 205 $edit['pass[pass2]'] = $edit['pass[pass1]'] = $this->randomMachineName();
Chris@4 206
Chris@4 207 // Create one account.
Chris@4 208 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 209 $this->assertResponse(200);
Chris@4 210
Chris@4 211 $user_storage = \Drupal::entityManager()->getStorage('user');
Chris@4 212
Chris@4 213 $this->assertTrue($user_storage->loadByProperties(['name' => $edit['name']]));
Chris@4 214 $this->drupalLogout();
Chris@4 215
Chris@4 216 // Create a second account.
Chris@4 217 $edit['name'] = $this->randomMachineName();
Chris@4 218 $edit['mail'] = $edit['name'] . '@example.com';
Chris@4 219 $edit['pass[pass2]'] = $edit['pass[pass1]'] = $this->randomMachineName();
Chris@4 220
Chris@4 221 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 222 $this->assertResponse(200);
Chris@4 223
Chris@4 224 $this->assertTrue($user_storage->loadByProperties(['name' => $edit['name']]));
Chris@4 225 }
Chris@4 226
Chris@4 227 public function testRegistrationDefaultValues() {
Chris@4 228 // Don't require email verification and allow registration by site visitors
Chris@4 229 // without administrator approval.
Chris@4 230 $config_user_settings = $this->config('user.settings')
Chris@4 231 ->set('verify_mail', FALSE)
Chris@5 232 ->set('register', UserInterface::REGISTER_VISITORS)
Chris@4 233 ->save();
Chris@4 234
Chris@4 235 // Set the default timezone to Brussels.
Chris@4 236 $config_system_date = $this->config('system.date')
Chris@4 237 ->set('timezone.user.configurable', 1)
Chris@4 238 ->set('timezone.default', 'Europe/Brussels')
Chris@4 239 ->save();
Chris@4 240
Chris@4 241 // Check the presence of expected cache tags.
Chris@4 242 $this->drupalGet('user/register');
Chris@4 243 $this->assertCacheTag('config:user.settings');
Chris@4 244
Chris@4 245 $edit = [];
Chris@4 246 $edit['name'] = $name = $this->randomMachineName();
Chris@4 247 $edit['mail'] = $mail = $edit['name'] . '@example.com';
Chris@4 248 $edit['pass[pass1]'] = $new_pass = $this->randomMachineName();
Chris@4 249 $edit['pass[pass2]'] = $new_pass;
Chris@4 250 $this->drupalPostForm(NULL, $edit, t('Create new account'));
Chris@4 251
Chris@4 252 // Check user fields.
Chris@4 253 $accounts = $this->container->get('entity_type.manager')->getStorage('user')
Chris@4 254 ->loadByProperties(['name' => $name, 'mail' => $mail]);
Chris@4 255 $new_user = reset($accounts);
Chris@5 256 $this->assertEqual($new_user->getAccountName(), $name, 'Username matches.');
Chris@4 257 $this->assertEqual($new_user->getEmail(), $mail, 'Email address matches.');
Chris@4 258 $this->assertTrue(($new_user->getCreatedTime() > REQUEST_TIME - 20), 'Correct creation time.');
Chris@5 259 $this->assertEqual($new_user->isActive(), $config_user_settings->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0, 'Correct status field.');
Chris@4 260 $this->assertEqual($new_user->getTimezone(), $config_system_date->get('timezone.default'), 'Correct time zone field.');
Chris@4 261 $this->assertEqual($new_user->langcode->value, \Drupal::languageManager()->getDefaultLanguage()->getId(), 'Correct language field.');
Chris@4 262 $this->assertEqual($new_user->preferred_langcode->value, \Drupal::languageManager()->getDefaultLanguage()->getId(), 'Correct preferred language field.');
Chris@4 263 $this->assertEqual($new_user->init->value, $mail, 'Correct init field.');
Chris@4 264 }
Chris@4 265
Chris@4 266 /**
Chris@4 267 * Tests username and email field constraints on user registration.
Chris@4 268 *
Chris@4 269 * @see \Drupal\user\Plugin\Validation\Constraint\UserNameUnique
Chris@4 270 * @see \Drupal\user\Plugin\Validation\Constraint\UserMailUnique
Chris@4 271 */
Chris@4 272 public function testUniqueFields() {
Chris@4 273 $account = $this->drupalCreateUser();
Chris@4 274
Chris@5 275 $edit = ['mail' => 'test@example.com', 'name' => $account->getAccountName()];
Chris@4 276 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@5 277 $this->assertRaw(new FormattableMarkup('The username %value is already taken.', ['%value' => $account->getAccountName()]));
Chris@4 278
Chris@4 279 $edit = ['mail' => $account->getEmail(), 'name' => $this->randomString()];
Chris@4 280 $this->drupalPostForm('user/register', $edit, t('Create new account'));
Chris@4 281 $this->assertRaw(new FormattableMarkup('The email address %value is already taken.', ['%value' => $account->getEmail()]));
Chris@4 282 }
Chris@4 283
Chris@4 284 /**
Chris@4 285 * Tests Field API fields on user registration forms.
Chris@4 286 */
Chris@4 287 public function testRegistrationWithUserFields() {
Chris@4 288 // Create a field on 'user' entity type.
Chris@4 289 $field_storage = FieldStorageConfig::create([
Chris@4 290 'field_name' => 'test_user_field',
Chris@4 291 'entity_type' => 'user',
Chris@4 292 'type' => 'test_field',
Chris@4 293 'cardinality' => 1,
Chris@4 294 ]);
Chris@4 295 $field_storage->save();
Chris@4 296 $field = FieldConfig::create([
Chris@4 297 'field_storage' => $field_storage,
Chris@4 298 'label' => 'Some user field',
Chris@4 299 'bundle' => 'user',
Chris@4 300 'required' => TRUE,
Chris@4 301 ]);
Chris@4 302 $field->save();
Chris@4 303 entity_get_form_display('user', 'user', 'default')
Chris@4 304 ->setComponent('test_user_field', ['type' => 'test_field_widget'])
Chris@4 305 ->save();
Chris@4 306 entity_get_form_display('user', 'user', 'register')
Chris@4 307 ->save();
Chris@4 308
Chris@4 309 // Check that the field does not appear on the registration form.
Chris@4 310 $this->drupalGet('user/register');
Chris@4 311 $this->assertNoText($field->label(), 'The field does not appear on user registration form');
Chris@4 312 $this->assertCacheTag('config:core.entity_form_display.user.user.register');
Chris@4 313 $this->assertCacheTag('config:user.settings');
Chris@4 314
Chris@4 315 // Have the field appear on the registration form.
Chris@4 316 entity_get_form_display('user', 'user', 'register')
Chris@4 317 ->setComponent('test_user_field', ['type' => 'test_field_widget'])
Chris@4 318 ->save();
Chris@4 319
Chris@4 320 $this->drupalGet('user/register');
Chris@4 321 $this->assertText($field->label(), 'The field appears on user registration form');
Chris@4 322 $this->assertRegistrationFormCacheTagsWithUserFields();
Chris@4 323
Chris@4 324 // Check that validation errors are correctly reported.
Chris@4 325 $edit = [];
Chris@4 326 $edit['name'] = $name = $this->randomMachineName();
Chris@4 327 $edit['mail'] = $mail = $edit['name'] . '@example.com';
Chris@4 328 // Missing input in required field.
Chris@4 329 $edit['test_user_field[0][value]'] = '';
Chris@4 330 $this->drupalPostForm(NULL, $edit, t('Create new account'));
Chris@4 331 $this->assertRegistrationFormCacheTagsWithUserFields();
Chris@4 332 $this->assertRaw(t('@name field is required.', ['@name' => $field->label()]), 'Field validation error was correctly reported.');
Chris@4 333 // Invalid input.
Chris@4 334 $edit['test_user_field[0][value]'] = '-1';
Chris@4 335 $this->drupalPostForm(NULL, $edit, t('Create new account'));
Chris@4 336 $this->assertRegistrationFormCacheTagsWithUserFields();
Chris@4 337 $this->assertRaw(t('%name does not accept the value -1.', ['%name' => $field->label()]), 'Field validation error was correctly reported.');
Chris@4 338
Chris@4 339 // Submit with valid data.
Chris@4 340 $value = rand(1, 255);
Chris@4 341 $edit['test_user_field[0][value]'] = $value;
Chris@4 342 $this->drupalPostForm(NULL, $edit, t('Create new account'));
Chris@4 343 // Check user fields.
Chris@4 344 $accounts = $this->container->get('entity_type.manager')->getStorage('user')
Chris@4 345 ->loadByProperties(['name' => $name, 'mail' => $mail]);
Chris@4 346 $new_user = reset($accounts);
Chris@4 347 $this->assertEqual($new_user->test_user_field->value, $value, 'The field value was correctly saved.');
Chris@4 348
Chris@4 349 // Check that the 'add more' button works.
Chris@4 350 $field_storage->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
Chris@4 351 $field_storage->save();
Chris@4 352 $this->drupalGet('user/register');
Chris@4 353 $this->assertRegistrationFormCacheTagsWithUserFields();
Chris@4 354 // Add two inputs.
Chris@4 355 $value = rand(1, 255);
Chris@4 356 $edit = [];
Chris@4 357 $edit['test_user_field[0][value]'] = $value;
Chris@4 358 $this->drupalPostForm(NULL, $edit, t('Add another item'));
Chris@4 359 $this->drupalPostForm(NULL, $edit, t('Add another item'));
Chris@4 360 // Submit with three values.
Chris@4 361 $edit['test_user_field[1][value]'] = $value + 1;
Chris@4 362 $edit['test_user_field[2][value]'] = $value + 2;
Chris@4 363 $edit['name'] = $name = $this->randomMachineName();
Chris@4 364 $edit['mail'] = $mail = $edit['name'] . '@example.com';
Chris@4 365 $this->drupalPostForm(NULL, $edit, t('Create new account'));
Chris@4 366 // Check user fields.
Chris@4 367 $accounts = $this->container->get('entity_type.manager')->getStorage('user')
Chris@4 368 ->loadByProperties(['name' => $name, 'mail' => $mail]);
Chris@4 369 $new_user = reset($accounts);
Chris@4 370 $this->assertEqual($new_user->test_user_field[0]->value, $value, 'The field value was correctly saved.');
Chris@4 371 $this->assertEqual($new_user->test_user_field[1]->value, $value + 1, 'The field value was correctly saved.');
Chris@4 372 $this->assertEqual($new_user->test_user_field[2]->value, $value + 2, 'The field value was correctly saved.');
Chris@4 373 }
Chris@4 374
Chris@4 375 /**
Chris@4 376 * Asserts the presence of cache tags on registration form with user fields.
Chris@4 377 */
Chris@4 378 protected function assertRegistrationFormCacheTagsWithUserFields() {
Chris@4 379 $this->assertCacheTag('config:core.entity_form_display.user.user.register');
Chris@4 380 $this->assertCacheTag('config:field.field.user.user.test_user_field');
Chris@4 381 $this->assertCacheTag('config:field.storage.user.test_user_field');
Chris@4 382 $this->assertCacheTag('config:user.settings');
Chris@4 383 }
Chris@4 384
Chris@4 385 }