Make WordPress Core

Opened 4 weeks ago

Last modified 4 weeks ago

#65652 new enhancement

Tests: Add PHPUnit tests for wp_json_encode(

Reported by: pbearne Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: tests

Description

The wp_json_encode() function in wp-includes/functions.php is a wrapper around json_encode() that provides additional logic, such as handling UTF-8 encoding issues and applying the wp_php_error_args filter.

Currently, this function lacks dedicated unit test coverage in the core suite. This patch introduces a new test class Tests_Functions_WpJsonEncode to verify:

  • Basic encoding of arrays and objects.
  • Correct handling of JSON encoding options.
  • Handling of depth parameters.
  • Ensuring the function returns the expected JSON string or false on failure.
  • (Optional) Verification of UTF-8 sanitization behaviour for invalid strings.

Change History (2)

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


4 weeks ago
#1

  • Keywords has-patch has-unit-tests added

This PR introduces dedicated unit tests for the wp_json_encode() function in wp-includes/functions.php.

The tests cover:
•Standard encoding of diverse data structures.
•Correct application of JSON encoding constants/options.
•Enforcement of maximum recursion depth.
•The function's ability to sanitize and encode strings containing invalid UTF-8 sequences, which is a key feature of the WordPress wrapper over native json_encode().

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

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 @wildworks
4 weeks ago

  • Milestone 7.1Awaiting Review

Since 7.1 Beta 1 has already been released and these functions were not newly introduced with the 7.1 release, I'd like to remove the milestone.

Note: See TracTickets for help on using tickets.