function NumberItemTest::testFormFieldMinMaxValue

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

Tests the validation of minimum and maximum values.

@dataProvider dataTestMinMaxValue

Parameters

int|float|string $min: Min value to be tested.

int|float|string $max: Max value to be tested.

int|float|string $value: Value to be tested with min and max values.

bool $hasError: Expected validation result.

string $message: (optional) Error message result.

File

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

Class

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

Namespace

Drupal\Tests\field\Kernel\Number

Code

public function testFormFieldMinMaxValue(int|float|string $min, int|float|string $max, int|float|string $value, bool $hasError, string $message = '') : void {
    $element = [
        '#type' => 'number',
        '#title' => 'min',
        '#default_value' => $min,
        '#element_validate' => [
            [
                NumericItemBase::class,
                'validateMinAndMaxConfig',
            ],
        ],
        '#description' => 'The minimum value that should be allowed in this field. Leave blank for no minimum.',
        '#parents' => [],
        '#name' => 'min',
    ];
    $form_state = new FormState();
    $form_state->setValue('min', $value);
    $form_state->setValue('settings', [
        'min' => $min,
        'max' => $max,
        'prefix' => '',
        'suffix' => '',
        'precision' => 10,
        'scale' => 2,
    ]);
    $completed_form = [];
    NumericItemBase::validateMinAndMaxConfig($element, $form_state, $completed_form);
    $errors = $form_state->getErrors();
    $this->assertEquals($hasError, count($errors) > 0);
    if ($errors) {
        $error = current($errors);
        $this->assertEquals($error, $message);
    }
}

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