Opened 5 weeks ago
Closed 4 weeks ago
#63175 closed defect (bug) (fixed)
Tests: Improve Unit tests for 'wp_unique_id_from_values()'
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 6.8 | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | has-patch has-unit-tests fixed-major commit dev-reviewed |
Focuses: | tests | Cc: |
Description
A new function, wp_unique_id_from_values()
and related unit tests were added in [60038]. The tests assume that the unique ID will always produce a consistent hash output regardless of hosting environment, due to the way the wp_json_encode()
and the md5()
algorithm works.
However, some hosting environments are reporting test failures due to a different unique hash being produced.
Examples:
- https://make.wordpress.org/hosting/test-results/r60038/bluehostbot-r60038-8-0-mysql-ver-14-14-distrib-5-7-23-23-for-linux-x86_64-using-6-2/
- https://make.wordpress.org/hosting/test-results/r60038/bluehostbot-r60038-8-3-mysql-ver-14-14-distrib-5-7-23-23-for-linux-x86_64-using-6-2/
- https://make.wordpress.org/hosting/test-results/r60040/xserverbot-r60040-7-4-mysql-ver-15-1-distrib-10-5-22-mariadb-for-linux-x86_64-using-editline-wrapper/
In this case, I believe the unit tests are likely assuming too much by expecting a specific hash output, and instead should be revised to assert that the same data passed multiple times will result in the same value each time, and that it will be different than an different value passed to the same function.
Change History (15)
This ticket was mentioned in Slack in #core by joemcgill. View the logs.
5 weeks ago
This ticket was mentioned in PR #8590 on WordPress/wordpress-develop by @debarghyabanerjee.
5 weeks ago
#2
- Keywords has-patch has-unit-tests added; needs-patch removed
This ticket was mentioned in Slack in #core by joemcgill. View the logs.
5 weeks ago
#4
follow-ups:
↓ 5
↓ 10
@
5 weeks ago
According to the Stack Overflow post json_encode behaving differently on dev vs live servers, the cause of the different results is due to different MySQL extensions.
The issue this presents is that if a developer writes CSS on the development server expecting wp_unique_id_from_values()
to return a consistent result, their CSS may end up broken on their live server as the unique ID is not in fact unique based on the values.
I think it would be good to test if md5( serialize() )
returns consistent results, regardless of the extensions installed. This may present a risk for the __serialize()
and __sleep()
magic methods, although it doesn't appear to be a huge risk as blocks typically use stdClass.
This may be moot if json_decode()
behaves differently on different systems too.
#5
in reply to:
↑ 4
;
follow-up:
↓ 9
@
5 weeks ago
Replying to peterwilsoncc:
According to the Stack Overflow post json_encode behaving differently on dev vs live servers, the cause of the different results is due to different MySQL extensions.
Are you sure it isn't just an issue with outputting floating-point numbers? All the failing tests seem to have floating-point numbers.
If it's an issue with floating-point numbers, you might be able to get more reproducible results by setting serialize_precision. (But I'm not sure if even that will guarantee that floating-point output will be exactly the same on all systems.)
@debarghyabanerjee commented on PR #8590:
5 weeks ago
#6
Hi @joemcgill, I have pushed the updates, can you please have a look into it once. Thanks.
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
5 weeks ago
#9
in reply to:
↑ 5
@
5 weeks ago
Replying to siliconforks:
Are you sure it isn't just an issue with outputting floating-point numbers? All the failing tests seem to have floating-point numbers.
Good point, yes it's the likely cause of the failures https://3v4l.org/q89Es
#10
in reply to:
↑ 4
@
5 weeks ago
Replying to peterwilsoncc:
The issue this presents is that if a developer writes CSS on the development server expecting
wp_unique_id_from_values()
to return a consistent result, their CSS may end up broken on their live server as the unique ID is not in fact unique based on the values.
This is one of the scenarios that I wanted to dig into a bit more...does the feature that makes use of the wp_unique_id_from_values()
function expect the output to always be consistent, or save the ID to the DB in a way that would create a bug when migrating to a system that would produce a different output for the same data? Is that even something that we should enforce as a requirment of this function?
#11
@
4 weeks ago
I finally found different systems I had access to that resulted in different hashes.
It's definitely caused by inaccuracies with the handling of floating points in some configurations.
It seems casting floats to strings increases the accuracy 🤷♂️:
$ wp shell wp> $v = 10.5363 => phar:///usr/local/src/wp-cli/bin/wp/vendor/wp-cli/shell-command/src/WP_CLI/Shell/REPL.php:52: double(10.536300000000001) wp> (string) $v => phar:///usr/local/src/wp-cli/bin/wp/vendor/wp-cli/shell-command/src/WP_CLI/Shell/REPL.php:52: string(7) "10.5363"
I think the accuracy could be improved by doing so but I also don't think it's worth introducing a recursive function to do so. Instead I think it best to explain the issue in the functions docblock:
Different versions and configurations of PHP may produce different results due to the way they handle floating-point numbers. When using the generated ID for CSS classes, it is recommended to generate the CSS inline to ensure the styles apply correctly.
#12
@
4 weeks ago
- Owner set to joemcgill
- Resolution set to fixed
- Status changed from new to closed
In 60113:
#13
@
4 weeks ago
- Keywords dev-feedback fixed-major added
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening this to consider backporting [60113] to the 6.8 branch.
#15
@
4 weeks ago
- Keywords commit dev-reviewed added; dev-feedback removed
[60113] looks good for backporting. I've confirmed that the tests are now passing on hosting platforms that were reporting failures previously.
Trac Ticket: Core-63175
### Problem Statement
The original unit tests for the
wp_unique_id_from_values()
function relied on specific hash outputs, which caused test failures in certain hosting environments due to differences in how data is serialized and hashed. These tests assumed consistent hash values across all environments for the same input, which was not guaranteed. Additionally, the tests did not properly validate the handling of the prefix parameter.### Solution