Skip to content

Tests: define UPLOADS constant for php unit. - #13065

Closed
OlegApanovich wants to merge 14 commits into
WordPress:trunkfrom
OlegApanovich:63532-define-uploads-constant-for-phpunit
Closed

OlegApanovich wants to merge 14 commits into
WordPress:trunkfrom
OlegApanovich:63532-define-uploads-constant-for-phpunit

Conversation

@OlegApanovich

@OlegApanovich OlegApanovich commented Aug 16, 2026 •

Copy link
Copy Markdown

Points the test suite at a dedicated wp-content/test-suite-uploads directory instead of the real wp-content/uploads, so test runs don't write to actual site content.

The directory wp-content/test-suite-uploads is deleted once before new tests run.

Adds an _upload_get_url() helper and updates tests that previously hardcoded wp-content/uploads.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): GPT-5.1
Used for: Only for simple, repetitive actions; all architectural decisions are mine alone.

Points the test suite at a dedicated wp-content/test-suite-uploads
directory instead of the real wp-content/uploads, so test runs don't
write to actual site content.

The directory wp-content/test-suite-uploads is deleted once before new tests run.

Adds an _upload_get_url() helper and updates tests that previously hardcoded
wp-content/uploads.

Trac ticket: https://core.trac.wordpress.org/ticket/63532
@github-actions

github-actions Bot commented Aug 16, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mrdollar4444, lancewillett.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please preserve the option-based upload tests: defining UPLOADS globally makes the relative-path, absolute-path and custom-URL cases exercise the override instead. Their original behavior needs to remain covered.

The new lock also fails when wp-content/uploads does not exist: touch() returns false, leaving deletion enabled without a marker. Please ensure successful lock creation before relying on it. A date hash also does not distinguish concurrent runs.

Finally, please run the ms-files group explicitly. Its main-site rewriting scenario ignores UPLOADS in production, so changing its expectation to test-suite-uploads does not match that path. The default multisite configuration excludes this group.

AI review · gpt-6

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Legacy multisite uploads remain outside the test directory and conflict with new assertions, while lock handling and filesystem initialization can delete fixtures or abort bootstrap.

4 open issues
What changed in this PR

This PR moves PHPUnit uploads to a dedicated test directory to keep test runs from writing to normal site uploads.

Changes:

  • Defines UPLOADS and adds lock-based cleanup during test bootstrap.
  • Adds _upload_get_url() and updates upload-related fixtures and assertions.
  • Excludes the test uploads directory from version control.
File Description
tests/​phpunit/​tests/​upload.php Updates upload path and URL expectations.
tests/​phpunit/​tests/​speculative-loading/​wpGetSpeculationRules.php Uses UPLOADS in exclusion patterns.
tests/​phpunit/​tests/​multisite/​site.php Updates site-switching upload expectations.
tests/​phpunit/​tests/​multisite/​msFilesRewriting.php Changes legacy multisite main-site expectations.
tests/​phpunit/​tests/​media/​previousImageLink.php Uses the helper for previous-image URLs.
tests/​phpunit/​tests/​media/​nextImageLink.php Uses the helper for next-image URLs.
tests/​phpunit/​tests/​media/​getPreviousImageLink.php Updates expected previous-image markup.
tests/​phpunit/​tests/​media/​getPostGalleries.php Updates gallery URLs and expected output.
tests/​phpunit/​tests/​media/​getNextImageLink.php Updates expected next-image markup.
tests/​phpunit/​tests/​media/​getAdjacentImageLink.php Updates expected adjacent-image markup.
tests/​phpunit/​tests/​media.php Replaces hardcoded media upload URLs.
tests/​phpunit/​tests/​fonts/​font-library/​wpRestFontFacesController.php Updates the expected font upload path.
tests/​phpunit/​tests/​fonts/​font-library/​wpFontsDir.php Derives font expectations from the switched site's uploads.
tests/​phpunit/​includes/​functions.php Adds the shared upload URL helper.
tests/​phpunit/​includes/​bootstrap.php Configures test uploads and startup cleanup.
.gitignore Ignores the dedicated test uploads directory.

Review effort: Balanced

Comment thread tests/phpunit/includes/bootstrap.php Resolved
Comment thread tests/phpunit/includes/bootstrap.php Resolved
Comment thread tests/phpunit/includes/bootstrap.php Resolved
Replaces the date-hashed lock file in wp-content/uploads with a
WP_TESTS_UPLOADS_RUN_ID environment variable, which is inherited by
@runInSeparateProcess child processes.

This prevents isolated tests from deleting the test uploads dir again
when a run crosses UTC midnight or when wp-content/uploads does not
exist, and no longer writes a lock file into real site content.

Trac ticket: https://core.trac.wordpress.org/ticket/63532
WP_Filesystem() may select the FTP or SSH transport based on FS_METHOD
or checkout ownership, then fail to connect without credentials, which
could abort the bootstrap instead of cleaning the directory.

The test uploads dir is always local, so use a WP_Filesystem_Direct
instance directly. This also avoids setting the global $wp_filesystem
for the whole test run.

Trac ticket: https://core.trac.wordpress.org/ticket/63532
Defining UPLOADS globally made the relative path, absolute path and
custom URL tests exercise the UPLOADS override instead of the
upload_path and upload_url_path options.

Moves these tests, unchanged from trunk, into a new
Tests_Upload_UploadDirOptions class that runs in separate processes.
The class sets WP_TESTS_SKIP_UPLOADS_CONSTANT so that bootstrap.php
skips defining UPLOADS in those processes.

Trac ticket: https://core.trac.wordpress.org/ticket/63532
With ms_files_rewriting enabled, _wp_upload_dir() ignores UPLOADS on the
main site and uses UPLOADBLOGSDIR for subsites, so this group still
wrote to wp-content/uploads and wp-content/blogs.dir. The main site
assertions only passed because wp_upload_dir() returned a value cached
before rewriting was enabled.

Points UPLOADBLOGSDIR and the main site upload_path option at the test
uploads dir, refreshes the upload dir cache, and updates the subsite
expectations to use UPLOADBLOGSDIR.

Trac ticket: https://core.trac.wordpress.org/ticket/63532
…ds-constant-for-phpunit

# Conflicts:
#	tests/phpunit/tests/multisite/msFilesRewriting.php
…m:OlegApanovich/wordpress-develop into 63532-define-uploads-constant-for-phpunit
The previous change built the expected font dir from the switched-to
site's own upload dir, which mirrors _wp_filter_font_directory() and
no longer verifies that subsite fonts live under /sites/{$blog_id}.

Restores the trunk expectations and only updates the URLs, which are
built from the switched-to site's siteurl when UPLOADS is defined.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The inherited setup creates real upload directories before options are changed, so this path-only test class needs an upload-scan override to preserve filesystem isolation.

1 open issue
4 resolved since last review

Review effort: Balanced

The inherited scan_user_uploads() calls wp_upload_dir(), which creates the
real wp-content/uploads/YYYY/MM directory in each isolated process because
UPLOADS is not defined there. These tests only inspect paths, so there are
no upload files to track.
@OlegApanovich

Copy link
Copy Markdown
Author

Please preserve the option-based upload tests: defining UPLOADS globally makes the relative-path, absolute-path and custom-URL cases exercise the override instead. Their original behavior needs to remain covered.

The new lock also fails when wp-content/uploads does not exist: touch() returns false, leaving deletion enabled without a marker. Please ensure successful lock creation before relying on it. A date hash also does not distinguish concurrent runs.

Finally, please run the ms-files group explicitly. Its main-site rewriting scenario ignores UPLOADS in production, so changing its expectation to test-suite-uploads does not match that path. The default multisite configuration excludes this group.

@lancewillett
Thanks for the review! All three points are addressed:

  1. Option-based tests: uploadDirOptions.php runs these cases in separate processes without UPLOADS, so the original behavior is still covered. Ideally, _wp_upload_dir() would be split into smaller functions that could be tested on their own, but I don't take a risk do that in this PR cos it change core code, though, so I kept this PR to a test-level fix.
  2. Lock: I replaced the file lock with a per-run environment variable that child processes inherit. It's simpler and shorter than the file-based version.
  3. ms-files: I reverted the main-site expectation in msFilesRewriting.php. The group passes when run explicitly, and it's green in CI.

Could you please take another look?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Unavailable test dependencies prevented runtime verification of the upload-option tests’ parent/child class-setup lifecycle and once-per-run cleanup across supported PHPUnit versions.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dedicated upload directory and the option/ms-files fixtures are useful. Please remove the recursive startup deletion and its WP_TESTS_UPLOADS_RUN_ID bookkeeping unless there is an existing failing case that requires them. The current runs establish compatibility, but do not show a consumer that needs the directory purged. Keep the upload-path override and separate-process option coverage; no additional synthetic test or locking mechanism is needed.

The isolated option tests pass in single-site and multisite, and the explicit ms-files tests pass. The broader affected selection has the same font-face test error on baseline and patch. This request concerns cleanup necessity, not a demonstrated regression.

AI review · gpt-6

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I investigated the startup cleanup further and can now demonstrate its benefit. This supersedes my request to remove it.

I stopped an existing media test immediately after its class fixture created the image files, then reran the unchanged tests from the same saved files. The dedicated UPLOADS path stayed enabled; only startup deletion was toggled.

In both single-site and multisite, Tests_Media::test_wp_get_attachment_image_should_use_wp_get_attachment_metadata:

  • passes with a clean directory and deletion disabled;
  • fails with interrupted-run leftovers and deletion disabled (test-image-large-1-150x150.jpg instead of test-image-large-150x150.jpg);
  • passes with those same leftovers and deletion enabled.

This reproduces the filename-collision problem described in #63532 using real class fixtures. The full media selection also passes with cleanup in both modes: 368 tests / 1,193 assertions, with one existing skip. No test assertions were changed or new test methods added. The earlier isolated upload-option and explicit ms-files checks passed too; the broader font-face error reproduced on baseline and patch.

Startup cleanup therefore has demonstrated value for recovery after an interrupted run. This does not establish support for independently concurrent runs sharing the directory. Current-head CI is green.

AI review · gpt-6

wporg-sync pushed a commit that referenced this pull request Nov 1, 2026
Use `wp-content/test-suite-uploads` for test uploads and remove leftover files once at startup. This prevents interrupted runs from causing filename collisions in later media tests and keeps test files separate from normal uploads.

Update upload URL expectations and run tests for the `upload_path` and `upload_url_path` options in separate processes without the `UPLOADS` constant.

Developed in: #13065

Props mrdollar4444.
Fixes #63532.


git-svn-id: https://develop.svn.wordpress.org/trunk@65177 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 65177
GitHub commit: 2700c1f

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Nov 1, 2026
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 1, 2026
Use `wp-content/test-suite-uploads` for test uploads and remove leftover files once at startup. This prevents interrupted runs from causing filename collisions in later media tests and keeps test files separate from normal uploads.

Update upload URL expectations and run tests for the `upload_path` and `upload_url_path` options in separate processes without the `UPLOADS` constant.

Developed in: WordPress/wordpress-develop#13065

Props mrdollar4444.
Fixes #63532.

Built from https://develop.svn.wordpress.org/trunk@65177


git-svn-id: http://core.svn.wordpress.org/trunk@64326 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants