Make WordPress Core

Opened 2 months ago

Closed 4 days ago

#66074 closed task (blessed) (fixed)

Tests: Reduce the cost of the heaviest PHPUnit test classes

Reported by: lancewillett Owned by: lancewillett
Priority: normal Milestone: 7.2
Component: Build/Test Tools Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: tests, performance

Description

#65887 gave the suite its first published timing data and closed. This ticket is where the follow-up work lives: the classes that cost the most, claimed one at a time.

What we can measure now

The first two trunk data points on August 31 were 443 s and 330 s for consecutive commits that touched no test, so single points are noise on shared runners. Only a sustained shift means anything. The same two runs counted 32 and 20 tests over one second.

Where the time is

One local run of trunk at 6c0f205fd6 on August 16 (PHP 8.3, MySQL 9.7, Docker, single site) gives the shape. Directional, not authoritative:

  • 30,865 tests in 1,066 classes, 234 s of summed per-test time.
  • Half of that time is in 22 classes. A quarter is in 7.
  • 61 REST API classes account for 49% of it.

The heaviest classes by summed time, in seconds:

sec tests avg class
16.3 197 0.083 WP_Test_REST_Attachments_Controller
11.8 281 0.042 WP_Test_REST_Posts_Controller
7.0 151 0.046 WP_Test_REST_Post_Meta_Fields
6.9 200 0.034 WP_Test_REST_Comments_Controller
6.9 259 0.027 Tests_REST_API
6.5 359 0.018 Tests_Media
6.4 452 0.014 Tests_Blocks_registerCoreBlockStyleHandles
5.2 129 0.041 WP_Test_REST_Users_Controller
4.9 113 0.043 Tests_Image_Functions
4.6 11 0.421 Tests_Admin_ExportWp
4.5 93 0.049 Tests_Theme
4.4 4,474 0.001 Tests_WpTokenMap
4.3 95 0.045 WP_Test_REST_Themes_Controller
4.3 848 0.005 Tests_Canonical_PostStatus
3.8 100 0.038 Tests_REST_WpRestUrlDetailsController
3.8 129 0.030 Tests_REST_Server
3.4 29 0.117 Tests_Image_Editor_Imagick
3.2 95 0.033 WP_REST_Global_Styles_Controller_Test
2.7 70 0.039 WP_Test_REST_Tags_Controller
2.6 49 0.054 Tests_REST_WpRestTemplatesController

Two profiles appear, and they need different fixes. Tests_WpTokenMap and Tests_Canonical_PostStatus are data-provider matrices of cheap tests, so the lever is the size of the matrix. Tests_Admin_ExportWp and Tests_Image_Editor_Imagick are a few expensive tests, so the lever is fixture and I/O cost inside each one.

How to work a class

  1. Claim it in a comment here, one class at a time.
  2. Measure before: npm run test:php -- --filter Class_Name, then read the time attributes in tests/phpunit/build/logs/junit.xml.
  3. Apply the levers the handbook already documents: hoist immutable fixtures into wpSetUpBeforeClass(), share expensive objects across the class, trim provider matrices that exercise identical code paths, and mock file or image work where the assertion does not need the real thing.
  4. Measure after, the same way, and post both numbers on this ticket with the PR.

Guardrails: no assertion is removed to save time, no expected value changes to make a test pass, no timeouts are raised, and a test that protects a specific behavior stays even if it is slow.

Related: #42904, #42278 and #41451 (older performance proposals that can now be evaluated against data), #63026 (the bcrypt precedent), #52645, #65887.

Change History (102)

#1 @sainathpoojary
2 months ago

Thanks for setting up this tracking ticket @lancewillett! I'll claim Tests_Blocks_registerCoreBlockStyleHandles. I'll work on reducing the repeated style registrations while keeping all assertions intact, and will share the before/after metrics here with the PR.

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


2 months ago
#2

  • Keywords has-patch has-unit-tests added; needs-patch removed

Updates Tests_Blocks_registerCoreBlockStyleHandles to avoid repeating the same setup for each block.

Previously, four test methods each ran separately for 113 blocks, giving 452 test cases. Each case called register_core_block_style_handles(), which registers styles for all blocks when separate block assets are enabled, but checked only one block.

Each method now calls the function once and loops through all blocks to check the results. This reduces the test cases from 452 to 4 while preserving all 1,873 assertions and their expected values. Failure messages include the block name to help identify problems.

### Performance
Tested locally across three consecutive runs on trunk versus the fix branch:

Run Trunk Test Time (junit.xml) Trunk Wall Time Fix Test Time (junit.xml) Fix Wall Time
Run 1 4.474s 4.798s 0.062s 0.342s
Run 2 4.062s 4.377s 0.057s 0.295s
Run 3 3.709s 4.029s 0.094s 0.352s
Average 4.082s 4.401s 0.071s 0.330s

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

## Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Model(s): Claude Sonnet 4.6
Used for: Edge-case checks, and assistance with the PR description, implementation and testing were reviewed and verified by me.

#3 @lancewillett
2 months ago

In 63563:

Tests: Reduce repeated core block style registration.

Register core block styles once per scenario, then check all block handles. Replace per-block data-provider cases with loops, retaining the assertions and adding block names to failure messages.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13457

Props sainathpoojary.
See #66074.

#5 @sainathpoojary
2 months ago

Thanks for the review and merge @lancewillett !

I’d take on Tests_WpTokenMap for my next PR. I'll focus on trimming the repeated setup while ensuring all assertions remain intact.

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


2 months ago
#6

Updates Tests_WpTokenMap to avoid repeated test runner overhead for each HTML5 named character reference.

Previously, test_reads_token_at_given_offset and test_detects_all_tokens each used a 2,231-item data provider (data_html5_test_dataset), creating 4,462 individual test invocations. Each invocation incurred PHPUnit test runner lifecycle overhead (setup, teardown, and event handling) for in-memory token map checks.

Both methods now retrieve the dataset via a private helper method and assert all tokens within internal loops. This reduces the test case count from 4,474 to 14 while preserving all 24,587 assertions. Failure messages have also been updated to include the token name to maintain precise diagnostics if a regression occurs.

### Performance

Performance measured locally across three consecutive runs comparing trunk to this branch:

Run Trunk Test Time Trunk Wall Time Fix Test Time Fix Wall Time
:--- :--- :--- :--- :---
Run 1 4.892s 5.277s 0.192s 0.218s
Run 2 5.628s 6.121s 0.192s 0.214s
Run 3 4.452s 4.790s 0.195s 0.232s
Average 4.991s 5.396s 0.193s 0.221s
  • Pure Test Time: ~96.1% reduction (from ~4.99s to ~0.19s).
  • PHPUnit Wall Time: ~95.9% reduction (from ~5.40s to ~0.22s).

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

## Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks, and assistance with the PR description, implementation and testing were reviewed and verified by me.

#7 @lancewillett
2 months ago

In 63591:

Tests: Reduce repeated token map test setup.

Check all HTML5 named character references within each token map test instead of running a separate test for every token. Preserve the per-token assertions and include token names in failure messages.

Reduce test invocations from 4,474 to 14, avoiding repeated test runner setup and teardown.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13478

Props sainathpoojary.
See #66074.

#9 @sainathpoojary
2 months ago

I'd like to claim Tests_Admin_ExportWp next.

I'll optimize wpSetUpBeforeClass() by mocking image uploads/thumbnail generation, as the export assertions only require attachment database records.

I'll open a PR with before/after measurements shortly.

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


2 months ago
#10

Optimizes fixture setup in Tests_Admin_ExportWp by avoiding physical image uploads and thumbnail generation during sample data preparation.

As noted in the ticket description for #66074, Tests_Admin_ExportWp is driven by fixture and I/O cost. Previously, wpSetUpBeforeClass() called create_upload_object(), which physically copied four JPEGs to the uploads folder and generated intermediate image sizes for each of the 11 tests running in separate PHP processes.

export_wp() tests only assert that post and attachment database records appear correctly in the exported XML, they do not inspect physical files or image dimensions. Replacing create_upload_object() with $factory->attachment->create() creates the necessary database records and thumbnail relationships directly, eliminating disk writes and GD/Imagick sub-size generation.

### Performance

Local measurements across three runs (each running 11 tests across separate processes):

Run Trunk Test Time Trunk Wall Time Fix Test Time Fix Wall Time
:--- :--- :--- :--- :---
Run 1 10.63s 34.17s 7.96s 27.24s
Run 2 8.01s 26.46s 8.36s 26.65s
Run 3 9.01s 29.13s 8.53s 29.20s
Average 9.22s 29.92s 8.28s 27.70s
  • Average JUnit runtime: ~10.1% reduction (from 9.22s to 8.28s).
  • Average PHPUnit runtime: ~7.4% reduction (from 29.92s to 27.70s).
  • Setup time per process: Dropped from ~0.72s to ~0.60s.

While the local time gain is modest and subject to process variance, eliminating disk I/O and thumbnail generation removes unnecessary filesystem churn across all separate test processes.

### Validation

  • All 11 tests and 35 assertions pass on both single-site and multisite.
  • Verified attachment parent relationships and featured image IDs (set_post_thumbnail) are intact.
  • PHPCS passes with 0 errors and 0 warnings.

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

## Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks, and assistance with the PR description. Implementation and testing were made and reviewed by me.

#11 @lancewillett
2 months ago

  • Owner set to lancewillett
  • Status new → accepted

​@sainathpoojary commented on ​PR #13495:


2 months ago
#12

Thanks for the feedback @lancewillett .

I investigated and found that Tests_Admin_IncludesMedia was not cleaning up its uploads, and scan_user_uploads() was rescanning when the directory started empty because an empty array evaluated as falsy.

This update:

  • Adds cleanup to Tests_Admin_IncludesMedia::tear_down().
  • Preserves empty initial snapshots in WP_UnitTestCase_Base.
  • Adds regression coverage in uploadSnapshot.php.

The suite passes cleanly locally. Could you please recheck when you have time?

#13 @lancewillett
2 months ago

In 63631:

Tests: Reduce export fixture cost and fix upload cleanup.

Create export attachment fixtures directly in the database instead of copying images and generating intermediate sizes. Preserve the attachment parent and featured-image relationships.

Cache an initially empty upload snapshot and clean up files created by the admin media tests, preventing leftover uploads from changing later filenames. Add regression coverage for empty and nonempty initial snapshots.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13495

Props sainathpoojary.
See #66074.

#15 @sainathpoojary
2 months ago

I'd like to take on WP_Test_REST_Post_Meta_Fields next. I'll optimize it by reusing the editor fixture to avoid repeated user creation, and will open a PR shortly.

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


2 months ago
#16

Optimizes fixture setup in WP_Test_REST_Post_Meta_Fields by avoiding repeated editor user creation across individual tests.

Previously, grant_write_permission() called $factory->user->create() each time it was invoked (65+ calls across test methods and data provider iterations). This introduced unnecessary database insertions and teardown overhead for tests that only needed an authenticated editor user.

This change creates a shared editor user once in wpSetUpBeforeClass(), sets that user in grant_write_permission(), and cleans it up via self::delete_user() in wpTearDownAfterClass(). All 151 tests and 581 assertions remain unchanged and pass.

Performance

Measured locally across three consecutive runs comparing trunk to this branch:

Run Trunk Test Time (junit.xml) Trunk Wall Time Fix Test Time (junit.xml) Fix Wall Time
:--- :--- :--- :--- :---
Run 1 2.386s 2.695s 1.829s 2.287s
Run 2 2.295s 2.612s 1.802s 2.219s
Run 3 2.242s 2.548s 1.850s 2.325s
Average 2.308s 2.618s 1.827s 2.277s

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

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks and assistance with the PR description, implementation and testing were done and reviewed by me.

​@sainathpoojary commented on ​PR #13589:


2 months ago
#17

I noticed a few of the CI test jobs are failing, so I spent some time investigating. I ran the tests locally, but they are passing for me without any issues.

I also checked trunk and a few other open PRs, and it looks like these similar failures are happening there as well. Since this is happening other places as well, it seems like these might be random test failures rather than something caused by these changes.

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


8 weeks ago
#18

Optimizes fixture setup in WP_Test_REST_Attachments_Controller by avoiding unnecessary image processing in tests that do not check generated image sizes.

Three permission tests previously uploaded JPEG files and generated intermediate image sizes before asserting that the REST request was rejected. The permission callback returns the expected error before image editing begins, so these tests now create attachment records directly with the required MIME type, status, and file path.

The EXIF orientation test still uploads and edits a real image, but it only checks the full-size image dimensions after rotation. It now skips intermediate image size generation while preserving the real upload, EXIF correction, rotation, and all assertions.

All 200 tests and 1,159 assertions remain unchanged and pass.

Performance

Measured locally across three consecutive runs comparing trunk to this branch:

Run Trunk Test Time (junit.xml) Trunk Wall Time Fix Test Time (junit.xml) Fix Wall Time
:--- :--- :--- :--- :---
Run 1 8.337s 9.176s 6.950s 7.777s
Run 2 8.178s 9.020s 6.904s 7.739s
Run 3 8.466s 9.314s 7.166s 7.984s
Average 8.327s 9.170s 7.007s 7.833s

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

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks and assistance with the PR description, implementation and testing were done and reviewed by me.

#19 @sainathpoojary
8 weeks ago

@lancewillett I also took a look at WP_Test_REST_Term_Meta_Fields. It's not on the main list, but it has the same setup overhead as the WP_Test_REST_Post_Meta_Fields class. Fixing it drops the local runtime from 0.843s to 0.769s (all 52 tests / 267 assertions intact). Would this smaller optimization be useful for this ticket, or should work remain limited to the listed classes?

#20 @lancewillett
8 weeks ago

@sainathpoojary Yes, useful here. Thanks!

#21 @lancewillett
8 weeks ago

In 63776:

Tests: Reuse the editor in REST post meta tests.

Create the editor once per class instead of recreating it for each write-permission check. Delete the shared user during class teardown.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13589

Props sainathpoojary.
See #66074.

#22 @lancewillett
8 weeks ago

In 63777:

Tests: Reduce image work in REST attachment tests.

Use attachment records without uploading images in three permission tests, and remove their unnecessary imagejpeg requirement. Skip intermediate image sizes in the EXIF orientation test while preserving the full-size image checks.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13619

Props sainathpoojary.
See #66074.

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


8 weeks ago
#25

Optimizes fixture setup in WP_Test_REST_Term_Meta_Fields by avoiding repeated editor user creation across individual tests.

Previously, grant_write_permission() created a new editor user each time it was called. This introduced unnecessary database work for tests that only needed an authenticated editor user.

This change creates one shared editor user in wpSetUpBeforeClass(), uses it in grant_write_permission(), and removes it with self::delete_user() in wpTearDownAfterClass(). All 52 tests and 267 assertions remain unchanged and pass.

Performance

Measured locally across three consecutive runs comparing trunk to this branch:

Run Trunk Test Time (junit.xml) Trunk Wall Time Fix Test Time (junit.xml) Fix Wall Time
:--- :--- :--- :--- :---
Run 1 0.597s 0.864s 0.400s 0.788s
Run 2 0.554s 0.811s 0.387s 0.766s
Run 3 0.596s 0.853s 0.379s 0.754s
Average 0.582s 0.843s 0.389s 0.769s

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

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks and assistance with the PR description, implementation and testing were done and reviewed by me.

#26 @lancewillett
8 weeks ago

In 63781:

Tests: Reuse the editor fixture in REST term meta tests.

Create one editor user for the REST term meta test class and reuse it when granting write permission. Delete the shared user during class teardown, avoiding repeated user creation while preserving the existing assertions.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13636

Props sainathpoojary.
See #66074.

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


7 weeks ago
#28

Reduces unnecessary REST server setup in Tests_REST_API.

Previously, every test created a Spy_REST_Server and initialized all REST routes, including helper tests that did not need them.

This change initializes the server only where needed using rest_get_server(). The test bootstrap continues to select Spy_REST_Server. Setup and teardown clear the server to preserve test isolation.

Performance

Measured locally across three runs:

Run Trunk Test Time Trunk Wall Time Fix Test Time Fix Wall Time
:--- :--- :--- :--- :---
Run 1 1.593s 1.884s 0.558s 0.852s
Run 2 1.756s 2.049s 0.535s 0.823s
Run 3 1.578s 1.877s 0.551s 0.850s
Average 1.642s 1.937s 0.548s 0.842s

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

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks, and assistance with the PR description. Implementation and testing were made and reviewed by me.

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


7 weeks ago
#29

Trac ticket: #66074

What

Optimizes Tests_Canonical_PostStatus by asserting all plain- and pretty-permalink matrix cases inside two tests instead of one PHPUnit invocation per row.

Previously, @dataProvider created hundreds of individual test cases. Each case paid PHPUnit setup/teardown cost for the same shared fixtures and permalink structure.

Why

Part of #66074. This class is on the heavy list as a data-provider matrix of cheap tests: the lever is reducing matrix invocations, not removing coverage.

Same approach as the landed Tests_WpTokenMap work: keep every assertion, cut repeated runner overhead.

How

  • test_canonical_redirects_to_plain_permalinks and test_canonical_redirects_to_pretty_permalinks loop over private case helpers
  • Dataset builders keep the same cases and expected URLs
  • Permalink structure is set once per method (identical for every case in that method)
  • Each case still resets attachment-pages option, current user, and post cache
  • Failure messages include post_key, user_role, and URLs for diagnosis
  • No assertions removed; no expected values changed

Testing instructions

  1. Start the local WordPress develop environment.
  2. Run:
npm run test:php -- --filter Tests_Canonical_PostStatus

Use of AI tools

Cursor

​@lancewillett commented on ​PR #13818:


7 weeks ago
#30

Optional, non-blocking improvement: could both exception handlers preserve PHPUnit’s comparison details when adding the case context?

A deliberate URL mismatch confirmed that catching ExpectationFailedException and calling $this->fail() loses the actual URL and PHPUnit’s expected/actual diff. The output includes the case identifiers and expected URL, followed by “Failed asserting that two strings are identical,” but not the actual result.

Keeping the original exception’s getComparisonFailure() when rethrowing would retain the useful diff alongside the new context.

Code: ​https://github.com/WordPress/wordpress-develop/blob/7f8774ed8c7dee0a5d08738afc622a5b34e4939b/tests/phpunit/tests/canonical/postStatus.php#L246

---
<sub>Adversarial review · <code>gpt-6</code></sub>

#31 @sainathpoojary
7 weeks ago

I'd like to claim Tests_REST_Server next. I'll look into optimizing the test setups to reduce its runtime while keeping all assertions intact. I'll open a PR shortly.

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


7 weeks ago
#32

Optimizes fixture setup in Tests_REST_Server by avoiding an unnecessary image upload and thumbnail generation.

Previously, wpSetUpBeforeClass() uploaded test-image-large.jpg and generated intermediate image sizes. The test using this attachment only checks the site icon ID and URL returned by the REST API. It does not inspect image dimensions or physical files.

This change creates the attachment record directly with a JPEG MIME type and file path. The site icon URL still resolves through the normal WordPress attachment APIs, and the existing teardown removes the attachment record. All 129 tests and 407 assertions remain unchanged.

Performance

Measured locally across three consecutive runs comparing trunk to this branch:

Run Trunk PHPUnit Time Fix PHPUnit Time
:--- :--- :---
Run 1 2.593s 1.153s
Run 2 2.534s 1.297s
Run 3 2.512s 1.266s
Average 2.546s 1.239s

Average PHPUnit runtime decreased by approximately 51.4%. The improvement comes from removing image upload and resizing work during class setup, which is not included in JUnit’s individual test timings.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: PR description

​@lancewillett commented on ​PR #13823:


7 weeks ago
#33

Separate, non-blocking follow-up: randomized runs exposed an existing test-isolation issue in this class, reproduced on both the unchanged baseline and this patch.

Seeds 13823 and 13824 fail test_nocache_headers_on_authenticated_requests() with “Header Expires is not present in the response”; seed 13825 passes. The nonce-test helper sets $wp_rest_auth_cookie = true without restoring it. A later authenticated request without a nonce is then treated as unauthenticated.

A diagnostic reset of that global before each test made seed 13823 pass on both versions, in single-site and multisite. Could we address restoration of this global in a separate PR? This fixture optimization does not introduce the failure.

Reproduce with:

npm run test:php -- --filter Tests_REST_Server --order-by=random --random-order-seed=13823

Code: ​https://github.com/WordPress/wordpress-develop/blob/21c80fd0d53a384afbeec613b7ee8a3486f431b2/tests/phpunit/tests/rest-api/rest-server.php#L1819

---
<sub>Adversarial review · <code>gpt-6</code></sub>

#34 @lancewillett
7 weeks ago

In 64005:

Tests: Avoid unnecessary image upload in REST server tests.

Create the site icon attachment record directly instead of uploading an image and generating thumbnails. The REST index test checks the attachment ID and URL, so physical image files and dimensions are unnecessary.

Preserve URL resolution through the WordPress attachment APIs and all existing assertions while reducing fixture setup time.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13823

Props sainathpoojary.
See #66074.

#36 @lancewillett
7 weeks ago

In 64006:

Tests: Initialize the REST API test server only when needed.

Avoid initializing the REST server for tests that do not use it. Initialize it explicitly in route tests and reset the global server between tests. Remove redundant server restoration from preload tests.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13694

Props sainathpoojary, mindctrl.
See #66074.

#38 @lancewillett
7 weeks ago

https://core.trac.wordpress.org/changeset/64007 -- I forgot the Trac reference. 😄

Version 0, edited 7 weeks ago by lancewillett (next)

#40 @sainathpoojary
6 weeks ago

I'd like to claim Tests_Media next. I'll optimize it by reusing the existing image fixture to avoid repeated uploads, and will open a PR shortly.

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


6 weeks ago
#41

Updates Tests_Media to reuse its existing image attachment in the attachment filesize fallback test.

Previously, each of the nine data provider cases uploaded canola.jpg and generated image sizes. The test only needs a readable attachment file to confirm that wp_prepare_attachment_for_js() falls back to the file size when attachment metadata contains an invalid filesize value.

This change reuses the class fixture, which is already a real uploaded image. All nine cases and 63 assertions remain unchanged.

Performance

Measured locally across three runs for the affected nine test cases:

Run Trunk Test Time Fix Test Time
:--- :--- :---
Run 1 0.385s 0.056s
Run 2 0.384s 0.045s
Run 3 0.390s 0.040s
Average 0.386s 0.047s

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

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: PR description

#42 @lancewillett
6 weeks ago

In 64033:

Tests: Reuse the media fixture for attachment filesize tests.

Reuse the existing uploaded image in the attachment filesize fallback test instead of uploading an image and generating image sizes for each data set. Preserve all nine cases and 63 assertions against a real attachment file.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13905

Props sainathpoojary.
See #66074.

#44 @sainathpoojary
6 weeks ago

I'd like to claim Tests_Image_Functions next. Will optimize it by using a database fixture instead of real file uploads, and will open a PR shortly.

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


6 weeks ago
#45

Three wp_image_editor() filter tests were uploading an image file and creating real thumbnail files on disk.

These tests only check if the image_edit_thumbnails_separately filter runs and if the imgedit-applyto markup is printed. They do not process image pixels.

This PR replaces the file upload and thumbnail generation with a fixture using self::factory()->attachment->create() and static metadata. A thumbnail entry is kept in the metadata so the editor markup still renders properly.

Performance

Measured locally across three consecutive runs comparing trunk against this branch:

Full Class (Tests_Image_Functions, 113 tests)

Run Trunk Test Time Fix Test Time
--- ---: ---:
1 5.993s 6.017s
2 6.067s 5.846s
3 6.374s 5.841s
Average 6.145s 5.901s

Affected Filter Tests (3 tests)

Run Trunk Test Time Fix Test Time

| --- | ---: | ---: |
| 1 | 0.133s | 0.019s |
| 2 | 0.131s | 0.019s |

3 0.134s 0.018s

| Average | 0.133s | 0.019s |

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

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks and drafting the PR description, implementation and testing were reviewed and verified by me.

#46 @lancewillett
6 weeks ago

In 64036:

Tests: Use static metadata for image editor filter tests.

Create attachment records with static image metadata instead of uploading images and generating thumbnails for the image_edit_thumbnails_separately filter tests. Preserve the filter invocation and conditional markup checks, including the thumbnail metadata needed to render the controls.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13909

Props sainathpoojary.
See #66074.

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


6 weeks ago
#48

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

Three REST attachment image-quality response tests currently upload a JPEG and generate thumbnails, even though their assertions only need the attachment MIME type, full-size dimensions, and registered image sizes.

This PR uses a private fixture helper to create an attachment record with static 640×480 metadata. Remove the unnecessary imagejpeg requirements. Keep all requests, filters, assertions, and expected values unchanged.

Measured the three affected tests from JUnit output across three full-class runs on PHP 8.3.33 / MySQL 8.4.11:

Run Before After
--- ---: ---:
1 0.240s 0.105s
2 0.268s 0.105s
3 0.269s 0.115s
Average 0.259s 0.108s

The affected tests average 58.3% less time. Full-class JUnit sums averaged 13.120s before and 11.740s after; unrelated test timings also varied, so this does not establish a full-suite speedup.

Use of AI Tools

AI assistance: Yes
Tool(s): Codex
Used for: Investigation, fixture implementation, and PR description. Testing and code review were performed manually to verify everything.

#49 @sainathpoojary
5 weeks ago

I'd like to claim WP_Test_REST_Posts_Controller next. I'll work on optimizing its setup overhead to reduce runtime while preserving all existing test coverage, and will follow up with the PR and metrics shortly.

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


5 weeks ago
#50

Optimizes WP_Test_REST_Posts_Controller by limiting private reader role setup to the tests that need it.

Previously, every test in the class registered the private_reader role and added the read_private_posts capability during setup. Only three tests use the private reader user to check private post permissions.

This change moves that setup into a private helper called by those three tests. All requests, assertions, and expected values remain unchanged.

Performance

Measured locally across three runs:

Run Trunk Test Time Trunk Wall Time Fix Test Time Fix Wall Time
:--- :--- :--- :--- :---
Run 1 4.134s 6.088s 3.711s 5.548s
Run 2 3.957s 5.491s 3.686s 5.320s
Run 3 4.070s 5.704s 3.760s 5.231s
Average 4.054s 5.761s 3.719s 5.366s
  • Average JUnit test time: ~8.3% reduction.
  • Average PHPUnit wall time: ~6.9% reduction.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks and assistance with the PR description. Implementation and testing were done and reviewed by me.

#51 @abcd95
5 weeks ago

I'd like to claim Tests_REST_WpRestUrlDetailsController next. I'll work on reducing repeated setup in its parsing tests while preserving all existing test coverage, and will follow up with the PR and before/after metrics shortly.

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


5 weeks ago
#52

​@abcd95 commented on ​PR #14103:


5 weeks ago
#53

One thing worth mentioning here is we are not anymore using the data providers. Updated the four helper comments to describe the parsing test cases they return. The methods remain in use by the test loops, so I kept them and their existing arrays to keep the examples separate from the test logic.

#54 @sainathpoojary
5 weeks ago

I'd like to claim WP_REST_Global_Styles_Controller_Test next. I'll work on optimizing the custom CSS validation tests to avoid repeated setup overhead per dataset while keeping all assertions intact, and will raise the PR shortly.

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


5 weeks ago
#55

Optimizes custom CSS validation tests in WP_REST_Global_Styles_Controller_Test.

Previously, 28 CSS validation cases used data providers. Each case ran the full test setup, including REST server and theme setup, even though the cases only call validate_custom_css().

This change keeps the same datasets and assertions, but runs each dataset inside its test method. Failure messages include the dataset name.

Performance

Measured locally across three runs comparing trunk to this branch:

Run Trunk Test Time Trunk Wall Time Fix Test Time Fix Wall Time
:--- :--- :--- :--- :---
Run 1 0.881s 1.651s 0.634s 1.352s
Run 2 0.859s 1.586s 0.676s 1.383s
Run 3 0.871s 1.724s 0.679s 1.419s
Average 0.870s 1.654s 0.663s 1.385s
  • Pure Test Time (JUnit): ~23.8% reduction
  • PHPUnit Wall Time: ~16.3% reduction

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks and assistance with the PR description. Implementation and testing were done and reviewed by me.

#56 @lancewillett
5 weeks ago

In 64260:

Tests: Reduce REST attachment quality fixture overhead.

Create attachment records with static image metadata for the three REST image quality response tests instead of uploading a JPEG and generating thumbnails. Preserve the requests, filters, and assertions while removing the unnecessary imagejpeg() requirements.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14026

Props abcd95.
See #66074.

#58 @lancewillett
5 weeks ago

In 64316:

Tests: Separate global styles CSS validation tests.

Move direct validate_custom_css() tests into a separate WP_UnitTestCase class, avoiding REST server and theme setup for each dataset. Preserve all 28 cases, their assertions, and independent dataset reporting.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14119

Props sainathpoojary.
See #66074.

#60 @lancewillett
5 weeks ago

In 64317:

Tests: Separate URL details parsing tests.

Move direct WP_REST_URL_Details_Controller parsing tests into a separate WP_UnitTestCase class. Preserve all 80 datasets and their assertions while avoiding REST setup and teardown for parsing-only checks. Keep endpoint tests in the existing controller test class.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14103

Props abcd95.
See #66074.

#62 @sainathpoojary
5 weeks ago

I'd like to claim WP_Test_REST_Tags_Controller next. I will share the PR along with before/after metrics shortly.

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


5 weeks ago
#63

Optimizes WP_Test_REST_Tags_Controller by limiting term metadata field registrations to the tests that need them.

Previously, every test in the class registered five metadata fields (test_single, test_multi, test_tag_single, test_tag_multi, and test_cat_meta) during set_up(). Only five tests exercise term metadata.

This change moves those registrations into a private helper method called only by those five tests. All assertions, schemas, and expectations remain unchanged.

Performance

Measured locally across three runs:

Run Trunk Test Time Fix Test Time
:--- :--- :---
Run 1 0.716s 0.664s
Run 2 0.715s 0.686s
Run 3 0.695s 0.640s
Average 0.709s 0.663s
  • Average JUnit test time: ~6.4% reduction.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Used for: Edge-case checks and assistance with timing measurements; implementation and testing were verified by me.

#64 @lancewillett
5 weeks ago

In 64326:

Tests: Limit metadata registration in REST tags tests.

Register the five term metadata fields only in the tests that use them, instead of in every test in WP_Test_REST_Tags_Controller. Preserve the existing assertions and inherited setup and cleanup.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14222

Props sainathpoojary.
See #66074.

#66 @abcd95
5 weeks ago

I'd like to claim Tests_Image_Editor_Imagick next. I'll reduce unnecessary image generation in its PDF preview test while preserving the existing assertions, and follow up with the PR and before/after metrics.

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


5 weeks ago
#67

​@abcd95 commented on ​PR #14103:


5 weeks ago
#68

Hi @lancewillett, I see Copilot has flagged some points here. Do you think a follow-up PR can be opened to address these?

​@lancewillett commented on ​PR #14103:


5 weeks ago
#69

Thanks @himanshupathak95! Yes, a small follow-up PR would be welcome for the duplicated “reverse order” fixture, the whitepace typo, and clarifying the malformed HTML fixture and wrapper documentation. Please preserve any intentional malformed-input coverage.

Two Copilot suggestions don’t need code changes:

  • Keep the data providers. We deliberately chose a separate class to avoid REST setup while preserving dataset filtering and independent failures. The PR description still describes the earlier loops; updating it would help.
  • assertEqualHTML() takes the fragment context as its third argument, so '<body>' is correct. The failure message is the fourth argument.

These are follow-up cleanups to existing fixtures moved by this PR, rather than regressions introduced by the extraction.

<sub>AI review · <code>gpt-6</code></sub>

#70 @sainathpoojary
5 weeks ago

I'd like to claim WP_Test_REST_Comments_Controller next. I will share the PR along with before/after metrics shortly.

This ticket was mentioned in ​PR #14258 on ​WordPress/wordpress-develop by ​SainathPoojary.


5 weeks ago
#71

Reduces test setup overhead in WP_Test_REST_Comments_Controller.

  • Removes $this->endpoint = new WP_REST_Comments_Controller(); from set_up() because $this->endpoint is not used in this test class.
  • Removes two unused post creations in test_update_item_no_content() and test_update_item_no_content_allow_empty_comment_filter().
  • Reuses existing class fixtures (self::$private_id and self::$post_id) in four permission and validation tests that return error responses and do not create database records.
  • Keeps all 202 tests and 1,003 assertions unchanged.

Performance

Measured locally from JUnit output across three runs of WP_Test_REST_Comments_Controller (202 tests):

Run Trunk Test Time Fix Test Time
:--- :--- :---
Run 1 3.232s 2.212s
Run 2 2.700s 2.174s
Run 3 2.631s 2.416s
Average 2.854s 2.267s

Test suite time reduced by about 0.587s (20.6% reduction).

Use of AI Tools

AI assistance: Yes
Tool(s): GitHub Copilot
Used for: Edge-case checks and drafting the PR description. Implementation and testing were reviewed and verified by me.

#72 @sainathpoojary
4 weeks ago

I'd like to claim Tests_Theme next. I will share the PR along with before/after metrics shortly.

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


4 weeks ago
#73

Reduces repeated theme setup in Tests_Theme.

Previously, test_register_theme_support_validation() used a data provider to run eight invalid-input cases separately. Each case repeated the class setup and cleanup, including clearing theme caches.

This change checks all eight cases in a loop within one test. All inputs, assertions, and expected errors remain unchanged. Failure messages include the case number.

Performance

Measured locally from JUnit output across three runs of Tests_Theme:

Run Trunk Class Time Fix Class Time Trunk Affected Cases Fix Affected Cases
:--- ---: ---: ---: ---:
Run 1 2.391s 2.306s 0.153s 0.017s
Run 2 2.299s 2.235s 0.150s 0.017s
Run 3 2.448s 2.196s 0.166s 0.020s
Average 2.379s 2.246s 0.156s 0.018s
  • Affected validation cases took about 88.4% less time.
  • Full-class test time averaged about 5.6% lower. Local timings vary.

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copilot
Used for: Edge-case checks and drafting the PR description. Implementation and testing were reviewed and verified by me.

#74 @lancewillett
4 weeks ago

In 64367:

Tests: Reduce setup overhead in REST comments tests.

Remove the unused controller instance and two unused post creations from WP_Test_REST_Comments_Controller. Reuse existing class fixtures in four permission and validation tests that reject requests before creating database records.

Preserve the existing assertions while avoiding unnecessary fixture setup.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14258

Props sainathpoojary.
See #66074.

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


4 weeks ago
#76

Follow-up to [https://core.trac.wordpress.org/changeset/64317 [64317]], which separated the URL details parsing tests into a standalone WP_UnitTestCase class (PR #14103).

#77 @abcd95
4 weeks ago

I'd like to claim WP_Test_REST_Users_Controller next. I'll reduce repeated user creation by reusing existing fixtures where appropriate, preserve all assertions and test coverage, and follow up with the PR and before/after metrics.

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


4 weeks ago
#78

#79 @sainathpoojary
4 weeks ago

I'd like to claim Tests_REST_WpRestTemplatesController next. I will share the PR along with before/after metrics shortly.

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


4 weeks ago
#80

Reduces unnecessary REST setup in Tests_REST_WpRestTemplatesController.

Moves test_sanitize_template_id() and its data provider into a lightweight WP_UnitTestCase class. These checks only format template ID strings and do not need the REST server, registered routes, or shared user and template fixtures.

All four datasets, assertions, and expected values remain unchanged. Each dataset remains independently reported and filterable.

Performance

Measured locally from JUnit output across three runs. The fix timings include both the remaining controller tests and the extracted class.

Run Trunk Full Test Set Fix Full Test Set
:--- ---: ---:
Run 1 0.443s 0.433s
Run 2 0.458s 0.413s
Run 3 0.447s 0.432s
Average 0.449s 0.426s

The four sanitization cases within those runs:

Run Before Extraction After Extraction
:--- ---: ---:
Run 1 0.029s 0.003s
Run 2 0.032s 0.003s
Run 3 0.029s 0.003s
Average 0.030s 0.003s

The affected cases averaged about 89% less time. The complete test set averaged about 5% lower.

Use of AI Tools

AI assistance: Yes
Tool(s): Github Copolit
Used for: Edge-case checks and drafting the PR description. Implementation and testing were reviewed and verified by me.

#81 @lancewillett
4 weeks ago

In 64389:

Tests: Correct URL details parsing fixtures and labels.

Reverse the attribute order in the multiline icon fixture so it exercises a distinct input. Correct a dataset label and document the malformed description fixture and minimal HTML wrapper.

Follow-up to [64317].

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14326

Props abcd95.
See #66074.

​@lancewillett commented on ​PR #14326:


4 weeks ago
#83

Thanks for the cleanup!

#84 @lancewillett
4 weeks ago

In 64390:

Tests: Separate template ID sanitization tests.

Move the four _sanitize_template_id() datasets into a separate WP_UnitTestCase class to avoid unnecessary REST server and fixture setup. Preserve all assertions, expected values, and independent dataset reporting.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14345

Props sainathpoojary.
See #66074.

#86 @lancewillett
4 weeks ago

In 64392:

Tests: Separate theme feature validation tests.

Move test_register_theme_support_validation() and its provider into a separate WP_UnitTestCase class to avoid unnecessary theme-directory and cache setup. Preserve all eight datasets, assertions, and expected errors.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14306

Props sainathpoojary.
See #66074.

#88 @lancewillett
4 weeks ago

In 64484:

Tests: Reuse existing fixtures in REST user tests.

Reuse the author and subscriber fixtures in read-only and rejected-request tests in WP_Test_REST_Users_Controller, avoiding 13 user creations per class run.

Preserve the requests, assertions, and include-query ordering checks. Keep fresh users for successful updates, deletions, and identity-specific scenarios, and retain per-test post and page fixtures.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14339

Props abcd95.
See #66074.

#90 @abcd95
4 weeks ago

I'd like to claim WP_Test_REST_Themes_Controller next. I'll replace repeated theme activation with direct test-theme setup, preserve the existing assertions and required state resets, and share the PR with before/after timings. I checked; the theme activation setup is not needed anywhere.

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


4 weeks ago
#91

This PR sets up the REST test theme directly instead of activating it before every test. It keeps the theme paths, template globals, and cache reset needed by these tests. Removes an unused saved theme object. All test methods, data providers, and assertions stay unchanged.

Tested across six alternating before/after pairs, the class averaged 1.125883s before and 1.072014s after: 4.8% less time, or about 54ms. These are local Docker results on PHP 8.3.35, PHPUnit 9.6.38, and MySQL 8.4.11.

Pair Before After Reduction
--- ---: ---: ---:
1 1.114765s 1.090136s 2.2%
2 1.114614s 1.085526s 2.6%
3 1.151654s 1.084307s 5.8%
4 1.152739s 1.053005s 8.7%
5 1.120534s 1.078993s 3.7%
6 1.100993s 1.040117s 5.5%

Validation: Focused checks pass on PHP 8.3 and 8.4, single-site and multisite, including random order (95 tests, 537 assertions). Full single-site suite: 29,151 tests, with the same failures, 86 warnings, and 68 skips on both versions; no new failing tests.

Use of AI Tools

AI assistance: Yes
Tool(s): Codex
Used for: Investigation, benchmarking, and drafting the PR description. The implementation, testing and verification is done manually.

#92 @lancewillett
4 weeks ago

In 64508:

Tests: Simplify theme fixture setup in REST API tests.

Select the fixture theme directly in WP_Test_REST_Themes_Controller::set_up() instead of running switch_theme() before each test. Preserve the theme roots, template globals, and theme JSON cache cleanup needed by the controller tests.

Remove the unused saved theme object. Keep all test methods, data providers, and assertions unchanged.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14470

Props abcd95, rejaulalomkhan, mukesh27.
See #66074.

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


3 weeks ago
#94

Trac ticket: #66074

Follow-up to: ​#13818

What

Updates both ExpectationFailedException handlers in Tests_Canonical_PostStatus so failures rethrow with the original getComparisonFailure() instead of calling $this->fail().

Updated file: tests/phpunit/tests/canonical/postStatus.php

Why

Optional follow-up from review on #13818. Catching the exception and calling $this->fail() kept the case context but dropped PHPUnit’s expected/actual URL diff. Preserving getComparisonFailure() keeps both the case identifiers and the useful comparison output.

How

  • In test_canonical_redirects_to_plain_permalinks() and test_canonical_redirects_to_pretty_permalinks(), replace $this->fail( ... ) with throw new ExpectationFailedException( ..., $e->getComparisonFailure() )
  • Keep the existing case-context message unchanged

Testing instructions

  1. Start the local WordPress develop environment.
  2. Run: npm run test:php -- --filter Tests_Canonical_PostStatus
  3. Expected: OK (2 tests, 848 assertions)

Use of AI Tools

  • Cursor

#95 @lancewillett
3 weeks ago

In 64820:

Tests: Preserve canonical redirect comparison diffs.

Retain the original ComparisonFailure when adding case context to canonical post status failures. Preserve expected/actual URL diffs for both plain and pretty permalink tests.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14984

Props shailu25.
See #66074.

#97 @lancewillett
3 weeks ago

In 64863:

Tests: Limit private reader role setup in REST posts tests.

Register the private_reader role and grant read_private_posts only in the three REST posts controller tests that use the private reader. Preserve the existing requests and assertions.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14087

Props sainathpoojary, huzaifaalmesbah.
See #66074.

#99 @lancewillett
11 days ago

In 65239:

Tests: Generate only the thumbnail in the Imagick PDF preview test.

The PDF alpha channel test needs one fallback size to create the full preview. Limit the fallback sizes to the thumbnail so the upload no longer also renders medium and large images.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14248

Props abcd95, huzaifaalmesbah.
See #66074.

#101 @lancewillett
4 days ago

The 20 classes listed in this ticket now have landed optimization work. The final listed class, Tests_Image_Editor_Imagick, was addressed in [65239] (​PR #14248).

That completes the initial list. Closing this tracking ticket as fixed; further optimization should start with fresh measurements and a specific, demonstrated cost. This records completion of the listed work, not a claim that every class is fully optimized or a new suite-wide performance measurement.

Thanks to everyone who contributed patches, measurements, and reviews.

#102 @lancewillett
4 days ago

  • Resolution → fixed
  • Status accepted → closed
Note: See TracTickets for help on using tickets.