You are here

function UserValidationTest::testValidation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserValidationTest.php \Drupal\user\Tests\UserValidationTest::testValidation()

Runs entity validation checks.

File

core/modules/user/src/Tests/UserValidationTest.php, line 76
Contains \Drupal\user\Tests\UserValidationTest.

Class

UserValidationTest
Verify that user validity checks behave as designed.

Namespace

Drupal\user\Tests

Code

function testValidation() {
  $user = User::create(array(
    'name' => 'test',
    'mail' => 'test@example.com',
  ));
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 0, 'No violations when validating a default user.');

  // Only test one example invalid name here, the rest is already covered in
  // the testUsernames() method in this class.
  $name = $this
    ->randomMachineName(61);
  $user
    ->set('name', $name);
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 1, 'Violation found when name is too long.');
  $this
    ->assertEqual($violations[0]
    ->getPropertyPath(), 'name');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('The username %name is too long: it must be %max characters or less.', array(
    '%name' => $name,
    '%max' => 60,
  )));

  // Create a second test user to provoke a name collision.
  $user2 = entity_create('user', array(
    'name' => 'existing',
    'mail' => 'existing@example.com',
  ));
  $user2
    ->save();
  $user
    ->set('name', 'existing');
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 1, 'Violation found on name collision.');
  $this
    ->assertEqual($violations[0]
    ->getPropertyPath(), 'name');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('The username %name is already taken.', array(
    '%name' => 'existing',
  )));

  // Make the name valid.
  $user
    ->set('name', $this
    ->randomMachineName());
  $user
    ->set('mail', 'invalid');
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 1, 'Violation found when email is invalid');
  $this
    ->assertEqual($violations[0]
    ->getPropertyPath(), 'mail.0.value');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('This value is not a valid email address.'));
  $mail = $this
    ->randomMachineName(Email::EMAIL_MAX_LENGTH - 11) . '@example.com';
  $user
    ->set('mail', $mail);
  $violations = $user
    ->validate();

  // @todo There are two violations because EmailItem::getConstraints()
  //   overlaps with the implicit constraint of the 'email' property type used
  //   in EmailItem::propertyDefinitions(). Resolve this in
  //   https://www.drupal.org/node/2023465.
  $this
    ->assertEqual(count($violations), 2, 'Violations found when email is too long');
  $this
    ->assertEqual($violations[0]
    ->getPropertyPath(), 'mail.0.value');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('%name: the email address can not be longer than @max characters.', array(
    '%name' => $user
      ->get('mail')
      ->getFieldDefinition()
      ->getLabel(),
    '@max' => Email::EMAIL_MAX_LENGTH,
  )));
  $this
    ->assertEqual($violations[1]
    ->getPropertyPath(), 'mail.0.value');
  $this
    ->assertEqual($violations[1]
    ->getMessage(), t('This value is not a valid email address.'));

  // Provoke an email collision with an existing user.
  $user
    ->set('mail', 'existing@example.com');
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 1, 'Violation found when email already exists.');
  $this
    ->assertEqual($violations[0]
    ->getPropertyPath(), 'mail');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('The email address %mail is already taken.', array(
    '%mail' => 'existing@example.com',
  )));
  $user
    ->set('mail', NULL);
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 1, 'Email addresses may not be removed');
  $this
    ->assertEqual($violations[0]
    ->getPropertyPath(), 'mail');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('@name field is required.', array(
    '@name' => $user
      ->getFieldDefinition('mail')
      ->getLabel(),
  )));
  $user
    ->set('mail', 'someone@example.com');
  $user
    ->set('timezone', $this
    ->randomString(33));
  $this
    ->assertLengthViolation($user, 'timezone', 32, 2, 1);
  $user
    ->set('timezone', 'invalid zone');
  $this
    ->assertAllowedValuesViolation($user, 'timezone');
  $user
    ->set('timezone', NULL);
  $user
    ->set('init', 'invalid');
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 1, 'Violation found when init email is invalid');
  $user
    ->set('init', NULL);
  $user
    ->set('langcode', 'invalid');
  $this
    ->assertAllowedValuesViolation($user, 'langcode');
  $user
    ->set('langcode', NULL);

  // Only configurable langcodes are allowed for preferred languages.
  $user
    ->set('preferred_langcode', Language::LANGCODE_NOT_SPECIFIED);
  $this
    ->assertAllowedValuesViolation($user, 'preferred_langcode');
  $user
    ->set('preferred_langcode', NULL);
  $user
    ->set('preferred_admin_langcode', Language::LANGCODE_NOT_SPECIFIED);
  $this
    ->assertAllowedValuesViolation($user, 'preferred_admin_langcode');
  $user
    ->set('preferred_admin_langcode', NULL);
  Role::create(array(
    'id' => 'role1',
  ))
    ->save();
  Role::create(array(
    'id' => 'role2',
  ))
    ->save();

  // Test cardinality of user roles.
  $user = entity_create('user', array(
    'name' => 'role_test',
    'mail' => 'test@example.com',
    'roles' => array(
      'role1',
      'role2',
    ),
  ));
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 0);
  $user->roles[1]->target_id = 'unknown_role';
  $violations = $user
    ->validate();
  $this
    ->assertEqual(count($violations), 1);
  $this
    ->assertEqual($violations[0]
    ->getPropertyPath(), 'roles.1.target_id');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('The referenced entity (%entity_type: %name) does not exist.', array(
    '%entity_type' => 'user_role',
    '%name' => 'unknown_role',
  )));
}