Skip to content

Commit ad25870

Browse files
author
boonebgorges
committed
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
1 parent 527efae commit ad25870

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

tests/phpunit/includes/factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-thing.php' );
44
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-post.php' );
5+
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-bookmark.php' );
56
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-attachment.php' );
67
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-user.php' );
78
require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-comment.php' );
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Factory for creating fixtures for the deprecated Links/Bookmarks API.
5+
*
6+
* @since 4.6.0
7+
*/
8+
class WP_UnitTest_Factory_For_Bookmark extends WP_UnitTest_Factory_For_Thing {
9+
10+
public function __construct( $factory = null ) {
11+
parent::__construct( $factory );
12+
$this->default_generation_definitions = array(
13+
'link_name' => new WP_UnitTest_Generator_Sequence( 'Bookmark name %s' ),
14+
'link_url' => new WP_UnitTest_Generator_Sequence( 'Bookmark URL %s' ),
15+
);
16+
}
17+
18+
public function create_object( $args ) {
19+
return wp_insert_link( $args );
20+
}
21+
22+
public function update_object( $link_id, $fields ) {
23+
$fields['link_id'] = $link_id;
24+
return wp_update_link( $fields );
25+
}
26+
27+
public function get_object_by_id( $link_id ) {
28+
return get_bookmark( $link_id );
29+
}
30+
}

tests/phpunit/includes/factory/class-wp-unittest-factory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class WP_UnitTest_Factory {
4242
*/
4343
public $tag;
4444

45+
/**
46+
* @since 4.6.0
47+
* @var WP_UnitTest_Factory_For_Bookmark
48+
*/
49+
public $bookmark;
50+
4551
/**
4652
* @var WP_UnitTest_Factory_For_Blog
4753
*/
@@ -60,6 +66,7 @@ function __construct() {
6066
$this->term = new WP_UnitTest_Factory_For_Term( $this );
6167
$this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
6268
$this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
69+
$this->bookmark = new WP_UnitTest_Factory_For_Bookmark( $this );
6370
if ( is_multisite() ) {
6471
$this->blog = new WP_UnitTest_Factory_For_Blog( $this );
6572
$this->network = new WP_UnitTest_Factory_For_Network( $this );
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* @group bookmark
5+
*/
6+
class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase {
7+
public function test_should_hit_cache() {
8+
global $wpdb;
9+
10+
$bookmarks = self::factory()->bookmark->create_many( 2 );
11+
12+
$found1 = get_bookmarks( array(
13+
'orderby' => 'link_id',
14+
) );
15+
16+
$num_queries = $wpdb->num_queries;
17+
18+
$found2 = get_bookmarks( array(
19+
'orderby' => 'link_id',
20+
) );
21+
22+
$this->assertEqualSets( $found1, $found2 );
23+
$this->assertSame( $num_queries, $wpdb->num_queries );
24+
}
25+
26+
public function test_adding_bookmark_should_bust_get_bookmarks_cache() {
27+
global $wpdb;
28+
29+
$bookmarks = self::factory()->bookmark->create_many( 2 );
30+
31+
// Prime cache.
32+
$found1 = get_bookmarks( array(
33+
'orderby' => 'link_id',
34+
) );
35+
36+
$num_queries = $wpdb->num_queries;
37+
38+
$bookmarks[] = wp_insert_link( array(
39+
'link_name' => 'foo',
40+
'link_url' => 'http://example.com',
41+
) );
42+
43+
$found2 = get_bookmarks( array(
44+
'orderby' => 'link_id',
45+
) );
46+
47+
$this->assertEqualSets( $bookmarks, wp_list_pluck( $found2, 'link_id' ) );
48+
$this->assertTrue( $num_queries < $wpdb->num_queries );
49+
}
50+
}

0 commit comments

Comments
 (0)