From ec53bf5bf6ca0db7857b8e049bc13869a56f8b58 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 25 May 2016 18:20:24 +0000 Subject: [PATCH] Add tests for `get_bookmarks()` cache. This changeset adds a unit test factory so that bookmark/link fixtures can be created during tests. Why are we writing tests for functionality that has been deprecated for years? Because it's the Right Thing to Do. See #18356. git-svn-id: https://develop.svn.wordpress.org/trunk@37563 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/factory.php | 1 + ...class-wp-unittest-factory-for-bookmark.php | 30 +++++++++++ .../factory/class-wp-unittest-factory.php | 7 +++ tests/phpunit/tests/bookmark/getBookmarks.php | 50 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 tests/phpunit/includes/factory/class-wp-unittest-factory-for-bookmark.php create mode 100644 tests/phpunit/tests/bookmark/getBookmarks.php diff --git a/tests/phpunit/includes/factory.php b/tests/phpunit/includes/factory.php index c6d55186d1..91d7da750e 100644 --- a/tests/phpunit/includes/factory.php +++ b/tests/phpunit/includes/factory.php @@ -2,6 +2,7 @@ require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-thing.php' ); require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-post.php' ); +require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-bookmark.php' ); require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-attachment.php' ); require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-user.php' ); require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-comment.php' ); diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-bookmark.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-bookmark.php new file mode 100644 index 0000000000..cd472271ba --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-bookmark.php @@ -0,0 +1,30 @@ +default_generation_definitions = array( + 'link_name' => new WP_UnitTest_Generator_Sequence( 'Bookmark name %s' ), + 'link_url' => new WP_UnitTest_Generator_Sequence( 'Bookmark URL %s' ), + ); + } + + public function create_object( $args ) { + return wp_insert_link( $args ); + } + + public function update_object( $link_id, $fields ) { + $fields['link_id'] = $link_id; + return wp_update_link( $fields ); + } + + public function get_object_by_id( $link_id ) { + return get_bookmark( $link_id ); + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory.php b/tests/phpunit/includes/factory/class-wp-unittest-factory.php index 58b0e8624c..32a463c3c0 100644 --- a/tests/phpunit/includes/factory/class-wp-unittest-factory.php +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory.php @@ -42,6 +42,12 @@ class WP_UnitTest_Factory { */ public $tag; + /** + * @since 4.6.0 + * @var WP_UnitTest_Factory_For_Bookmark + */ + public $bookmark; + /** * @var WP_UnitTest_Factory_For_Blog */ @@ -60,6 +66,7 @@ class WP_UnitTest_Factory { $this->term = new WP_UnitTest_Factory_For_Term( $this ); $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' ); $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' ); + $this->bookmark = new WP_UnitTest_Factory_For_Bookmark( $this ); if ( is_multisite() ) { $this->blog = new WP_UnitTest_Factory_For_Blog( $this ); $this->network = new WP_UnitTest_Factory_For_Network( $this ); diff --git a/tests/phpunit/tests/bookmark/getBookmarks.php b/tests/phpunit/tests/bookmark/getBookmarks.php new file mode 100644 index 0000000000..accc3af72e --- /dev/null +++ b/tests/phpunit/tests/bookmark/getBookmarks.php @@ -0,0 +1,50 @@ +bookmark->create_many( 2 ); + + $found1 = get_bookmarks( array( + 'orderby' => 'link_id', + ) ); + + $num_queries = $wpdb->num_queries; + + $found2 = get_bookmarks( array( + 'orderby' => 'link_id', + ) ); + + $this->assertEqualSets( $found1, $found2 ); + $this->assertSame( $num_queries, $wpdb->num_queries ); + } + + public function test_adding_bookmark_should_bust_get_bookmarks_cache() { + global $wpdb; + + $bookmarks = self::factory()->bookmark->create_many( 2 ); + + // Prime cache. + $found1 = get_bookmarks( array( + 'orderby' => 'link_id', + ) ); + + $num_queries = $wpdb->num_queries; + + $bookmarks[] = wp_insert_link( array( + 'link_name' => 'foo', + 'link_url' => 'http://example.com', + ) ); + + $found2 = get_bookmarks( array( + 'orderby' => 'link_id', + ) ); + + $this->assertEqualSets( $bookmarks, wp_list_pluck( $found2, 'link_id' ) ); + $this->assertTrue( $num_queries < $wpdb->num_queries ); + } +}