function UserValidationTest::testValidation

Same name in other branches
  1. 9 core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::testValidation()
  2. 8.9.x core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::testValidation()
  3. 10 core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::testValidation()

Runs entity validation checks.

File

core/modules/user/tests/src/Kernel/UserValidationTest.php, line 83

Class

UserValidationTest
Verify that user validity checks behave as designed.

Namespace

Drupal\Tests\user\Kernel

Code

public function testValidation() : void {
    $user = User::create([
        'name' => 'test',
        'mail' => 'test@example.com',
    ]);
    $violations = $user->validate();
    $this->assertCount(0, $violations, '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->assertCount(1, $violations, 'Violation found when name is too long.');
    $this->assertEquals('name', $violations[0]->getPropertyPath());
    $this->assertEquals(sprintf('The username %s is too long: it must be 60 characters or less.', $name), $violations[0]->getMessage());
    // Create a second test user to provoke a name collision.
    $user2 = User::create([
        'name' => 'existing',
        'mail' => 'existing@example.com',
    ]);
    $user2->save();
    $user->set('name', 'existing');
    $violations = $user->validate();
    $this->assertCount(1, $violations, 'Violation found on name collision.');
    $this->assertEquals('name', $violations[0]->getPropertyPath());
    $this->assertEquals('The username existing is already taken.', $violations[0]->getMessage());
    // Make the name valid.
    $user->set('name', $this->randomMachineName());
    $user->set('mail', 'invalid');
    $violations = $user->validate();
    $this->assertCount(1, $violations, 'Violation found when email is invalid');
    $this->assertEquals('mail.0.value', $violations[0]->getPropertyPath());
    $this->assertEquals('This value is not a valid email address.', $violations[0]->getMessage());
    $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->assertCount(2, $violations, 'Violations found when email is too long');
    $this->assertEquals('mail.0.value', $violations[0]->getPropertyPath());
    $this->assertEquals(sprintf('%s: the email address can not be longer than %s characters.', $user->get('mail')
        ->getFieldDefinition()
        ->getLabel(), Email::EMAIL_MAX_LENGTH), $violations[0]->getMessage());
    $this->assertEquals('mail.0.value', $violations[1]->getPropertyPath());
    $this->assertEquals('This value is not a valid email address.', $violations[1]->getMessage());
    // Provoke an email collision with an existing user.
    $user->set('mail', 'existing@example.com');
    $violations = $user->validate();
    $this->assertCount(1, $violations, 'Violation found when email already exists.');
    $this->assertEquals('mail', $violations[0]->getPropertyPath());
    $this->assertEquals('The email address existing@example.com is already taken.', $violations[0]->getMessage());
    $user->set('mail', NULL);
    $violations = $user->validate();
    $this->assertCount(1, $violations, 'Email addresses may not be removed');
    $this->assertEquals('mail', $violations[0]->getPropertyPath());
    $this->assertEquals(sprintf('%s field is required.', $user->getFieldDefinition('mail')
        ->getLabel()), $violations[0]->getMessage());
    $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->assertCount(1, $violations, '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([
        'id' => 'role1',
        'label' => 'Role 1',
    ])->save();
    Role::create([
        'id' => 'role2',
        'label' => 'Role 2',
    ])->save();
    // Test cardinality of user roles.
    $user = User::create([
        'name' => 'role_test',
        'mail' => 'test@example.com',
        'roles' => [
            'role1',
            'role2',
        ],
    ]);
    $violations = $user->validate();
    $this->assertCount(0, $violations);
    $user->roles[1]->target_id = 'unknown_role';
    $violations = $user->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('roles.1.target_id', $violations[0]->getPropertyPath());
    $this->assertEquals('The referenced entity (user_role: unknown_role) does not exist.', $violations[0]->getMessage());
}

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