Make WordPress Core

Opened 6 months ago

Last modified 61 minutes ago

#65183 new enhancement

Tests: Add unit tests for set_screen_options()

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

Description

This ticket adds unit tests for the set_screen_options() function in wp-admin/includes/misc.php. The tests will cover saving screen options, handling missing or invalid data, and verifying user authentication and nonce checks.

Change History (8)

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


6 months ago
#1

  • Keywords has-patch has-unit-tests added

Description:
This PR adds unit tests for the set_screen_options() function in wp-admin/includes/misc.php. These tests ensure that the function correctly saves screen options (like items per page) for the current user, handles non-standard options via filters, and correctly validates input ranges.

The tests cover:

  • Saving standard _per_page options.
  • Saving custom options using the set-screen-option filter.
  • Handling layout_columns options.
  • Validating that values are within the allowed range (1-999).
  • Correct handling of missing or invalid $_POST data.
  • Ensuring the function correctly performs nonce and referer checks.

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

AI Disclosure:

  • AI assistance: Yes
  • Tool(s): Junie (JetBrains)
  • Model(s): gemini-3-flash-preview
  • Used for: Code analysis, test implementation, and workflow management.

#2 @desrosj
6 months ago

  • Focuses tests added

Adding the tests focus, which is used to indicate a ticket is solely focused on adding tests.

​@lancewillett commented on ​PR #11739:


8 days ago
#3

Pushed 988c2dd3830125fb064ae8703f5670da37255250 to remove the local die-handler overrides and ineffective nonce-action callbacks. The inherited test harness already supplies the terminal die handler; the requests create valid nonces. Redirect interception remains because the function exits after saving. No assertions changed.

The original and simplified tests both pass in randomized single-site and multisite runs: 7 cases and 7 assertions in each. The change removes 20 lines of setup/helpers. Fresh CI and review are still pending.

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

​@lancewillett commented on ​PR #11739:


8 days ago
#4

Follow-up in a10d314540544954b1b706921aa7e15ee08ec023: made the data provider static. It uses no instance state, so this follows PHPUnit’s provider requirements without changing the cases or assertions. Lint and PHPCS pass; randomized single-site and multisite runs each pass all seven tests and assertions.

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

#5 @lancewillett
8 days ago

  • Milestone Awaiting Review → 7.2

PR #11739 now relies on the inherited die handler, removing duplicate setup and ineffective nonce-action callbacks. Its provider is static.

Its seven cases pass locally before and after the simplification in single-site and multisite. The tests protect saved/rejected screen-option metadata; invalid-nonce and redirect-destination coverage are not claimed. PHPCS and current-head CI pass, and the PR is approved.

​https://github.com/WordPress/wordpress-develop/pull/11739

#6 @lancewillett
8 days ago

In 65181:

Tests: Cover saved and rejected screen options.

Verify user metadata written by set_screen_options() for built-in and filtered custom options, rejection of an out-of-range value, and requests without screen-option data. Use the inherited die handler, a redirect interceptor, and a static data provider.

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

Props pbearne.

See #65183.

#8 @audrasjb
61 minutes ago

  • Component Administration → Build/Test Tools

Bulk edit: move all unit tests related tickets in milestone 7.2 to the Build/Test Tools component, to clear up the milestone.

Note: See TracTickets for help on using tickets.