diff --git a/src/wp-admin/includes/bookmark.php b/src/wp-admin/includes/bookmark.php index 6cc6683f029a4..f1c7f77aa0a1f 100644 --- a/src/wp-admin/includes/bookmark.php +++ b/src/wp-admin/includes/bookmark.php @@ -126,7 +126,12 @@ function wp_delete_link( $link_id ) { */ function wp_get_link_cats( $link_id = 0 ) { $cats = wp_get_object_terms( $link_id, 'link_category', array( 'fields' => 'ids' ) ); - return array_unique( $cats ); + + if ( is_array( $cats ) ) { + return array_unique( $cats ); + } + + return array(); } /** @@ -307,16 +312,17 @@ function wp_update_link( $linkdata ) { $link = wp_slash( $link ); // Passed link category list overwrites existing category list if not empty. - if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] ) - && count( $linkdata['link_category'] ) > 0 - ) { + $link_cats = null; + if ( ! empty( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] ) ) { $link_cats = $linkdata['link_category']; - } else { + } elseif ( ! empty( $link['link_category'] ) && is_array( $link['link_category'] ) ) { $link_cats = $link['link_category']; } // Merge old and new fields with new fields overwriting old ones. - $linkdata = array_merge( $link, $linkdata ); + if ( ! empty( $link ) && is_array( $link ) ) { + $linkdata = array_merge( $link, $linkdata ); + } $linkdata['link_category'] = $link_cats; return wp_insert_link( $linkdata ); diff --git a/tests/phpunit/tests/bookmark/getBookmarks.php b/tests/phpunit/tests/bookmark/getBookmarks.php index 9200ed9d09094..649a66bd0ab44 100644 --- a/tests/phpunit/tests/bookmark/getBookmarks.php +++ b/tests/phpunit/tests/bookmark/getBookmarks.php @@ -2,8 +2,10 @@ /** * @group bookmark + * @covers ::get_bookmarks */ class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase { + public function test_should_hit_cache() { global $wpdb; diff --git a/tests/phpunit/tests/bookmark/wpUpdateLink.php b/tests/phpunit/tests/bookmark/wpUpdateLink.php new file mode 100644 index 0000000000000..dc28e87c66b84 --- /dev/null +++ b/tests/phpunit/tests/bookmark/wpUpdateLink.php @@ -0,0 +1,34 @@ +bookmark->create(); + $link_name = 'foo'; + $result = wp_update_link( + array( + 'link_id' => $bookmark_id, + 'link_name' => $link_name, + ) + ); + $this->assertSame( $bookmark_id, $result ); + $this->assertSame( $link_name, get_bookmark( $bookmark_id )->link_name ); + } + + public function test_should_not_update_non_existing_bookmark() { + $bookmark_id = -1; + $link_name = 'foo'; + $result = wp_update_link( + array( + 'link_id' => $bookmark_id, + 'link_name' => $link_name, + ) + ); + $this->assertNotSame( $bookmark_id, $result ); + $this->assertWPError( $result ); + } +}