Problem/Motivation

When the maximum length of a text field is set to less than 3 the generateSampleValue() function will throw an error:
mt_rand(): max(0) is smaller than min(1)

This is because the max value is the max_length divided by three, implicitly cast to an int.

Proposed resolution

Use the ceil() function to ensure the division provides a max of at least 1 on max_lengths greater than 0.

Remaining tasks

Patch needs review.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Comments

JvE created an issue. See original summary.

JvE’s picture

The change of filter_fallback_format() is to decouple from a hard dependency on the filter module and improve testability.

JvE’s picture

Title: TextItemBase mt_rand(): max(0) is smaller than min(1) » Text item sample generation fails if max length < 3

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.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.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.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.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.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.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

kristen pol’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative

Thanks for the issue and patch. I found a few minor things.

  1. +++ b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php
    @@ -79,13 +79,14 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
    +      'format' => \Drupal::config('filter.settings')->get('fallback_format'),
    

    Isn't this out of scope for this issue?

  2. +++ b/core/modules/text/tests/src/Unit/TextItemTest.php
    @@ -0,0 +1,60 @@
    +    // Set up a configfactory for the filter settings fallback format.
    

    Nitpick: change "configfactory" to "config factory".

  3. +++ b/core/modules/text/tests/src/Unit/TextItemTest.php
    @@ -0,0 +1,60 @@
    +    $configs = array(
    +      'filter.settings' => array(
    +        'fallback_format' => 'plain_text',
    +      ),
    +    );
    

    Switch from array() to [] syntax.

  4. +++ b/core/modules/text/tests/src/Unit/TextItemTest.php
    @@ -0,0 +1,60 @@
    +    $configFactoryStub = $this->getConfigFactoryStub($configs);
    

    Since there is only one config, it could be passed in directly like many tests do, e.g.

    $config_factory = $this->getConfigFactoryStub([
          'filter.settings' => ['fallback_format' => 'plain_text'],
        ]);
    
  5. +++ b/core/modules/text/tests/src/Unit/TextItemTest.php
    @@ -0,0 +1,60 @@
    +    $container->set('config.factory', $configFactoryStub);
    

    Other tests use $config_factory.

  6. +++ b/core/modules/text/tests/src/Unit/TextItemTest.php
    @@ -0,0 +1,60 @@
    +    $this->assertEquals(1, Unicode::strlen($sample_value['value']), 'Sample value is correct');
    

    I don't see Unicode::strlen used anywhere in core code.

ankithashetty’s picture

Status: Needs work » Needs review
StatusFileSize
new2.67 KB
new2.54 KB

Rerolled the patch in #1. Also addressed all the points from #13 13.1, 13.2, 13.3, 13.4, 13.5, 13.6.

Thank you!

pavnish’s picture

StatusFileSize
new6.83 KB

@Kristen Pol Rerolled the patch for drupal 9.2.x.
#13 Has been addressed with some additional changes use getMockBuilder function

pavnish’s picture

StatusFileSize
new2.82 KB

Ignore #15 @Kristen Pol Rerolled the patch for drupal 9.2.x.
#13 Has been addressed with some additional changes use getMockBuilder function

pavnish’s picture

StatusFileSize
new2.79 KB

There were some linting issues in #16 Please Ignore #16. I have fixed linting issues and Rerolled the patch for drupal 9.2.x.
#13 Has been addressed with some additional changes use getMockBuilder function

pavnish’s picture

pavnish’s picture

Status: Needs review » Needs work

The last submitted patch, 17: 2750925-17.patch, failed testing. View results

kristen pol’s picture

@pavnish I don't understand why you created your own new patch rather than starting with #14.

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new2.63 KB
new471 bytes

Tried to remove the errors from #14 patch. Please review.

Status: Needs review » Needs work

The last submitted patch, 22: 2750925-22.patch, failed testing. View results

nikitagupta’s picture

Status: Needs work » Needs review
StatusFileSize
new2.68 KB
new1021 bytes

fixed the test case form #22.

Status: Needs review » Needs work

The last submitted patch, 24: 2750925-24.patch, failed testing. View results

vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new2.15 KB
new2.15 KB

Updated the Tests and Made some changes to generateSampleValue() method.

vakulrai’s picture

StatusFileSize
new3.38 KB

Missed the Patch from previous commits , adding that.

gauravvvv’s picture

StatusFileSize
new3.39 KB

I have provided a patch, please verify if it is working or not.

vakulrai’s picture

StatusFileSize
new3.39 KB

Updated coding standards in the tests.

vakulrai’s picture

StatusFileSize
new3.4 KB
vakulrai’s picture

StatusFileSize
new3.39 KB

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kristen pol’s picture

Status: Needs review » Needs work

Hard to follow all the patches but I looked at the last one.

  1. The issues noted in #13 seem addressed
  2. In #2, it says:

    The change of filter_fallback_format() is to decouple from a hard dependency on the filter module and improve testability.

    but that is out of scope of this issue so would need to be separated out.

    +++ b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php
    @@ -70,7 +70,7 @@ public function onChange($property_name, $notify = TRUE) {
    +  public static function generateSampleValue(FieldDefinitionInterface $field_definition, $format = NULL) {
    
    +++ b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php
    @@ -80,13 +80,14 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
    +      'format' => isset($format) ? $format : filter_fallback_format(),
    
neslee canil pinto’s picture

@Kristen pol, so you are telling us we need to remove #2 from this issue and create a new one for that?

neslee canil pinto’s picture

Status: Needs work » Needs review
StatusFileSize
new2.8 KB
new846 bytes

As per #33's 2nd point, this patch removes the code which was requested to be added in #2

Status: Needs review » Needs work

The last submitted patch, 35: 2750925-35.patch, failed testing. View results

neslee canil pinto’s picture

Status: Needs work » Needs review
StatusFileSize
new2.79 KB
new636 bytes

Status: Needs review » Needs work

The last submitted patch, 37: 2750925-37.patch, failed testing. View results

meenakshi_j’s picture

Status: Needs work » Needs review
StatusFileSize
new2.92 KB
new440 bytes
quietone’s picture

StatusFileSize
new3.54 KB
new2.08 KB

@Meenakshi_j. Welcome to Drupal. Thanks for adding an interdiff. When adding a patch it helps the next person working on the issue (whether reviewer or developer) if you add a comment explaining why you made the changes you made.

  1. +++ b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php
    @@ -80,13 +80,13 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
    -      'format' => filter_fallback_format(),
    

    ? No, we can just remove the filter.

  2. +++ b/core/modules/text/tests/src/Unit/TextItemTest.php
    @@ -0,0 +1,61 @@
    +  protected static $modules = ['filter'];
    

    Unit tests do not have a database. Therefor this does nothing.

  3. +++ b/core/modules/text/tests/src/Unit/TextItemTest.php
    @@ -0,0 +1,61 @@
    +  public function testTextFieldSampleValue() {
    

    This should test more than a length of 1.

I started to review and then I realized that this needs will need a Kernel test so that the function, filter_fallback_format, is available. As pointed out in #33 changing that is out of scope.

Then, even thought it is late I decided to make a patch myself.

quietone’s picture

It may look like there is work to do in #30 but there isn't. I abandoned the review and wrote a new patch.

The patch in #40 is ready for review.

larowlan’s picture

Thanks @quietone - can we get a test-only patch?

Thanks

quietone’s picture

StatusFileSize
new603 bytes
new1.35 KB
new2.08 KB

Oops, you are right, I forgot.

Here it is.

The last submitted patch, 43: 2750925-43-fail.patch, failed testing. View results

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Looks great, thanks

catch’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/text/tests/src/Kernel/TextItemBaseTest.php
@@ -0,0 +1,39 @@
+
+      $this->assertEquals($string_length, strlen($sample_value['value']), "Sample value failed for string_length '$string_length'");
+    }

We don't normally use the fail condition for custom phpunit assertion messages, I think we'd be better of without this.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

We don't normally use the fail condition for custom phpunit assertion messages, I think we'd be better of without this.

Without it we just get the line number, which is in the loop, so we don't know what failed

Tentatively putting back to rtbc

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

Actually no we don't we get the size

acbramley’s picture

My 2c - make it a dataProvider instead of an array with a loop for more context :)

quietone’s picture

Status: Needs work » Needs review

I must not be understanding the problem here.

Actually no we don't we get the size

Sorry, size of what?

For example, an assertion inside a loop needs a custom message so you know which loop iteration you were on when the assertion failed.

That is from #3131946: [policy] Remove PHPUnit assertion messages when possible, and standardize remaining messages . And I do not think that this needs to a new test environment for each source value.

Finally, lets see what happens with a failure.

When using a string length that fails, such as 0, the error message is

1) Drupal\Tests\text\Kernel\TextItemBaseTest::testTextFieldSampleValue
Sample value failed for string_length '0'
Failed asserting that 3414 matches expected 0.

That tells me exactly which of the input $string_lengths is causing the problem.

Setting to NR.

paulocs’s picture

I think @larowlan is pointing that when the test only patch is applied, the error message is not the error of the PHPUnit. It is the PHP error of the currently bug. For me is okay to let it as it is because the assertion is in a foreach and if the $this->assertEquals fails, it will be displayed a good help message.
See:

1) Drupal\Tests\text\Kernel\TextItemBaseTest::testTextFieldSampleValue
ValueError: mt_rand(): Argument #2 ($max) must be greater than or equal to argument #1 ($min)

/app/drupal/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php:83
/app/drupal/core/modules/text/tests/src/Kernel/TextItemBaseTest.php:33
/app/drupal/vendor/phpunit/phpunit/src/Framework/TestResult.php:703

I agree that no data provider is required as it will need a new test environment for each source value so #49 is not necessary.

Just one thing:

// Test requesting strings of various lengths, ensuring that lengths 1, 2,
    // and 3 are tested.
    $string_lengths = [1, 2, 3, 4];

Do we need to explain why we are specifically testing 1, 2, and 3? Maybe adding a @see tag to the comment.

larowlan’s picture

Just to be clear, I was agreeing with catch, the output will say 'failed asserting that 3414 matches expected 0' where 0 is the length, so you can discern which case failed.

But I do agree with Adam, that this would make sense as a data provider, however we're paying a setup cost for the kernel test each time, so the added test execution time might be a factor there

And I agree with Paulo, some context of why those lengths are significant would be great too

quietone’s picture

StatusFileSize
new1.9 KB
new2.35 KB

@paulocs, thanks for the explanation.

I didn't find anything in the issue that added TestItemBase to explain why the max_length is divided by three. #2320157: Generate placeholder content for Field types.

This patch adds a dataprovider and a comment in the test to explain the values chosen.

quietone’s picture

StatusFileSize
new466 bytes
new2.35 KB

Oh bother, I did fix that but neglected to make a new patch.

paulocs’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.
Patch is working as expected and it addresses #52.

alexpott’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed 12aec14 and pushed to 9.3.x. Thanks!

I'll backport to 8.2.x once the freeze is over.

  • alexpott committed 12aec14 on 9.3.x
    Issue #2750925 by quietone, vakulrai, pavnish, Neslee Canil Pinto,...

  • alexpott committed 8465b6e on 9.2.x
    Issue #2750925 by quietone, vakulrai, pavnish, Neslee Canil Pinto,...
alexpott’s picture

Status: Patch (to be ported) » Fixed

Cherry-picked to 9.2.x

Status: Fixed » Closed (fixed)

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