Opened 6 weeks ago
Last modified 5 weeks ago
#65181 new enhancement
Tests: Add unit tests for show_message()
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Administration | Keywords: | dev-feedback has-patch has-unit-tests |
| Focuses: | tests | Cc: |
Description
This ticket adds unit tests for the show_message() function in wp-admin/includes/misc.php. This function is responsible for displaying administration messages and handling WP_Error objects.
Change History (3)
#2
@
5 weeks ago
- Focuses tests added
Adding the tests focus, which is used to indicate a ticket is solely focused on adding tests.
This ticket was mentioned in PR #11807 on WordPress/wordpress-develop by @shreyasikhar26.
5 weeks ago
#3
- Keywords has-patch has-unit-tests added
This pull request updates the show_message function in src/wp-admin/includes/misc.php to add support for returning the formatted message as a string instead of always printing it.
Without this change, it is almost impossible to add tests for this show_message function because of buffer flushing functions called at the end of it's definition.
Enhancements to message display:
- Updated the
show_messagefunction to accept a new$echoparameter (defaulting totrue). When$echoisfalse, the function returns the formatted HTML string instead of printing it and flushing output buffers. The function signature and docblock were updated accordingly.
Testing improvements:
- Added a new test class
Tests_Admin_Includes_Misc_ShowMessage_Testintests/phpunit/tests/admin/includes/misc/ShowMessage_Test.phpto verify thatshow_messagecorrectly returns the formatted HTML string for plain messages andWP_Errorobjects when$echoisfalse.
The show_message() function is currently difficult to test because it has two major side effects:
Proposed Refactoring
To make show_message() testable while maintaining backward compatibility, we can split the formatting logic from the output logic.
Option 1: Add a return parameter (Recommended) We can add an optional second parameter $echo that defaults to true. When set to false, the function returns the HTML string instead of printing and flushing.
Option 2: Extract a "get" version Create a new function get_show_message() that handles the string conversion and wrapping, and have show_message() call it.
Why Option 1 is better for WordPress Core:
Would you like me to implement one of these changes and update the tests accordingly?