Make WordPress Core

Opened 8 months ago

Closed 8 months ago

Last modified 2 months ago

#64963 closed defect (bug) (fixed)

Unit tests make unconditional HTTP request for GP_Locales class PHP file

Reported by: westonruter Owned by: westonruter
Priority: normal Milestone: 7.1
Component: Build/Test Tools Version: 4.9
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

I noticed that running PHPUnit can be quite slow on a poor connection, even though I'm not doing any external-http tests. I threw in a debug_print_backtrace() to WP_Http::request() and I found that after these lines are printed:

Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.

An HTTP request is made from the PluralFormsTest::data_locales() data provider:

#0 /var/www/src/wp-includes/class-wp-http.php(659): WP_Http->request()
#1 /var/www/src/wp-includes/http.php(84): WP_Http->get()
#2 /var/www/src/wp-admin/includes/file.php(1166): wp_safe_remote_get()
#3 /var/www/tests/phpunit/tests/pomo/pluralForms.php(75): download_url()
#4 [internal function]: PluralFormsTest::data_locales()
#5 /var/www/vendor/phpunit/phpunit/src/Util/Annotation/DocBlock.php(421): ReflectionMethod->invoke()
#6 /var/www/vendor/phpunit/phpunit/src/Util/Annotation/DocBlock.php(278): PHPUnit\Util\Annotation\DocBlock->getDataFromDataProviderAnnotation()
#7 /var/www/vendor/phpunit/phpunit/src/Util/Test.php(329): PHPUnit\Util\Annotation\DocBlock->getProvidedData()
#8 /var/www/vendor/phpunit/phpunit/src/Framework/TestBuilder.php(72): PHPUnit\Util\Test::getProvidedData()
#9 /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php(891): PHPUnit\Framework\TestBuilder->build()
#10 /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php(232): PHPUnit\Framework\TestSuite->addTestMethod()
#11 /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php(362): PHPUnit\Framework\TestSuite->__construct()
#12 /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php(512): PHPUnit\Framework\TestSuite->addTestSuite()
#13 /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php(537): PHPUnit\Framework\TestSuite->addTestFile()
#14 /var/www/vendor/phpunit/phpunit/src/TextUI/TestSuiteMapper.php(67): PHPUnit\Framework\TestSuite->addTestFiles()
#15 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(391): PHPUnit\TextUI\TestSuiteMapper->map()
#16 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(114): PHPUnit\TextUI\Command->handleArguments()
#17 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(99): PHPUnit\TextUI\Command->run()
#18 /var/www/vendor/phpunit/phpunit/phpunit(107): PHPUnit\TextUI\Command::main()
#19 /var/www/vendor/bin/phpunit(122): include('/var/www/vendor...')

This is in spite of me running npm run test:php -- --group=64890 to limit execution to a specific group without any external-http requests. It seems the PluralFormsTest::test_regression() test needs to be refactored to not use PluralFormsTest::data_locales() as a data provider, since apparently PHPUnit has a test collector which runs all data providers unconditionally when initializing.

This issue was introduced in r41722 for #41562.

Attachments (1)

64963.0.diff​ (2.0 KB ) - added by westonruter 8 months ago.

Download all attachments as: .zip

Change History (12)

#1 @westonruter
8 months ago

  • Keywords has-patch added; needs-patch removed

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


8 months ago
#2

  • Keywords has-unit-tests added

Trac ticket: https://core.trac.wordpress.org/ticket/64963

## Use of AI Tools

None

#3 @westonruter
8 months ago

  • Owner set to westonruter
  • Status new → accepted

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


8 months ago

​@westonruter commented on ​PR #11382:


8 months ago
#5

Fixed in r62174 (9cee59516df60b1b51ff163763082a0464617995)

#6 @westonruter
8 months ago

  • Resolution → fixed
  • Status accepted → closed

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


6 months ago

This ticket was mentioned in ​PR #13407 on ​WordPress/wordpress-develop by ​@adrianmoldovanwp.


2 months ago
#8

Fails any test that makes an external HTTP request without @group external-http.

WP_UnitTestCase_Base::set_up() adds a pre_http_request filter to every test outside the group. The filter runs at PHP_INT_MAX, so a mock that the test adds itself answers first. A request that gets that far is recorded and blocked with a WP_Error, and assert_post_conditions() then fails the test:

This test made an external HTTP request but is not in the `external-http` group.
Add `@group external-http` to it, or mock the request with the `pre_http_request` filter.
- http://example.com/embed/foo

The check runs only when WP_RUN_CORE_TESTS is set, so plugin and theme suites that build on the core test suite keep working.

Blocking the request and failing in assert_post_conditions() are both needed. Blocking alone would silently satisfy a test that asserts only is_wp_error(). Failing from inside the filter instead is not safe: core has 193 PHP try blocks with about 85 catch ( Exception ) or catch ( Throwable ) clauses, several of them on HTTP paths (oEmbed discovery, SimplePie, the AI client's PSR-18 wrapper), and AssertionFailedError descends from RuntimeException.

Tests_oEmbed_WpEmbed::test_run_shortcode_url_only gets the missing annotation. It requests http://example.com/embed/foo, and it is the only test in trunk that the check catches. Its own assertions pass either way, because run_shortcode() falls back to maybe_make_link() and produces the same link.

Left out of this patch:

  • The second check in the ticket, that a test in external-http does make a request. Four tests fail it today. Three carry a wrong annotation: Tests_HTTP_Functions::test_get_cookie_host_only, Tests_oEmbed_WpEmbed::test_shortcode_should_return_empty_string_for_missing_url, and Tests_oEmbed_WpEmbed::test_autoembed_should_return_modified_content. The fourth, PluralFormsTest::test_locales_file_not_empty, is a false positive, because data_locales() downloads only when GP_Locales is absent and an earlier test in the class already loaded it. That check needs an opt-out, so it belongs in its own patch.
  • Tests_Admin_IncludesTheme::test_get_theme_featured_list_api under multisite. get_theme_feature_list() returns the hardcoded list early when the user cannot install_themes, which a plain administrator cannot do on multisite, so the test asserts nothing about the API and still passes. The same second check found it.
  • Requests from data providers, which run before set_up(). See #64963.
  • Requests that do not use WP_Http, such as file_get_contents() or fsockopen().

getGroups() works on every PHPUnit version the supported branches use, but PHPUnit 10 removes it. That migration will have to touch this one condition.

## Testing Instructions

  1. Start the environment: npm run env:start && npm run env:install.
  2. Run the suite: npm run test:php. Expect no failures (31,050 tests).
  3. Run the group: npm run test:php -- --group external-http. Expect no failures (86 tests).
  4. Run the AJAX group: npm run test:php -- --group ajax. Expect no failures (190 tests).
  5. Run multisite: npm run test:php -- -c tests/phpunit/multisite.xml. Expect no failures (31,885 tests).
  6. Run multisite external HTTP: npm run test:php -- -c tests/phpunit/multisite.xml --group external-http. Expect no failures (87 tests).
  7. Confirm the check catches a new offender. Save this as tests/phpunit/tests/guardDemo.php:
<?php
    class Tests_Guard_Demo extends WP_UnitTestCase {
        public function test_external_request() {
            wp_remote_get( 'http://example.com/' );
            $this->assertTrue( true );
        }
    }

Run npm run test:php -- --filter Tests_Guard_Demo. Expect one failure that names the URL and tells you to add the group. Add @group external-http to the test and run it again: the default run now excludes it. Delete the file.

  1. Confirm the annotation in this patch is what keeps the suite green. Remove @group external-http from Tests_oEmbed_WpEmbed::test_run_shortcode_url_only and run npm run test:php -- --filter Tests_oEmbed_WpEmbed::test_run_shortcode_url_only. Expect the same failure. Restore the annotation.
  2. Check the coding standards: composer lint tests/phpunit/includes/abstract-testcase.php tests/phpunit/tests/oembed/WpEmbed.php.

## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: The report-only audit of the test suite, the implementation, and this description.

​@lancewillett commented on ​PR #13407:


2 months ago
#9

@adimoldovan Want to take out of draft now? What's next to land this?

​@johnbillion commented on ​PR #13407:


2 months ago
#11

Neat!

Note: See TracTickets for help on using tickets.