function NumberItemTest::testConstraints

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/Number/NumberItemTest.php \Drupal\Tests\field\Kernel\Number\NumberItemTest::testConstraints()
  2. 8.9.x core/modules/field/tests/src/Kernel/Number/NumberItemTest.php \Drupal\Tests\field\Kernel\Number\NumberItemTest::testConstraints()
  3. 11.x 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 149

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 = '') : void {
    $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());
    }
}

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