UserCreateTest.php

Same filename and directory in other branches
  1. 11.x core/modules/user/tests/src/Functional/UserCreateTest.php
  2. 10 core/modules/user/tests/src/Functional/UserCreateTest.php
  3. 9 core/modules/user/tests/src/Functional/UserCreateTest.php
  4. 8.9.x core/modules/user/tests/src/Functional/UserCreateTest.php

Namespace

Drupal\Tests\user\Functional

File

core/modules/user/tests/src/Functional/UserCreateTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\user\Functional;

use Drupal\Core\Test\AssertMailTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests the create user administration page.
 */
class UserCreateTest extends BrowserTestBase {
  use AssertMailTrait {
    getMails as drupalGetMails;
  }
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'image',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * Tests user creation and display from the administration interface.
   */
  public function testUserAdd() : void {
    $user = $this->drupalCreateUser([
      'administer users',
    ]);
    $this->drupalLogin($user);
    $this->assertEquals(\Drupal::time()->getRequestTime(), $user->getCreatedTime(), 'Creating a user sets default "created" timestamp.');
    $this->assertEquals(\Drupal::time()->getRequestTime(), $user->getChangedTime(), 'Creating a user sets default "changed" timestamp.');
    // Create a field.
    $field_name = 'test_field';
    FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'user',
      'module' => 'image',
      'type' => 'image',
      'cardinality' => 1,
      'locked' => FALSE,
      'indexes' => [
        'target_id' => [
          'target_id',
        ],
      ],
      'settings' => [
        'uri_scheme' => 'public',
      ],
    ])->save();
    FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'user',
      'label' => 'Picture',
      'bundle' => 'user',
      'description' => 'Your virtual face or picture.',
      'required' => FALSE,
      'settings' => [
        'file_extensions' => 'png gif jpg jpeg webp',
        'file_directory' => 'pictures',
        'max_filesize' => '30 KB',
        'alt_field' => 0,
        'title_field' => 0,
        'max_resolution' => '85x85',
        'min_resolution' => '',
      ],
    ])->save();
    // Test user creation page for valid fields.
    $this->drupalGet('admin/people/create');
    $this->assertSession()
      ->fieldValueEquals('edit-status-0', '1');
    $this->assertSession()
      ->fieldValueEquals('edit-status-1', '1');
    $this->assertSession()
      ->checkboxChecked('edit-status-1');
    // Test that browser autocomplete behavior does not occur.
    $this->assertSession()
      ->responseNotContains('data-user-info-from-browser');
    // Test that the password strength indicator displays.
    $config = $this->config('user.settings');
    $config->set('password_strength', TRUE)
      ->save();
    $this->drupalGet('admin/people/create');
    $this->assertSession()
      ->responseContains("Password strength:");
    $config->set('password_strength', FALSE)
      ->save();
    $this->drupalGet('admin/people/create');
    $this->assertSession()
      ->responseNotContains("Password strength:");
    // We create two users, notifying one and not notifying the other, to
    // ensure that the tests work in both cases.
    foreach ([
      FALSE,
      TRUE,
    ] as $notify) {
      $name = $this->randomMachineName();
      $edit = [
        'name' => $name,
        'mail' => $this->randomMachineName() . '@example.com',
        'pass[pass1]' => $pass = $this->randomString(),
        'pass[pass2]' => $pass,
        'notify' => $notify,
      ];
      $this->drupalGet('admin/people/create');
      $this->submitForm($edit, 'Create new account');
      if ($notify) {
        $this->assertSession()
          ->pageTextContains('A welcome message with further instructions has been emailed to the new user ' . $edit['name'] . '.');
        $this->assertCount(1, $this->drupalGetMails(), 'Notification email sent');
      }
      else {
        $this->assertSession()
          ->pageTextContains('Created a new user account for ' . $edit['name'] . '. No email has been sent.');
        $this->assertCount(0, $this->drupalGetMails(), 'Notification email not sent');
      }
      $this->drupalGet('admin/people');
      $this->assertSession()
        ->pageTextContains($edit['name']);
      $users = \Drupal::entityTypeManager()->getStorage('user')
        ->loadByProperties([
        'name' => $name,
      ]);
      $user = reset($users);
      $this->assertTrue($user->isActive(), 'User is not blocked');
    }
    // Test that the password '0' is considered a password.
    // @see https://www.drupal.org/node/2563751.
    $name = $this->randomMachineName();
    $edit = [
      'name' => $name,
      'mail' => $this->randomMachineName() . '@example.com',
      'pass[pass1]' => 0,
      'pass[pass2]' => 0,
      'notify' => FALSE,
    ];
    $this->drupalGet('admin/people/create');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()
      ->pageTextContains("Created a new user account for {$name}. No email has been sent");
    $this->assertSession()
      ->pageTextNotContains('Password field is required');
  }
  
  /**
   * Tests that the password is discarded when notify is enabled.
   */
  public function testUserAddPasswordWithNotify() : void {
    $admin = $this->drupalCreateUser([
      'administer users',
    ]);
    $this->drupalLogin($admin);
    $password_checker = \Drupal::service('password');
    $typed_pass = 'known_password_for_testing';
    // Create a user with notify enabled and a password filled in.
    // The typed password should be discarded.
    $name_notify = $this->randomMachineName();
    $edit = [
      'name' => $name_notify,
      'mail' => $this->randomMachineName() . '@example.com',
      'pass[pass1]' => $typed_pass,
      'pass[pass2]' => $typed_pass,
      'notify' => TRUE,
    ];
    $this->drupalGet('admin/people/create');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()
      ->pageTextContains('A welcome message with further instructions has been emailed to the new user ' . $name_notify . '.');
    $users = \Drupal::entityTypeManager()->getStorage('user')
      ->loadByProperties([
      'name' => $name_notify,
    ]);
    $user_notify = reset($users);
    // The stored password should not match the typed password.
    $this->assertFalse($password_checker->check($typed_pass, $user_notify->getPassword()), 'Typed password is not used when notify is enabled.');
    // The stored password should not be a hash of an empty string.
    $this->assertFalse($password_checker->check('', $user_notify->getPassword()), 'Password is not a hash of an empty string when notify is enabled.');
    // Create a user with notify disabled and a password filled in.
    // The typed password should be used.
    $name_no_notify = $this->randomMachineName();
    $edit = [
      'name' => $name_no_notify,
      'mail' => $this->randomMachineName() . '@example.com',
      'pass[pass1]' => $typed_pass,
      'pass[pass2]' => $typed_pass,
      'notify' => FALSE,
    ];
    $this->drupalGet('admin/people/create');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()
      ->pageTextContains('Created a new user account for ' . $name_no_notify . '. No email has been sent.');
    $users = \Drupal::entityTypeManager()->getStorage('user')
      ->loadByProperties([
      'name' => $name_no_notify,
    ]);
    $user_no_notify = reset($users);
    // The stored password should match the typed password.
    $this->assertTrue($password_checker->check($typed_pass, $user_no_notify->getPassword()), 'Typed password is used when notify is disabled.');
  }

}

Classes

Title Deprecated Summary
UserCreateTest Tests the create user administration page.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.