Closed (fixed)
Project:
Drupal core
Version:
9.2.x-dev
Component:
other
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
29 Jan 2020 at 11:06 UTC
Updated:
5 Jun 2021 at 00:59 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
mtodor commentedHere is the patch with the proposed solution.
Comment #4
kristen pol@mtodor Would you provide manual test instructions to reproduce the issue? Thanks!
Comment #5
kristen polMoving to "Postponed" as it's hard to know how to review this without more information. Need to also make sure this is covered by existing tests or needs new one.
Comment #6
kristen polIn an attempt to understand this better, I see the use of
word()here for string and link. I still don't understand how to manually test this though.StringItem.php
LinkItem.php
Comment #7
kristen polIf this is technically not working as the issue summary explains, IMO this should be a bug rather than feature request.
Comment #8
kristen polWhoops. Trying again.
Comment #9
mtodor commentedSorry @kristen-pol for the delayed response. I'm not working with Drupal actively anymore.
The problem here is the following. With every execution of
Random::wordrandom seed will be changed. And that's not something we should do inside a function that generates random values. The main reason for that is that seed is defined globally and changing it could affect the behavior of other functions.The main purpose of using defined seeds is if you want to have defined order of random values. And place when you could need that are tests. We needed defined seeding for our performance testing in Thunder distribution. What we do, is that we define seed at the beginning of testing
srandand then we are using Drupal random generator function. With definedsrandwe have always the same order of random values in our tests. All other random generating functions are not changing defined seed, exceptRandom::wordand that makes unpredictable test values.If you execute following code, you will get better picture:
So the only bad thing that
Random::wordis doing, that it's changing definedsrandevery time you call it.If you want to test it, you can simply set
srandand callRandom::wordand you should always get same order of random values. With the old version, you would always get an unpredictable order of random values.Comment #10
kristen pol@mtodor Thanks for the info! Moving back to needs review.
Comment #12
quietone commentedthe use of mt_srand was added early in the issue that added Random with no explanation for that particular line.
The PHP documentation for srand states "Note: There is no need to seed the random number generator with srand() or mt_srand() as this is done automatically." So, that line is unnecessary.
The code sample in #9 isn't quite right.
The only way to get the same random value is to set the seed before each rand(). That agrees with the doc.
Anyway, the line isn't really needed. Here is a patch with a fail test.
Comment #14
kristen polThanks for the test. Some nitpicks and a question :)
Nitpick: Wording seems a bit awkward... maybe something like:
Without a seed, test a different word is returned each time.
I see this is the approach used in
testRandomStringValidatorbut I'm unclear why this code isn't similar to the seeded section so that it's something like the following. i.e. why does there need to be a check between an empty string and a random word?Nitpick: Wording seems a bit awkward... maybe something like:
With a seed, test the same word is returned each time.
Comment #16
quietone commented@Kristen Pol, thanks for the review
1 and 3. Yes, your suggestion is better.
2. I have no idea why I did that. And again, your suggestion is better.
Since these are changes to the test I made a new fail patch.
Comment #18
kristen polThanks for the updates! This looks good to me and tests are passing, so marking RTBC.
Comment #19
larowlanI did some further digging and found the code that added the use of srand was added to devel_generate in #160557: abstract taxonomy creation to devel_generate.inc which was then ported to core in #2320157: Generate placeholder content for Field types
There is no discussion of why srand was used there.
I agree with the reporter, the use of Random::word should not generate side-effects.
Comment #20
larowlanCrediting myself for going back into ancient git logs to find the origins of that code.
Comment #22
larowlanCommitted 30e300f and pushed to 9.3.x. Thanks!
Backported to 9.2.x