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
This commit is contained in:
parent
a34e38513f
commit
ec53bf5bf6
|
@ -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-thing.php' );
|
||||||
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-post.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-attachment.php' );
|
||||||
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-user.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' );
|
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-comment.php' );
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for creating fixtures for the deprecated Links/Bookmarks API.
|
||||||
|
*
|
||||||
|
* @since 4.6.0
|
||||||
|
*/
|
||||||
|
class WP_UnitTest_Factory_For_Bookmark extends WP_UnitTest_Factory_For_Thing {
|
||||||
|
|
||||||
|
public function __construct( $factory = null ) {
|
||||||
|
parent::__construct( $factory );
|
||||||
|
$this->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 );
|
||||||
|
}
|
||||||
|
}
|
|
@ -42,6 +42,12 @@ class WP_UnitTest_Factory {
|
||||||
*/
|
*/
|
||||||
public $tag;
|
public $tag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.6.0
|
||||||
|
* @var WP_UnitTest_Factory_For_Bookmark
|
||||||
|
*/
|
||||||
|
public $bookmark;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var WP_UnitTest_Factory_For_Blog
|
* @var WP_UnitTest_Factory_For_Blog
|
||||||
*/
|
*/
|
||||||
|
@ -60,6 +66,7 @@ class WP_UnitTest_Factory {
|
||||||
$this->term = new WP_UnitTest_Factory_For_Term( $this );
|
$this->term = new WP_UnitTest_Factory_For_Term( $this );
|
||||||
$this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
|
$this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
|
||||||
$this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
|
$this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
|
||||||
|
$this->bookmark = new WP_UnitTest_Factory_For_Bookmark( $this );
|
||||||
if ( is_multisite() ) {
|
if ( is_multisite() ) {
|
||||||
$this->blog = new WP_UnitTest_Factory_For_Blog( $this );
|
$this->blog = new WP_UnitTest_Factory_For_Blog( $this );
|
||||||
$this->network = new WP_UnitTest_Factory_For_Network( $this );
|
$this->network = new WP_UnitTest_Factory_For_Network( $this );
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group bookmark
|
||||||
|
*/
|
||||||
|
class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase {
|
||||||
|
public function test_should_hit_cache() {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$bookmarks = self::factory()->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 );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue