annotate core/modules/user/src/Tests/UserCreateTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\user\Tests;
Chris@0 4
Chris@0 5 use Drupal\field\Entity\FieldConfig;
Chris@0 6 use Drupal\simpletest\WebTestBase;
Chris@0 7 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests the create user administration page.
Chris@0 11 *
Chris@0 12 * @group user
Chris@0 13 */
Chris@0 14 class UserCreateTest extends WebTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Modules to enable.
Chris@0 18 *
Chris@0 19 * @var array
Chris@0 20 */
Chris@0 21 public static $modules = ['image'];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Create a user through the administration interface and ensure that it
Chris@0 25 * displays in the user list.
Chris@0 26 */
Chris@0 27 public function testUserAdd() {
Chris@0 28 $user = $this->drupalCreateUser(['administer users']);
Chris@0 29 $this->drupalLogin($user);
Chris@0 30
Chris@0 31 $this->assertEqual($user->getCreatedTime(), REQUEST_TIME, 'Creating a user sets default "created" timestamp.');
Chris@0 32 $this->assertEqual($user->getChangedTime(), REQUEST_TIME, 'Creating a user sets default "changed" timestamp.');
Chris@0 33
Chris@0 34 // Create a field.
Chris@0 35 $field_name = 'test_field';
Chris@0 36 FieldStorageConfig::create([
Chris@0 37 'field_name' => $field_name,
Chris@0 38 'entity_type' => 'user',
Chris@0 39 'module' => 'image',
Chris@0 40 'type' => 'image',
Chris@0 41 'cardinality' => 1,
Chris@0 42 'locked' => FALSE,
Chris@0 43 'indexes' => ['target_id' => ['target_id']],
Chris@0 44 'settings' => [
Chris@0 45 'uri_scheme' => 'public',
Chris@0 46 ],
Chris@0 47 ])->save();
Chris@0 48
Chris@0 49 FieldConfig::create([
Chris@0 50 'field_name' => $field_name,
Chris@0 51 'entity_type' => 'user',
Chris@0 52 'label' => 'Picture',
Chris@0 53 'bundle' => 'user',
Chris@0 54 'description' => t('Your virtual face or picture.'),
Chris@0 55 'required' => FALSE,
Chris@0 56 'settings' => [
Chris@0 57 'file_extensions' => 'png gif jpg jpeg',
Chris@0 58 'file_directory' => 'pictures',
Chris@0 59 'max_filesize' => '30 KB',
Chris@0 60 'alt_field' => 0,
Chris@0 61 'title_field' => 0,
Chris@0 62 'max_resolution' => '85x85',
Chris@0 63 'min_resolution' => '',
Chris@0 64 ],
Chris@0 65 ])->save();
Chris@0 66
Chris@0 67 // Test user creation page for valid fields.
Chris@0 68 $this->drupalGet('admin/people/create');
Chris@0 69 $this->assertFieldbyId('edit-status-0', 0, 'The user status option Blocked exists.', 'User login');
Chris@0 70 $this->assertFieldbyId('edit-status-1', 1, 'The user status option Active exists.', 'User login');
Chris@0 71 $this->assertFieldByXPath('//input[@type="radio" and @id="edit-status-1" and @checked="checked"]', NULL, 'Default setting for user status is active.');
Chris@0 72
Chris@0 73 // Test that browser autocomplete behavior does not occur.
Chris@0 74 $this->assertNoRaw('data-user-info-from-browser', 'Ensure form attribute, data-user-info-from-browser, does not exist.');
Chris@0 75
Chris@0 76 // Test that the password strength indicator displays.
Chris@0 77 $config = $this->config('user.settings');
Chris@0 78
Chris@0 79 $config->set('password_strength', TRUE)->save();
Chris@0 80 $this->drupalGet('admin/people/create');
Chris@0 81 $this->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.');
Chris@0 82
Chris@0 83 $config->set('password_strength', FALSE)->save();
Chris@0 84 $this->drupalGet('admin/people/create');
Chris@0 85 $this->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.');
Chris@0 86
Chris@0 87 // We create two users, notifying one and not notifying the other, to
Chris@0 88 // ensure that the tests work in both cases.
Chris@0 89 foreach ([FALSE, TRUE] as $notify) {
Chris@0 90 $name = $this->randomMachineName();
Chris@0 91 $edit = [
Chris@0 92 'name' => $name,
Chris@0 93 'mail' => $this->randomMachineName() . '@example.com',
Chris@0 94 'pass[pass1]' => $pass = $this->randomString(),
Chris@0 95 'pass[pass2]' => $pass,
Chris@0 96 'notify' => $notify,
Chris@0 97 ];
Chris@0 98 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
Chris@0 99
Chris@0 100 if ($notify) {
Chris@0 101 $this->assertText(t('A welcome message with further instructions has been emailed to the new user @name.', ['@name' => $edit['name']]), 'User created');
Chris@0 102 $this->assertEqual(count($this->drupalGetMails()), 1, 'Notification email sent');
Chris@0 103 }
Chris@0 104 else {
Chris@0 105 $this->assertText(t('Created a new user account for @name. No email has been sent.', ['@name' => $edit['name']]), 'User created');
Chris@0 106 $this->assertEqual(count($this->drupalGetMails()), 0, 'Notification email not sent');
Chris@0 107 }
Chris@0 108
Chris@0 109 $this->drupalGet('admin/people');
Chris@0 110 $this->assertText($edit['name'], 'User found in list of users');
Chris@0 111 $user = user_load_by_name($name);
Chris@0 112 $this->assertEqual($user->isActive(), 'User is not blocked');
Chris@0 113 }
Chris@0 114
Chris@0 115 // Test that the password '0' is considered a password.
Chris@0 116 // @see https://www.drupal.org/node/2563751.
Chris@0 117 $name = $this->randomMachineName();
Chris@0 118 $edit = [
Chris@0 119 'name' => $name,
Chris@0 120 'mail' => $this->randomMachineName() . '@example.com',
Chris@0 121 'pass[pass1]' => 0,
Chris@0 122 'pass[pass2]' => 0,
Chris@0 123 'notify' => FALSE,
Chris@0 124 ];
Chris@0 125 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
Chris@0 126 $this->assertText("Created a new user account for $name. No email has been sent");
Chris@0 127 $this->assertNoText('Password field is required');
Chris@0 128 }
Chris@0 129
Chris@0 130 }