View source  
  <?php
namespace Drupal\user\Tests;
use Drupal\Core\Form\FormState;
use Drupal\simpletest\KernelTestBase;
class UserAccountFormFieldsTest extends KernelTestBase {
  
  public static $modules = array(
    'system',
    'user',
    'field',
  );
  
  function testInstallConfigureForm() {
    require_once \Drupal::root() . '/core/includes/install.core.inc';
    require_once \Drupal::root() . '/core/includes/install.inc';
    $install_state = install_state_defaults();
    $form_state = new FormState();
    $form_state
      ->addBuildInfo('args', [
      &$install_state,
    ]);
    $form = $this->container
      ->get('form_builder')
      ->buildForm('Drupal\\Core\\Installer\\Form\\SiteConfigureForm', $form_state);
    
    $this
      ->assertFieldOrder($form['admin_account']['account']);
    
    foreach (array(
      'mail',
      'name',
      'pass',
    ) as $key) {
      $this
        ->assertFalse(isset($form['account'][$key]['#attributes']['autocomplete']), "'{$key}' field: 'autocomplete' attribute not found.");
    }
  }
  
  function testUserRegistrationForm() {
    
    $this
      ->installConfig(array(
      'user',
    ));
    
    $this
      ->config('user.settings')
      ->set('verify_mail', FALSE)
      ->save();
    $form = $this
      ->buildAccountForm('register');
    
    $this
      ->assertFieldOrder($form['account']);
    
    foreach (array(
      'mail',
      'name',
      'pass',
    ) as $key) {
      $this
        ->assertFalse(isset($form['account'][$key]['#attributes']['autocomplete']), "'{$key}' field: 'autocomplete' attribute not found.");
    }
  }
  
  function testUserEditForm() {
    
    $this
      ->installConfig(array(
      'user',
    ));
    
    $this
      ->installSchema('system', [
      'router',
    ]);
    \Drupal::service('router.builder')
      ->rebuild();
    $form = $this
      ->buildAccountForm('default');
    
    $this
      ->assertFieldOrder($form['account']);
    
    foreach (array(
      'mail',
      'name',
      'pass',
    ) as $key) {
      $this
        ->assertIdentical($form['account'][$key]['#attributes']['autocomplete'], 'off', "'{$key}' field: 'autocomplete' attribute is 'off'.");
    }
  }
  
  protected function assertFieldOrder(array $elements) {
    $name_index = 0;
    $name_weight = 0;
    $pass_index = 0;
    $pass_weight = 0;
    $index = 0;
    foreach ($elements as $key => $element) {
      if ($key === 'name') {
        $name_index = $index;
        $name_weight = $element['#weight'];
        $this
          ->assertTrue($element['#sorted'], "'name' field is #sorted.");
      }
      elseif ($key === 'pass') {
        $pass_index = $index;
        $pass_weight = $element['#weight'];
        $this
          ->assertTrue($element['#sorted'], "'pass' field is #sorted.");
      }
      $index++;
    }
    $this
      ->assertEqual($name_index, $pass_index - 1, "'name' field ({$name_index}) appears before 'pass' field ({$pass_index}).");
    $this
      ->assertTrue($name_weight < $pass_weight, "'name' field weight ({$name_weight}) is smaller than 'pass' field weight ({$pass_weight}).");
  }
  
  protected function buildAccountForm($operation) {
    
    $entity_type = 'user';
    $fields = array();
    if ($operation != 'register') {
      $fields['uid'] = 2;
    }
    $entity = $this->container
      ->get('entity.manager')
      ->getStorage($entity_type)
      ->create($fields);
    $form_object = $this->container
      ->get('entity.manager')
      ->getFormObject($entity_type, $operation)
      ->setEntity($entity);
    
    return $this->container
      ->get('entity.form_builder')
      ->getForm($entity, $operation);
  }
}