Make WordPress Core

Opened 8 weeks ago

Closed 8 weeks ago

Last modified 6 weeks ago

#64412 closed defect (bug) (fixed)

Sporadic unit test failure in Tests_REST_WpRestUrlDetailsController::test_get_items

Reported by: westonruter's profile westonruter Owned by: westonruter's profile westonruter
Milestone: 6.9.1 Priority: normal
Severity: normal Version: 5.9
Component: Editor Keywords: has-patch has-unit-tests fixed-major dev-reviewed
Focuses: rest-api Cc:

Description (last modified by westonruter)

In #54358 via [51973] tests for the URL Details endpoint were introduced. These used a https://placeholder-site.com URL as the test URL for the unit tests in Tests_REST_WpRestUrlDetailsController. As discussed in Slack, recently, one of these tests has been failing sporadically:

1) Tests_REST_WpRestUrlDetailsController::test_get_items
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
 Array &0 (
-    'title' => 'Example Website — - with encoded content.'
-    'icon' => 'https://placeholder-site.com/favicon.ico?querystringaddedfortesting'
-    'description' => 'Example description text here. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore.'
-    'image' => 'https://placeholder-site.com/images/home/screen-themes.png?3'
+    'code' => 'rest_invalid_param'
+    'message' => 'Invalid parameter(s): url'
+    'data' => Array &1 (
+        'status' => 400
+        'params' => Array &2 (
+            'url' => 'Invalid parameter.'
+        )
+        'details' => Array &3 ()
+    )
 )

/var/www/tests/phpunit/tests/rest-api/wpRestUrlDetailsController.php:131

Even though the HTTP request is intercepted with a mocked response via the pre_http_request filter, it appears this mocking is not complete. In particular, the wp-block-editor/v1/url-details endpoint includes a url param which has wp_http_validate_url() as its validate_callback. This validation function ends up calling gethostbyname(). Since https://placeholder-site.com is not an actual site, it appears that this gethostbyname() often returns with a failure. I can reproduce the unit test failure by patching wp_http_validate_url() as follows:

  • src/wp-includes/http.php

    a b function wp_http_validate_url( $url ) { 
    593593                        $ip = $host;
    594594                } else {
    595595                        $ip = gethostbyname( $host );
    596                         if ( $ip === $host ) { // Error condition for gethostbyname().
     596                        if ( true || $ip === $host ) { // Error condition for gethostbyname().
    597597                                return false;
    598598                        }
    599599                }

I think we should simply use the domain for a real site, like https://example.com.

Change History (8)

This ticket was mentioned in Slack in #core by westonruter. View the logs.


8 weeks ago

This ticket was mentioned in PR #10625 on WordPress/wordpress-develop by @westonruter.


8 weeks ago
#2

  • Keywords has-patch has-unit-tests added

#3 @westonruter
8 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 61377:

REST API: Use valid host in unit tests for URL Details endpoint.

This ensures that the url parameter is not marked as invalid due to wp_http_validate_url() failing because of a gethostbyname() failure.

Follow-up to [51973].

Props westonruter, swissspidy.
See #54358.
Fixes #64412.

#4 @westonruter
8 weeks ago

  • Description modified (diff)

#5 @westonruter
8 weeks ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Re-opening for 6.9 backport consideration.

#6 @peterwilsoncc
8 weeks ago

  • Keywords dev-reviewed added

r61377 looks good for merging to the 6.9 branch.

#7 @peterwilsoncc
8 weeks ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 61378:

REST API: Use valid host in unit tests for URL Details endpoint.

This ensures that the url parameter is not marked as invalid due to wp_http_validate_url() failing because of a gethostbyname() failure.

Follow-up to [51973].

Reviewed by peterwilsoncc.
Merges [61377] to the 6.9 branch.

Props westonruter, swissspidy.
See #54358.
Fixes #64412.

Note: See TracTickets for help on using tickets.