From 26f6f758a94d9291e3f9c7afc72f4a9b1d287e1e Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Sun, 31 Mar 2019 16:33:58 -0400 Subject: [PATCH] test fix for 46660 --- src/wp-includes/meta.php | 6 +++++- tests/phpunit/tests/meta.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index 2332d9b5af050..4c4cad1f9d0e5 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -207,10 +207,14 @@ function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_ // Compare existing value to new value if no prev value given and the key exists only once. if ( empty( $prev_value ) ) { $old_value = get_metadata( $meta_type, $object_id, $meta_key ); - if ( count( $old_value ) == 1 ) { + if ( is_array( $old_value ) && count( $old_value ) == 1 ) { if ( $old_value[0] === $meta_value ) { return false; } + } else if ( ! is_array( $old_value ) ){ + if ( $old_value === $meta_value ) { + return false; + } } } diff --git a/tests/phpunit/tests/meta.php b/tests/phpunit/tests/meta.php index b20f2be90d761..d7eefaa269034 100644 --- a/tests/phpunit/tests/meta.php +++ b/tests/phpunit/tests/meta.php @@ -416,6 +416,20 @@ public function test_get_metadata_with_empty_key_nested_array_value() { $this->assertSame( array( $value ), $found['foo'] ); } + /** + * @ticket 46660 + */ + public function test_update_metadata_for_nonexistant_meta() { + $data = array( + array( 1, 2 ), + array( 3, 4 ), + ); + update_metadata( 'user', $this->author->ID, 'non_existant_foo', $data ); + + $found = get_metadata( 'user', $this->author->ID, 'non_existant_foo' ); + $this->assertSame( $found, $data ); + } + /** Helpers */ public function updated_meta( $meta_id ) {