Unverified Commit 399c0a85 authored by alexpott's avatar alexpott
Browse files

Issue #2916142 by beatrizrodrigues, Chi, yogeshmpawar, tobiberlin, quietone,...

Issue #2916142 by beatrizrodrigues, Chi, yogeshmpawar, tobiberlin, quietone, borisson_: Decimal and Float item generates wrong sample values

(cherry picked from commit b2901ce7)
parent 1d64f860
......@@ -136,8 +136,8 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
// point.
// The maximum number you can get with 3 digits is 10^3 - 1 --> 999.
// The minimum number you can get with 3 digits is -1 * (10^3 - 1).
$max = is_numeric($settings['max']) ?: pow(10, ($precision - $scale)) - 1;
$min = is_numeric($settings['min']) ?: -pow(10, ($precision - $scale)) + 1;
$max = is_numeric($settings['max']) ? $settings['max'] : pow(10, ($precision - $scale)) - 1;
$min = is_numeric($settings['min']) ? $settings['min'] : -pow(10, ($precision - $scale)) + 1;
// Get the number of decimal digits for the $max
$decimal_digits = self::getDecimalDigits($max);
......
......@@ -64,8 +64,8 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
$settings = $field_definition->getSettings();
$precision = rand(10, 32);
$scale = rand(0, 2);
$max = is_numeric($settings['max']) ?: pow(10, ($precision - $scale)) - 1;
$min = is_numeric($settings['min']) ?: -pow(10, ($precision - $scale)) + 1;
$max = is_numeric($settings['max']) ? $settings['max'] : pow(10, ($precision - $scale)) - 1;
$min = is_numeric($settings['min']) ? $settings['min'] : -pow(10, ($precision - $scale)) + 1;
// @see "Example #1 Calculate a random floating-point number" in
// http://php.net/manual/function.mt-getrandmax.php
$random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
......
......@@ -95,9 +95,28 @@ public function testNumberItem() {
// Test sample item generation.
$entity = EntityTest::create();
// Make sure that field settings are respected by the generation.
$entity->field_decimal
->getFieldDefinition()
->setSetting('min', 99)
->setSetting('max', 100);
$entity->field_float
->getFieldDefinition()
->setSetting('min', 99)
->setSetting('max', 100);
$entity->field_integer
->getFieldDefinition()
->setSetting('min', 99)
->setSetting('max', 100);
$entity->field_decimal->generateSampleItems();
$entity->field_integer->generateSampleItems();
$entity->field_float->generateSampleItems();
$entity->field_decimal->generateSampleItems();
// Confirm that the generated sample values are within range.
$this->entityValidateAndSave($entity);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment