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.
- Add field of type Number (Decimal) to some content type
- Set minimum and maximum options for this field
- Generate sample data using Devel generate module
- 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
Comment #2
tobiberlinPlease find attached patch
Comment #3
tobiberlinI am a novice so I hope it was ok to upload this patch and move the ticket status to "Needs review"?
Comment #4
chi commented@tobiberlin it's ok.
Comment #5
chi commentedThe patch works for me. Devel generate now produces correct random values for decimal field. Thanks.
Comment #6
xjmNice 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!
Comment #7
chi commentedComment #8
chi commentedThis should fail.
Comment #10
chi commentedComment #11
borisson_I don't like that this test relies on a sideeffect, there's no positive test that tests that the generated values are correct.
/s/through/throw/
Comment #12
NSI commentedHere is a patch.
Comment #13
tstoecklerI think #12 is in the wrong issue.
Comment #18
quietone commentedThe 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.
Comment #19
yogeshmpawarAddressed #11
Comment #20
quietone commented@yogeshmpawar, thanks.
Why are 99 and 100 used?
This is only testing generateSampleItems for decimal. I think we should add tests for Float and Integer as well while we are here.
Comment #21
beatrizrodriguesI add the lines that tests for float and integer as @quietone suggested on #20
attaching a patch and a interdiff.
Comment #22
quietone commented@beatrizrodrigues, thanks.
I noticed some more things this time.
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.
Can be deleted because the method generateSampleItems does not throw an exception
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.
Comment #23
beatrizrodriguesoh, right, @quietone. I work on the points you said. Thanks!
Comment #24
beatrizrodriguesSo, 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.
Comment #25
beatrizrodriguesSending patch again because of the phpcs problems that were found.
Comment #26
quietone commented@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.
Comment #27
beatrizrodriguesThank you so much @quietone for share your knowledge with me. I'm will work on the points you said.
Comment #28
beatrizrodriguesHere's the patch and the failing patch, as was asked. I checked for code standards problems and all seems fine. Thank you again!
Comment #30
quietone commented@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.
Comment #32
quietone commentedTesting is begin run on the fail patch, starting a test on the success patch.
Comment #35
spokjeBack to RTBC per #30 after #3255836: Test fails due to Composer 2.2 solved the unrelated test failure.
Comment #37
spokjeLast test failure looks like a random test failure to me, back to RTBC to trigger retest.
Comment #38
alexpottCommitted and pushed b2901ce78ca to 10.0.x and 399c0a851e3 to 9.4.x and d277261d2ff to 9.3.x. Thanks!