-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Post: Ensure get_post_custom_values() non-array returns grace fully. #11640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liaisontw
wants to merge
1
commit into
WordPress:trunk
Choose a base branch
from
liaisontw:fix/65044-get-post-custom-values-handles-non-array
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @group post | ||
| * | ||
| * @covers ::get_post_custom | ||
| */ | ||
| class Tests_Post_GetPostCustom extends WP_UnitTestCase { | ||
|
|
||
| protected static int $post_id; | ||
|
|
||
| public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void { | ||
| self::$post_id = $factory->post->create(); | ||
| add_post_meta( self::$post_id, 'test_key_1', 'value_a' ); | ||
| add_post_meta( self::$post_id, 'test_key_1', 'value_b' ); | ||
| add_post_meta( self::$post_id, 'test_key_2', 'value_c' ); | ||
| } | ||
|
|
||
| /** | ||
| * Test get_post_custom() happy path. | ||
| */ | ||
| public function test_get_post_custom_happy_path() { | ||
| $custom = get_post_custom( self::$post_id ); | ||
|
|
||
| $this->assertIsArray( $custom ); | ||
| $this->assertArrayHasKey( 'test_key_1', $custom ); | ||
| $this->assertCount( 2, $custom['test_key_1'] ); | ||
| $this->assertSame( array( 'value_a', 'value_b' ), $custom['test_key_1'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Test get_post_custom_keys() happy path. | ||
| */ | ||
| public function test_get_post_custom_keys_happy_path() { | ||
| $keys = get_post_custom_keys( self::$post_id ); | ||
|
|
||
| $this->assertIsArray( $keys ); | ||
| $this->assertContains( 'test_key_1', $keys ); | ||
| $this->assertContains( 'test_key_2', $keys ); | ||
| } | ||
|
|
||
| /** | ||
| * Test get_post_custom_values() happy path. | ||
| */ | ||
| public function test_get_post_custom_values_happy_path() { | ||
| $values = get_post_custom_values( 'test_key_1', self::$post_id ); | ||
|
|
||
| $this->assertIsArray( $values ); | ||
| $this->assertSame( array( 'value_a', 'value_b' ), $values ); | ||
| } | ||
|
|
||
| /** | ||
| * Test functions with non-existent post ID (The core of ticket #65044). | ||
| * | ||
| * @ticket 65044 | ||
| */ | ||
| public function test_custom_functions_with_invalid_post_id() { | ||
| $invalid_id = 99999; | ||
|
|
||
| // get_post_custom returns empty array for non-existent post in current WP | ||
| $this->assertSame( array(), get_post_custom( $invalid_id ) ); | ||
|
|
||
| // get_post_custom_keys returns null/void if array is empty/invalid | ||
| $this->assertNull( get_post_custom_keys( $invalid_id ) ); | ||
|
|
||
| // get_post_custom_values should return null (Fix for #65044) | ||
| $this->assertNull( get_post_custom_values( 'test_key_1', $invalid_id ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test get_post_custom_values() with empty key. | ||
| * | ||
| * @ticket 65044 | ||
| */ | ||
| public function test_get_post_custom_values_empty_key() { | ||
| $this->assertNull( get_post_custom_values( '', self::$post_id ) ); | ||
| $this->assertNull( get_post_custom_values( null, self::$post_id ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test behavior when post_id is 0 and no global post is set. | ||
| */ | ||
| public function test_custom_functions_with_zero_id_no_global_post() { | ||
| // Ensure global post is null | ||
| $GLOBALS['post'] = null; | ||
|
|
||
| $custom = get_post_custom( 0 ); | ||
| $this->assertEmpty( $custom, 'get_post_custom(0) should return an empty value (false or empty array).' ); | ||
| $this->assertNull( get_post_custom_keys( 0 ) ); | ||
| $this->assertNull( get_post_custom_values( 'test_key_1', 0 ) ); | ||
| } | ||
|
liaisontw marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Test get_post_custom_values() when key does not exist. | ||
| */ | ||
| public function test_get_post_custom_values_non_existent_key() { | ||
| $this->assertNull( get_post_custom_values( 'non_existent_key', self::$post_id ) ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.