You are here

public function NumberItemTest::testConstraints in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/Number/NumberItemTest.php \Drupal\Tests\field\Kernel\Number\NumberItemTest::testConstraints()
  2. 10 core/modules/field/tests/src/Kernel/Number/NumberItemTest.php \Drupal\Tests\field\Kernel\Number\NumberItemTest::testConstraints()

Tests constraints on numeric item fields.

@dataProvider dataNumberFieldSettingsProvider

Parameters

string $type: The field type.

int|float $min: The minimum field value.

int|float $max: The maximum field value.

int|float $value: The test value.

bool $expect_constraints: If TRUE this data set will trigger a validation constraint.

string $expected_constraint_message: The expected constraint violation message.

Throws

\Drupal\Core\Entity\EntityStorageException

File

core/modules/field/tests/src/Kernel/Number/NumberItemTest.php, line 124

Class

NumberItemTest
Tests the new entity API for the number field type.

Namespace

Drupal\Tests\field\Kernel\Number

Code

public function testConstraints($type, $min, $max, $value, $expect_constraints, $expected_constraint_message = '') {
  $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_' . $type);
  $field
    ->setSetting('min', $min);
  $field
    ->setSetting('max', $max);
  $field
    ->save();
  $entity = EntityTest::create();
  $entity->{'field_' . $type} = $value;
  $violations = $entity
    ->validate();
  $this
    ->assertEquals($expect_constraints, $violations
    ->count() > 0);
  if ($expect_constraints) {
    $this
      ->assertEquals($expected_constraint_message, $violations
      ->get(0)
      ->getMessage());
  }
}