function UserValidationTest::assertAllowedValuesViolation

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

Verifies that an AllowedValues violation exists for the given field.

@internal

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object to validate.

string $field_name: The name of the field to verify.

1 call to UserValidationTest::assertAllowedValuesViolation()
UserValidationTest::testValidation in core/modules/user/tests/src/Kernel/UserValidationTest.php
Runs entity validation checks.

File

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

Class

UserValidationTest
Verify that user validity checks behave as designed.

Namespace

Drupal\Tests\user\Kernel

Code

protected function assertAllowedValuesViolation(EntityInterface $entity, string $field_name) : void {
    $violations = $entity->validate();
    $this->assertCount(1, $violations, "Allowed values violation for {$field_name} found.");
    $this->assertEquals($field_name === 'langcode' ? "{$field_name}.0" : "{$field_name}.0.value", $violations[0]->getPropertyPath());
    $this->assertEquals('The value you selected is not a valid choice.', $violations[0]->getMessage());
}

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