Problem/Motivation

Generating decimal values does not respect the min and max values.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21P...

$max = is_numeric($settings['max']) ?: pow(10, ($precision - $scale)) - 1;
$min = is_numeric($settings['min']) ?: -pow(10, ($precision - $scale)) + 1;

This should be turned into normal ternary operator because is_numeric() returns boolean value.

Steps to reproduce

Steps to reproduce.

  1. Add field of type Number (Decimal) to some content type
  2. Set minimum and maximum options for this field
  3. Generate sample data using Devel generate module
  4. Verify that generated value for this field is always 1.00.

Proposed resolution

Change the min/max for float and decimal to something like this:

    $max = is_numeric($settings['max']) ? $settings['max'] : pow(10, ($precision - $scale)) - 1;
    $min = is_numeric($settings['min']) ? $settings['min'] : -pow(10, ($precision - $scale)) + 1;

Remaining tasks

Review
Commit

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Comments

Chi created an issue. See original summary.

tobiberlin’s picture

Status: Active » Needs review
StatusFileSize
new1.03 KB

Please find attached patch

tobiberlin’s picture

I am a novice so I hope it was ok to upload this patch and move the ticket status to "Needs review"?

chi’s picture

Status: Needs review » Reviewed & tested by the community

@tobiberlin it's ok.

chi’s picture

The patch works for me. Devel generate now produces correct random values for decimal field. Thanks.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Nice find!

We need a small automated test for this as well, to assert that correct values are generated so we don't break it in the future. Thanks!

chi’s picture

Issue tags: -Novice
chi’s picture

Status: Needs work » Needs review
StatusFileSize
new872 bytes

This should fail.

Status: Needs review » Needs work

The last submitted patch, 8: 2916142-8-decimal_items_generation-test_only.patch, failed testing. View results

chi’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.88 KB
borisson_’s picture

Status: Needs review » Needs work

I don't like that this test relies on a sideeffect, there's no positive test that tests that the generated values are correct.

+++ b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php
@@ -97,7 +97,15 @@ public function testNumberItem() {
+    // This should through an exception if generated value is out of range.

/s/through/throw/

NSI’s picture

Status: Needs work » Needs review
StatusFileSize
new1.56 KB

Here is a patch.

tstoeckler’s picture

Status: Needs review » Needs work

I think #12 is in the wrong issue.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Version: 8.9.x-dev » 9.3.x-dev
Component: other » field system
Issue summary: View changes
Issue tags: +Bug Smash Initiative

The patch in #12 is for another issue, use the patch in #10.

Setting NW to resolve #11.

Updating version and moving to field system component, which seems a better fit.

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new1.89 KB
new710 bytes

Addressed #11

quietone’s picture

Status: Needs review » Needs work

@yogeshmpawar, thanks.

  1. +++ b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php
    @@ -97,7 +97,15 @@ public function testNumberItem() {
    +      ->setSetting('min', 99)
    +      ->setSetting('max', 100);
    

    Why are 99 and 100 used?

  2. +++ b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php
    @@ -97,7 +97,15 @@ public function testNumberItem() {
         $entity->field_decimal->generateSampleItems();
    

    This is only testing generateSampleItems for decimal. I think we should add tests for Float and Integer as well while we are here.

beatrizrodrigues’s picture

Status: Needs work » Needs review
StatusFileSize
new2.05 KB
new802 bytes

I add the lines that tests for float and integer as @quietone suggested on #20

This is only testing generateSampleItems for decimal. I think we should add tests for Float and Integer as well while we are here.

attaching a patch and a interdiff.

quietone’s picture

Status: Needs review » Needs work

@beatrizrodrigues, thanks.

I noticed some more things this time.

  1. +++ b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php
    @@ -97,7 +97,18 @@ public function testNumberItem() {
    +    $entity->field_decimal
    

    Setting of min/max values needs to be set for all three fields being tested here. Right not the min/max for field_integer and field_float are NULL, so the generated samples can be anything.

    And above this line are two lines that are generating samples values for field_integer and field_float. The sample values are not tested and are soon overwritten a few lines down (the lines added in the patch). These two lines need to be removed. Hope that makes sense.

  2. +++ b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php
    @@ -97,7 +97,18 @@ public function testNumberItem() {
    +    // This should throw an exception if generated value is out of range for
    +    // decimal, integer and float fields, respectively.
    

    Can be deleted because the method generateSampleItems does not throw an exception

  3. +++ b/core/modules/field/tests/src/Kernel/Number/NumberItemTest.php
    @@ -97,7 +97,18 @@ public function testNumberItem() {
         $this->entityValidateAndSave($entity);
    

    Add a comment here which is where an error will be found. I like "Confirm that the generated sample values are within range.' but you may prefer something else.

beatrizrodrigues’s picture

Assigned: Unassigned » beatrizrodrigues

oh, right, @quietone. I work on the points you said. Thanks!

beatrizrodrigues’s picture

Assigned: beatrizrodrigues » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.28 KB
new2.21 KB

So, I did the changes @quietone suggested, and when I run the tests (now defining min and max to float and integer too) I got the same problem that was happen in DecimalItem, but now, happening in FloatItem. I followed the same logic and I fixed it, and all the tests passed. Here is the new patch, and the interdiff.

beatrizrodrigues’s picture

StatusFileSize
new3.28 KB
new2.2 KB

Sending patch again because of the phpcs problems that were found.

quietone’s picture

Issue summary: View changes
Status: Needs review » Needs work

@beatrizrodrigues, thanks again. This looks much better. I notice that there were coding standard errors in #24. You can run those tests locally. Have a look at the instructions for running the coding standard checks locally so you can be sure the tests will run before uploading a patch. Thanks.

I've gone through the comments again. My question in #20.1 has been answered by myself. The actual values don't matter here, what matters is that the entity save and validation does. There are no other comments that need attention.

The last failing patch was in #8. Since we have changed the tests a new failing patch needs to be made.

@beatrizrodrigues, just in case you don't know, a failing patch is a patch with just the changes to the test. I usually name mine the same as the success patch but with '-fail' before the dot. And then run the test locally to make sure it really does fail. Assuming all is well then upload the failing patch and the success patch in the same comment. Make sure to upload the failing patch first. It is last, when the test fails the issue will be set to NW, which we don't want. I hope all that makes sense.

beatrizrodrigues’s picture

Assigned: Unassigned » beatrizrodrigues

Thank you so much @quietone for share your knowledge with me. I'm will work on the points you said.

beatrizrodrigues’s picture

Assigned: beatrizrodrigues » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.18 KB
new3.28 KB

Here's the patch and the failing patch, as was asked. I checked for code standards problems and all seems fine. Thank you again!

The last submitted patch, 28: 2916142-26-fail.patch, failed testing. View results

quietone’s picture

Title: Decimal item generates wrong sample values » Decimal and Float item generates wrong sample values
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

@beatrizrodrigues, Awesome! thanks again.

I was surprised that the failing test did not also fail for the field_integer. I looked at \Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem::generateSampleValue correctly sets the min/max values for integers so that is why there is no change for that in this patch. (It has been a long, unusual day for me). Yes, this is ready.

Updated the IS.

RTBC from me.

The last submitted patch, 28: 2916142-26-fail.patch, failed testing. View results

quietone’s picture

Testing is begin run on the fail patch, starting a test on the success patch.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 28: 2916142-26.patch, failed testing. View results

spokje’s picture

Status: Needs work » Reviewed & tested by the community

Back to RTBC per #30 after #3255836: Test fails due to Composer 2.2 solved the unrelated test failure.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 28: 2916142-26.patch, failed testing. View results

spokje’s picture

Status: Needs work » Reviewed & tested by the community

Last test failure looks like a random test failure to me, back to RTBC to trigger retest.

alexpott’s picture

Version: 9.4.x-dev » 9.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed b2901ce78ca to 10.0.x and 399c0a851e3 to 9.4.x and d277261d2ff to 9.3.x. Thanks!

  • alexpott committed b2901ce on 10.0.x
    Issue #2916142 by beatrizrodrigues, Chi, yogeshmpawar, tobiberlin,...

  • alexpott committed 399c0a8 on 9.4.x
    Issue #2916142 by beatrizrodrigues, Chi, yogeshmpawar, tobiberlin,...

  • alexpott committed d277261 on 9.3.x
    Issue #2916142 by beatrizrodrigues, Chi, yogeshmpawar, tobiberlin,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.