File tree Expand file tree Collapse file tree 4 files changed +88
-0
lines changed Expand file tree Collapse file tree 4 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
require_once ( dirname ( __FILE__ ) . '/factory/class-wp-unittest-factory-for-thing.php ' );
4
4
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 ' );
5
6
require_once ( dirname ( __FILE__ ) . '/factory/class-wp-unittest-factory-for-attachment.php ' );
6
7
require_once ( dirname ( __FILE__ ) . '/factory/class-wp-unittest-factory-for-user.php ' );
7
8
require_once ( dirname ( __FILE__ ) . '/factory/class-wp-unittest-factory-for-comment.php ' );
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -42,6 +42,12 @@ class WP_UnitTest_Factory {
42
42
*/
43
43
public $ tag ;
44
44
45
+ /**
46
+ * @since 4.6.0
47
+ * @var WP_UnitTest_Factory_For_Bookmark
48
+ */
49
+ public $ bookmark ;
50
+
45
51
/**
46
52
* @var WP_UnitTest_Factory_For_Blog
47
53
*/
@@ -60,6 +66,7 @@ function __construct() {
60
66
$ this ->term = new WP_UnitTest_Factory_For_Term ( $ this );
61
67
$ this ->category = new WP_UnitTest_Factory_For_Term ( $ this , 'category ' );
62
68
$ this ->tag = new WP_UnitTest_Factory_For_Term ( $ this , 'post_tag ' );
69
+ $ this ->bookmark = new WP_UnitTest_Factory_For_Bookmark ( $ this );
63
70
if ( is_multisite () ) {
64
71
$ this ->blog = new WP_UnitTest_Factory_For_Blog ( $ this );
65
72
$ this ->network = new WP_UnitTest_Factory_For_Network ( $ this );
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments