annotate core/modules/user/tests/src/Functional/UserRegistrationTest.php @ 19:fa3358dc1485 tip

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