From eb99b6b50f76f86365c5f707cb0703a253c1dedd Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Mon, 4 Nov 2019 13:15:12 +0100 Subject: [PATCH] Issue #3090629 by mglaman, BR0kEN, oriol_e9g: NumericItemBase never sets minimum value constraint if minimum value is zero --- .../Field/FieldType/NumericItemBase.php | 4 +- .../src/Kernel/Number/NumberItemTest.php | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php index 50eb767f9971..a0c2af0e21bd 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php @@ -79,7 +79,7 @@ public function getConstraints() { $settings = $this->getSettings(); $label = $this->getFieldDefinition()->getLabel(); - if (!empty($settings['min'])) { + if (isset($settings['min']) && $settings['min'] !== '') { $min = $settings['min']; $constraints[] = $constraint_manager->create('ComplexData', [ 'value' => [ @@ -91,7 +91,7 @@ public function getConstraints() { ]); } - if (!empty($settings['max'])) { + if (isset($settings['max']) && $settings['max'] !== '') { $max = $settings['max']; $constraints[] = $constraint_manager->create('ComplexData', [ 'value' => [ diff --git a/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php index f9a82a4d546b..3864d5503b48 100644 --- a/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php +++ b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php @@ -101,4 +101,69 @@ public function testNumberItem() { $this->entityValidateAndSave($entity); } + /** + * Tests constraints on numeric item fields. + * + * @dataProvider dataNumberFieldSettingsProvider + * + * @param string $type + * The field type. + * @param int|float $min + * The minimum field value. + * @param int|float $max + * The maximum field value. + * @param int|float $value + * The test value. + * @param bool $expect_constraints + * If TRUE this data set will trigger a validation constraint. + * @param string $expected_constraint_message + * The expected constraint violation message. + * + * @throws \Drupal\Core\Entity\EntityStorageException + */ + 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()); + } + + } + + /** + * Data provider for testConstraints. + * + * @return \Generator + * The test data. + */ + public function dataNumberFieldSettingsProvider() { + yield ['integer', NULL, NULL, -100, FALSE]; + yield ['integer', 0, NULL, -100, TRUE, '<em class="placeholder">field_integer</em>: the value may be no less than <em class="placeholder">0</em>.']; + yield ['integer', 10, NULL, 100, FALSE]; + yield ['integer', 10, NULL, 5, TRUE, '<em class="placeholder">field_integer</em>: the value may be no less than <em class="placeholder">10</em>.']; + yield ['integer', 10, 20, 25, TRUE, '<em class="placeholder">field_integer</em>: the value may be no greater than <em class="placeholder">20</em>.']; + yield ['integer', 10, 20, 15, FALSE]; + + yield ['float', NULL, NULL, -100, FALSE]; + yield ['float', 0.003, NULL, 0.0029, TRUE, '<em class="placeholder">field_float</em>: the value may be no less than <em class="placeholder">0.003</em>.']; + yield ['float', 10.05, NULL, 13.4, FALSE]; + yield ['float', 10, NULL, 9.999, TRUE, '<em class="placeholder">field_float</em>: the value may be no less than <em class="placeholder">10</em>.']; + yield ['float', 1, 2, 2.5, TRUE, '<em class="placeholder">field_float</em>: the value may be no greater than <em class="placeholder">2</em>.']; + yield ['float', 1, 2, 1.5, FALSE]; + + yield ['decimal', NULL, NULL, -100, FALSE]; + yield ['decimal', 0.001, NULL, -0.05, TRUE, '<em class="placeholder">field_decimal</em>: the value may be no less than <em class="placeholder">0.001</em>.']; + yield ['decimal', 10.05, NULL, 13.4, FALSE]; + yield ['decimal', 10, NULL, 9.999, TRUE, '<em class="placeholder">field_decimal</em>: the value may be no less than <em class="placeholder">10</em>.']; + yield ['decimal', 1, 2, 2.5, TRUE, '<em class="placeholder">field_decimal</em>: the value may be no greater than <em class="placeholder">2</em>.']; + yield ['decimal', 1, 2, 1.5, FALSE]; + } + } -- GitLab