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

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