Opened 5 weeks ago
Closed 4 weeks ago
#65595 closed defect (bug) (fixed)
Fix grunt precommit failure by making sure to delete the 'canola.jpg' attachment uploaded by the ajax group test
| Reported by: | afercia | Owned by: | afercia |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.1 |
| Component: | Build/Test Tools | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests has-test-info |
| Cc: | Focuses: |
Description
Originally reported on the WordPress Slack at https://wordpress.slack.com/archives/C18723MQ8/p1781864911327509 on June 19. I can still reproduce the issue.
When running the grunt precommit task, I get a failing test on the multisite test suite. Error message:
There was 1 failure: 1) WP_Test_REST_Attachments_Controller::test_sideload_scaled_unique_filename Scaled filename should not have numeric suffix appended. Failed asserting that 'canola-scaled-1.jpg' matches PCRE pattern "/canola-scaled\.jpg$/". .../tests/phpunit/tests/rest-api/rest-attachments-controller.php:3950 .../vendor/phpunit/phpunit/phpunit:107
As reported on Slack the failing test passes when ran individually and when running only the multisite test suite.
The difference with grunt precommit is that it runs other tests before the multisite ones, specifically:
- The single site tests suite.
- The single site
ajaxgroup tests.
Turns out one of the ajax tests, specifically Tests_Ajax_wpAjaxSendAttachmentToEditor->test_wp_ajax_send_attachment_to_editor_should_return_an_image creates and uploads a canola.jpg attachment but it misses to delete it when the class tests complete.
The proposed patch just uses the standard pattern to delete the uploaded test attachments in the tear down method.
Steps to reproduce:
- Make sure in your WP Admin > Media Library, there's no
canola.jpgimage. Delete it if necessary. - Make sure in your uploads directory there's no
canola.jpgimage. Delete it if necessary. - Manually monitor the contents of the uploads sub-directory of the current month. We're now on July 2026 so that's
src/wp-content/uploads/2026/07. - Make sure to make a small edit to a PHP file so that the phpunit tests will run. Applying the attached patch should be enough.
- Run
grunt precommit. - Observe it runs the single site test suite first.
- Observe various files get uploaded and then deleted in
src/wp-content/uploads/2026/07including acanola.jpgfile. - Wait for the single site test suite to complete.
- The single site
ajaxgroup tests will run. - Observe that when they complete, these files stays in the
src/wp-content/uploads/2026/07directory:- canola.jpg
- canola-150x150.jpg
- canola-300x225.jpg
- Observe that when the multi site test suite starts, those files are still there.
- Observe that approximately at 60% progress of the multi site test suite there is a test failure.
- Wait for the test suite to complete.
- Observe the error message.
Test the patch:
- Apply the attached patch.
- Repeat the steps above.
- First, make sure to delete any
canola.jpgfrom your Media Library and uploads directory, if any. Delete also anycanolaresized image. - Run grunt precommit.
- Observe it completes with no failures.
Attachments (1)
Change History (15)
#1
@
5 weeks ago
- Summary Fix `grunt precommit` failure by making sure to delete the 'canola.jpg' attachment uploaded by the ajax group test → Fix grunt precommit failure by making sure to delete the 'canola.jpg' attachment uploaded by the ajax group test
This ticket was mentioned in Slack in #core-committers by afercia. View the logs.
5 weeks ago
#4
@
5 weeks ago
It is worth noting that grunt precommit runs the multisite test suite regardless of whether there are changes to multisite files. I don't see the same behavior on GitHub, see for example here https://github.com/WordPress/wordpress-develop/actions/runs/28957129060/job/85925345801?pr=12194 where the multisite tests didn't run.
This appears to be a misalignment between the two tools. I'm not sure it's ideal as committers may not notice failures on GitHub while they may see them when running grunt precommit.
Also, tests should not fail depending on which suite runs or now and in which order they are ran.
This ticket was mentioned in PR #12457 on WordPress/wordpress-develop by @mohamedahamed.
4 weeks ago
#5
- Keywords has-patch has-unit-tests added
## Summary
Fixes a grunt precommit multisite test suite failure by ensuring attachment files uploaded during Tests_Ajax_wpAjaxSendAttachmentToEditor are deleted when the tests complete.
### Root Cause / Context
When running grunt precommit, test suites execute sequentially in this order:
- Single-site test suite (
phpunit) - Single-site ajax group tests (
phpunit --group ajax) - Multisite test suite (
phpunit -c tests/phpunit/multisite.xml)
During step 2 (ajax group tests), Tests_Ajax_wpAjaxSendAttachmentToEditor runs several tests (e.g. test_wp_ajax_send_attachment_to_editor_should_return_an_image) that upload test attachments (canola.jpg, waffles.jpg, small-audio.mp3, entities.txt) via wp_upload_bits(). Because Tests_Ajax_wpAjaxSendAttachmentToEditor lacked a tear_down() method, these uploaded files were not deleted from disk and remained in src/wp-content/uploads/YYYY/MM/.
When step 3 (multisite test suite) ran subsequently, WP_Test_REST_Attachments_Controller::test_sideload_scaled_unique_filename attempted to upload canola.jpg. Because canola.jpg already existed on disk from the earlier ajax test run, wp_unique_filename() appended -1 to avoid file collision (canola-1.jpg), which then scaled to canola-scaled-1.jpg. The test assertion expecting /canola-scaled\.jpg$/ subsequently failed.
### Solution
- Added the standard
tear_down()method to `Tests_Ajax_wpAjaxSendAttachmentToEditor` insidetests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php. - Invoked
$this->remove_added_uploads();before callingparent::tear_down(), ensuring any physical files created during test execution are removed immediately upon completion.
## Testing Instructions
- Verify isolated Ajax tests:
npm run test:php -- --group ajax --filter Tests_Ajax_wpAjaxSendAttachmentToEditor
- Verify that the
uploadsdirectory is completely clean after the test run:find src/wp-content/uploads/ -type f
*(Should output nothing / 0 files).*
- Verify the Multisite test suite passes cleanly immediately following the Ajax tests:
npm run test:php -- -c tests/phpunit/multisite.xml --filter test_sideload_scaled_unique_filename
*(Should complete with 0 failures).*
---
Trac Ticket: https://core.trac.wordpress.org/ticket/65595
---
| Test Case | Expected / Result | Status |
|---|---|---|
Tests_Ajax_wpAjaxSendAttachmentToEditor execution | All 4 tests pass and all uploaded attachment files (canola.jpg, waffles.jpg, etc.) are deleted from disk upon teardown | ✅ Passed |
test_sideload_scaled_unique_filename (Multisite) after Ajax run | Passes cleanly without -1 numeric suffix collision | ✅ Passed |
PHPCS Linting (composer lint -- tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php) | No syntax or coding standards errors (100% clean) | ✅ Passed |
PHPStan Static Analysis (npm run typecheck:php) | Zero errors reported across all files | ✅ Passed |
## Use of AI Tools
AI assistance: No
@mukesh27 commented on PR #12457:
4 weeks ago
#6
Closing as we already have patch https://core.trac.wordpress.org/attachment/ticket/65595/65595.diff and it working fine.
#7
@
4 weeks ago
- Keywords commit added
@afercia I'm not a committer, but cleaning up uploaded media is always a good practice. The changes look good to me.
#9
@
4 weeks ago
@mukesh27 thanks for testing. Yes, let's wait. Also, I would appreciate some feedback about the misalignment between grunt precommit and the GitHub workflows, see comment:4
This ticket was mentioned in Slack in #core-test by nikunj8866. View the logs.
4 weeks ago
#11
@
4 weeks ago
Test Report
Patch tested: https://core.trac.wordpress.org/attachment/ticket/65595/65595.diff
Environment
- WordPress: 7.1-alpha-62161-src
- Subdirectory: No
- PHP: 8.3.31
- Server: nginx/1.31.2
- Database: mysqli (Server: 9.7.1 / Client: mysqlnd 8.3.31)
- Browser: Chrome 149.0.0.0
- OS: macOS
- Theme: Twenty Twenty-Five 1.5
- MU Plugins: None activated
- Plugins:
- Test Reports 1.3.0
Steps taken
- Started the local WordPress development environment using: npm run env:start
- Installed/reset the local environment using: npm run env:install
- Removed any existing uploaded test files before running the test: find src/wp-content/uploads/ -type f -name "canola*" -delete find src/wp-content/uploads/ -type f -name "waffles*" -delete find src/wp-content/uploads/ -type f -name "small-audio*" -delete find src/wp-content/uploads/ -type f -name "entities*" -delete
- Confirmed that the uploads directory was clean before testing: find src/wp-content/uploads/ -type f
- Ran the AJAX test group for the affected test class before applying the patch: npm run test:php -- --group ajax --filter Tests_Ajax_wpAjaxSendAttachmentToEditor
- Confirmed the AJAX tests passed: OK (4 tests, 6 assertions)
- Checked the uploads directory after the test run: find src/wp-content/uploads/ -type f
- Confirmed that uploaded files were left behind after the AJAX tests completed, including:
- src/wp-content/uploads/2026/07/canola.jpg
- src/wp-content/uploads/2026/07/canola-150x150.jpg
- src/wp-content/uploads/2026/07/canola-300x225.jpg
- src/wp-content/uploads/2026/07/waffles.jpg
- src/wp-content/uploads/2026/07/waffles-300x200.jpg
- src/wp-content/uploads/2026/07/small-audio.mp3
- src/wp-content/uploads/2026/07/entities.txt
- Attempted to apply the patch using: npm run grunt patch:65595
- The patch command failed with:
getPatchFromTicket fail
status: 403
- Applied the patch manually by adding a tear_down() method to:
tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php
- The added teardown cleanup calls:
$this->remove_added_uploads();
- Cleaned the uploads directory again after applying the patch:
find src/wp-content/uploads/ -type f -name "canola*" -delete
find src/wp-content/uploads/ -type f -name "waffles*" -delete
find src/wp-content/uploads/ -type f -name "small-audio*" -delete
find src/wp-content/uploads/ -type f -name "entities*" -delete
- Ran the AJAX test group again after applying the patch:
npm run test:php -- --group ajax --filter Tests_Ajax_wpAjaxSendAttachmentToEditor
- Confirmed the AJAX tests passed after applying the patch.
- Checked the uploads directory again:
find src/wp-content/uploads/ -type f
- Confirmed that no leftover uploaded test files remained after the patched test run.
- ✅ Patch is solving the problem
Expected result
- The AJAX tests should pass.
- Files uploaded during Tests_Ajax_wpAjaxSendAttachmentToEditor should be removed during teardown.
- After the test finishes, running: find src/wp-content/uploads/ -type f should return no leftover test-uploaded files.
- The cleanup should prevent later multisite REST attachment tests from failing because of duplicate canola.jpg files or filenames such as canola-scaled-1.jpg.
Actual result
- Before applying the patch, the AJAX tests passed, but uploaded files remained in src/wp-content/uploads/2026/07/.
- After applying the patch, the AJAX tests still passed.
- After applying the patch, the uploaded test files were cleaned up successfully.
- The patch works as expected.
Additional Notes
- The issue was successfully reproduced before applying the patch.
- The local Docker environment displayed a platform warning because the machine is running macOS on Apple Silicon/ARM, while the requested image platform is linux/amd64. This did not block the PHPUnit test from running.
- The patch could not be applied using npm run grunt patch:65595 because the patch fetch returned a 403 error.
- The patch was applied manually and confirmed to resolve the cleanup issue.
Screenshots/Screencast with results
- Screenshot before: Terminal output showing the uploads directory was clean before running the AJAX test.
- Screenshot after reproducing the issue: Terminal output showing the AJAX test passed with: OK (4 tests, 6 assertions)
Terminal output showing leftover files remained in:
src/wp-content/uploads/2026/07/
- Screenshot after applying the patch: Terminal output showing the AJAX test passed after applying the patch.
Terminal output showing that:
find src/wp-content/uploads/ -type f
returned no leftover uploaded test files.
Support Content
- Commands used:
npm run env:start
npm run env:install
find src/wp-content/uploads/ -type f -name "canola*" -delete
find src/wp-content/uploads/ -type f -name "waffles*" -delete
find src/wp-content/uploads/ -type f -name "small-audio*" -delete
find src/wp-content/uploads/ -type f -name "entities*" -delete
find src/wp-content/uploads/ -type f
npm run test:php -- --group ajax --filter Tests_Ajax_wpAjaxSendAttachmentToEditor
find src/wp-content/uploads/ -type f
npm run grunt patch:65595
- Test result before patch:
PHPUnit 9.6.35 by Sebastian Bergmann and contributors.
.... 4 / 4 (100%)
Time: 00:00.515, Memory: 215.00 MB
OK (4 tests, 6 assertions)
- Leftover files after running the AJAX test before the patch:
src/wp-content/uploads/2026/07/canola-150x150.jpg
src/wp-content/uploads/2026/07/entities.txt
src/wp-content/uploads/2026/07/waffles-300x200.jpg
src/wp-content/uploads/2026/07/waffles-1.jpg
src/wp-content/uploads/2026/07/canola-300x225.jpg
src/wp-content/uploads/2026/07/small-audio-1.mp3
src/wp-content/uploads/2026/07/waffles.jpg
src/wp-content/uploads/2026/07/waffles-1-300x200.jpg
src/wp-content/uploads/2026/07/waffles-150x150.jpg
src/wp-content/uploads/2026/07/small-audio.mp3
src/wp-content/uploads/2026/07/waffles-1-150x150.jpg
src/wp-content/uploads/2026/07/canola.jpg
- Patch application issue:
Running:
npm run grunt patch:65595
Returned:
getPatchFromTicket fail
status: 403
- Patch result:
After applying the patch manually and rerunning the AJAX test, the test passed and the uploaded test files were cleaned up successfully.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
At first, this issue appeared to be related to recent changes to the media tests but that doesn't seem the case Cc @adamsilverstein
Instead, it appears to be a pre-existing issue that wasn't noticed so far. I wonder how many core committers are actually using
grunt precommittoday as the failure is very evident and appears to be there since several weeks.Some test and review would be very welcome. Cc @desrosj